mirror-chatterino2/src/common/UniqueAccess.hpp

104 lines
1.8 KiB
C++
Raw Normal View History

2018-07-14 14:24:18 +02:00
#pragma once
#include <mutex>
#include <type_traits>
namespace chatterino {
template <typename T>
2018-08-06 18:25:47 +02:00
class AccessGuard
2018-07-14 14:24:18 +02:00
{
public:
AccessGuard(T &element, std::mutex &mutex)
2018-08-06 18:25:47 +02:00
: element_(&element)
, mutex_(&mutex)
2018-07-14 14:24:18 +02:00
{
2018-08-06 18:25:47 +02:00
this->mutex_->lock();
}
AccessGuard(AccessGuard<T> &&other)
: element_(other.element_)
, mutex_(other.mutex_)
{
other.isValid_ = false;
}
AccessGuard<T> &operator=(AccessGuard<T> &&other)
{
other.isValid_ = false;
this->element_ = other.element_;
this->mutex_ = other.element_;
2018-07-14 14:24:18 +02:00
}
~AccessGuard()
{
2018-08-06 18:25:47 +02:00
if (this->isValid_) this->mutex_->unlock();
2018-07-14 14:24:18 +02:00
}
2018-08-02 14:23:27 +02:00
T *operator->() const
2018-07-14 14:24:18 +02:00
{
2018-08-06 18:25:47 +02:00
return this->element_;
2018-07-14 14:24:18 +02:00
}
2018-08-02 14:23:27 +02:00
T &operator*() const
2018-07-14 14:24:18 +02:00
{
2018-08-06 18:25:47 +02:00
return *this->element_;
2018-07-14 14:24:18 +02:00
}
private:
2018-08-06 18:25:47 +02:00
T *element_;
std::mutex *mutex_;
bool isValid_ = true;
2018-07-14 14:24:18 +02:00
};
template <typename T>
class UniqueAccess
{
public:
2018-08-06 18:25:47 +02:00
// template <typename X = decltype(T())>
2018-07-14 14:24:18 +02:00
UniqueAccess()
: element_(T())
{
}
UniqueAccess(const T &element)
: element_(element)
{
}
UniqueAccess(T &&element)
: element_(element)
{
}
UniqueAccess<T> &operator=(const T &element)
{
this->element_ = element;
return *this;
}
UniqueAccess<T> &operator=(T &&element)
{
this->element_ = element;
return *this;
}
2018-08-02 14:23:27 +02:00
AccessGuard<T> access() const
2018-07-14 14:24:18 +02:00
{
return AccessGuard<T>(this->element_, this->mutex_);
}
2018-08-11 14:12:41 +02:00
template <typename X = T,
typename = std::enable_if_t<!std::is_const<X>::value>>
2018-08-02 14:23:27 +02:00
AccessGuard<const X> accessConst() const
2018-07-15 20:28:54 +02:00
{
2018-08-02 14:23:27 +02:00
return AccessGuard<const T>(this->element_, this->mutex_);
2018-07-15 20:28:54 +02:00
}
2018-07-14 14:24:18 +02:00
private:
2018-07-15 20:28:54 +02:00
mutable T element_;
mutable std::mutex mutex_;
2018-07-14 14:24:18 +02:00
};
} // namespace chatterino