ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
scopestore.hpp
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/// Copyright 2013-2026 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 #"ScopeStore::access" for each boolean value of template argument
30/// \p{TStackedThreadValues}.<br>
31///
32/// @tparam T The stored object type.
33/// @tparam TStackedThreadValues If true, values stored for thread scopes will be always replaced
34/// instead of appended.
35/// This is, for example, \c false for <em>Log Data</em> and
36/// <em>Log Once</em> and \c true for <em>Scope Domains</em>
37/// and <em>Prefix Logables</em>.
38template<typename T, bool TStackedThreadValues>
40 /// Implements #"ScopeStore::Walk" with two specializations.
41 /// @param self The #"%ScopeStore" that invoked us.
42 /// @return The result as specified in #"ScopeStore::Walk".
44
45 /// Implements #"ScopeStore::access" with two specializations.
46 /// @param self The #"%ScopeStore" that invoked us.
47 /// @param cmd Parameter \p{cmd} of the original method.
48 /// @param value Parameter \p{value} of the original method.
49 /// @return The result as specified in #"ScopeStore::access".
50 T doAccess( ScopeStore<T, TStackedThreadValues>& self, int cmd, T value );
51};
52
53#if !DOXYGEN
54// specializations for true/false of TStackedThreadValues
55template<typename T > struct ScopeStoreHelper<T, false> {
56 T doWalk ( ScopeStore<T, false>& self );
57 T doAccess( ScopeStore<T, false>& self, int cmd, T value );
58};
59
60template<typename T > struct ScopeStoreHelper<T, true> {
61 T doWalk ( ScopeStore<T, true>& self );
62 T doAccess( ScopeStore<T, true>& self, int cmd, T value );
63};
64#endif
65
66//==================================================================================================
67/// This class is responsible for scope-related functionality of class Lox.
68/// \note This is a pure internal helper-class. Documentation may be limited.
69/// @tparam T The stored object type.
70/// @tparam TStackedThreadValues If true, values stored for thread scopes will be always replaced
71/// instead of appended.
72/// This is, for example, \c false for <em>Log Data</em> and
73/// <em>Log Once</em> and \c true for <em>Scope Domains</em>
74/// and <em>Prefix Logables</em>.
75//==================================================================================================
76template<typename T, bool TStackedThreadValues>
78 #if !DOXYGEN
79 friend struct ScopeStoreHelper<T, TStackedThreadValues>;
80 #endif
81
82 public:
83 /// Alias name for the string tree template used for storing language-related data.
84 /// The language store uses a \c StringTree with a monotonic allocator.
85 /// This does not lead to memory leaks, because during the life-time of a #"%Lox" objects
86 /// are only added, but never deleted. If a value is unset, the node is not deleted but
87 /// set to a \e nulled value. This makes the language store very memory efficient (and fast).
89 T,
91
92 /// The type of object stored for the thread values. This depends on whether multiple
93 /// (a stack of) values can be stored, which is not true for log data and log once, as
94 /// those operate with hash maps.
95 using TThreadMapValue= std::conditional_t<TStackedThreadValues, StdVectorMA<T>, T>;
96
97 /// The value of the global scope.
99
100 /// #"%StringTree" to store data for language-related scopes (path,source,method).
102
103#if !ALIB_SINGLE_THREADED
104 /// Key type for the thread store.
105 using ThreadMapKey = std::pair<bool,threads::ThreadID>;
106
107 /// Hash functor for <c>std::pair<bool,ThreadID></c>.
109 /// Calculates a hash code.
110 /// @param src The object to hash.
111 /// @return The hash code.
112 std::size_t operator()(const std::pair<bool, threads::ThreadID>& src) const {
113 return src.first ? std::size_t( src.second * 282312799l )
114 : std::size_t( src.second * 573292817l ) ^ std::size_t(-1);
115 }
116 };
117
118 /// The inner/outer thread map of values. The boolean value of the key is \c true for
119 /// the inner store and \c false for the outer.
123#endif
124
125
126 //################################################################################################
127 // Protected fields
128 //################################################################################################
129 protected:
130
131 /// ScopeInfo of 'our' lox.
133
134 /// Flag used to lazily create the key to language-related scope values.
136
137 /// Indicates if currently a scope walk is active.
139
140 /// The actual scope of a walk.
142
143 /// The actual language related scope's map node.
145
146 /// The path level when using access methods.
148
149#if !ALIB_SINGLE_THREADED
150 /// Actual thread ID
152#endif
153
154 /// The 'local object' returned by a walk after Scope::ThreadInner and before Scope::Method.
156
157 /// The next value of a walk during \e Scope::ThreadInner/Outer.
159
160 /// The list of values of \e Scope::ThreadOuter/Inner during a walk.
162
163 //################################################################################################
164 // Public interface
165 //################################################################################################
166 public:
167
168 /// Constructor
169 /// @param scopeInfo The ScopeInfo singleton of 'our' Lox.
170 /// @param monoAllocator The monotonic allocator used for the
171 /// #"StringTree" needed by member #"languageStore".
173 ScopeStore( ScopeInfo& scopeInfo, MonoAllocator& monoAllocator );
174
175 /// Destructor
177 ~ScopeStore();
178
179 /// Initializes access methods #".Store", #".Get" and #".Remove" and has to be invoked before
180 /// using them
181 /// @param scope Scope to use.
182 /// @param pathLevel Used only if parameter \p{scope} equals #"lox Scope::Path;*"
183 /// to reference parent directories. Optional and defaults to \c 0.
184 /// @param threadID ID of the associated thread (for thread-related scopes only).
185 /// If #"threads::UNDEFINED" is given, the ID provided in
186 /// \p{scopeInfo} is used.
188 void InitAccess ( Scope scope, int pathLevel, threads::ThreadID threadID );
189
190 /// Stores a new value.
191 /// @param value The value to set.
192 /// @return Returns the previous value stored.
193 T Store ( T value ) { ALIB_ASSERT( value != nullptr, "ALOX" ) return access( 0, value ); }
194
195 /// Removes a value.
196 /// @param value The value to remove (must only be given for thread-related \e Scopes).
197 /// @return Returns the previous value stored.
198 T Remove ( T value ) { return access( 1, value ); }
199
200 /// Retrieves the value.
201 /// @return Returns the current value stored.
202 T Get () { return access( 2, nullptr ); }
203
204 /// Initializes a scope 'walk' by storing the given scope information and
205 /// setting fields of our walk 'state-machine' to proper start values.
206 ///
207 /// @param startScope The \e Scope to start searching for.
208 /// @param localObject The 'local object' returned by a walk after Scope::ThreadInner
209 /// and before Scope::Method
211 void InitWalk( Scope startScope, const T localObject );
212
213 /// Searches next value in the actual scope. While not found, moves walk state to next outer
214 /// state and continues there.
215 /// @return The next object found in the current or any next outer scope.
217
218 //###############################################################################################
219 // Internals
220 //###############################################################################################
221 protected:
222 /// Retrieves and optionally creates an entry in the map that stores language-related
223 /// scope information. The result is stored in field #".actStringTreeNode".
224 /// @param create If \c true, a non-existing entry is created.
225 void initCursor( bool create );
226
227 /// Receives, inserts or removes a value.
228 /// @param cmd 0= insert, 1= remove, 2= get.
229 /// @param value The new value or the one to be removed.
230 /// @return Returns the previous value stored.
231 T access( int cmd, T value )
232 { return ScopeStoreHelper<T, TStackedThreadValues>().doAccess( *this, cmd, value ); }
233}; // ScopeStore
234
235
236#if !DOXYGEN
237
238extern template struct ScopeStoreHelper<NString , true>;
239extern template class ScopeStore <NString , true>;
240
241extern template struct ScopeStoreHelper<PrefixLogable* , true>;
242extern template class ScopeStore <PrefixLogable* , true>;
243
244
245extern template struct ScopeStoreHelper<SSMap<int>*, false>;
246extern template class ScopeStore <SSMap<int>*, false>;
247
248extern template struct ScopeStoreHelper<SSMap<Box>*, false>;
249extern template class ScopeStore <SSMap<Box>*, false>;
250
251#endif
252
253} // namespace [alib::lox::detail]
#define ALIB_DLL
#define ALIB_ASSERT(cond, domain)
#define ALIB_EXPORT
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.hpp:23
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
strings::TString< nchar > NString
Type alias in namespace #"%alib".
Definition string.hpp:2174
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