ALib C++ Library
Library Version: 2511 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 (\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 \b CallerInfo.
18/// Use macro \ref ALIB_CALLER_PRUNED to prune those with release-builds.
19///
20/// In the rather unusual case that it is a run-time 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 \ref alib_threads_locks_auto_owner of the
27/// Programmer's Manual of module \alib_threads.
28/// - Besides generic preprocessor macro \ref ALIB_OWN, several application-specific alias macros
29/// exist.
30/// For example, macros
31/// - \ref ALIB_LOCK,
32/// - \ref ALIB_LOCK_WITH,
33/// - \ref ALIB_DCS, or
34/// - \ref ALIB_DCS_WITH
35///
36/// provide a convenient and "speaking" way to use this class.
37/// - Chapter \ref 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
46{
48
49 public:
50 /// The pointer type of the owned object.
51 using OwnablePointer= std::remove_reference_t<TOwnable>*;
52
53 protected:
54 OwnablePointer owned; ///< The resource to acquire and release.
55 #if ALIB_DEBUG
56 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
57 #endif
58
59 #if !DOXYGEN
60 // choosing the right release method, with or without caller parameters.
61 #if ALIB_DEBUG
62 template<typename TRequires= TOwnable>
63 requires ( ALIB_HAS_METHOD(TRequires, Release, dbgCI))
64 void callRelease() { owned->Release(dbgCI); }
65 #endif
66
67 template<typename TRequires= TOwnable>
68 requires ( ALIB_HAS_METHOD(TRequires, Release, ))
69 void callRelease() { owned->Release(); }
70 #endif
71 public:
72
73 #if ALIB_DEBUG
74 /// Constructor. Invokes Acquire() on the ownable.
75 /// @param ownable The ownable to acquire.
76 /// @param ci Caller information.
77 Owner( TOwnable& ownable, const CallerInfo& ci)
78 : owned(&ownable)
79 , dbgCI(ci) { ownable.Acquire( ci ); }
80
81 /// Constructor taking a pointer to the ownable. This constructor is only available if
82 /// the template parameter \p{TOptional} is \c true.
83 /// @param ownable The ownable to acquire.
84 /// @param ci Caller information.
85 Owner( OwnablePointer ownable, const CallerInfo& ci)
86 requires TOptional
87 : owned(ownable)
88 , dbgCI(ci) { if (ownable != nullptr) ownable->Acquire( ci ); }
89 #else
90 Owner( TOwnable& ownable ) : owned(&ownable) { ownable.Acquire(); }
91 Owner( OwnablePointer ownable)
92 requires TOptional
93 : owned(ownable) { if (ownable != nullptr) ownable->Acquire(); }
94 #endif
95
96 /// Destructor. Releases the owner by invoking Release().
97 ~Owner() { if (!TOptional || owned) callRelease(); }
98
99 /// Sets the ownable after construction. This method is only available if the template
100 /// parameter \p{TOptional} is \c true. It must not be called if an object was given with
101 /// construction already, and it must not be called twice. If done, an assertion is raised.
102 /// @param ownable The ownable to acquire.
103 void Set(OwnablePointer ownable)
104 requires TOptional {
105 ALIB_ASSERT_ERROR(owned==nullptr, "LANG", "Owner already set.")
106 (owned= ownable)->Acquire( ALIB_DBG(dbgCI) );
107 }
108}; //class Owner
109
110/// Similar to class \alib{lang;Owner}, but calls method \b TryAcquire instead of \b Acquire.
111/// The result is retrievable with method #IsOwning().
112/// @see Chapter \ref 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 \b %TryAcquire and \b %Release available.
116/// @tparam TOptional If \c true, then checks is performed, whether the given #owned is
117/// \e nulled and thus not acquired and released.
118/// If \c false such checks are omitted.
119template <typename TOwnable, bool TOptional= false>
121{
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 \alib{lang;Owner}, but calls method \b TryAcquireTimed instead of \b Acquire.
207/// The result is retrievable with method #IsOwning().
208/// @see Chapter \ref alib_threads_locks_auto_owner of the Programmer's Manual of module
209/// \alib_threads
210/// @tparam TOwnable The type to own.
211/// Requirements are to have methods \b %TryAcquireTimed and \b %Release
212/// available.
213/// @tparam TOptional If \c true, then checks is performed, whether the given #owned is
214/// \e nulled and thus not acquired and released.
215/// If \c false such checks are omitted.
216template <typename TOwnable, bool TOptional= false>
218{
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 TryAcquire.
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 Type of time parameter accepted with construction and passed to
264 /// method <b>TOwnable::TryAcquireTimed</b>.<br>
265 /// Usually this is type \alib{time;Ticks} or
266 /// \alib{time;TimePointBase::Duration;Ticks::Duration}.
267 template<typename TTimeValue>
268 OwnerTimed( TOwnable& ownable, const TTimeValue& time, const CallerInfo& ci)
269 : owned(&ownable)
270 , dbgCI(ci) { isOwning= ownable.TryAcquireTimed( time, ci); }
271
272 /// Constructor taking a pointer to the ownable. This constructor is only available if
273 /// the template parameter \p{TOptional} is \c true.
274 /// @param ownable The ownable to acquire.
275 /// @param time The duration to wait for, or point in time to wait until.
276 /// @param ci Caller information.
277 /// @tparam TTimeValue Type of time parameter accepted with construction and passed to
278 /// method <b>TOwnable::TryAcquireTimed</b>.<br>
279 /// Usually this is type \alib{time;Ticks} or
280 /// \alib{time;TimePointBase::Duration;Ticks::Duration}.
281 template<typename TTimeValue>
282 requires TOptional
283 OwnerTimed( OwnablePointer ownable, const TTimeValue& time, const CallerInfo& ci)
284 : owned(ownable)
285 , dbgCI(ci) { isOwning = !ownable || ownable->TryAcquireTimed( time, ci ); }
286 #else
287 template<typename TTimeValue>
288 OwnerTimed( TOwnable& ownable, const TTimeValue& time )
289 : owned(&ownable) { isOwning= ownable.TryAcquireTimed( time ); }
290
291 template<typename TTimeValue>
292 requires TOptional
293 OwnerTimed( OwnablePointer ownable, const TTimeValue& time)
294 : owned(ownable) { isOwning = !ownable || ownable->TryAcquireTimed(time); }
295 #endif
296
297
298 /// Destructor. Invokes Release() on member #owned.
299 ~OwnerTimed() { if ((!TOptional || owned) && isOwning) callRelease(); }
300
301 /// Sets the ownable after construction. This method is only available if the template
302 /// parameter \p{TOptional} is \c true. It must not be called if an object was given with
303 /// construction already, and it must not be called twice. If done, an assertion is raised.
304 /// @param ownable The ownable to acquire.
305 /// @param time The duration to wait for, or point in time to wait until.
306 /// @tparam TTimeValue Type of time parameter accepted with construction and passed to
307 /// method <b>TOwnable::TryAcquireTimed</b>.<br>
308 /// Usually this is type \alib{time;Ticks} or
309 /// \alib{time;TimePointBase::Duration;Ticks::Duration}.
310 /// @return \c true if the try to acquire the owner was successful, \c false if not.
311 template<typename TTimeValue>
312 requires TOptional
313 bool Set(OwnablePointer ownable, const TTimeValue& time) {
314 ALIB_ASSERT_ERROR(owned==nullptr, "LANG", "Owner already set.")
315 return isOwning= (owned= ownable)->TryAcquireTimed( time ALIB_DBG(, dbgCI) );
316 }
317
318 /// @return \c true if the try to acquire the #owned with construction of this type was
319 /// successful. Furthermore, \c true is returned in the case that the template
320 /// parameter \p{TOptional} is \c true and no ownable was given with construction.
321 /// Otherwise, \c false is returned.
322 bool IsOwning() const noexcept { return isOwning; }
323}; //class OwnerTimed
324
325
326/// Similar to class \alib{lang;Owner}, but calls methods \b AcquireRecursive and
327/// \b ReleaseRecursive.
328/// @see Chapter \ref alib_threads_locks_auto_owner of the Programmer's Manual of module
329/// \alib_threads
330/// @tparam TOwnable The type to own.
331/// Requirements are to have methods \b %AcquireRecursive and \b %ReleaseRecursive
332/// available.
333/// @tparam TOptional If \c true, then checks is performed, whether the given #owned is
334/// \e nulled and thus not acquired and released.
335/// If \c false such checks are omitted.
336template <typename TOwnable, bool TOptional= false>
338{
340
341 public:
342 /// The pointer type of the owned object.
343 using OwnablePointer= std::remove_reference_t<TOwnable>*;
344
345 protected:
346 OwnablePointer owned; ///< The resource to acquire and release.
347 #if ALIB_DEBUG
348 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
349 #endif
350
351 #if !DOXYGEN
352 // choosing the right release method, with or without caller parameters.
353 #if ALIB_DEBUG
354 template<typename TRequires= TOwnable>
355 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive,dbgCI))
356 void callRelease() { owned->ReleaseRecursive(dbgCI); }
357 #endif
358
359 template<typename TRequires= TOwnable>
360 requires ( ALIB_HAS_METHOD(TRequires, ReleaseRecursive, ))
361 void callRelease() { owned->ReleaseRecursive(); }
362 #endif
363 public:
364
365 #if ALIB_DEBUG
366 /// Constructor. Invokes AcquireRecursive() on member #owned.
367 /// @param ownable The ownable to acquire.
368 /// @param ci Caller information.
369 OwnerRecursive( TOwnable& ownable, const CallerInfo& ci)
370 : owned(&ownable)
371 , dbgCI(ci) { ownable.AcquireRecursive(ci); }
372
373 /// Constructor taking a pointer to the ownable. This constructor is only available if
374 /// the template parameter \p{TOptional} is \c true.
375 /// @param ownable The ownable to acquire.
376 /// @param ci Caller information.
378 requires TOptional
379 : owned(ownable)
380 , dbgCI(ci) { if (ownable != nullptr) ownable->AcquireRecursive( ci ); }
381 #else
382 OwnerRecursive( TOwnable& ownable ) : owned(&ownable) { ownable.AcquireRecursive(); }
383
385 requires TOptional
386 : owned(ownable) { if (ownable != nullptr) ownable->AcquireRecursive(); }
387 #endif
388
389 /// Destructor. Invokes ReleaseRecursive() on member #owned.
390 ~OwnerRecursive() { if (!TOptional || owned) callRelease(); }
391
392 /// Sets the ownable after construction. This method is only available if the template
393 /// parameter \p{TOptional} is \c true. It must not be called if an object was given with
394 /// construction already, and it must not be called twice. If done, an assertion is raised.
395 /// @param ownable The ownable to acquire.
396 void Set(OwnablePointer ownable)
397 requires TOptional {
398 ALIB_ASSERT_ERROR(owned==nullptr, "LANG", "Owner already set.")
399 (owned= ownable)->AcquireRecursive( ALIB_DBG(dbgCI) );
400 }
401}; //class OwnerRecursive
402
403/// Similar to class \alib{lang;Owner}, but calls methods \b AcquireShared and
404/// \b ReleaseShared.
405/// @see Chapter \ref alib_threads_locks_auto_owner of the Programmer's Manual of module
406/// \alib_threads
407/// @tparam TOwnable The type to own.
408/// Requirements are to have methods \b %AcquireShared and \b %ReleaseShared
409/// available.
410/// @tparam TOptional If \c true, then checks is performed, whether the given #owned is
411/// \e nulled and thus not acquired and released.
412/// If \c false such checks are omitted.
413template <typename TOwnable, bool TOptional= false>
415{
417
418 public:
419 /// The pointer type of the owned object.
420 using OwnablePointer= std::remove_reference_t<TOwnable>*;
421
422 protected:
423 OwnablePointer owned; ///< The resource to acquire and release.
424 #if ALIB_DEBUG
425 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
426 #endif
427
428 #if !DOXYGEN
429 // choosing the right release method, with or without caller parameters.
430 #if ALIB_DEBUG
431 template<typename TRequires= TOwnable>
432 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, dbgCI))
433 void callRelease() const { owned->ReleaseShared(dbgCI); }
434 #endif
435
436 template<typename TRequires= TOwnable>
437 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, ))
438 void callRelease() const { owned->ReleaseShared(); }
439 #endif
440 public:
441
442 #if ALIB_DEBUG
443 /// Constructor. Invokes AcquireShared() on member #owned.
444 /// @param ownable The ownable to acquire.
445 /// @param ci Caller information.
446 OwnerShared( TOwnable& ownable, const CallerInfo& ci)
447 : owned(&ownable)
448 , dbgCI(ci) { ownable.AcquireShared(ci); }
449
450 /// Constructor taking a pointer to the ownable. This constructor is only available if
451 /// the template parameter \p{TOptional} is \c true.
452 /// @param ownable The ownable to acquire.
453 /// @param ci Caller information.
455 requires TOptional
456 : owned(ownable)
457 , dbgCI(ci) { if (ownable != nullptr) ownable->AcquireShared( ci ); }
458 #else
459 OwnerShared( TOwnable& ownable ) : owned(&ownable) { ownable.AcquireShared(); }
460 OwnerShared( OwnablePointer ownable )
461 requires TOptional
462 : owned(ownable) { if (ownable != nullptr) ownable->AcquireShared(); }
463 #endif
464
465
466 /// Destructor. Invokes ReleaseShared() on member #owned.
467 ~OwnerShared() { if (!TOptional || owned) callRelease(); }
468
469 /// Sets the ownable after construction. This method is only available if the template
470 /// parameter \p{TOptional} is \c true. It must not be called if an object was given with
471 /// construction already, and it must not be called twice. If done, an assertion is raised.
472 /// @param ownable The ownable to acquire.
473 void Set(OwnablePointer ownable)
474 requires TOptional {
475 ALIB_ASSERT_ERROR(owned==nullptr, "LANG", "Owner already set.")
476 (owned= ownable)->AcquireShared( ALIB_DBG(dbgCI) );
477 }
478}; //class OwnerShared
479
480/// Similar to class \alib{lang;Owner}, but calls method \b TryAcquireShared instead of \b Acquire
481/// and \b ReleaseShared instead of \b Release.
482/// The result is retrievable with the method #IsOwning().
483/// @see Chapter \ref alib_threads_locks_auto_owner of the Programmer's Manual of module
484/// \alib_threads
485/// @tparam TOwnable The type to own.
486/// Requirements are to have methods \b %TryAcquireShared and \b %ReleaseShared
487/// available.
488/// @tparam TOptional If \c true, then checks is performed, whether the given #owned is
489/// \e nulled and thus not acquired and released.
490/// If \c false such checks are omitted.
491template <typename TOwnable, bool TOptional= false>
493{
495
496 public:
497 /// The pointer type of the owned object.
498 using OwnablePointer= std::remove_reference_t<TOwnable>*;
499
500 protected:
501 OwnablePointer owned; ///< The resource to acquire and release.
502 bool isOwning; ///< The result of the call to \b TryAcquire.
503 #if ALIB_DEBUG
504 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
505 #endif
506
507 #if !DOXYGEN
508 // choosing the right release method, with or without caller parameters.
509 #if ALIB_DEBUG
510 template<typename TRequires= TOwnable>
511 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, dbgCI))
512 void callRelease() { owned->ReleaseShared(dbgCI); }
513 #endif
514
515 template<typename TRequires= TOwnable>
516 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, ))
517 void callRelease() { owned->ReleaseShared(); }
518 #endif
519 public:
520
521 #if ALIB_DEBUG
522 /// Constructor. Invokes TryAcquire() on member #owned.
523 /// @param ownable The ownable to acquire.
524 /// @param ci Caller information.
525 OwnerTryShared( TOwnable& ownable, const CallerInfo& ci)
526 : owned(&ownable)
527 , dbgCI(ci) { isOwning= ownable.TryAcquireShared(ci); }
528
529 /// Constructor taking a pointer to the ownable. This constructor is only available if
530 /// the template parameter \p{TOptional} is \c true.
531 /// @param ownable The ownable to acquire.
532 /// @param ci Caller information.
534 requires TOptional
535 : owned(ownable)
536 , dbgCI(ci) { isOwning = !ownable || ownable->TryAcquireShared( ci ); }
537 #else
538 OwnerTryShared( TOwnable& ownable )
539 : owned(&ownable) { isOwning= ownable.TryAcquireShared(); }
540
542 requires TOptional
543 : owned(ownable) { isOwning = !ownable || ownable->TryAcquireShared(); }
544 #endif
545
546
547 /// Destructor. Invokes Release() on member #owned.
548 ~OwnerTryShared() { if ((!TOptional || owned) && isOwning) callRelease(); }
549
550 /// @return \c true if the try to acquire the #owned with construction of this type was
551 /// successful. Furthermore, \c true is returned in the case that the template
552 /// parameter \p{TOptional} is \c true and no ownable was given with construction.
553 /// Otherwise, \c false is returned.
554 bool IsOwning() const noexcept { return isOwning; }
555
556 /// Sets the ownable after construction. This method is only available if the template
557 /// parameter \p{TOptional} is \c true. It must not be called if an object was given with
558 /// construction already, and it must not be called twice. If done, an assertion is raised.
559 /// @param ownable The ownable to acquire.
560 /// @return \c true if the try to acquire the owner was successful, \c false if not.
561 bool Set(OwnablePointer ownable)
562 requires TOptional {
563 ALIB_ASSERT_ERROR(owned==nullptr, "LANG", "Owner already set.")
564 return isOwning= (owned= ownable)->TryAcquireShared( ALIB_DBG(dbgCI) );
565 }
566
567}; //class OwnerTryShared
568
569
570/// Similar to class \alib{lang;Owner}, but calls method \b TryAcquireSharedTimed instead
571/// of \b Acquire and \b ReleaseShared instead of \b Shared.
572/// The result is retrievable with the method #IsOwning().
573/// @see Chapter \ref alib_threads_locks_auto_owner of the Programmer's Manual of module
574/// \alib_threads
575/// @tparam TOwnable The type to own.
576/// Requirements are to have methods \b %TryAcquireSharedTimed and
577/// \b %ReleaseShared available.
578/// @tparam TOptional If \c true, then checks is performed, whether the given #owned is
579/// \e nulled and thus not acquired and released.
580/// If \c false such checks are omitted.
581template <typename TOwnable, bool TOptional= false>
583{
585
586 public:
587 /// The pointer type of the owned object.
588 using OwnablePointer= std::remove_reference_t<TOwnable>*;
589
590 protected:
591 OwnablePointer owned; ///< The resource to acquire and release.
592 bool isOwning; ///< The result of the call to \b TryAcquire.
593 #if ALIB_DEBUG
594 CallerInfo dbgCI; ///< Caller information. Available only with debug-builds.
595 #endif
596
597 #if !DOXYGEN
598 // choosing the right release method, with or without caller parameters.
599 #if ALIB_DEBUG
600 template<typename TRequires= TOwnable>
601 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, dbgCI))
602 void callRelease() { owned->ReleaseShared(dbgCI); }
603 #endif
604
605 template<typename TRequires= TOwnable>
606 requires ( ALIB_HAS_METHOD(TRequires, ReleaseShared, ))
607 void callRelease() { owned->ReleaseShared(); }
608 #endif
609 public:
610
611 #if ALIB_DEBUG
612 /// Constructor. Invokes TryAcquire() on member #owned.
613 /// @tparam TTimeValue Type of time parameter accepted with construction and passed to
614 /// method <b>TOwnable::TryAcquireTimed</b>.<br>
615 /// Usually this is type \alib{time;Ticks} or
616 /// \alib{time;TimePointBase::Duration;Ticks::Duration}.
617 /// @param time The duration to wait for, or point in time to wait until.
618 /// @param ownable The ownable to acquire.
619 /// @param ci Caller information.
620 template<typename TTimeValue>
621 OwnerSharedTimed( TOwnable& ownable, const TTimeValue& time, const CallerInfo& ci)
622 : owned(&ownable)
623 , dbgCI(ci) { isOwning= ownable.TryAcquireSharedTimed( time, ci); }
624
625 /// Constructor taking a pointer to the ownable. This constructor is only available if
626 /// the template parameter \p{TOptional} is \c true.
627 /// @tparam TTimeValue Type of time parameter accepted with construction and passed to
628 /// method <b>TOwnable::TryAcquireTimed</b>.<br>
629 /// Usually this is type \alib{time;Ticks} or
630 /// \alib{time;TimePointBase::Duration;Ticks::Duration}.
631 /// @param time The duration to wait for, or point in time to wait until.
632 /// @param ownable The ownable to acquire.
633 /// @param ci Caller information.
634 template<typename TTimeValue>
635 requires TOptional
636 OwnerSharedTimed( OwnablePointer ownable, const TTimeValue& time, const CallerInfo& ci)
637 : owned(ownable)
638 , dbgCI(ci) { isOwning= !ownable || ownable->TryAcquireSharedTimed( time, ci); }
639 #else
640 template<typename TTimeValue>
641 OwnerSharedTimed( TOwnable& ownable, const TTimeValue& time )
642 : owned(&ownable) { isOwning= ownable.TryAcquireSharedTimed( time ); }
643
644 template<typename TTimeValue>
645 requires TOptional
646 OwnerSharedTimed( OwnablePointer ownable, const TTimeValue& time)
647 : owned(ownable) { isOwning= !ownable || ownable->TryAcquireSharedTimed( time); }
648 #endif
649
650
651 /// Destructor. Invokes Release() on member #owned.
652 ~OwnerSharedTimed() { if ((!TOptional || owned) && isOwning) callRelease(); }
653
654 /// @return \c true if the try to acquire the #owned with construction of this type was
655 /// successful. Furthermore, \c true is returned in the case that the template
656 /// parameter \p{TOptional} is \c true and no ownable was given with construction.
657 /// Otherwise, \c false is returned.
658 bool IsOwning() const noexcept { return isOwning; }
659
660 /// Sets the ownable after construction. This method is only available if the template
661 /// parameter \p{TOptional} is \c true. It must not be called if an object was given with
662 /// construction already, and it must not be called twice. If done, an assertion is raised.
663 /// @param ownable The ownable to acquire.
664 /// @param time The duration to wait for, or point in time to wait until.
665 /// @tparam TTimeValue Type of time parameter accepted with construction and passed to
666 /// method <b>TOwnable::TryAcquireTimed</b>.<br>
667 /// Usually this is type \alib{time;Ticks} or
668 /// \alib{time;TimePointBase::Duration;Ticks::Duration}.
669 /// @return \c true if the try to acquire the owner was successful, \c false if not.
670 template<typename TTimeValue>
671 bool Set(OwnablePointer ownable, const TTimeValue& time)
672 requires TOptional {
673 ALIB_ASSERT_ERROR(owned==nullptr, "LANG", "Owner already set.")
674 return isOwning= (owned= ownable)->TryAcquireSharedTimed( time ALIB_DBG(, dbgCI) );
675 }
676
677}; //class OwnerSharedTimed
678
679} // namespace alib[::lang]
680
681} // namespace [alib]
void Set(OwnablePointer ownable)
Definition owner.inl:396
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.inl:343
OwnerRecursive(OwnablePointer ownable, const CallerInfo &ci)
Definition owner.inl:377
OwnerRecursive(TOwnable &ownable, const CallerInfo &ci)
Definition owner.inl:369
OwnablePointer owned
The resource to acquire and release.
Definition owner.inl:346
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:348
~OwnerRecursive()
Destructor. Invokes ReleaseRecursive() on member owned.
Definition owner.inl:390
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.inl:588
~OwnerSharedTimed()
Destructor. Invokes Release() on member owned.
Definition owner.inl:652
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:594
OwnerSharedTimed(OwnablePointer ownable, const TTimeValue &time, const CallerInfo &ci)
Definition owner.inl:636
bool Set(OwnablePointer ownable, const TTimeValue &time)
Definition owner.inl:671
OwnerSharedTimed(TOwnable &ownable, const TTimeValue &time, const CallerInfo &ci)
Definition owner.inl:621
bool isOwning
The result of the call to TryAcquire.
Definition owner.inl:592
bool IsOwning() const noexcept
Definition owner.inl:658
OwnablePointer owned
The resource to acquire and release.
Definition owner.inl:591
OwnerShared(TOwnable &ownable, const CallerInfo &ci)
Definition owner.inl:446
OwnablePointer owned
The resource to acquire and release.
Definition owner.inl:423
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.inl:420
~OwnerShared()
Destructor. Invokes ReleaseShared() on member owned.
Definition owner.inl:467
OwnerShared(OwnablePointer ownable, const CallerInfo &ci)
Definition owner.inl:454
void Set(OwnablePointer ownable)
Definition owner.inl:473
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:425
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.inl:223
bool IsOwning() const noexcept
Definition owner.inl:322
~OwnerTimed()
Destructor. Invokes Release() on member owned.
Definition owner.inl:299
OwnerTimed(OwnablePointer ownable, const TTimeValue &time, const CallerInfo &ci)
Definition owner.inl:283
OwnerTimed(TOwnable &ownable, const TTimeValue &time, const CallerInfo &ci)
Definition owner.inl:268
bool Set(OwnablePointer ownable, const TTimeValue &time)
Definition owner.inl:313
OwnablePointer owned
The resource to acquire and release.
Definition owner.inl:226
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:229
bool isOwning
The result of the call to TryAcquire.
Definition owner.inl:227
bool IsOwning() const noexcept
Definition owner.inl:554
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:504
bool isOwning
The result of the call to TryAcquire.
Definition owner.inl:502
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.inl:498
OwnablePointer owned
The resource to acquire and release.
Definition owner.inl:501
bool Set(OwnablePointer ownable)
Definition owner.inl:561
OwnerTryShared(TOwnable &ownable, const CallerInfo &ci)
Definition owner.inl:525
OwnerTryShared(OwnablePointer ownable, const CallerInfo &ci)
Definition owner.inl:533
~OwnerTryShared()
Destructor. Invokes Release() on member owned.
Definition owner.inl:548
OwnablePointer owned
The resource to acquire and release.
Definition owner.inl:129
bool Set(OwnablePointer ownable)
Definition owner.inl:193
OwnerTry(OwnablePointer ownable, const CallerInfo &ci)
Definition owner.inl:172
~OwnerTry()
Destructor. Invokes Release() on member owned.
Definition owner.inl:186
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:132
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.inl:126
bool IsOwning() const noexcept
Definition owner.inl:203
OwnerTry(TOwnable &ownable, const CallerInfo &ci)
Definition owner.inl:164
bool isOwning
The result of the call to TryAcquire.
Definition owner.inl:130
OwnablePointer owned
The resource to acquire and release.
Definition owner.inl:54
std::remove_reference_t< TOwnable > * OwnablePointer
The pointer type of the owned object.
Definition owner.inl:51
~Owner()
Destructor. Releases the owner by invoking Release().
Definition owner.inl:97
Owner(TOwnable &ownable, const CallerInfo &ci)
Definition owner.inl:77
Owner(OwnablePointer ownable, const CallerInfo &ci)
Definition owner.inl:85
CallerInfo dbgCI
Caller information. Available only with debug-builds.
Definition owner.inl:56
void Set(OwnablePointer ownable)
Definition owner.inl:103
#define ALIB_HAS_METHOD(T, Method,...)
Definition alib.inl:1005
#define ALIB_STACK_ALLOCATED_TYPE(T)
Definition alib.inl:992
#define ALIB_EXPORT
Definition alib.inl:497
#define ALIB_DBG(...)
Definition alib.inl:853
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1066