header_utils
Loading...
Searching...
No Matches
ghassanpl::protected_object< T, MUTEX_TYPE > Struct Template Reference

A class that stores an object to which access is protected via a mutexlike; all operations are thread-safe unless specified otherwise. More...

#include <threading.h>

Public Types

using object_type
using reference
using const_reference
using mutex_type

Public Member Functions

T get () const
 Returns a copy of the object.
T take ()
 Takes the value of the object via move.
template<typename U>
T exchange (U &&u)
 Exchanges the value of the object with u and returns the old value.
template<typename FUNC>
auto read_only_access (FUNC &&func) const
 Calls func with a const reference to the object which can be safely accessed; only available if the mutex is a shared mutex.
template<typename U = T>
void set (U &&value)
 Sets a new value for the object.
template<typename FUNC>
auto mutate_in_place (FUNC &&func)
 Calls func with a reference to the object which can be safely mutated.
template<typename FUNC>
void mutate_by_copy (FUNC &&func)
 Calls func with a reference to a copy of the object, but not under the protection of the mutex.
mutex_type const & mutex () const

Static Public Attributes

static constexpr bool is_shared
static constexpr bool is_timed

Protected Attributes

T m_value
mutex_type m_mutex

Detailed Description

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
struct ghassanpl::protected_object< T, MUTEX_TYPE >

A class that stores an object to which access is protected via a mutexlike; all operations are thread-safe unless specified otherwise.

Definition at line 93 of file threading.h.

Member Typedef Documentation

◆ const_reference

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
using ghassanpl::protected_object< T, MUTEX_TYPE >::const_reference

Definition at line 100 of file threading.h.

◆ mutex_type

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
using ghassanpl::protected_object< T, MUTEX_TYPE >::mutex_type

Definition at line 101 of file threading.h.

◆ object_type

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
using ghassanpl::protected_object< T, MUTEX_TYPE >::object_type

Definition at line 98 of file threading.h.

◆ reference

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
using ghassanpl::protected_object< T, MUTEX_TYPE >::reference

Definition at line 99 of file threading.h.

Member Function Documentation

◆ exchange()

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
template<typename U>
T ghassanpl::protected_object< T, MUTEX_TYPE >::exchange ( U && u)
inline

Exchanges the value of the object with u and returns the old value.

Definition at line 119 of file threading.h.

◆ get()

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
T ghassanpl::protected_object< T, MUTEX_TYPE >::get ( ) const
inline

Returns a copy of the object.

Definition at line 106 of file threading.h.

◆ mutate_by_copy()

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
template<typename FUNC>
void ghassanpl::protected_object< T, MUTEX_TYPE >::mutate_by_copy ( FUNC && func)
inline

Calls func with a reference to a copy of the object, but not under the protection of the mutex.

You can modify this copy safely. After func returns, the mutated copy will be set as the new value.

Definition at line 151 of file threading.h.

◆ mutate_in_place()

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
template<typename FUNC>
auto ghassanpl::protected_object< T, MUTEX_TYPE >::mutate_in_place ( FUNC && func)
inline

Calls func with a reference to the object which can be safely mutated.

Definition at line 142 of file threading.h.

◆ mutex()

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
mutex_type const & ghassanpl::protected_object< T, MUTEX_TYPE >::mutex ( ) const
inline

Definition at line 159 of file threading.h.

◆ read_only_access()

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
template<typename FUNC>
auto ghassanpl::protected_object< T, MUTEX_TYPE >::read_only_access ( FUNC && func) const
inline

Calls func with a const reference to the object which can be safely accessed; only available if the mutex is a shared mutex.

Definition at line 127 of file threading.h.

◆ set()

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
template<typename U = T>
void ghassanpl::protected_object< T, MUTEX_TYPE >::set ( U && value)
inline

Sets a new value for the object.

Definition at line 135 of file threading.h.

◆ take()

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
T ghassanpl::protected_object< T, MUTEX_TYPE >::take ( )
inline

Takes the value of the object via move.

Definition at line 112 of file threading.h.

Member Data Documentation

◆ is_shared

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
bool ghassanpl::protected_object< T, MUTEX_TYPE >::is_shared
staticconstexpr

Definition at line 102 of file threading.h.

◆ is_timed

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
bool ghassanpl::protected_object< T, MUTEX_TYPE >::is_timed
staticconstexpr

Definition at line 103 of file threading.h.

◆ m_mutex

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
mutex_type ghassanpl::protected_object< T, MUTEX_TYPE >::m_mutex
mutableprotected

Definition at line 164 of file threading.h.

◆ m_value

template<typename T, mutexlike MUTEX_TYPE = std::mutex>
T ghassanpl::protected_object< T, MUTEX_TYPE >::m_value
protected

Definition at line 163 of file threading.h.