8#ifndef SEN_CORE_BASE_MUTEX_UTILS_H
9#define SEN_CORE_BASE_MUTEX_UTILS_H
47template <
typename T,
typename MutexType = std::mutex>
52 template <
typename... TArgs>
53 explicit Guarded(TArgs... args): value_(
std::forward<TArgs>(args)...)
60 std::lock_guard lock(mutex_);
68 template <
typename U, std::enable_if_t<std::is_convertible_v<U, T>,
bool> = true>
71 std::lock_guard lock(mutex_);
72 value_ = std::forward<U>(newValue);
79 std::lock_guard lock(mutex_);
86 std::lock_guard lock(mutex_);
103 std::conditional_t<std::is_pointer_v<T>, T&, T*>
operator->()
105 if constexpr (std::is_pointer_v<T>)
117 std::lock_guard<MutexType> lock_;
128 template <
typename CallableType>
131 std::lock_guard lock(mutex_);
132 return std::invoke(callable, value_);
139 std::enable_if_t<std::disjunction_v<std::is_same<LHSType, Guarded>, std::is_same<RHSType, Guarded>>,
bool> =
true>
140 friend inline bool operator==(
const LHSType& lhs,
const RHSType& rhs)
noexcept
142 if constexpr (std::is_same_v<LHSType, Guarded> && std::is_same_v<RHSType, Guarded>)
144 return lhs.getValue() == rhs.getValue();
146 else if constexpr (std::is_same_v<LHSType, Guarded>)
148 return lhs.getValue() == rhs;
152 return lhs == rhs.getValue();
162 std::enable_if_t<std::disjunction_v<std::is_same<LHSType, Guarded>, std::is_same<RHSType, Guarded>>,
bool> =
true>
163 friend inline bool operator!=(
const LHSType& lhs,
const RHSType& rhs)
noexcept
165 return !(lhs == rhs);
170 mutable MutexType mutex_;
#define SEN_UNREACHABLE()
Definition compiler_macros.h:420
Access token that enables the user to interact with the protected object while simultaneously holding...
Definition mutex_utils.h:100
std::conditional_t< std::is_pointer_v< T >, T &, T * > operator->()
Definition mutex_utils.h:103
TemporaryAccessToken(T *valuePtr, MutexType &m)
Definition mutex_utils.h:101
friend bool operator!=(const LHSType &lhs, const RHSType &rhs) noexcept
Definition mutex_utils.h:163
Guarded(TArgs... args)
Definition mutex_utils.h:53
Guarded & operator=(U &&newValue)
Definition mutex_utils.h:69
T operator+(U &&rhs) const
Definition mutex_utils.h:77
Guarded & operator+=(U &&rhs)
Definition mutex_utils.h:84
UnderlyingValueTypeOfT getValue() const
Returns a copy of the protected value.
Definition mutex_utils.h:58
std::remove_reference_t< T > UnderlyingValueTypeOfT
Definition mutex_utils.h:50
TemporaryAccessToken operator->()
Definition mutex_utils.h:120
auto invoke(CallableType callable)
Immediately invokes the given callable while holding a block, passing in the protected value.
Definition mutex_utils.h:129
TemporaryAccessToken createAccessToken()
Definition mutex_utils.h:122
friend bool operator==(const LHSType &lhs, const RHSType &rhs) noexcept
Definition mutex_utils.h:140