ALib C++ Library
Library Version: 2402 R1
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 the \aliblong. It does not belong to an \alibmod and is
4 * included in any \alibdist.
5 *
6 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
7 * Published under \ref mainpage_license "Boost Software License".
8 **************************************************************************************************/
9#ifndef HPP_ALIB_LANG_OWNER
10#define HPP_ALIB_LANG_OWNER 1
11
12#if !defined(HPP_ALIB) && !defined(ALIB_DOX)
13# include "alib/alib.hpp"
14#endif
15
16#if ALIB_STRINGS
17
18#if !defined(HPP_ALIB_STRINGS_CSTRING)
20#endif
21
22namespace alib { namespace lang {
23
24/** ************************************************************************************************
25 * Ensures that an object of template type \p{TOwnable} is acquired and properly released when
26 * unwinding the stack. This class is meant to be allocated only on the stack and not on the heap.
27 * Therefore, the new operators are declared private.
28 *
29 * With debug builds the constructor expects caller source information parameters
30 * \p{file}, \p{line} and \p{func}. It is convenient to use macro
31 * \ref ALIB_CALLER_PRUNED to provide those.
32 *
33 * Note that this type is available only if \alib_strings is included in the \alibdist.
34 *
35 * \see
36 * Preprocessor macros \ref ALIB_OWN, \ref ALIB_LOCK and \ref ALIB_LOCK_WITH for a convenient way
37 * to use this class.
38 *
39 * @tparam TOwnable The type to own. The type needs to have methods \b %Acquire and \b %Release
40 * available.
41 **************************************************************************************************/
42template <typename TOwnable>
43class Owner
44{
45 // #############################################################################################
46 // Disallow new operations.
47 // #############################################################################################
48 private:
49 /** Private new to disallow heap allocation.
50 * @return Never called. */
51 void* operator new (size_t);
52 /** Private new to disallow heap allocation.
53 * @return Never called. */
54 void* operator new (size_t, void*);
55 /** Private new to disallow heap allocation.
56 * @return Never called. */
57 void* operator new[](size_t);
58 /** Private new to disallow heap allocation.
59 * @return Never called. */
60 void* operator new[](size_t, void*);
61 /** Private assignment operator. */
62 void operator = (const Owner& );
63
64 protected:
65 /** All we own is this. */
66 TOwnable& theOwnable;
67
68 public:
69
70 /** ****************************************************************************************
71 * The constructor. Invokes Acquire() on the owner.
72 * @param ownable The ownable to acquire.
73 *
74 * @param dbgFile Caller information. Available only with debug builds.
75 * @param dbgLine Caller information. Available only with debug builds.
76 * @param dbgFunc Caller information. Available only with debug builds.
77 ******************************************************************************************/
78 #if ALIB_DEBUG
79 Owner( TOwnable& ownable, const NCString& dbgFile, int dbgLine, const NCString& dbgFunc)
80 : theOwnable(ownable)
81 {
82 ownable.Acquire( dbgFile, dbgLine, dbgFunc );
83 }
84 #else
85 Owner( TOwnable& ownable ) : theOwnable(ownable)
86 {
87 ownable.Acquire();
88 }
89 #endif
90
91 /** ****************************************************************************************
92 * The destructor. Releases the owner by invoking Release().
93 ******************************************************************************************/
94 ~Owner() { theOwnable.Release(); }
95}; //class Owner
96
97} // namespace alib[::lang]
98
99/// Type alias in namespace \b alib.
100template <typename TOwnable>
102
103} // namespace [alib]
104
105
106// #################################################################################################
107// Macros
108// #################################################################################################
109#define ALIB_OWN(ownable) \
110alib::Owner<decltype(ownable)> ALIB_IDENTIFIER(owner) (ownable ALIB_COMMA_DBG ALIB_CALLER_PRUNED);
111
112
113#if !ALIB_DEBUG
114#define ALIB_DBG_PREVENT_RECURSIVE_METHOD_CALLS_MEMBER_DECL
115#define ALIB_DBG_PREVENT_RECURSIVE_METHOD_CALLS
116
117#else
118
119#define ALIB_DBG_PREVENT_RECURSIVE_METHOD_CALLS_MEMBER_DECL \
120bool dbgRecursionDetectionFlag = false;
121
122#define ALIB_DBG_PREVENT_RECURSIVE_METHOD_CALLS \
123struct RecursionDetection \
124{ \
125 bool& TestMember; \
126 RecursionDetection( bool& testMember ) : TestMember(testMember) {} \
127 \
128 void Acquire( const NCString&, int, const NCString& func ) \
129 { \
130 ALIB_ASSERT_ERROR(TestMember==false, "FSOWNER", "Forbidden recursive use of method ", func) \
131 TestMember= true; \
132 } \
133 void Release() { TestMember= false; } \
134}; \
135RecursionDetection dbgRecursionDetection( dbgRecursionDetectionFlag ); \
136ALIB_OWN(dbgRecursionDetection);
137#endif
138
139#endif // ALIB_STRINGS
140
141#endif // HPP_ALIB_LANG_OWNER
Owner(TOwnable &ownable, const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
Definition owner.hpp:79
TOwnable & theOwnable
Definition owner.hpp:66
void operator=(const Owner &)
Definition alib.cpp:57
lang::Owner< TOwnable > Owner
Type alias in namespace alib.
Definition owner.hpp:101