5#include <Common/assertion.h>
6#include <Common/exceptions.h>
48template<
typename T,
typename Mutex = std::shared_mutex>
49class TSynchronized final :
private INoCopyAndMove
55 class AutoLockingPtr final :
private INoCopyAndMove
58 explicit AutoLockingPtr(TSynchronized& parent)
61 m_parent.m_mutex.lock();
66 m_parent.m_mutex.unlock();
71 return std::addressof(m_parent.m_value);
76 return m_parent.m_value;
80 TSynchronized& m_parent;
86 class AutoConstLockingPtr final :
private INoCopyAndMove
89 explicit AutoConstLockingPtr(
const TSynchronized& parent)
92 m_parent.m_mutex.lock_shared();
95 ~AutoConstLockingPtr()
97 m_parent.m_mutex.unlock_shared();
100 const T* operator -> ()
const
102 return std::addressof(m_parent.m_value);
105 const T& operator * ()
const
107 return m_parent.m_value;
111 const TSynchronized& m_parent;
116 class AutoTryLockingPtr final :
private INoCopyAndMove
119 explicit AutoTryLockingPtr(TSynchronized& parent)
123 m_isLocked = m_parent.m_mutex.try_lock();
130 m_parent.m_mutex.unlock();
138 return std::addressof(m_parent.m_value);
150 return m_parent.m_value;
154 throw IllegalOperationException(
155 "Accessing value without successful non-const locking.");
159 operator bool ()
const
165 TSynchronized& m_parent;
171 class AutoTryConstLockingPtr final :
private INoCopyAndMove
174 explicit AutoTryConstLockingPtr(
const TSynchronized& parent)
178 m_isLocked = m_parent.m_mutex.try_lock_shared();
181 ~AutoTryConstLockingPtr()
185 m_parent.m_mutex.unlock_shared();
189 const T* operator -> ()
const
193 return std::addressof(m_parent.m_value);
201 const T& operator * ()
const
205 return m_parent.m_value;
209 throw IllegalOperationException(
210 "Accessing value without successful const locking.");
214 operator bool ()
const
220 const TSynchronized& m_parent;
225 TSynchronized() =
default;
227 explicit TSynchronized(
const T& value)
noexcept(std::is_nothrow_copy_constructible_v<T>)
232 explicit TSynchronized(T&& value)
noexcept(std::is_nothrow_move_constructible_v<T>)
233 : m_value(
std::move(value))
239 const TSynchronized& asConst()
const
244 AutoLockingPtr lockedPtr()
247 return AutoLockingPtr(*
this);
250 AutoConstLockingPtr constLockedPtr()
const
253 return AutoConstLockingPtr(*
this);
264 template<
typename LockedFunc>
265 void locked(LockedFunc func)
267 static_assert(std::is_invocable_v<LockedFunc, T&>,
268 "LockedFunc must take (T&).");
270 AutoLockingPtr lockedPtr(*
this);
282 template<
typename ConstLockedFunc>
283 void constLocked(ConstLockedFunc func)
const
285 static_assert(std::is_invocable_v<ConstLockedFunc, const T&>,
286 "ConstLockedFunc must take (const T&).");
288 AutoConstLockingPtr lockedPtr(*
this);
292 AutoTryLockingPtr tryLock()
295 return AutoTryLockingPtr(*
this);
298 AutoTryConstLockingPtr tryConstLock()
const
301 return AutoTryConstLockingPtr(*
this);
309 AutoConstLockingPtr lockedPtr(*
this);
320 T& unsafeGetReference()
325 const T& unsafeGetReference()
const
334 TSynchronized& operator = (
const T& rhsValue)
336 PH_ASSERT(&m_value != &rhsValue);
338 AutoLockingPtr lockedPtr(*
this);
339 *lockedPtr = rhsValue;
347 TSynchronized& operator = (T&& rhsValue)
349 PH_ASSERT(&m_value != &rhsValue);
351 AutoLockingPtr lockedPtr(*
this);
352 *lockedPtr = std::move(rhsValue);
359 AutoLockingPtr operator -> ()
362 return AutoLockingPtr(*
this);
367 AutoConstLockingPtr operator -> ()
const
370 return AutoConstLockingPtr(*
this);
375 mutable Mutex m_mutex;
The root for all renderer implementations.
Definition EEngineProject.h:6