ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
owner.hpp
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/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_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 (\https{RAII idiom,https://en.cppreference.com/w/cpp/language/raii}).
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 #"%CallerInfo".
18/// Use the macro #"ALIB_CALLER_PRUNED" to prune those with release-builds.
19///
20/// In the rather unusual case that it is a runtime decision whether this object should
21/// carry an object to acquire or not, the template parameter \p{TOptional} may be set to \c true.
22/// With that, pointers are accepted with construction and method #"Set" becomes available.
23///
24/// \see
25/// - Several sibling types exist.
26/// They are described and listed in chapter #"alib_threads_locks_auto_owner" of the
27/// Programmer's Manual of module \alib_threads.
28/// - Besides generic preprocessor macro #"ALIB_OWN", several application-specific alias macros
29/// exist.
30/// For example, macros
31/// - #"ALIB_LOCK",
32/// - #"ALIB_LOCK_WITH",
33/// - #"ALIB_DCS", or
34/// - #"ALIB_DCS_WITH"
35///
36/// provide a convenient and "speaking" way to use this class.
37/// - Chapter #"alib_manual_appendix_callerinfo" of the General Programmer's Manual.
38///
39/// @tparam TOwnable The type to own.
40/// Requirements are to have methods \b Acquire and \b Release available.
41/// @tparam TOptional If \c true, then checks is performed, whether the given #".owned" is
42/// \e nulled and thus not acquired and released.
43/// If \c false such checks are omitted.
44template <typename TOwnable, bool TOptional= false>
45class Owner {
47
48 public:
49 /// The pointer type of the owned object.
50 using OwnablePointer= std::remove_reference_t<TOwnable>*;
51
52 protected:
53 OwnablePointer owned; ///< The resource to acquire and release.
54 #if ALIB_DEBUG
55 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
56 #endif
57
58 #if !DOXYGEN
59 // choosing the right release method, with or without caller parameters.
60 #if ALIB_DEBUG
61 template<typename TRequires= TOwnable>
62 requires ( ALIB_HAS_METHOD(TRequires, Release, dbgCI))
63 void callRelease() { owned->Release(dbgCI); }
64 #endif
65
66 template<typename TRequires= TOwnable>
67 requires ( ALIB_HAS_METHOD(TRequires, Release, ))
68 void callRelease() { owned->Release(); }
69 #endif
70 public:
71
72 #if ALIB_DEBUG
73 /// Constructor. Invokes Acquire() on the ownable.
74 /// @param ownable The ownable to acquire.
75 /// @param ci Caller information.
76 Owner( TOwnable& ownable, const CallerInfo& ci)
77 : owned(&ownable)
78 , dbgCI(ci) { ownable.Acquire( ci ); }
79
80 /// Constructor taking a pointer to the ownable. This constructor is only available if
81 /// the template parameter \p{TOptional} is \c true.
82 /// @param ownable The ownable to acquire.
83 /// @param ci Caller information.
84 Owner( OwnablePointer ownable, const CallerInfo& ci)
85 requires TOptional
86 : owned(ownable)
87 , dbgCI(ci) { if (ownable != nullptr) ownable->Acquire( ci ); }
88 #else
89 Owner( TOwnable& ownable ) : owned(&ownable) { ownable.Acquire(); }
90 Owner( OwnablePointer ownable)
91 requires TOptional
92 : owned(ownable) { if (ownable != nullptr) ownable->Acquire(); }
93 #endif
94
95 /// Destructor. Releases the owner by invoking Release().
96 ~Owner() { if (!TOptional || owned) callRelease(); }
97
98 /// Sets the ownable after construction. This method is only available if the template
99 /// parameter \p{TOptional} is \c true. It must not be called if an object was given with
100 /// construction already, and it must not be called twice. If done, an assertion is raised.
101 /// @param ownable The ownable to acquire.
102 void Set(OwnablePointer ownable)
103 requires TOptional {
104 ALIB_ASSERT_ERROR(owned==nullptr, "LANG", "Owner already set.")
105 (owned= ownable)->Acquire( ALIB_DBG(dbgCI) );
106 }
107}; //class Owner
108
109/// Similar to class #"lang::Owner", but calls method #"%Lock::TryAcquire" instead of
110/// #"%Lock::Acquire".
111/// The result is retrievable with method #".IsOwning".
112/// @see Chapter #"alib_threads_locks_auto_owner" of the Programmer's Manual of module
113/// \alib_threads
114/// @tparam TOwnable The type to own.
115/// Requirements are to have methods #"%Lock::TryAcquire" and #"%Lock::Release"
116/// available.
117/// @tparam TOptional If \c true, then checks is performed, whether the given #".owned" is
118/// \e nulled and thus not acquired and released.
119/// If \c false such checks are omitted.
120template <typename TOwnable, bool TOptional= false>
121class OwnerTry {
123
124 public:
125 /// The pointer type of the owned object.
126 using OwnablePointer= std::remove_reference_t<TOwnable>*;
127
128 protected:
129 OwnablePointer owned; ///< The resource to acquire and release.
130 bool isOwning; ///< The result of the call to \b TryAcquire.
131 #if ALIB_DEBUG
132 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
133 #endif
134
135 #if !DOXYGEN
136 // choosing the right release method, with or without caller parameters.
137 #if ALIB_DEBUG
138 template<typename TRequires= TOwnable>
139 requires ( ALIB_HAS_METHOD(TRequires, Release, dbgCI))
140 void callRelease() { if (!TOptional || owned) owned->Release(dbgCI); }
141 #endif
142
143 template<typename TRequires= TOwnable>
144 requires ( ALIB_HAS_METHOD(TRequires, Release, ))
145 void callRelease() { if (!TOptional || owned) owned->Release(); }
146
147 #if ALIB_DEBUG
148 template<typename TRequires= TOwnable>
149 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive, dbgCI))
150 void callRelease() { if (!TOptional || owned) owned->ReleaseRecursive(dbgCI); }
151 #endif
152
153 template<typename TRequires= TOwnable>
154 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive, ))
155 void callRelease() { if (!TOptional || owned) owned->ReleaseRecursive(); }
156
157 #endif
158 public:
159
160 #if ALIB_DEBUG
161 /// Constructor. Invokes TryAcquire() on member #".owned".
162 /// @param ownable The ownable to acquire.
163 /// @param ci Caller information.
164 OwnerTry( TOwnable& ownable, const CallerInfo& ci)
165 : owned(&ownable)
166 , dbgCI(ci) { isOwning= ownable.TryAcquire(ci); }
167
168 /// Constructor taking a pointer to the ownable. This constructor is only available if
169 /// the template parameter \p{TOptional} is \c true.
170 /// @param ownable The ownable to acquire.
171 /// @param ci Caller information.
172 OwnerTry( OwnablePointer ownable, const CallerInfo& ci)
173 requires TOptional
174 : owned(ownable)
175 , dbgCI(ci) { isOwning = !ownable || ownable->TryAcquire( ci ); }
176#else
177 OwnerTry( TOwnable& ownable )
178 : owned(&ownable) { isOwning= ownable.TryAcquire(); }
179 OwnerTry( OwnablePointer ownable)
180 requires TOptional
181 : owned(ownable) { isOwning = !ownable || ownable->TryAcquire(); }
182 #endif
183
184
185 /// Destructor. Invokes Release() on member #".owned".
186 ~OwnerTry() { if ((!TOptional || owned) && isOwning) callRelease(); }
187
188 /// Sets the ownable after construction. This method is only available if the template
189 /// parameter \p{TOptional} is \c true. It must not be called if an object was given with
190 /// construction already, and it must not be called twice. If done, an assertion is raised.
191 /// @param ownable The ownable to acquire.
192 /// @return \c true if the try to acquire the owner was successful, \c false if not.
193 bool Set(OwnablePointer ownable)
194 requires TOptional {
195 ALIB_ASSERT_ERROR(owned==nullptr, "LANG", "Owner already set.")
196 return isOwning= (owned= ownable)->TryAcquire( ALIB_DBG(dbgCI) );
197 }
198
199 /// @return \c true if the try to acquire the #".owned" with construction of this type was
200 /// successful. Furthermore, \c true is returned in the case that the template
201 /// parameter \p{TOptional} is \c true and no ownable was given with construction.
202 /// Otherwise, \c false is returned.
203 bool IsOwning() const noexcept { return isOwning; }
204}; //class OwnerTry
205
206/// Similar to class #"lang::Owner", but calls method \b TryAcquireTimed instead of
207/// #"%TimedLock::Acquire".
208/// The result is retrievable with method #".IsOwning".
209/// @see Chapter #"alib_threads_locks_auto_owner" of the Programmer's Manual of module
210/// \alib_threads
211/// @tparam TOwnable The type to own.
212/// Requirements are to have methods \b TryAcquireTimed and
213/// \b Release available.
214/// @tparam TOptional If \c true, then checks is performed, whether the given #".owned" is
215/// \e nulled and thus not acquired and released.
216/// If \c false such checks are omitted.
217template <typename TOwnable, bool TOptional= false>
220
221 public:
222 /// The pointer type of the owned object.
223 using OwnablePointer= std::remove_reference_t<TOwnable>*;
224
225 protected:
226 OwnablePointer owned; ///< The resource to acquire and release.
227 bool isOwning; ///< The result of the call to \b TryAcquireTimed.
228 #if ALIB_DEBUG
229 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
230 #endif
231
232 #if !DOXYGEN
233 // choosing the right release method, with or without caller parameters.
234 #if ALIB_DEBUG
235 template<typename TRequires= TOwnable>
236 requires ( ALIB_HAS_METHOD(TRequires, Release, dbgCI))
237 void callRelease() { owned->Release(dbgCI); }
238 #endif
239
240 template<typename TRequires= TOwnable>
241 requires ( ALIB_HAS_METHOD(TRequires, Release, ))
242 void callRelease() { owned->Release(); }
243
244 // choosing the right release method, with or without caller parameters.
245 #if ALIB_DEBUG
246 template<typename TRequires= TOwnable>
247 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive, dbgCI))
248 void callRelease() { owned->ReleaseRecursive(dbgCI); }
249 #endif
250
251 template<typename TRequires= TOwnable>
252 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive, ))
253 void callRelease() { owned->ReleaseRecursive(); }
254
255 #endif
256 public:
257
258 #if ALIB_DEBUG
259 /// Constructor. Invokes TryAcquire() on member #".owned".
260 /// @param ownable The ownable to acquire.
261 /// @param time The duration to wait for, or point in time to wait until.
262 /// @param ci Caller information.
263 /// @tparam TTimeValue The type of the parameter \p{time} accepted with construction and passed
264 /// to the method <b>TOwnable::TryAcquireTimed</b>.<br>
265 /// Usually this is type #"Ticks" or its inherited type #"^Ticks::Duration".
266 template<typename TTimeValue>
267 OwnerTimed( TOwnable& ownable, const TTimeValue& time, const CallerInfo& ci)
268 : owned(&ownable)
269 , dbgCI(ci) { isOwning= ownable.TryAcquireTimed( time, ci); }
270
271 /// Constructor taking a pointer to the ownable. This constructor is only available if
272 /// the template parameter \p{TOptional} is \c true.
273 /// @param ownable The ownable to acquire.
274 /// @param time The duration to wait for, or point in time to wait until.
275 /// @param ci Caller information.
276 /// @tparam TTimeValue The type of the parameter \p{time} accepted with construction and passed
277 /// to the method <b>TOwnable::TryAcquireTimed</b>.<br>
278 /// Usually this is type #"Ticks" or its inherited type #"^Ticks::Duration".
279 template<typename TTimeValue>
280 requires TOptional
281 OwnerTimed( OwnablePointer ownable, const TTimeValue& time, const CallerInfo& ci)
282 : owned(ownable)
283 , dbgCI(ci) { isOwning = !ownable || ownable->TryAcquireTimed( time, ci ); }
284 #else
285 template<typename TTimeValue>
286 OwnerTimed( TOwnable& ownable, const TTimeValue& time )
287 : owned(&ownable) { isOwning= ownable.TryAcquireTimed( time ); }
288
289 template<typename TTimeValue>
290 requires TOptional
291 OwnerTimed( OwnablePointer ownable, const TTimeValue& time)
292 : owned(ownable) { isOwning = !ownable || ownable->TryAcquireTimed(time); }
293 #endif
294
295
296 /// Destructor. Invokes Release() on member #".owned".
297 ~OwnerTimed() { if ((!TOptional || owned) && isOwning) callRelease(); }
298
299 /// Sets the ownable after construction. This method is only available if the template
300 /// parameter \p{TOptional} is \c true. It must not be called if an object was given with
301 /// construction already, and it must not be called twice. If done, an assertion is raised.
302 /// @param ownable The ownable to acquire.
303 /// @param time The duration to wait for, or point in time to wait until.
304 /// @tparam TTimeValue The type of the parameter \p{time} accepted with construction and passed
305 /// to the method <b>TOwnable::TryAcquireTimed</b>.<br>
306 /// Usually this is type #"Ticks" or its inherited type #"^Ticks::Duration".
307 /// @return \c true if the try to acquire the owner was successful, \c false if not.
308 template<typename TTimeValue>
309 requires TOptional
310 bool Set(OwnablePointer ownable, const TTimeValue& time) {
311 ALIB_ASSERT_ERROR(owned==nullptr, "LANG", "Owner already set.")
312 return isOwning= (owned= ownable)->TryAcquireTimed( time ALIB_DBG(, dbgCI) );
313 }
314
315 /// @return \c true if the try to acquire the #".owned" with construction of this type was
316 /// successful. Furthermore, \c true is returned in the case that the template
317 /// parameter \p{TOptional} is \c true and no ownable was given with construction.
318 /// Otherwise, \c false is returned.
319 bool IsOwning() const noexcept { return isOwning; }
320}; //class OwnerTimed
321
322
323/// Similar to class #"lang::Owner", but calls methods \b AcquireRecursive and
324/// \b ReleaseRecursive.
325/// @see Chapter #"alib_threads_locks_auto_owner" of the Programmer's Manual of module
326/// \alib_threads
327/// @tparam TOwnable The type to own.
328/// Requirements are to have methods \b AcquireRecursive and \b ReleaseRecursive
329/// available.
330/// @tparam TOptional If \c true, then checks is performed, whether the given #".owned" is
331/// \e nulled and thus not acquired and released.
332/// If \c false such checks are omitted.
333template <typename TOwnable, bool TOptional= false>
336
337 public:
338 /// The pointer type of the owned object.
339 using OwnablePointer= std::remove_reference_t<TOwnable>*;
340
341 protected:
342 OwnablePointer owned; ///< The resource to acquire and release.
343 #if ALIB_DEBUG
344 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
345 #endif
346
347 #if !DOXYGEN
348 // choosing the right release method, with or without caller parameters.
349 #if ALIB_DEBUG
350 template<typename TRequires= TOwnable>
351 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive,dbgCI))
352 void callRelease() { owned->ReleaseRecursive(dbgCI); }
353 #endif
354
355 template<typename TRequires= TOwnable>
356 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive, ))
357 void callRelease() { owned->ReleaseRecursive(); }
358 #endif
359 public:
360
361 #if ALIB_DEBUG
362 /// Constructor. Invokes AcquireRecursive() on member #".owned".
363 /// @param ownable The ownable to acquire.
364 /// @param ci Caller information.
365 OwnerRecursive( TOwnable& ownable, const CallerInfo& ci)
366 : owned(&ownable)
367 , dbgCI(ci) { ownable.AcquireRecursive(ci); }
368
369 /// Constructor taking a pointer to the ownable. This constructor is only available if
370 /// the template parameter \p{TOptional} is \c true.
371 /// @param ownable The ownable to acquire.
372 /// @param ci Caller information.
374 requires TOptional
375 : owned(ownable)
376 , dbgCI(ci) { if (ownable != nullptr) ownable->AcquireRecursive( ci ); }
377 #else
378 OwnerRecursive( TOwnable& ownable ) : owned(&ownable) { ownable.AcquireRecursive(); }
379
381 requires TOptional
382 : owned(ownable) { if (ownable != nullptr) ownable->AcquireRecursive(); }
383 #endif
384
385 /// Destructor. Invokes ReleaseRecursive() on member #".owned".
386 ~OwnerRecursive() { if (!TOptional || owned) callRelease(); }
387
388 /// Sets the ownable after construction. This method is only available if the template
389 /// parameter \p{TOptional} is \c true. It must not be called if an object was given with
390 /// construction already, and it must not be called twice. If done, an assertion is raised.
391 /// @param ownable The ownable to acquire.
392 void Set(OwnablePointer ownable)
393 requires TOptional {
394 ALIB_ASSERT_ERROR(owned==nullptr, "LANG", "Owner already set.")
395 (owned= ownable)->AcquireRecursive( ALIB_DBG(dbgCI) );
396 }
397}; //class OwnerRecursive
398
399/// Similar to class #"lang::Owner", but calls methods \b AcquireShared and
400/// \b ReleaseShared.
401/// @see Chapter #"alib_threads_locks_auto_owner" of the Programmer's Manual of module
402/// \alib_threads
403/// @tparam TOwnable The type to own.
404/// Requirements are to have methods \b AcquireShared and \b ReleaseShared
405/// available.
406/// @tparam TOptional If \c true, then checks is performed, whether the given #".owned" is
407/// \e nulled and thus not acquired and released.
408/// If \c false such checks are omitted.
409template <typename TOwnable, bool TOptional= false>
412
413 public:
414 /// The pointer type of the owned object.
415 using OwnablePointer= std::remove_reference_t<TOwnable>*;
416
417 protected:
418 OwnablePointer owned; ///< The resource to acquire and release.
419 #if ALIB_DEBUG
420 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
421 #endif
422
423 #if !DOXYGEN
424 // choosing the right release method, with or without caller parameters.
425 #if ALIB_DEBUG
426 template<typename TRequires= TOwnable>
427 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, dbgCI))
428 void callRelease() const { owned->ReleaseShared(dbgCI); }
429 #endif
430
431 template<typename TRequires= TOwnable>
432 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, ))
433 void callRelease() const { owned->ReleaseShared(); }
434 #endif
435 public:
436
437 #if ALIB_DEBUG
438 /// Constructor. Invokes AcquireShared() on member #".owned".
439 /// @param ownable The ownable to acquire.
440 /// @param ci Caller information.
441 OwnerShared( TOwnable& ownable, const CallerInfo& ci)
442 : owned(&ownable)
443 , dbgCI(ci) { ownable.AcquireShared(ci); }
444
445 /// Constructor taking a pointer to the ownable. This constructor is only available if
446 /// the template parameter \p{TOptional} is \c true.
447 /// @param ownable The ownable to acquire.
448 /// @param ci Caller information.
450 requires TOptional
451 : owned(ownable)
452 , dbgCI(ci) { if (ownable != nullptr) ownable->AcquireShared( ci ); }
453 #else
454 OwnerShared( TOwnable& ownable ) : owned(&ownable) { ownable.AcquireShared(); }
455 OwnerShared( OwnablePointer ownable )
456 requires TOptional
457 : owned(ownable) { if (ownable != nullptr) ownable->AcquireShared(); }
458 #endif
459
460
461 /// Destructor. Invokes ReleaseShared() on member #".owned".
462 ~OwnerShared() { if (!TOptional || owned) callRelease(); }
463
464 /// Sets the ownable after construction. This method is only available if the template
465 /// parameter \p{TOptional} is \c true. It must not be called if an object was given with
466 /// construction already, and it must not be called twice. If done, an assertion is raised.
467 /// @param ownable The ownable to acquire.
468 void Set(OwnablePointer ownable)
469 requires TOptional {
470 ALIB_ASSERT_ERROR(owned==nullptr, "LANG", "Owner already set.")
471 (owned= ownable)->AcquireShared( ALIB_DBG(dbgCI) );
472 }
473}; //class OwnerShared
474
475/// Similar to class #"lang::Owner", but calls method \b TryAcquireShared instead of
476/// #"%SharedLock::Acquire" and #"%SharedLock::ReleaseShared" instead of #"%SharedLock::Release".
477/// The result is retrievable with the method #".IsOwning".
478/// @see Chapter #"alib_threads_locks_auto_owner" of the Programmer's Manual of module
479/// \alib_threads
480/// @tparam TOwnable The type to own.
481/// Requirements are to have methods \b TryAcquireShared and \b ReleaseShared
482/// available.
483/// @tparam TOptional If \c true, then checks is performed, whether the given #".owned" is
484/// \e nulled and thus not acquired and released.
485/// If \c false such checks are omitted.
486template <typename TOwnable, bool TOptional= false>
489
490 public:
491 /// The pointer type of the owned object.
492 using OwnablePointer= std::remove_reference_t<TOwnable>*;
493
494 protected:
495 OwnablePointer owned; ///< The resource to acquire and release.
496 bool isOwning; ///< The result of the call to \b TryAcquireShared.
497 #if ALIB_DEBUG
498 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
499 #endif
500
501 #if !DOXYGEN
502 // choosing the right release method, with or without caller parameters.
503 #if ALIB_DEBUG
504 template<typename TRequires= TOwnable>
505 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, dbgCI))
506 void callRelease() { owned->ReleaseShared(dbgCI); }
507 #endif
508
509 template<typename TRequires= TOwnable>
510 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, ))
511 void callRelease() { owned->ReleaseShared(); }
512 #endif
513 public:
514
515 #if ALIB_DEBUG
516 /// Constructor. Invokes TryAcquire() on member #".owned".
517 /// @param ownable The ownable to acquire.
518 /// @param ci Caller information.
519 OwnerTryShared( TOwnable& ownable, const CallerInfo& ci)
520 : owned(&ownable)
521 , dbgCI(ci) { isOwning= ownable.TryAcquireShared(ci); }
522
523 /// Constructor taking a pointer to the ownable. This constructor is only available if
524 /// the template parameter \p{TOptional} is \c true.
525 /// @param ownable The ownable to acquire.
526 /// @param ci Caller information.
528 requires TOptional
529 : owned(ownable)
530 , dbgCI(ci) { isOwning = !ownable || ownable->TryAcquireShared( ci ); }
531 #else
532 OwnerTryShared( TOwnable& ownable )
533 : owned(&ownable) { isOwning= ownable.TryAcquireShared(); }
534
536 requires TOptional
537 : owned(ownable) { isOwning = !ownable || ownable->TryAcquireShared(); }
538 #endif
539
540
541 /// Destructor. Invokes Release() on member #".owned".
542 ~OwnerTryShared() { if ((!TOptional || owned) && isOwning) callRelease(); }
543
544 /// @return \c true if the try to acquire the #".owned" with construction of this type was
545 /// successful. Furthermore, \c true is returned in the case that the template
546 /// parameter \p{TOptional} is \c true and no ownable was given with construction.
547 /// Otherwise, \c false is returned.
548 bool IsOwning() const noexcept { return isOwning; }
549
550 /// Sets the ownable after construction. This method is only available if the template
551 /// parameter \p{TOptional} is \c true. It must not be called if an object was given with
552 /// construction already, and it must not be called twice. If done, an assertion is raised.
553 /// @param ownable The ownable to acquire.
554 /// @return \c true if the try to acquire the owner was successful, \c false if not.
555 bool Set(OwnablePointer ownable)
556 requires TOptional {
557 ALIB_ASSERT_ERROR(owned==nullptr, "LANG", "Owner already set.")
558 return isOwning= (owned= ownable)->TryAcquireShared( ALIB_DBG(dbgCI) );
559 }
560
561}; //class OwnerTryShared
562
563
564/// Similar to class #"lang::Owner", but calls method \b TryAcquireSharedTimed instead
565/// of \b Acquire and \b ReleaseShared instead of #"%Shared".
566/// The result is retrievable with the method #".IsOwning".
567/// @see Chapter #"alib_threads_locks_auto_owner" of the Programmer's Manual of module
568/// \alib_threads
569/// @tparam TOwnable The type to own.
570/// Requirements are to have methods \b TryAcquireSharedTimed and
571/// \b ReleaseShared available.
572/// @tparam TOptional If \c true, then checks is performed, whether the given #".owned" is
573/// \e nulled and thus not acquired and released.
574/// If \c false such checks are omitted.
575template <typename TOwnable, bool TOptional= false>
578
579 public:
580 /// The pointer type of the owned object.
581 using OwnablePointer= std::remove_reference_t<TOwnable>*;
582
583 protected:
584 OwnablePointer owned; ///< The resource to acquire and release.
585 bool isOwning; ///< The result of the call to \b TryAcquireSharedTimed.
586 #if ALIB_DEBUG
587 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
588 #endif
589
590 #if !DOXYGEN
591 // choosing the right release method, with or without caller parameters.
592 #if ALIB_DEBUG
593 template<typename TRequires= TOwnable>
594 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, dbgCI))
595 void callRelease() { owned->ReleaseShared(dbgCI); }
596 #endif
597
598 template<typename TRequires= TOwnable>
599 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, ))
600 void callRelease() { owned->ReleaseShared(); }
601 #endif
602 public:
603
604 #if ALIB_DEBUG
605 /// Constructor. Invokes TryAcquire() on member #".owned".
606 /// @tparam TTimeValue The type of the parameter \p{time} accepted with construction and passed
607 /// to the method <b>TOwnable::TryAcquireTimed</b>.<br>
608 /// Usually this is type #"Ticks" or its inherited type #"^Ticks::Duration".
609 /// @param time The duration to wait for, or point in time to wait until.
610 /// @param ownable The ownable to acquire.
611 /// @param ci Caller information.
612 template<typename TTimeValue>
613 OwnerSharedTimed( TOwnable& ownable, const TTimeValue& time, const CallerInfo& ci)
614 : owned(&ownable)
615 , dbgCI(ci) { isOwning= ownable.TryAcquireSharedTimed( time, ci); }
616
617 /// Constructor taking a pointer to the ownable. This constructor is only available if
618 /// the template parameter \p{TOptional} is \c true.
619 /// @tparam TTimeValue The type of the parameter \p{time} accepted with construction and passed
620 /// to method <b>TOwnable::TryAcquireTimed</b>.<br>
621 /// Usually this is type #"Ticks" or its inherited type #"^Ticks::Duration".
622 /// @param time The duration to wait for, or point in time to wait until.
623 /// @param ownable The ownable to acquire.
624 /// @param ci Caller information.
625 template<typename TTimeValue>
626 requires TOptional
627 OwnerSharedTimed( OwnablePointer ownable, const TTimeValue& time, const CallerInfo& ci)
628 : owned(ownable)
629 , dbgCI(ci) { isOwning= !ownable || ownable->TryAcquireSharedTimed( time, ci); }
630 #else
631 template<typename TTimeValue>
632 OwnerSharedTimed( TOwnable& ownable, const TTimeValue& time )
633 : owned(&ownable) { isOwning= ownable.TryAcquireSharedTimed( time ); }
634
635 template<typename TTimeValue>
636 requires TOptional
637 OwnerSharedTimed( OwnablePointer ownable, const TTimeValue& time)
638 : owned(ownable) { isOwning= !ownable || ownable->TryAcquireSharedTimed( time); }
639 #endif
640
641
642 /// Destructor. Invokes Release() on member #".owned".
643 ~OwnerSharedTimed() { if ((!TOptional || owned) && isOwning) callRelease(); }
644
645 /// @return \c true if the try to acquire the #".owned" with construction of this type was
646 /// successful. Furthermore, \c true is returned in the case that the template
647 /// parameter \p{TOptional} is \c true and no ownable was given with construction.
648 /// Otherwise, \c false is returned.
649 bool IsOwning() const noexcept { return isOwning; }
650
651 /// Sets the ownable after construction. This method is only available if the template
652 /// parameter \p{TOptional} is \c true. It must not be called if an object was given with
653 /// construction already, and it must not be called twice. If done, an assertion is raised.
654 /// @param ownable The ownable to acquire.
655 /// @param time The duration to wait for, or point in time to wait until.
656 /// @tparam TTimeValue The type of the parameter \p{time} accepted with construction and passed
657 /// to the method <b>TOwnable::TryAcquireTimed</b>.<br>
658 /// Usually this is type #"Ticks" or its inherited type #"^Ticks::Duration".
659 /// @return \c true if the try to acquire the owner was successful, \c false if not.
660 template<typename TTimeValue>
661 bool Set(OwnablePointer ownable, const TTimeValue& time)
662 requires TOptional {
663 ALIB_ASSERT_ERROR(owned==nullptr, "LANG", "Owner already set.")
664 return isOwning= (owned= ownable)->TryAcquireSharedTimed( time ALIB_DBG(, dbgCI) );
665 }
666
667}; //class OwnerSharedTimed
668
669} // namespace alib[::lang]
670
671} // namespace [alib]
#define ALIB_HAS_METHOD(T, Method,...)
#define ALIB_STACK_ALLOCATED_TYPE(T)
#define ALIB_EXPORT
#define ALIB_DBG(...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
void Set(OwnablePointer ownable)
Definition owner.hpp:392
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.hpp:339
OwnerRecursive(OwnablePointer ownable, const CallerInfo &ci)
Definition owner.hpp:373
OwnerRecursive(TOwnable &ownable, const CallerInfo &ci)
Definition owner.hpp:365
OwnablePointer owned
The resource to acquire and release.
Definition owner.hpp:342
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.hpp:344
~OwnerRecursive()
Destructor. Invokes ReleaseRecursive() on member #".owned".
Definition owner.hpp:386
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.hpp:581
OwnerSharedTimed(OwnablePointer ownable, const TTimeValue &time, const CallerInfo &ci)
Definition owner.hpp:627
~OwnerSharedTimed()
Destructor. Invokes Release() on member #".owned".
Definition owner.hpp:643
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.hpp:587
bool Set(OwnablePointer ownable, const TTimeValue &time)
Definition owner.hpp:661
OwnerSharedTimed(TOwnable &ownable, const TTimeValue &time, const CallerInfo &ci)
Definition owner.hpp:613
bool isOwning
The result of the call to TryAcquireSharedTimed.
Definition owner.hpp:585
bool IsOwning() const noexcept
Definition owner.hpp:649
OwnablePointer owned
The resource to acquire and release.
Definition owner.hpp:584
OwnerShared(TOwnable &ownable, const CallerInfo &ci)
Definition owner.hpp:441
OwnablePointer owned
The resource to acquire and release.
Definition owner.hpp:418
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.hpp:415
~OwnerShared()
Destructor. Invokes ReleaseShared() on member #".owned".
Definition owner.hpp:462
OwnerShared(OwnablePointer ownable, const CallerInfo &ci)
Definition owner.hpp:449
void Set(OwnablePointer ownable)
Definition owner.hpp:468
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.hpp:420
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.hpp:223
bool IsOwning() const noexcept
Definition owner.hpp:319
~OwnerTimed()
Destructor. Invokes Release() on member #".owned".
Definition owner.hpp:297
OwnerTimed(OwnablePointer ownable, const TTimeValue &time, const CallerInfo &ci)
Definition owner.hpp:281
OwnerTimed(TOwnable &ownable, const TTimeValue &time, const CallerInfo &ci)
Definition owner.hpp:267
bool Set(OwnablePointer ownable, const TTimeValue &time)
Definition owner.hpp:310
OwnablePointer owned
The resource to acquire and release.
Definition owner.hpp:226
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.hpp:229
bool isOwning
The result of the call to TryAcquireTimed.
Definition owner.hpp:227
bool IsOwning() const noexcept
Definition owner.hpp:548
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.hpp:498
bool isOwning
The result of the call to TryAcquireShared.
Definition owner.hpp:496
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.hpp:492
OwnablePointer owned
The resource to acquire and release.
Definition owner.hpp:495
bool Set(OwnablePointer ownable)
Definition owner.hpp:555
OwnerTryShared(TOwnable &ownable, const CallerInfo &ci)
Definition owner.hpp:519
OwnerTryShared(OwnablePointer ownable, const CallerInfo &ci)
Definition owner.hpp:527
~OwnerTryShared()
Destructor. Invokes Release() on member #".owned".
Definition owner.hpp:542
OwnablePointer owned
The resource to acquire and release.
Definition owner.hpp:129
bool Set(OwnablePointer ownable)
Definition owner.hpp:193
OwnerTry(OwnablePointer ownable, const CallerInfo &ci)
Definition owner.hpp:172
~OwnerTry()
Destructor. Invokes Release() on member #".owned".
Definition owner.hpp:186
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.hpp:132
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.hpp:126
bool IsOwning() const noexcept
Definition owner.hpp:203
OwnerTry(TOwnable &ownable, const CallerInfo &ci)
Definition owner.hpp:164
bool isOwning
The result of the call to TryAcquire.
Definition owner.hpp:130
OwnablePointer owned
The resource to acquire and release.
Definition owner.hpp:53
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.hpp:50
~Owner()
Destructor. Releases the owner by invoking Release().
Definition owner.hpp:96
Owner(TOwnable &ownable, const CallerInfo &ci)
Definition owner.hpp:76
Owner(OwnablePointer ownable, const CallerInfo &ci)
Definition owner.hpp:84
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.hpp:55
void Set(OwnablePointer ownable)
Definition owner.hpp:102
Definition alox.cpp:14