Sen API
Sen Libraries
Loading...
Searching...
No Matches
mutex_utils.h
Go to the documentation of this file.
1// === mutex_utils.h ===================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_BASE_MUTEX_UTILS_H
9#define SEN_CORE_BASE_MUTEX_UTILS_H
10
11// sen
13
14// std
15#include <functional>
16#include <mutex>
17
18namespace sen
19{
20
47template <typename T, typename MutexType = std::mutex>
48struct Guarded
49{
50 using UnderlyingValueTypeOfT = std::remove_reference_t<T>;
51
52 template <typename... TArgs>
53 explicit Guarded(TArgs... args): value_(std::forward<TArgs>(args)...)
54 {
55 }
56
59 {
60 std::lock_guard lock(mutex_);
61 return value_;
62 }
63
64public: // Operators
65 // NOLINTNEXTLINE(hicpp-explicit-conversions): intended to be used as a wrapper
66 operator UnderlyingValueTypeOfT() const { return getValue(); }
67
68 template <typename U, std::enable_if_t<std::is_convertible_v<U, T>, bool> = true>
69 Guarded& operator=(U&& newValue)
70 {
71 std::lock_guard lock(mutex_);
72 value_ = std::forward<U>(newValue);
73 return *this;
74 }
75
76 template <typename U>
77 T operator+(U&& rhs) const
78 {
79 std::lock_guard lock(mutex_);
80 return value_ + rhs;
81 }
82
83 template <typename U>
85 {
86 std::lock_guard lock(mutex_);
87 value_ += rhs;
88 return *this;
89 }
90
91public: // Access token handling
100 {
101 TemporaryAccessToken(T* valuePtr, MutexType& m): valuePtr_(valuePtr), lock_(m) {}
102
103 std::conditional_t<std::is_pointer_v<T>, T&, T*> operator->()
104 {
105 if constexpr (std::is_pointer_v<T>)
106 {
107 return *valuePtr_;
108 }
109 else
110 {
111 return valuePtr_;
112 }
113 }
114
115 private:
116 T* valuePtr_;
117 std::lock_guard<MutexType> lock_;
118 };
119
121
122 [[nodiscard]] TemporaryAccessToken createAccessToken() { return TemporaryAccessToken(&value_, mutex_); }
123
124public:
128 template <typename CallableType>
129 auto invoke(CallableType callable)
130 {
131 std::lock_guard lock(mutex_);
132 return std::invoke(callable, value_);
133 }
134
135public: // Enable comparisions between wrapped and unwrapped types
136 template <
137 typename LHSType,
138 typename RHSType,
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
141 {
142 if constexpr (std::is_same_v<LHSType, Guarded> && std::is_same_v<RHSType, Guarded>)
143 {
144 return lhs.getValue() == rhs.getValue();
145 }
146 else if constexpr (std::is_same_v<LHSType, Guarded>)
147 {
148 return lhs.getValue() == rhs;
149 }
150 else
151 {
152 return lhs == rhs.getValue();
153 }
154
156 return false;
157 }
158
159 template <
160 typename LHSType,
161 typename RHSType,
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
164 {
165 return !(lhs == rhs);
166 }
167
168private:
169 T value_;
170 mutable MutexType mutex_;
171};
172
173} // namespace sen
174
175#endif // SEN_CORE_BASE_MUTEX_UTILS_H
#define SEN_UNREACHABLE()
Definition compiler_macros.h:420
Definition assert.h:17
STL namespace.
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