mirror-chatterino2/src/common/MutexValue.hpp

41 lines
617 B
C++
Raw Normal View History

2018-05-25 13:53:55 +02:00
#pragma once
2018-06-22 23:24:45 +02:00
#include <boost/noncopyable.hpp>
2018-05-25 13:53:55 +02:00
#include <mutex>
namespace chatterino {
template <typename T>
2018-06-22 23:24:45 +02:00
class MutexValue : boost::noncopyable
2018-05-25 13:53:55 +02:00
{
public:
MutexValue()
{
}
MutexValue(T &&val)
2018-07-06 17:56:11 +02:00
: value_(val)
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-05-25 13:53:55 +02:00
2018-07-06 17:56:11 +02:00
return this->value_;
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-05-25 13:53:55 +02:00
2018-07-06 17:56:11 +02:00
this->value_ = val;
2018-05-25 13:53:55 +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
};
} // namespace chatterino