Template Class RealtimeThreadSafeBox
Defined in File realtime_thread_safe_box.hpp
Class Documentation
-
template<class T, typename mutex_type = DEFAULT_MUTEX>
class RealtimeThreadSafeBox 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.
Public Functions
-
inline constexpr RealtimeThreadSafeBox(const RealtimeThreadSafeBox &o)
-
inline constexpr RealtimeThreadSafeBox &operator=(const RealtimeThreadSafeBox &o)
-
inline constexpr RealtimeThreadSafeBox(RealtimeThreadSafeBox &&o)
-
template<typename U = T>
inline constexpr RealtimeThreadSafeBox(const std::initializer_list<U> &init, std::enable_if_t<std::is_constructible_v<U, std::initializer_list<U>>>)
-
inline constexpr RealtimeThreadSafeBox &operator=(RealtimeThreadSafeBox &&o)
-
template<typename U = T>
inline std::enable_if_t<!is_ptr_or_smart_ptr<U>, bool> try_set(const T &value) set a new content with best effort
Note
disabled for pointer types
- Returns:
false if mutex could not be locked
-
inline bool try_set(const std::function<void(T&)> &func)
access the content readable with best effort
Note
only safe way to access pointer type content (rw)
- Returns:
false if the mutex could not be locked
-
template<typename U = T>
inline std::enable_if_t<!is_ptr_or_smart_ptr<U>, std::optional<U>> try_get() const get the content with best effort
- Returns:
std::nullopt if content could not be access, otherwise the content is returned
-
inline bool try_get(const std::function<void(const T&)> &func)
access the content (r) with best effort
Note
only safe way to access pointer type content (r)
- Returns:
false if the mutex could not be locked
-
template<typename U = T>
inline 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)
Note
disabled for pointer types
Note
same signature as in the existing RealtimeThreadSafeBox<T>
-
template<typename F, typename = std::enable_if_t<std::is_invocable_v<F, T&> && !std::is_invocable_v<F, T>>>
inline void set(F &&func) wait until the mutex could be locked and access the content (rw)
Note
Only accepts callables that take T& as argument (not by value).
-
template<typename U = T, typename = std::enable_if_t<is_ptr_or_smart_ptr<U>>>
inline void set(std::nullptr_t) wait until the mutex could be locked and access the content (rw)
Note
Overload to allow setting pointer types to nullptr directly.
-
template<typename U = T>
inline 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)
- Returns:
copy of the value
-
template<typename U = T>
inline 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)
Note
same signature as in the existing RealtimeThreadSafeBox<T>
-
inline void get(const std::function<void(const T&)> &func)
Wait until the mutex could be locked and access the content (r)
Note
only safe way to access pointer type content (r)
Note
same signature as in the existing RealtimeThreadSafeBox<T>
-
template<typename U = T>
inline std::enable_if_t<!is_ptr_or_smart_ptr<U>, void> operator=(const T &value) provide a custom assignment operator for easier usage
Note
only to be used from non-RT!
-
template<typename U = T, typename = typename std::enable_if_t<!is_ptr_or_smart_ptr<U>>>
inline operator T() const provide a custom conversion operator
Note
Can only be used from non-RT!
-
template<typename U = T, typename = typename std::enable_if_t<!is_ptr_or_smart_ptr<U>>>
inline operator std::optional<T>() const provide a custom conversion operator
Note
Can be used from non-RT and RT contexts
-
inline constexpr RealtimeThreadSafeBox(const RealtimeThreadSafeBox &o)