mirror-chatterino2/src/common/LockedObject.hpp

39 lines
561 B
C++
Raw Normal View History

2017-06-15 23:13:01 +02:00
#pragma once
#include <mutex>
namespace chatterino {
template <typename Type>
class LockedObject
{
public:
LockedObject &operator=(const LockedObject<Type> &other)
{
2018-07-06 17:56:11 +02:00
this->mutex_.lock();
2017-06-15 23:13:01 +02:00
this->data = other.getValue();
2018-07-06 17:56:11 +02:00
this->mutex_.unlock();
2017-06-15 23:13:01 +02:00
return *this;
}
LockedObject &operator=(const Type &other)
{
2018-07-06 17:56:11 +02:00
this->mutex_.lock();
2017-06-15 23:13:01 +02:00
this->data = other;
2018-07-06 17:56:11 +02:00
this->mutex_.unlock();
2017-06-15 23:13:01 +02:00
return *this;
}
private:
2018-07-06 17:56:11 +02:00
Type value_;
std::mutex mutex_;
2017-06-15 23:13:01 +02:00
};
} // namespace chatterino