CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
CeresEngine::LockedObject< T, Mutex > Class Template Reference

A helper class that wraps and protects a value from non-synchronized access. More...

#include <CeresEngine/Foundation/Threading/Threading.hpp>

Classes

class  TemporaryLock
 A helper class that acquires a temporary lock and overloads operator-> to allow access to the object. More...
 

Public Types

using ValueType = T
 The type of value used by the LockedObject instance.
 
using MutexType = Mutex
 The type of mutex used by the LockedObject instance.
 

Public Member Functions

template<typename... Args>
 LockedObject (Args &&... args)
 Creates a new LockedObject instance by constructing a new object of type T by forwarding Args to the constructor.
 
template<typename OtherMutex >
 LockedObject (const LockedObject< T, OtherMutex > &other)
 Creates a new LockedObject by copying the value of another.
 
template<typename OtherMutex >
LockedObjectoperator= (const LockedObject< T, OtherMutex > &other)
 Assigns the LockedObject by copying the value of another.
 
LockedObjectoperator= (const T &other)
 Assigns the LockedObject by copying a value.
 
template<typename OtherMutex >
LockedObjectoperator= (LockedObject< T, OtherMutex > &&other) noexcept
 Assigns the LockedObject by moving the value of another.
 
LockedObjectoperator= (T &&other) noexcept
 Assigns the LockedObject by copying a value.
 
