2 #ifndef OPENLIBRARY_PALGORITHM_TS_RESOURCE
3 #define OPENLIBRARY_PALGORITHM_TS_RESOURCE
44 Accessor(std::mutex& mutex, T*
object): m_lock(mutex), m_object(object), m_notify(nullptr) {}
48 m_object(other.m_object),
49 m_notify(other.m_notify)
51 other.m_object =
nullptr;
52 other.m_notify =
nullptr;
57 Accessor(): m_lock(), m_object(nullptr), m_notify(nullptr) {}
60 Accessor(std::unique_lock<std::mutex>&&
lock, T*
object): m_lock(std::move(
lock)), m_object(object), m_notify(nullptr) {}
68 m_lock = std::move(other.m_lock);
69 m_object = other.m_object;
70 m_notify = other.m_notify;
72 other.m_object =
nullptr;
73 other.m_notify =
nullptr;
88 if (m_notify !=
nullptr)
133 return m_object !=
nullptr;
138 std::unique_lock<std::mutex> m_lock;
150 template<
typename... Args>
160 std::unique_lock<std::mutex> l(m_mutex);
170 Accessor accessor(m_mutex, &m_resource);
180 accessor.set(notify);
190 std::unique_lock<std::mutex> l(m_mutex, std::defer_lock);
191 const bool locked = l.try_lock();
195 result =
Accessor(std::move(l), &m_resource);
T * operator->()
get pointer to resource
Definition: ts_resource.hpp:105
virtual ~Accessor()
Destructor.
Definition: ts_resource.hpp:83
Accessor try_lock()
Definition: ts_resource.hpp:188
const T * operator->() const
get pointer to const resource
Definition: ts_resource.hpp:111
bool is_valid() const
Check if Accessor is valid.
Definition: ts_resource.hpp:131
Thread safe wrapper for objects.
Definition: ts_resource.hpp:24
Accessor()
Definition: ts_resource.hpp:57
T & operator*()
get reference to resource
Definition: ts_resource.hpp:117
Accessor lock()
Definition: ts_resource.hpp:168
const T & operator*() const
get reference to const resource
Definition: ts_resource.hpp:123
virtual void unlocked()=0
Called when Accessor is being destructed and resource is unlocked.
ThreadSafeResource(const Args &...args)
Contructor.
Definition: ts_resource.hpp:151
Accessor(std::unique_lock< std::mutex > &&lock, T *object)
Constructor.
Definition: ts_resource.hpp:60
ThreadSafeResource's helper class.
Definition: ts_resource.hpp:40
Notification interface.
Definition: ts_resource.hpp:28
Accessor & operator=(Accessor &&other)
Move operator.
Definition: ts_resource.hpp:66
~ThreadSafeResource()
Definition: ts_resource.hpp:157
Accessor lock(INotify *notify)
Definition: ts_resource.hpp:177
Accessor(Accessor &&other)
Move Constructor.
Definition: ts_resource.hpp:47
Accessor(std::mutex &mutex, T *object)
Definition: ts_resource.hpp:44