worked on Image
This commit is contained in:
+25
-11
@@ -1,47 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/noncopyable.hpp>
|
||||
#include <mutex>
|
||||
#include <type_traits>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
template <typename T>
|
||||
class AccessGuard : boost::noncopyable
|
||||
class AccessGuard
|
||||
{
|
||||
public:
|
||||
AccessGuard(T &element, std::mutex &mutex)
|
||||
: element_(element)
|
||||
, mutex_(mutex)
|
||||
: element_(&element)
|
||||
, mutex_(&mutex)
|
||||
{
|
||||
this->mutex_.lock();
|
||||
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_;
|
||||
}
|
||||
|
||||
~AccessGuard()
|
||||
{
|
||||
this->mutex_.unlock();
|
||||
if (this->isValid_) this->mutex_->unlock();
|
||||
}
|
||||
|
||||
T *operator->() const
|
||||
{
|
||||
return &this->element_;
|
||||
return this->element_;
|
||||
}
|
||||
|
||||
T &operator*() const
|
||||
{
|
||||
return this->element_;
|
||||
return *this->element_;
|
||||
}
|
||||
|
||||
private:
|
||||
T &element_;
|
||||
std::mutex &mutex_;
|
||||
T *element_;
|
||||
std::mutex *mutex_;
|
||||
bool isValid_ = true;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class UniqueAccess
|
||||
{
|
||||
public:
|
||||
// template <typename X = decltype(T())>
|
||||
// template <typename X = decltype(T())>
|
||||
UniqueAccess()
|
||||
: element_(T())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user