template<typename Func >
decltype(autowith (Func &&func)
 Executes a function Func within a synchronized block.
 
template<typename Func >
decltype(autooperator| (Func &&func)
 Executes a function Func within a synchronized block.
 
template<typename Func >
decltype(autooperator() (Func &&func)
 Executes a function Func within a synchronized block.
 
auto lock ()
 Access the LockedObject contents by acquiring a temporary lock.
 
auto operator-> ()
 Executes a function Func within a synchronized block by locking all objects.
 
auto operator* ()
 Executes a function Func within a synchronized block by locking all objects.
 
template<typename Func >
decltype(autowith (Func &&func) const
 Executes a function Func within a synchronized block.
 
template<typename Func >
decltype(autooperator| (Func &&func) const
 Executes a function Func within a synchronized block.
 
template<typename Func >
decltype(autooperator() (Func &&func) const
 Executes a function Func within a synchronized block.
 
auto lock () const
 Access the LockedObject contents by acquiring a temporary lock.
 
auto operator-> () const
 Executes a function Func within a synchronized block by locking all objects.
 
auto operator* () const
 Executes a function Func within a synchronized block by locking all objects.
 
T copy () const
 Returns a copy of the locked object.
 

Private Attributes

T mValue
 The value that is locked and guarded by the mutex.
 
Mutex mMutex
 The mutex guarding the value.
 

Friends

template<typename , typename >
class LockedObject
 
template<CLockedObject... Ts, typename Func >
decltype(autolock (Func &&func, Ts &... objects)
 Executes a function Func within a synchronized block by locking all objects.
 

Detailed Description

template<typename T, typename Mutex = Mutex>
class CeresEngine::LockedObject< T, Mutex >

A helper class that wraps and protects a value from non-synchronized access.

Access to the underlying stored value must be done via with or copy methods.

Template Parameters
TThe object type that is guarded by the object.
MutexThe type of mutex to be used by the guard.

Member Typedef Documentation

◆ MutexType

template<typename T , typename Mutex = Mutex>
using CeresEngine::LockedObject< T, Mutex >::MutexType = Mutex

The type of mutex used by the LockedObject instance.

◆ ValueType

template<typename T , typename Mutex = Mutex>
using CeresEngine::LockedObject< T, Mutex >::ValueType = T

The type of value used by the LockedObject instance.

Constructor & Destructor Documentation

◆ LockedObject() [1/2]

template<typename T , typename Mutex = Mutex>
template<typename... Args>
CeresEngine::LockedObject< T, Mutex >::LockedObject ( Args &&...  args)
inline

Creates a new LockedObject instance by constructing a new object of type T by forwarding Args to the constructor.

Template Parameters
ArgsThe constructor argument types.
Parameters
argsThe argument arguments.

◆ LockedObject() [2/2]

template<typename T , typename Mutex = Mutex>
template<typename OtherMutex >
CeresEngine::LockedObject< T, Mutex >::LockedObject ( const LockedObject< T, OtherMutex > &  other)
inline

Creates a new LockedObject by copying the value of another.

Parameters
otherThe LockedObject to copy from.

Member Function Documentation

◆ copy()

template<typename T , typename Mutex = Mutex>
T CeresEngine::LockedObject< T, Mutex >::copy ( ) const
inline

Returns a copy of the locked object.

The copy is done safely with the mutex locked.

Returns
A copy of the contained value.

◆ lock() [1/2]

template<typename T , typename Mutex = Mutex>
auto CeresEngine::LockedObject< T, Mutex >::lock ( )
inline

Access the LockedObject contents by acquiring a temporary lock.

◆ lock() [2/2]

template<typename T , typename Mutex = Mutex>
auto CeresEngine::LockedObject< T, Mutex >::lock ( ) const
inline

Access the LockedObject contents by acquiring a temporary lock.

◆ operator()() [1/2]

template<typename T , typename Mutex = Mutex>
template<typename Func >
decltype(auto) CeresEngine::LockedObject< T, Mutex >::operator() ( Func &&  func)
inline

Executes a function Func within a synchronized block.

Template Parameters
FuncThe function to be called with the synchronized object.
Parameters
funcThe function to be called with the synchronized object.
Returns
The value returned by func, if any.

◆ operator()() [2/2]

template<typename T , typename Mutex = Mutex>
template<typename Func >
decltype(auto) CeresEngine::LockedObject< T, Mutex >::operator() ( Func &&  func) const
inline

Executes a function Func within a synchronized block.

This is similar to the non-const variant but acquires a shared lock and only calls func with a const value.

Template Parameters
FuncThe function to be called with the synchronized object.
Parameters
funcThe function to be called with the synchronized object.
Returns
The value returned by func, if any.

◆ operator*() [1/2]

template<typename T , typename Mutex = Mutex>
auto CeresEngine::LockedObject< T, Mutex >::operator* ( )
inline

Executes a function Func within a synchronized block by locking all objects.

Parameters
objectsThe objects to be synchronized.
funcThe function to be called with the synchronized objects.
Returns
The value returned by func, if any.

◆ operator*() [2/2]

template<typename T , typename Mutex = Mutex>
auto CeresEngine::LockedObject< T, Mutex >::operator* ( ) const
inline

Executes a function Func within a synchronized block by locking all objects.

Parameters
objectsThe objects to be synchronized.
funcThe function to be called with the synchronized objects.
Returns
The value returned by func, if any.

◆ operator->() [1/2]

template<typename T , typename Mutex = Mutex>
auto CeresEngine::LockedObject< T, Mutex >::operator-> ( )
inline

Executes a function Func within a synchronized block by locking all objects.

Parameters
objectsThe objects to be synchronized.
funcThe function to be called with the synchronized objects.
Returns
The value returned by func, if any.

◆ operator->() [2/2]

template<typename T , typename Mutex = Mutex>
auto CeresEngine::LockedObject< T, Mutex >::operator-> ( ) const
inline

Executes a function Func within a synchronized block by locking all objects.

Parameters
objectsThe objects to be synchronized.
funcThe function to be called with the synchronized objects.
Returns
The value returned by func, if any.

◆ operator=() [1/4]

Assigns the LockedObject by copying the value of another.

Parameters
otherThe LockedObject to copy from.

◆ operator=() [2/4]

template<typename T , typename Mutex = Mutex>
LockedObject & CeresEngine::LockedObject< T, Mutex >::operator= ( const T other)
inline

Assigns the LockedObject by copying a value.

Parameters
otherThe valued to be copy assigned.

◆ operator=() [3/4]

template<typename T , typename Mutex = Mutex>
template<typename OtherMutex >
LockedObject & CeresEngine::LockedObject< T, Mutex >::operator= ( LockedObject< T, OtherMutex > &&  other)
inlinenoexcept

Assigns the LockedObject by moving the value of another.

Parameters
otherThe LockedObject to move from.

◆ operator=() [4/4]

template<typename T , typename Mutex = Mutex>
LockedObject & CeresEngine::LockedObject< T, Mutex >::operator= ( T &&  other)
inlinenoexcept

Assigns the LockedObject by copying a value.

Parameters
otherThe valued to be copy assigned.

◆ operator|() [1/2]

template<typename T , typename Mutex = Mutex>
template<typename Func >
decltype(auto) CeresEngine::LockedObject< T, Mutex >::operator| ( Func &&  func)
inline

Executes a function Func within a synchronized block.

Template Parameters
FuncThe function to be called with the synchronized object.
Parameters
funcThe function to be called with the synchronized object.
Returns
The value returned by func, if any.

◆ operator|() [2/2]

template<typename T , typename Mutex = Mutex>
template<typename Func >
decltype(auto) CeresEngine::LockedObject< T, Mutex >::operator| ( Func &&  func) const
inline

Executes a function Func within a synchronized block.

This is similar to the non-const variant but acquires a shared lock and only calls func with a const value.

Template Parameters
FuncThe function to be called with the synchronized object.
Parameters
funcThe function to be called with the synchronized object.
Returns
The value returned by func, if any.

◆ with() [1/2]

template<typename T , typename Mutex = Mutex>
template<typename Func >
decltype(auto) CeresEngine::LockedObject< T, Mutex >::with ( Func &&  func)
inline

Executes a function Func within a synchronized block.

Template Parameters
FuncThe function to be called with the synchronized object.
Parameters
funcThe function to be called with the synchronized object.
Returns
The value returned by func, if any.

◆ with() [2/2]

template<typename T , typename Mutex = Mutex>
template<typename Func >
decltype(auto) CeresEngine::LockedObject< T, Mutex >::with ( Func &&  func) const
inline

Executes a function Func within a synchronized block.

This is similar to the non-const variant but acquires a shared lock and only calls func with a const value.

Template Parameters
FuncThe function to be called with the synchronized object.
Parameters
funcThe function to be called with the synchronized object.
Returns
The value returned by func, if any.

Friends And Related Symbol Documentation

◆ lock

template<typename T , typename Mutex = Mutex>
template<CLockedObject... Ts, typename Func >
decltype(auto) lock ( Func &&  func,
Ts &...  objects 
)
friend

Executes a function Func within a synchronized block by locking all objects.

Parameters
objectsThe objects to be synchronized.
funcThe function to be called with the synchronized objects.
Returns
The value returned by func, if any.

◆ LockedObject

template<typename T , typename Mutex = Mutex>
template<typename , typename >
friend class LockedObject
friend

Member Data Documentation

◆ mMutex

template<typename T , typename Mutex = Mutex>
Mutex CeresEngine::LockedObject< T, Mutex >::mMutex
mutableprivate

The mutex guarding the value.

◆ mValue

template<typename T , typename Mutex = Mutex>
T CeresEngine::LockedObject< T, Mutex >::mValue
private

The value that is locked and guarded by the mutex.


The documentation for this class was generated from the following file: