ros2_control - jazzy
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
realtime_tools::RealtimeThreadSafeBox< T, mutex_type > Class Template Reference

#include <realtime_thread_safe_box.hpp>

Public Types

using mutex_t = mutex_type
 
using type = T
 

Public Member Functions

constexpr RealtimeThreadSafeBox (const T &init=T{})
 
constexpr RealtimeThreadSafeBox (const T &&init)
 
constexpr RealtimeThreadSafeBox (const RealtimeThreadSafeBox &o)
 
constexpr RealtimeThreadSafeBoxoperator= (const RealtimeThreadSafeBox &o)
 
constexpr RealtimeThreadSafeBox (RealtimeThreadSafeBox &&o)
 
template<typename U = T>
constexpr RealtimeThreadSafeBox (const std::initializer_list< U > &init, std::enable_if_t< std::is_constructible_v< U, std::initializer_list< U > > >)
 
constexpr RealtimeThreadSafeBoxoperator= (RealtimeThreadSafeBox &&o)
 
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, bool > try_set (const T &value)
 set a new content with best effort
 
bool try_set (const std::function< void(T &)> &func)
 access the content readable with best effort
 
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, bool > trySet (const T &value)
 set a new content with best effort
 
template<typename U = T>
bool trySet (const std::function< void(T &)> &func)
 access the content readable with best effort
 
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, std::optional< U > > try_get () const
 get the content with best effort
 
bool try_get (const std::function< void(const T &)> &func)
 access the content (r) with best effort
 
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, std::optional< U > > tryGet () const
 get the content with best effort
 
template<typename U = T>
bool tryGet (const std::function< void(const T &)> &func)
 access the content (r) with best effort
 
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, void > set (const T &value)
 Wait until the mutex can be locked and set the content (RealtimeThreadSafeBox behavior)
 
template<typename U = T>
std::enable_if_t< is_ptr_or_smart_ptr< U >, void > set (const T &value)
 Wait until the mutex can be locked and set the content (RealtimeThreadSafeBox behavior)
 
template<typename F , typename = std::enable_if_t<std::is_invocable_v<F, T &> && !std::is_invocable_v<F, T>>>
void set (F &&func)
 wait until the mutex could be locked and access the content (rw)
 
template<typename U = T, typename = std::enable_if_t<is_ptr_or_smart_ptr<U>>>
void set (std::nullptr_t)
 wait until the mutex could be locked and access the content (rw)
 
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, U > get () const
 Wait until the mutex could be locked and get the content (RealtimeThreadSafeBox behaviour)
 
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, void > get (T &in) const
 Wait until the mutex could be locked and get the content (r)
 
template<typename U = T>
std::enable_if_t< is_ptr_or_smart_ptr< U >, void > get (T &in) const
 Wait until the mutex could be locked and get the content (r)
 
void get (const std::function< void(const T &)> &func)
 Wait until the mutex could be locked and access the content (r)
 
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, void > operator= (const T &value)
 provide a custom assignment operator for easier usage
 
template<typename U = T, typename = typename std::enable_if_t<!is_ptr_or_smart_ptr<U>>>
 operator T () const
 provide a custom conversion operator
 
template<typename U = T, typename = typename std::enable_if_t<!is_ptr_or_smart_ptr<U>>>
 operator std::optional< T > () const
 provide a custom conversion operator
 
const mutex_t & get_mutex () const
 
mutex_t & get_mutex ()
 
mutex_t & getMutex ()
 
const mutex_t & getMutex () const
 

Detailed Description

template<class T, typename mutex_type = DEFAULT_MUTEX>
class realtime_tools::RealtimeThreadSafeBox< T, mutex_type >

A Box that ensures thread safe access to the boxed contents. Access is best effort. If it can not lock it will return.

NOTE about pointers: You can use pointers with this box but the access will be different. Only use the get/set methods that take function pointer for accessing the internal value.

Member Function Documentation

◆ get() [1/4]

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, U > realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::get ( ) const
inline

Wait until the mutex could be locked and get the content (RealtimeThreadSafeBox behaviour)

Returns
copy of the value

◆ get() [2/4]

template<class T , typename mutex_type = DEFAULT_MUTEX>
void realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::get ( const std::function< void(const T &)> &  func)
inline

Wait until the mutex could be locked and access the content (r)

Note
only safe way to access pointer type content (r)
same signature as in the existing RealtimeThreadSafeBox<T>

◆ get() [3/4]

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, void > realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::get ( T &  in) const
inline

Wait until the mutex could be locked and get the content (r)

Note
same signature as in the existing RealtimeThreadSafeBox<T>

◆ get() [4/4]

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T>
std::enable_if_t< is_ptr_or_smart_ptr< U >, void > realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::get ( T &  in) const
inline

Wait until the mutex could be locked and get the content (r)

Note
same signature as in the existing RealtimeThreadSafeBox<T>
Not the safest way to access pointer type content (r)
Deprecated:
Use get(const std::function<void(const T &)> & func) instead!

◆ operator std::optional< T >()

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T, typename = typename std::enable_if_t<!is_ptr_or_smart_ptr<U>>>
realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::operator std::optional< T > ( ) const
inline

provide a custom conversion operator

Note
Can be used from non-RT and RT contexts

◆ operator T()

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T, typename = typename std::enable_if_t<!is_ptr_or_smart_ptr<U>>>
realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::operator T ( ) const
inline

provide a custom conversion operator

Note
Can only be used from non-RT!

◆ operator=()

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, void > realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::operator= ( const T &  value)
inline

provide a custom assignment operator for easier usage

Note
only to be used from non-RT!

◆ set() [1/4]

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, void > realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::set ( const T &  value)
inline

Wait until the mutex can be locked and set the content (RealtimeThreadSafeBox behavior)

Note
disabled for pointer types
same signature as in the existing RealtimeThreadSafeBox<T>

◆ set() [2/4]

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T>
std::enable_if_t< is_ptr_or_smart_ptr< U >, void > realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::set ( const T &  value)
inline

Wait until the mutex can be locked and set the content (RealtimeThreadSafeBox behavior)

Note
same signature as in the existing RealtimeThreadSafeBox<T>
Not the safest way to access pointer type content (rw)
Deprecated:
Use set(const std::function<void(T &)> & func) instead!

◆ set() [3/4]

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename F , typename = std::enable_if_t<std::is_invocable_v<F, T &> && !std::is_invocable_v<F, T>>>
void realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::set ( F &&  func)
inline

wait until the mutex could be locked and access the content (rw)

Note
Only accepts callables that take T& as argument (not by value).

◆ set() [4/4]

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T, typename = std::enable_if_t<is_ptr_or_smart_ptr<U>>>
void realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::set ( std::nullptr_t  )
inline

wait until the mutex could be locked and access the content (rw)

Note
Overload to allow setting pointer types to nullptr directly.

◆ try_get() [1/2]

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, std::optional< U > > realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::try_get ( ) const
inline

get the content with best effort

Returns
std::nullopt if content could not be access, otherwise the content is returned

◆ try_get() [2/2]

template<class T , typename mutex_type = DEFAULT_MUTEX>
bool realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::try_get ( const std::function< void(const T &)> &  func)
inline

access the content (r) with best effort

Returns
false if the mutex could not be locked
Note
only safe way to access pointer type content (r)

◆ try_set() [1/2]

template<class T , typename mutex_type = DEFAULT_MUTEX>
bool realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::try_set ( const std::function< void(T &)> &  func)
inline

access the content readable with best effort

Returns
false if the mutex could not be locked
Note
only safe way to access pointer type content (rw)

◆ try_set() [2/2]

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, bool > realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::try_set ( const T &  value)
inline

set a new content with best effort

Returns
false if mutex could not be locked
Note
disabled for pointer types

◆ tryGet() [1/2]

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, std::optional< U > > realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::tryGet ( ) const
inline

get the content with best effort

Returns
std::nullopt if content could not be access, otherwise the content is returned
Deprecated:
Use try_get() instead!

◆ tryGet() [2/2]

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T>
bool realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::tryGet ( const std::function< void(const T &)> &  func)
inline

access the content (r) with best effort

Returns
false if the mutex could not be locked
Note
only safe way to access pointer type content (r)
Deprecated:
Use try_get(const std::function<void(const T &)> & func) instead!

◆ trySet() [1/2]

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T>
bool realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::trySet ( const std::function< void(T &)> &  func)
inline

access the content readable with best effort

Returns
false if the mutex could not be locked
Note
only safe way to access pointer type content (rw)
Deprecated:
Use try_set(const std::function<void(T &)> & func) instead!

◆ trySet() [2/2]

template<class T , typename mutex_type = DEFAULT_MUTEX>
template<typename U = T>
std::enable_if_t<!is_ptr_or_smart_ptr< U >, bool > realtime_tools::RealtimeThreadSafeBox< T, mutex_type >::trySet ( const T &  value)
inline

set a new content with best effort

Returns
false if mutex could not be locked
Note
disabled for pointer types
Deprecated:
Use try_set(const T & value) instead!

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