mirror-chatterino2/src/common/Atomic.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
804 B
C++
Raw Normal View History

2018-05-25 13:53:55 +02:00
#pragma once
2018-05-25 13:53:55 +02:00
#include <mutex>
2018-05-25 13:53:55 +02:00
namespace chatterino {
2018-05-25 13:53:55 +02:00
template <typename T>
class Atomic
2018-05-25 13:53:55 +02:00
{
public:
Atomic() = default;
2018-08-11 14:20:53 +02:00
Atomic(T &&val)
2018-07-06 17:56:11 +02:00
: value_(val)
2018-05-25 13:53:55 +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_);
2018-07-06 17:56:11 +02:00
return this->value_;
2018-05-25 13:53:55 +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_);
2018-07-06 17:56:11 +02:00
this->value_ = val;
2018-05-25 13:53:55 +02:00
}
2018-08-11 14:20:53 +02:00
void set(T &&val)
{
std::lock_guard<std::mutex> guard(this->mutex_);
2018-08-11 14:20:53 +02:00
this->value_ = std::move(val);
}
2018-07-06 17:56:11 +02:00
private:
mutable std::mutex mutex_;
T value_;
2018-05-25 13:53:55 +02:00
};
2018-05-25 13:53:55 +02:00
} // namespace chatterino