2018-05-25 13:53:55 +02:00
|
|
|
#pragma once
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2024-01-26 21:55:30 +01:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
2018-05-25 13:53:55 +02:00
|
|
|
#include <mutex>
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-05-25 13:53:55 +02:00
|
|
|
namespace chatterino {
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-05-25 13:53:55 +02:00
|
|
|
template <typename T>
|
2023-09-09 12:23:20 +02:00
|
|
|
class Atomic
|
2018-05-25 13:53:55 +02:00
|
|
|
{
|
|
|
|
public:
|
2023-09-09 12:23:20 +02:00
|
|
|
Atomic() = default;
|
2024-01-26 21:55:30 +01:00
|
|
|
~Atomic() = default;
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
Atomic(T &&val)
|
2024-01-26 21:55:30 +01:00
|
|
|
: value_(std::move(val))
|
2018-05-25 13:53:55 +02:00
|
|
|
{
|
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2023-09-09 12:23:20 +02:00
|
|
|
Atomic(const Atomic &) = delete;
|
|
|
|
Atomic &operator=(const Atomic &) = delete;
|
|
|
|
|
|
|
|
Atomic(Atomic &&) = delete;
|
|
|
|
Atomic &operator=(Atomic &&) = delete;
|
|
|
|
|
2018-05-25 13:53:55 +02:00
|
|
|
T get() const
|
|
|
|
{
|
2018-07-06 17:56:11 +02:00
|
|
|
std::lock_guard<std::mutex> guard(this->mutex_);
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-07-06 17:56:11 +02:00
|
|
|
return this->value_;
|
2018-05-25 13:53:55 +02:00
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-05-25 13:53:55 +02:00
|
|
|
void set(const T &val)
|
|
|
|
{
|
2018-07-06 17:56:11 +02:00
|
|
|
std::lock_guard<std::mutex> guard(this->mutex_);
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-07-06 17:56:11 +02:00
|
|
|
this->value_ = val;
|
2018-05-25 13:53:55 +02:00
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
void set(T &&val)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(this->mutex_);
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-08-11 14:20:53 +02:00
|
|
|
this->value_ = std::move(val);
|
|
|
|
}
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2018-07-06 17:56:11 +02:00
|
|
|
private:
|
|
|
|
mutable std::mutex mutex_;
|
|
|
|
T value_;
|
2018-05-25 13:53:55 +02:00
|
|
|
};
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2024-01-26 21:55:30 +01:00
|
|
|
#if defined(__cpp_lib_atomic_shared_ptr) && defined(__cpp_concepts)
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Atomic<std::shared_ptr<T>>
|
|
|
|
{
|
|
|
|
// Atomic<std::shared_ptr<T>> must be instantated with a const T
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
requires std::is_const_v<T>
|
|
|
|
class Atomic<std::shared_ptr<T>>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Atomic() = default;
|
|
|
|
~Atomic() = default;
|
|
|
|
|
|
|
|
Atomic(T &&val)
|
|
|
|
: value_(std::make_shared<T>(std::move(val)))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Atomic(std::shared_ptr<T> &&val)
|
|
|
|
: value_(std::move(val))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Atomic(const Atomic &) = delete;
|
|
|
|
Atomic &operator=(const Atomic &) = delete;
|
|
|
|
|
|
|
|
Atomic(Atomic &&) = delete;
|
|
|
|
Atomic &operator=(Atomic &&) = delete;
|
|
|
|
|
|
|
|
std::shared_ptr<T> get() const
|
|
|
|
{
|
|
|
|
return this->value_.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(const T &val)
|
|
|
|
{
|
|
|
|
this->value_.store(std::make_shared<T>(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(T &&val)
|
|
|
|
{
|
|
|
|
this->value_.store(std::make_shared<T>(std::move(val)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(const std::shared_ptr<T> &val)
|
|
|
|
{
|
|
|
|
this->value_.store(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(std::shared_ptr<T> &&val)
|
|
|
|
{
|
|
|
|
this->value_.store(std::move(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::atomic<std::shared_ptr<T>> value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-05-25 13:53:55 +02:00
|
|
|
} // namespace chatterino
|