3#ifndef MRS_LIB_UTILITY_OWNING_MUTEX_HPP_
4#define MRS_LIB_UTILITY_OWNING_MUTEX_HPP_
23 template <
typename T,
typename Mutex = std::mutex>
24 requires(std::same_as<T, std::remove_cvref_t<T>> && std::default_initializable<Mutex>)
28 template <
bool Mutable>
29 class [[nodiscard]] OwningMutexGuard;
40 requires(std::default_initializable<T>)
80 return OwningMutexGuard<true>(*
this);
90 return OwningMutexGuard<false>(*
this);
98 [[nodiscard]] T
load()
const
100 std::lock_guard lock(mutex_);
114 requires(std::is_nothrow_move_assignable_v<T>)
116 std::lock_guard lock(mutex_);
117 data_ = std::move(val);
121 mutable Mutex mutex_;
139 template <
typename T,
typename Mutex>
140 requires(std::same_as<T, std::remove_cvref_t<T>> && std::default_initializable<Mutex>)
141 template <bool Mutable>
142 class OwningMutex<T, Mutex>::OwningMutexGuard
146 ~OwningMutexGuard() =
default;
149 OwningMutexGuard(
const OwningMutexGuard&) =
delete;
151 OwningMutexGuard& operator=(
const OwningMutexGuard&) =
delete;
153 OwningMutexGuard(OwningMutexGuard&&) =
delete;
155 OwningMutexGuard& operator=(OwningMutexGuard&&) =
delete;
168 return parent_.data_;
176 const T& operator*() const&
178 return parent_.data_;
182 T& operator*() && =
delete;
184 const T& operator*() const&& = delete;
197 return std::addressof(parent_.data_);
205 const T* operator->() const&
207 return std::addressof(parent_.data_);
211 T* operator->() && =
delete;
213 const T* operator->() const&& = delete;
223 explicit OwningMutexGuard(RefType parent) : lock_(parent.mutex_), parent_(parent)
227 std::lock_guard<Mutex> lock_;
A thread-safe wrapper for shared data with mutex protection.
Definition owning_mutex.hpp:26
T load() const
Loads the current value of the data.
Definition owning_mutex.hpp:98
OwningMutex()=default
Construct with default-initialized data.
OwningMutex(OwningMutex &&)=delete
OwningMutex is immovable.
OwningMutex(const T &val)
Construct by copying the data.
Definition owning_mutex.hpp:48
OwningMutex(const OwningMutex &)=delete
OwningMutex is immovable.
void store(T val)
Stores a new value into the data.
Definition owning_mutex.hpp:113
OwningMutex(T &&val)
Construct by moving the data.
Definition owning_mutex.hpp:57
OwningMutex & operator=(const OwningMutex &)=delete
OwningMutex is immovable.
OwningMutex & operator=(OwningMutex &&)=delete
OwningMutex is immovable.
OwningMutexGuard< false > acquire() const
Acquires a const guard for the OwningMutex.
Definition owning_mutex.hpp:88
OwningMutexGuard< true > acquire()
Acquires a mutable guard for the OwningMutex.
Definition owning_mutex.hpp:78
~OwningMutex()=default
Default destructor.
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24