summaryrefslogtreecommitdiffstats
path: root/src/safe/safe.h
blob: 2beab55badf22a81805df13aae573448349eef3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
 * @file safe.h
 * @author L.-C. C.
 * @brief
 * @version 0.1
 * @date 2018-09-21
 *
 * @copyright Copyright (c) 2018
 *
 */

#pragma once

#include "accessmode.h"
#include "defaulttypes.h"
#include "mutableref.h"

#include <type_traits>
#include <utility>

#if __cplusplus >= 201703L
#define EXPLICIT_IF_CPP17 explicit
#define EXPLICITLY_CONSTRUCT_RETURN_TYPE_IF_CPP17 ReturnType
#else
#define EXPLICIT_IF_CPP17
#define EXPLICITLY_CONSTRUCT_RETURN_TYPE_IF_CPP17
#endif

namespace safe
{
	/**
	 * @brief Use this tag to default construct the mutex when constructing a
	 * Safe object.
	 */
	struct DefaultConstructMutex {};
	static constexpr DefaultConstructMutex default_construct_mutex;

	/**
	 * @brief Wraps a value together with a mutex.
	 *
	 * @tparam ValueType The type of the value to protect.
	 * @tparam MutexType The type of the mutex.
	 */
	template<typename ValueType, typename MutexType = DefaultMutex>
	class Safe
	{
	private:
		/// Type ValueType with reference removed, if present
		using RemoveRefValueType = typename std::remove_reference<ValueType>::type;
		/// Type MutexType with reference removed, if present
		using RemoveRefMutexType = typename std::remove_reference<MutexType>::type;

		/**
		 * @brief Manages a mutex and gives pointer-like access to a value
		 * object.
		 *
		 * @tparam LockType The type of the lock object that manages the
		 * mutex, example: std::lock_guard.
		 * @tparam Mode Determines the access mode of the Access
		 * object. Can be either AccessMode::ReadOnly or
		 * AccessMode::ReadWrite.
		 */
		template<template<typename> class LockType, AccessMode Mode>
		class Access
		{
			// Make sure AccessMode is ReadOnly if a read-only lock is used
			static_assert(!(AccessTraits<LockType<RemoveRefMutexType>>::IsReadOnly && Mode==AccessMode::ReadWrite), "Cannot have ReadWrite access mode with ReadOnly lock. Check the value of AccessTraits<LockType>::IsReadOnly if it exists.");

			/// ValueType with const qualifier if AccessMode is ReadOnly.
			using ConstIfReadOnlyValueType = typename std::conditional<Mode==AccessMode::ReadOnly, const RemoveRefValueType, RemoveRefValueType>::type;

		public:
			/// Pointer-to-const ValueType
			using ConstPointerType = const ConstIfReadOnlyValueType*;
			/// Pointer-to-const ValueType if Mode is ReadOnly, pointer to ValueType otherwise.
			using PointerType = ConstIfReadOnlyValueType*;
			/// Reference-to-const ValueType
			using ConstReferenceType = const ConstIfReadOnlyValueType&;
			/// Reference-to-const ValueType if Mode is ReadOnly, reference to ValueType otherwise.
			using ReferenceType = ConstIfReadOnlyValueType&;

			/**
			 * @brief Construct an Access object from a possibly const
			 * reference to the value object and any additionnal argument
			 * needed to construct the Lock object.
			 *
			 * @tparam LockArgs Deduced from lockArgs.
			 * @param value Reference to the value.
			 * @param lockArgs Arguments needed to construct the lock object.
			 */
			template<typename... OtherLockArgs>
			EXPLICIT_IF_CPP17
			Access(ReferenceType value, MutexType& mutex, OtherLockArgs&&... otherLockArgs):
				lock(mutex, std::forward<OtherLockArgs>(otherLockArgs)...),
				m_value(value)
			{}

			/**
			 * @brief Construct a read-only Access object from a const
			 * safe::Safe object and any additionnal argument needed to
			 * construct the Lock object.
			 *
			 * If needed, you can provide additionnal arguments to construct
			 * the lock object (such as std::adopt_lock). The mutex from the
			 * safe::Locakble object is already passed to the lock object's
			 * constructor though, you must not provide it.
			 *
			 * @tparam OtherLockArgs Deduced from otherLockArgs.
			 * @param safe The const Safe object to give protected access to.
			 * @param otherLockArgs Other arguments needed to construct the lock
			 * object.
			 */
			template<typename... OtherLockArgs>
			EXPLICIT_IF_CPP17
			Access(const Safe& safe, OtherLockArgs&&... otherLockArgs):
				Access(safe.m_value, safe.m_mutex.get, std::forward<OtherLockArgs>(otherLockArgs)...)
			{}

