ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
scopestore.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_alox of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
9
10// forward declarations
11class ScopeInfo;
12class PrefixLogable;
13
14/// Shortcut to the ScopeStore's hashmap.
15template <typename T> using SSMap = HashMap< PoolAllocator ,
16 NString ,// key
17 T ,// map
18 std::hash <NString> ,
19 std::equal_to<NString> ,
21 Recycling::None >;
22
23
24// forwards
25template<typename T, bool TStackedThreadValues>
26class ScopeStore;
27
28/// A helper-class that has two specializations to implement different versions of methods
29/// #"ScopeStore::Walk" and
30/// #"ScopeStore::access" for each boolean value of template argument
31/// \p{TStackedThreadValues}.<br>
32///
33/// @tparam T The stored object type.
34/// @tparam TStackedThreadValues If true, values stored for thread scopes will be always replaced
35/// instead of appended.
36/// This is, for example, \c false for <em>Log Data</em> and
37/// <em>Log Once</em> and \c true for <em>Scope Domains</em>
38/// and <em>Prefix Logables</em>.
39template<typename T, bool TStackedThreadValues>
41{
42 /// Implements #"ScopeStore::Walk" with two specializations.
43 /// @param self The \b ScopeStore that invoked us.
44 /// @return The result as specified in #"ScopeStore::Walk".
46
47 /// Implements #"ScopeStore::access" with two specializations.
48 /// @param self The \b ScopeStore that invoked us.
49 /// @param cmd Parameter \p{cmd} of the original method.
50 /// @param value Parameter \p{value} of the original method.
51 /// @return The result as specified in #"ScopeStore::access".
52 T doAccess( ScopeStore<T, TStackedThreadValues>& self, int cmd, T value );
53};
54
55#if !DOXYGEN
56// specializations for true/false of TStackedThreadValues
57template<typename T > struct ScopeStoreHelper<T, false>
58{
59 T doWalk ( ScopeStore<T, false>& self );
60 T doAccess( ScopeStore<T, false>& self, int cmd, T value );
61};
62
63template<typename T > struct ScopeStoreHelper<T, true>
64{
65 T doWalk ( ScopeStore<T, true>& self );
66 T doAccess( ScopeStore<T, true>& self, int cmd, T value );
67};
68#endif
69
70//==================================================================================================
71/// This class is responsible for scope-related functionality of class Lox.
72/// \note This is a pure internal helper-class. Documentation may be limited.
73/// @tparam T The stored object type.
74/// @tparam TStackedThreadValues If true, values stored for thread scopes will be always replaced
75/// instead of appended.
76/// This is, for example, \c false for <em>Log Data</em> and
77/// <em>Log Once</em> and \c true for <em>Scope Domains</em>
78/// and <em>Prefix Logables</em>.
79//==================================================================================================
80template<typename T, bool TStackedThreadValues>
82{
83 #if !DOXYGEN
84 friend struct ScopeStoreHelper<T, TStackedThreadValues>;
85 #endif
86
87 public:
88 /// Alias name for the string tree template used for storing language-related data.
89 /// The language store uses a \c StringTree with a monotonic allocator.
90 /// This does not lead to memory leaks, because during the life-time of a \b %Lox objects
91 /// are only added, but never deleted. If a value is unset, the node is not deleted but
92 /// set to a \e nulled value. This makes the language store very memory efficient (and fast).
94 T,
96
97 /// The type of object stored for the thread values. This depends on whether multiple
98 /// (a stack of) values can be stored, which is not true for log data and log once, as
99 /// those operate with hash maps.
100 using TThreadMapValue= std::conditional_t<TStackedThreadValues, StdVectorMA<T>, T>;
101
102 /// The value of the global scope.
104
105 /// \b %StringTree to store data for language-related scopes (path,source,method).
107
108#if !ALIB_SINGLE_THREADED
109 /// Key type for the thread store.
110 using ThreadMapKey = std::pair<bool,threads::ThreadID>;
111
112 /// Hash functor for <c>std::pair<bool,ThreadID></c>.
114 /// Calculates a hash code.
115 /// @param src The object to hash.
116 /// @return The hash code.
117 std::size_t operator()(const std::pair<bool, threads::ThreadID>& src) const {
118 return src.first ? std::size_t( src.second * 282312799l )
119 : std::size_t( src.second * 573292817l ) ^ std::size_t(-1);
120 }
121 };
122
123 /// The inner/outer thread map of values. The boolean value of the key is \c true for
124 /// the inner store and \c false for the outer.
128#endif
129
130
131 //################################################################################################
132 // Protected fields
133 //################################################################################################
134 protected:
135
136 /// ScopeInfo of 'our' lox.
138
139 /// Flag used to lazily create the key to language-related scope values.
141
142 /// Indicates if currently a scope walk is active.
144
145 /// The actual scope of a walk.
147
148 /// The actual language related scope's map node.
150
151 /// The path level when using access methods.
153
154#if !ALIB_SINGLE_THREADED
155 /// Actual thread ID
157#endif
158
159 /// The 'local object' returned by a walk after Scope::ThreadInner and before Scope::Method.
161
162 /// The next value of a walk during \e Scope::ThreadInner/Outer.
164
165 /// The list of values of \e Scope::ThreadOuter/Inner during a walk.
167
168 //################################################################################################
169 // Public interface
170 //################################################################################################
171 public:
172
173 /// Constructor
174 /// @param scopeInfo The ScopeInfo singleton of 'our' Lox.
175 /// @param monoAllocator The monotonic allocator used for the
176 /// #"StringTree" needed by member #"languageStore".
178 ScopeStore( ScopeInfo& scopeInfo, MonoAllocator& monoAllocator );
179
180 /// Destructor
182 ~ScopeStore();
183
184 /// Initializes access methods #".Store", #".Get" and #Remove and has to be invoked before
185 /// using them
186 /// @param scope Scope to use.
187 /// @param pathLevel Used only if parameter \p{scope} equals #"lox Scope::Path;*"
188 /// to reference parent directories. Optional and defaults to \c 0.
189 /// @param threadID ID of the associated thread (for thread-related scopes only).
190 /// If #"threads::UNDEFINED" is given, the ID provided in
191 /// \p{scopeInfo} is used.
193 void InitAccess ( Scope scope, int pathLevel, threads::ThreadID threadID );
194
195 /// Stores a new value.
196 /// @param value The value to set.
197 /// @return Returns the previous value stored.
198 T Store ( T value ) { ALIB_ASSERT( value != nullptr, "ALOX" ) return access( 0, value ); }
199
200 /// Removes a value.
201 /// @param value The value to remove (must only be given for thread-related \e Scopes).
202 /// @return Returns the previous value stored.
203 T Remove ( T value ) { return access( 1, value ); }
204
205 /// Retrieves the value.
206 /// @return Returns the current value stored.
207 T Get () { return access( 2, nullptr ); }
208
209 /// Initializes a scope 'walk' by storing the given scope information and
210 /// setting fields of our walk 'state-machine' to proper start values.
211 ///
212 /// @param startScope The \e Scope to start searching for.
213 /// @param localObject The 'local object' returned by a walk after Scope::ThreadInner
214 /// and before Scope::Method
216 void InitWalk( Scope startScope, const T localObject );
217
218 /// Searches next value in the actual scope. While not found, moves walk state to next outer
219 /// state and continues there.
220 /// @return The next object found in the current or any next outer scope.
222
223 //###############################################################################################
224 // Internals
225 //###############################################################################################
226 protected:
227 /// Retrieves and optionally creates an entry in the map that stores language-related
228 /// scope information. The result is stored in field #actStringTreeNode.
229 /// @param create If \c true, a non-existing entry is created.
230 void initCursor( bool create );
231
232 /// Receives, inserts or removes a value.
233 /// @param cmd 0= insert, 1= remove, 2= get.
234 /// @param value The new value or the one to be removed.
235 /// @return Returns the previous value stored.
236 T access( int cmd, T value )
237 { return ScopeStoreHelper<T, TStackedThreadValues>().doAccess( *this, cmd, value ); }
238}; // ScopeStore
239
240
241#if !DOXYGEN
242
243extern template struct ScopeStoreHelper<NString , true>;
244extern template class ScopeStore <NString , true>;
245
246extern template struct ScopeStoreHelper<PrefixLogable* , true>;
247extern template class ScopeStore <PrefixLogable* , true>;
248
249
250extern template struct ScopeStoreHelper<SSMap<int>*, false>;
251extern template class ScopeStore <SSMap<int>*, false>;
252
253extern template struct ScopeStoreHelper<SSMap<Box>*, false>;
254extern template class ScopeStore <SSMap<Box>*, false>;
255
256#endif
257
258} // namespace [alib::lox::detail]
#define ALIB_DLL
Definition alib.inl:573
#define ALIB_ASSERT(cond, domain)
Definition alib.inl:1143
#define ALIB_EXPORT
Definition alib.inl:562
threads::ThreadID actThreadID
Actual thread ID.
std::conditional_t< TStackedThreadValues, StdVectorMA< T >, T > TThreadMapValue
Scope actScope
The actual scope of a walk.
TLanguageStore languageStore
StringTree to store data for language-related scopes (path,source,method).
containers::StringTree< MonoAllocator, T, StringTreeNamesAlloc< character > > TLanguageStore
T globalStore
The value of the global scope.
ScopeStore(ScopeInfo &scopeInfo, MonoAllocator &monoAllocator)
bool lazyLanguageNode
Flag used to lazily create the key to language-related scope values.
HashMap< MonoAllocator, ThreadMapKey, TThreadMapValue, BoolThreadIDHash > threadStore
T access(int cmd, T value)
std::pair< bool, threads::ThreadID > ThreadMapKey
Key type for the thread store.
TLanguageStore::Cursor actStringTreeNode
The actual language related scope's map node.
int walkNextThreadIdx
The next value of a walk during Scope::ThreadInner/Outer.
T walkLocalObject
The 'local object' returned by a walk after Scope::ThreadInner and before Scope::Method.
void InitWalk(Scope startScope, const T localObject)
void InitAccess(Scope scope, int pathLevel, threads::ThreadID threadID)
bool walking
Indicates if currently a scope walk is active.
int actPathLevel
The path level when using access methods.
ScopeInfo & scopeInfo
ScopeInfo of 'our' lox.
TThreadMapValue * walkThreadValues
The list of values of Scope::ThreadOuter/Inner during a walk.
@ Enabled
Caching is enabled.
HashMap< PoolAllocator, NString, T, std::hash< NString >, std::equal_to< NString >, lang::Caching::Enabled, Recycling::None > SSMap
Shortcut to the ScopeStore's hashmap.
integer ThreadID
The ALib thread identifier type.
Definition thread.inl:23
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2181
monomem::TPoolAllocator< MonoAllocator > PoolAllocator
containers::HashMap< TAllocator, TKey, TMapped, THash, TEqual, THashCaching, TRecycling > HashMap
Type alias in namespace alib.
containers::StringTreeNamesAlloc< TChar > StringTreeNamesAlloc
Type alias in namespace alib.
T doWalk(ScopeStore< T, TStackedThreadValues > &self)
T doAccess(ScopeStore< T, TStackedThreadValues > &self, int cmd, T value)
Hash functor for std::pair<bool,ThreadID>.
std::size_t operator()(const std::pair< bool, threads::ThreadID > &src) const