ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
owner.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_lang of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace lang {
9
10/// Ensures that an object of template type \p{TOwnable} is acquired and properly released when
11/// unwinding the stack.
12/// This class is meant to be allocated only on the stack and not on the heap.
13/// Therefore, the new operators as well as copy and move constructors and assignment operators
14/// are declared private.
15///
16/// With debug-builds, the constructor expects caller source information parameters
17/// of type \b CallerInfo.
18/// Use macro \ref ALIB_CALLER_PRUNED to prune those with release-builds.
19///
20/// \see
21/// - Several sibling types exist.
22/// They are described and listed in chapter \ref alib_threads_locks_auto_owner of the
23/// Programmer's Manual of module \alib_threads.
24/// - Besides generic preprocessor macro \ref ALIB_OWN, several application-specific alias macros
25/// exist.
26/// For example, macros
27/// - \ref ALIB_LOCK,
28/// - \ref ALIB_LOCK_WITH,
29/// - \ref ALIB_DCS, or
30/// - \ref ALIB_DCS_WITH
31///
32/// provide a convenient and "speaking" way to use this class.
33/// - Chapter \ref alib_manual_appendix_callerinfo of the General Programmer's Manual.
34///
35/// @tparam TOwnable The type to own.
36/// Requirements are to have methods \b %Acquire and \b %Release available.
37template <typename TOwnable>
38class Owner
39{
41
42 protected:
43 TOwnable& owned; ///< The resource to acquire and release.
44 #if ALIB_DEBUG
45 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
46 #endif
47
48 #if !DOXYGEN
49 // choosing the right release method, with or without caller parameters.
50 #if ALIB_DEBUG
51 template<typename TRequires= TOwnable>
52 requires ( ALIB_HAS_METHOD(TRequires, Release, dbgCI))
53 void callRelease() { owned.Release(dbgCI); }
54 #endif
55
56 template<typename TRequires= TOwnable>
57 requires ( ALIB_HAS_METHOD(TRequires, Release, ))
58 void callRelease() { owned.Release(); }
59 #endif
60 public:
61
62 /// Constructor. Invokes Acquire() on the owner.
63 /// @param ownable The ownable to acquire.
64 /// @param ci Caller information.
65 #if ALIB_DEBUG
66 Owner( TOwnable& ownable, const CallerInfo& ci)
67 : owned(ownable)
68 , dbgCI(ci) { ownable.Acquire( ci ); }
69 #else
70 Owner( TOwnable& ownable ) : owned(ownable) { ownable.Acquire(); }
71 #endif
72
73
74 /// Destructor. Releases the owner by invoking Release().
75 ~Owner() { callRelease(); }
76}; //class Owner
77
78/// Similar to class \alib{lang;Owner}, but calls method \b TryAcquire instead of \b Acquire.
79/// The result is retrievable with method #IsOwning().
80/// @see Chapter \ref alib_threads_locks_auto_owner of the Programmer's Manual of module
81/// \alib_threads
82/// @tparam TOwnable The type to own.
83/// Requirements are to have methods \b %TryAcquire and \b %Release available.
84template <typename TOwnable>
86{
88
89 protected:
90 TOwnable& owned; ///< The resource to acquire and release.
91 bool isOwning; ///< The result of the call to \b TryAcquire.
92 #if ALIB_DEBUG
93 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
94 #endif
95
96 #if !DOXYGEN
97 // choosing the right release method, with or without caller parameters.
98 #if ALIB_DEBUG
99 template<typename TRequires= TOwnable>
100 requires ( ALIB_HAS_METHOD(TRequires, Release, dbgCI))
101 void callRelease() { owned.Release(dbgCI); }
102 #endif
103
104 template<typename TRequires= TOwnable>
105 requires ( ALIB_HAS_METHOD(TRequires, Release, ))
106 void callRelease() { owned.Release(); }
107
108 #if ALIB_DEBUG
109 template<typename TRequires= TOwnable>
110 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive, dbgCI))
111 void callRelease() { owned.ReleaseRecursive(dbgCI); }
112 #endif
113
114 template<typename TRequires= TOwnable>
115 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive, ))
116 void callRelease() { owned.ReleaseRecursive(); }
117
118 #endif
119 public:
120
121 /// Constructor. Invokes TryAcquire() on member #owned.
122 /// @param ownable The ownable to acquire.
123 /// @param ci Caller information.
124 #if ALIB_DEBUG
125 OwnerTry( TOwnable& ownable, const CallerInfo& ci)
126 : owned(ownable)
127 , dbgCI(ci) { isOwning= ownable.TryAcquire(ci); }
128 #else
129 OwnerTry( TOwnable& ownable )
130 : owned(ownable) { isOwning= ownable.TryAcquire(); }
131 #endif
132
133
134 /// Destructor. Invokes Release() on member #owned.
135 ~OwnerTry() { if (isOwning) callRelease(); }
136
137 /// @return \c true if the try to acquire the #owned with construction of this type was
138 /// successful, \c false otherwise.
139 bool IsOwning() const noexcept { return isOwning; }
140}; //class OwnerTry
141
142/// Similar to class \alib{lang;Owner}, but calls method \b TryAcquireTimed instead of \b Acquire.
143/// The result is retrievable with method #IsOwning().
144/// @see Chapter \ref alib_threads_locks_auto_owner of the Programmer's Manual of module
145/// \alib_threads
146/// @tparam TOwnable The type to own.
147/// Requirements are to have methods \b %TryAcquireTimed and \b %Release available.
148template <typename TOwnable>
150{
152
153 protected:
154 TOwnable& owned; ///< The resource to acquire and release.
155 bool isOwning; ///< The result of the call to \b TryAcquire.
156 #if ALIB_DEBUG
157 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
158 #endif
159
160 #if !DOXYGEN
161 // choosing the right release method, with or without caller parameters.
162 #if ALIB_DEBUG
163 template<typename TRequires= TOwnable>
164 requires ( ALIB_HAS_METHOD(TRequires, Release, dbgCI))
165 void callRelease() { owned.Release(dbgCI); }
166 #endif
167
168 template<typename TRequires= TOwnable>
169 requires ( ALIB_HAS_METHOD(TRequires, Release, ))
170 void callRelease() { owned.Release(); }
171
172 // choosing the right release method, with or without caller parameters.
173 #if ALIB_DEBUG
174 template<typename TRequires= TOwnable>
175 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive, dbgCI))
176 void callRelease() { owned.ReleaseRecursive(dbgCI); }
177 #endif
178
179 template<typename TRequires= TOwnable>
180 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive, ))
181 void callRelease() { owned.ReleaseRecursive(); }
182
183 #endif
184 public:
185
186 /// Constructor. Invokes TryAcquire() on member #owned.
187 /// @tparam TTimeValue Type of time parameter accepted with construction and passed to
188 /// method <b>TOwnable::TryAcquireTimed</b>.<br>
189 /// Usually this is type \alib{time;Ticks} or
190 /// \alib{time;TimePointBase::Duration;Ticks::Duration}.
191 /// @param ownable The ownable to acquire.
192 /// @param time The duration to wait for, or point in time to wait until.
193 /// @param ci Caller information.
194 #if ALIB_DEBUG
195 template<typename TTimeValue>
196 OwnerTimed( TOwnable& ownable, const TTimeValue& time, const CallerInfo& ci)
197 : owned(ownable)
198 , dbgCI(ci) { isOwning= ownable.TryAcquireTimed( time,ci); }
199 #else
200 template<typename TTimeValue>
201 OwnerTimed( TOwnable& ownable, const TTimeValue& time )
202 : owned(ownable) { isOwning= ownable.TryAcquireTimed( time ); }
203 #endif
204
205
206 /// Destructor. Invokes Release() on member #owned.
207 ~OwnerTimed() { if (isOwning) callRelease(); }
208
209 /// @return \c true if the try to acquire the #owned with construction of this type was
210 /// successful, \c false otherwise.
211 bool IsOwning() const noexcept { return isOwning; }
212}; //class OwnerTimed
213
214
215/// Similar to class \alib{lang;Owner}, but calls methods \b AcquireRecursive and
216/// \b ReleaseRecursive.
217/// @see Chapter \ref alib_threads_locks_auto_owner of the Programmer's Manual of module
218/// \alib_threads
219/// @tparam TOwnable The type to own.
220/// Requirements are to have methods \b %AcquireRecursive and \b %ReleaseRecursive
221/// available.
222template <typename TOwnable>
224{
226
227 protected:
228 TOwnable& owned; ///< The resource to acquire and release.
229 #if ALIB_DEBUG
230 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
231 #endif
232
233 #if !DOXYGEN
234 // choosing the right release method, with or without caller parameters.
235 #if ALIB_DEBUG
236 template<typename TRequires= TOwnable>
237 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive,dbgCI))
238 void callRelease() { owned.ReleaseRecursive(dbgCI); }
239 #endif
240
241 template<typename TRequires= TOwnable>
242 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive, ))
243 void callRelease() { owned.ReleaseRecursive(); }
244 #endif
245 public:
246
247 /// Constructor. Invokes AcquireRecursive() on member #owned.
248 /// @param ownable The ownable to acquire.
249 /// @param ci Caller information.
250 #if ALIB_DEBUG
251 OwnerRecursive( TOwnable& ownable, const CallerInfo& ci)
252 : owned(ownable)
253 , dbgCI(ci) { ownable.AcquireRecursive(ci); }
254 #else
255 OwnerRecursive( TOwnable& ownable ) : owned(ownable) { ownable.AcquireRecursive(); }
256 #endif
257
258
259 /// Destructor. Invokes ReleaseRecursive() on member #owned.
260 ~OwnerRecursive() { callRelease(); }
261}; //class OwnerRecursive
262
263/// Similar to class \alib{lang;Owner}, but calls methods \b AcquireShared and
264/// \b ReleaseShared.
265/// @see Chapter \ref alib_threads_locks_auto_owner of the Programmer's Manual of module
266/// \alib_threads
267/// @tparam TOwnable The type to own.
268/// Requirements are to have methods \b %AcquireShared and \b %ReleaseShared
269/// available.
270template <typename TOwnable>
272{
274
275 protected:
276 TOwnable& owned; ///< The resource to acquire and release.
277 #if ALIB_DEBUG
278 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
279 #endif
280
281 #if !DOXYGEN
282 // choosing the right release method, with or without caller parameters.
283 #if ALIB_DEBUG
284 template<typename TRequires= TOwnable>
285 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, dbgCI))
286 void callRelease() const { owned.ReleaseShared(dbgCI); }
287 #endif
288
289 template<typename TRequires= TOwnable>
290 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, ))
291 void callRelease() const { owned.ReleaseShared(); }
292 #endif
293 public:
294
295 /// Constructor. Invokes AcquireShared() on member #owned.
296 /// @param ownable The ownable to acquire.
297 /// @param ci Caller information.
298 #if ALIB_DEBUG
299 OwnerShared( TOwnable& ownable, const CallerInfo& ci)
300 : owned(ownable)
301 , dbgCI(ci) { ownable.AcquireShared(ci); }
302 #else
303 OwnerShared( TOwnable& ownable ) : owned(ownable) { ownable.AcquireShared(); }
304 #endif
305
306
307 /// Destructor. Invokes ReleaseShared() on member #owned.
308 ~OwnerShared() { callRelease(); }
309}; //class OwnerShared
310
311/// Similar to class \alib{lang;Owner}, but calls method \b TryAcquireShared instead of \b Acquire
312/// and \b ReleaseShared instead of \b Release.
313/// The result is retrievable with method #IsOwning().
314/// @see Chapter \ref alib_threads_locks_auto_owner of the Programmer's Manual of module
315/// \alib_threads
316/// @tparam TOwnable The type to own.
317/// Requirements are to have methods \b %TryAcquireShared and \b %ReleaseShared
318/// available.
319template <typename TOwnable>
321{
323
324 protected:
325 TOwnable& owned; ///< The resource to acquire and release.
326 bool isOwning; ///< The result of the call to \b TryAcquire.
327 #if ALIB_DEBUG
328 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
329 #endif
330
331 #if !DOXYGEN
332 // choosing the right release method, with or without caller parameters.
333 #if ALIB_DEBUG
334 template<typename TRequires= TOwnable>
335 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, dbgCI))
336 void callRelease() { owned.ReleaseShared(dbgCI); }
337 #endif
338
339 template<typename TRequires= TOwnable>
340 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, ))
341 void callRelease() { owned.ReleaseShared(); }
342 #endif
343 public:
344
345 /// Constructor. Invokes TryAcquire() on member #owned.
346 /// @param ownable The ownable to acquire.
347 /// @param ci Caller information.
348 #if ALIB_DEBUG
349 OwnerTryShared( TOwnable& ownable, const CallerInfo& ci)
350 : owned(ownable)
351 , dbgCI(ci) { isOwning= ownable.TryAcquireShared(ci); }
352 #else
353 OwnerTryShared( TOwnable& ownable )
354 : owned(ownable) { isOwning= ownable.TryAcquireShared(); }
355 #endif
356
357
358 /// Destructor. Invokes Release() on member #owned.
359 ~OwnerTryShared() { if (isOwning) callRelease(); }
360
361 /// @return \c true if the try to acquire the #owned with construction of this type was
362 /// successful, \c false otherwise.
363 bool IsOwning() const noexcept { return isOwning; }
364}; //class OwnerTryShared
365
366
367/// Similar to class \alib{lang;Owner}, but calls method \b TryAcquireSharedTimed instead
368/// of \b Acquire and \b ReleaseShared instead of \b Shared.
369/// The result is retrievable with method #IsOwning().
370/// @see Chapter \ref alib_threads_locks_auto_owner of the Programmer's Manual of module
371/// \alib_threads
372/// @tparam TOwnable The type to own.
373/// Requirements are to have methods \b %TryAcquireSharedTimed and
374/// \b %ReleaseShared available.
375template <typename TOwnable>
377{
379
380 protected:
381 TOwnable& owned; ///< The resource to acquire and release.
382 bool isOwning; ///< The result of the call to \b TryAcquire.
383 #if ALIB_DEBUG
384 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
385 #endif
386
387 #if !DOXYGEN
388 // choosing the right release method, with or without caller parameters.
389 #if ALIB_DEBUG
390 template<typename TRequires= TOwnable>
391 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, dbgCI))
392 void callRelease() { owned.ReleaseShared(dbgCI); }
393 #endif
394
395 template<typename TRequires= TOwnable>
396 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, ))
397 void callRelease() { owned.ReleaseShared(); }
398 #endif
399 public:
400
401 /// Constructor. Invokes TryAcquire() on member #owned.
402 /// @tparam TTimeValue Type of time parameter accepted with construction and passed to
403 /// method <b>TOwnable::TryAcquireTimed</b>.<br>
404 /// Usually this is type \alib{time;Ticks} or
405 /// \alib{time;TimePointBase::Duration;Ticks::Duration}.
406 /// @param time The duration to wait for, or point in time to wait until.
407 /// @param ownable The ownable to acquire.
408 /// @param ci Caller information.
409 #if ALIB_DEBUG
410 template<typename TTimeValue>
411 OwnerSharedTimed( TOwnable& ownable, const TTimeValue& time, const CallerInfo& ci)
412 : owned(ownable)
413 , dbgCI(ci) { isOwning= ownable.TryAcquireSharedTimed( time, ci); }
414 #else
415 template<typename TTimeValue>
416 OwnerSharedTimed( TOwnable& ownable, const TTimeValue& time )
417 : owned(ownable) { isOwning= ownable.TryAcquireSharedTimed( time ); }
418 #endif
419
420
421 /// Destructor. Invokes Release() on member #owned.
422 ~OwnerSharedTimed() { if (isOwning) callRelease(); }
423
424 /// @return \c true if the try to acquire the #owned with construction of this type was
425 /// successful, \c false otherwise.
426 bool IsOwning() const noexcept { return isOwning; }
427}; //class OwnerSharedTimed
428
429} // namespace alib[::lang]
430
431} // namespace [alib]
432
433
434
TOwnable & owned
The resource to acquire and release.
Definition owner.inl:228
OwnerRecursive(TOwnable &ownable, const CallerInfo &ci)
Definition owner.inl:251
~OwnerRecursive()
Destructor. Invokes ReleaseRecursive() on member owned.
Definition owner.inl:260
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:230
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:384
bool IsOwning() const noexcept
Definition owner.inl:426
OwnerSharedTimed(TOwnable &ownable, const TTimeValue &time, const CallerInfo &ci)
Definition owner.inl:411
~OwnerSharedTimed()
Destructor. Invokes Release() on member owned.
Definition owner.inl:422
TOwnable & owned
The resource to acquire and release.
Definition owner.inl:381
bool isOwning
The result of the call to TryAcquire.
Definition owner.inl:382
~OwnerShared()
Destructor. Invokes ReleaseShared() on member owned.
Definition owner.inl:308
TOwnable & owned
The resource to acquire and release.
Definition owner.inl:276
OwnerShared(TOwnable &ownable, const CallerInfo &ci)
Definition owner.inl:299
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:278
TOwnable & owned
The resource to acquire and release.
Definition owner.inl:154
bool IsOwning() const noexcept
Definition owner.inl:211
~OwnerTimed()
Destructor. Invokes Release() on member owned.
Definition owner.inl:207
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:157
OwnerTimed(TOwnable &ownable, const TTimeValue &time, const CallerInfo &ci)
Definition owner.inl:196
bool isOwning
The result of the call to TryAcquire.
Definition owner.inl:155
bool isOwning
The result of the call to TryAcquire.
Definition owner.inl:326
~OwnerTryShared()
Destructor. Invokes Release() on member owned.
Definition owner.inl:359
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:328
bool IsOwning() const noexcept
Definition owner.inl:363
TOwnable & owned
The resource to acquire and release.
Definition owner.inl:325
OwnerTryShared(TOwnable &ownable, const CallerInfo &ci)
Definition owner.inl:349
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:93
OwnerTry(TOwnable &ownable, const CallerInfo &ci)
Definition owner.inl:125
TOwnable & owned
The resource to acquire and release.
Definition owner.inl:90
~OwnerTry()
Destructor. Invokes Release() on member owned.
Definition owner.inl:135
bool IsOwning() const noexcept
Definition owner.inl:139
bool isOwning
The result of the call to TryAcquire.
Definition owner.inl:91
TOwnable & owned
The resource to acquire and release.
Definition owner.inl:43
~Owner()
Destructor. Releases the owner by invoking Release().
Definition owner.inl:75
Owner(TOwnable &ownable, const CallerInfo &ci)
Definition owner.inl:66
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:45
#define ALIB_HAS_METHOD(T, Method,...)
Definition alib.inl:988
#define ALIB_STACK_ALLOCATED_TYPE(T)
Definition alib.inl:975
#define ALIB_EXPORT
Definition alib.inl:488