			/**
			 * @brief Construct a read-write Access object from a
			 * safe::Safe object and any additionnal argument needed to
			 * construct the Lock object.
			 *
			 * If needed, you can provide additionnal arguments to construct
			 * the lock object (such as std::adopt_lock). The mutex from the
			 * safe object is already passed to the lock object's constructor
			 * though, you must not provide it.
			 *
			 * @tparam OtherLockArgs Deduced from otherLockArgs.
			 * @param safe The Safe object to give protected access to.
			 * @param otherLockArgs Other arguments needed to construct the lock
			 * object.
			 */
			template<typename... OtherLockArgs>
			EXPLICIT_IF_CPP17
			Access(Safe& safe, OtherLockArgs&&... otherLockArgs):
				Access(safe.m_value, safe.m_mutex.get, std::forward<OtherLockArgs>(otherLockArgs)...)
			{}

			/**
			 * @brief Construct an Access object from another one.
			 * OtherLockType must implement release() like std::unique_lock
			 * does.
			 *
			 * @tparam OtherLockType Deduced from otherAccess.
			 * @tparam OtherMode Deduced from otherAccess.
			 * @tparam OtherLockArgs Deduced from otherLockArgs.
			 * @param otherAccess The Access object to construct from.
			 * @param otherLockArgs Other arguments needed to construct the lock
			 * object.
			 */
			template<template<typename> class OtherLockType, AccessMode OtherMode, typename... OtherLockArgs>
			EXPLICIT_IF_CPP17
			Access(Access<OtherLockType, OtherMode>& otherAccess, OtherLockArgs&&... otherLockArgs):
				Access(*otherAccess, *otherAccess.lock.release(), std::adopt_lock, std::forward<OtherLockArgs>(otherLockArgs)...)
			{
				static_assert(OtherMode == AccessMode::ReadWrite || OtherMode == Mode, "Cannot construct a ReadWrite Access object from a ReadOnly one!");
			}

			/**
			 * @brief Const accessor to the value.
			 * @return ConstPointerType Const pointer to the protected value.
			 */
			ConstPointerType operator->() const noexcept
			{
				return &m_value;
			}

			/**
			 * @brief Accessor to the value.
			 * @return ValuePointerType Pointer to the protected value.
			 */
			PointerType operator->() noexcept
			{
				return &m_value;
			}

			/**
			 * @brief Const accessor to the value.
			 * @return ConstValueReferenceType Const reference to the protected
			 * value.
			 */
			ConstReferenceType operator*() const noexcept
			{
				return m_value;
			}

			/**
			 * @brief Accessor to the value.
			 * @return ValueReferenceType Reference to the protected.
			 */
			ReferenceType operator*() noexcept
			{
				return m_value;
			}

			/// The lock that manages the mutex.
			mutable LockType<RemoveRefMutexType> lock;

		private:
			/// The protected value.
			ReferenceType m_value;
		};

		/// Reference-to-const ValueType.
		using ConstValueReferenceType = const RemoveRefValueType&;
		/// Reference to ValueType.
		using ValueReferenceType = RemoveRefValueType&;
		/// Reference to MutexType.
		using MutexReferenceType = RemoveRefMutexType&;

	public:
		/// Aliases to ReadAccess and WriteAccess classes for this Safe class.
		template<template<typename> class LockType=DefaultReadOnlyLock>
		using ReadAccess = Access<LockType, AccessMode::ReadOnly>;
		template<template<typename> class LockType=DefaultReadWriteLock>
		using WriteAccess = Access<LockType, AccessMode::ReadWrite>;

		/**
		 * @brief Construct a Safe object
		 */
		Safe() = default;

		/**
		 * @brief Construct a Safe object with default construction of
		 * the mutex and perfect forwarding of the other arguments to
		 * construct the value object.
		 *
		 * @tparam ValueArgs Deduced from valueArgs.
		 * @param valueArgs Perfect forwarding arguments to construct the value object.
		 * @param tag Indicates that the mutex should be default constructed.
		 */
		template<typename... ValueArgs>
		explicit Safe(DefaultConstructMutex, ValueArgs&&... valueArgs):
			m_mutex(),
			m_value(std::forward<ValueArgs>(valueArgs)...)
		{}
		/**
		 * @brief Construct a Safe object, forwarding the first
		 * argument to construct the mutex and the other arguments to
		 * construct the value object.
		 *
		 * @tparam MutexArg Deduced from mutexArg.
		 * @tparam ValueArgs Deduced from valueArgs.
		 * @param valueArgs Perfect forwarding arguments to construct the
		 * value object.
		 * @param mutexArg Perfect forwarding argument to construct the
		 * mutex object.
		 */
		template<typename MutexArg, typename... ValueArgs>
		explicit Safe(MutexArg&& mutexArg, ValueArgs&&... valueArgs):
			m_mutex{std::forward<MutexArg>(mutexArg)},
			m_value(std::forward<ValueArgs>(valueArgs)...)
		{}

		/// Delete all copy/move construction/assignment, as these operations
		/// require locking the mutex under the covers.
		/// Use copy(), assign() and other defined constructors to get the behavior
		/// you need with an explicit syntax.
		Safe(const Safe&) = delete;
		Safe(Safe&&) = delete;
		Safe& operator =(const Safe&) = delete;
		Safe& operator =(Safe&&) = delete;

		template<template<typename> class LockType=DefaultReadOnlyLock, typename... LockArgs>
		ReadAccess<LockType> readAccess(LockArgs&&... lockArgs) const
		{
			// using ReturnType = ReadAccess<LockType>;
			return EXPLICITLY_CONSTRUCT_RETURN_TYPE_IF_CPP17{*this, std::forward<LockArgs>(lockArgs)...};
		}

		template<template<typename> class LockType=DefaultReadWriteLock, typename... LockArgs>
		WriteAccess<LockType> writeAccess(LockArgs&&... lockArgs)
		{
			// using ReturnType = WriteAccess<LockType>;
			return EXPLICITLY_CONSTRUCT_RETURN_TYPE_IF_CPP17{*this, std::forward<LockArgs>(lockArgs)...};
		}

		template<template<typename> class LockType=DefaultReadOnlyLock, typename... LockArgs>
		RemoveRefValueType copy(LockArgs&&... lockArgs) const
		{
			return *readAccess<LockType>(std::forward<LockArgs>(lockArgs)...);
		}

		template<template<typename> class LockType=DefaultReadWriteLock, typename... LockArgs>
		void assign(ConstValueReferenceType value, LockArgs&&... lockArgs)
		{
			*writeAccess<LockType>(std::forward<LockArgs>(lockArgs)...) = value;
		}
		template<template<typename> class LockType=DefaultReadWriteLock, typename... LockArgs>
		void assign(RemoveRefValueType&& value, LockArgs&&... lockArgs)
		{
			*writeAccess<LockType>(std::forward<LockArgs>(lockArgs)...) = std::move(value);
		}

		/**
		 * @brief Unsafe const accessor to the value. If you use this
		 * function, you exit the realm of safe!
		 *
		 * @return ConstValueReferenceType Const reference to the value
		 * object.
		 */
		ConstValueReferenceType unsafe() const noexcept
		{
			return m_value;
		}
		/**
		 * @brief Unsafe accessor to the value. If you use this function,
		 * you exit the realm of safe!
		 *
		 * @return ValueReferenceType Reference to the value object.
		 */
		ValueReferenceType unsafe() noexcept
		{
			return m_value;
		}

		/**
		 * @brief Accessor to the mutex.
		 *
		 * @return MutexReferenceType Reference to the mutex.
		 */
		MutexReferenceType mutex() const noexcept
		{
			return m_mutex.get;
		}

	private:
		/// The helper object that holds the mutable mutex, or a reference to a mutex.
		impl::MutableIfNotReference<MutexType> m_mutex;
		/// The value to protect.
		ValueType m_value;
	};

	/**
	 * @brief Type alias for read-only Access.
	 *
	 * @tparam SafeType The type of Safe object to give read-only access to.
	 * @tparam LockType=DefaultReadOnlyLock The type of lock.
	 */
	template<
		typename SafeType,
		template<typename> class LockType=DefaultReadOnlyLock>
	using ReadAccess = typename SafeType::template ReadAccess<LockType>;

	/**
	 * @brief Type alias for read-write Access.
	 *
	 * @tparam SafeType The type of Safe object to give read-write access to.
	 * @tparam LockType=DefaultReadWriteLock The type of lock.
	 */
	template<
		typename SafeType,
		template<typename> class LockType=DefaultReadWriteLock>
	using WriteAccess = typename SafeType::template WriteAccess<LockType>;
}  // namespace safe

#undef EXPLICIT_IF_CPP17
#undef EXPLICITLY_CONSTRUCT_RETURN_TYPE_IF_CPP17