ALib C++ Library
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 \ref mainpage_license "Boost Software 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/// \alib{lox::detail;ScopeStore::Walk} and
30/// \alib{lox::detail;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 \alib{lox::detail;ScopeStore::Walk} with two specializations.
43 /// @param self The \b ScopeStore that invoked us.
44 /// @return The result as specified in \alib{lox::detail;ScopeStore::Walk}.
46
47 /// Implements \alib{lox::detail;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 \alib{lox::detail;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 {
115 /// Calculates a hash code.
116 /// @param src The object to hash.
117 /// @return The hash code.
118 std::size_t operator()(const std::pair<bool, threads::ThreadID>& src) const {
119 return src.first ? std::size_t( src.second * 282312799l )
120 : std::size_t( src.second * 573292817l ) ^ std::size_t(-1);
121 }
122 };
123
124 /// The inner/outer thread map of values. The boolean value of the key is \c true for
125 /// the inner store and \c false for the outer.
129#endif
130
131
132 //################################################################################################
133 // Protected fields
134 //################################################################################################
135 protected:
136
137 /// ScopeInfo of 'our' lox.
139
140 /// Flag used to lazily create the key to language-related scope values.
142
143 /// Indicates if currently a scope walk is active.
145
146 /// The actual scope of a walk.
148
149 /// The actual language related scope's map node.
151
152 /// The path level when using access methods.
154
155#if !ALIB_SINGLE_THREADED
156 /// Actual thread ID
158#endif
159
160 /// The 'local object' returned by a walk after Scope::ThreadInner and before Scope::Method.
162
163 /// The next value of a walk during \e Scope::ThreadInner/Outer.
165
166 /// The list of values of \e Scope::ThreadOuter/Inner during a walk.
168
169 //################################################################################################
170 // Public interface
171 //################################################################################################
172 public:
173
174 /// Constructor
175 /// @param scopeInfo The ScopeInfo singleton of 'our' Lox.
176 /// @param monoAllocator The monotonic allocator used for the
177 /// \alib{containers;StringTree} needed by member #languageStore.
179 ScopeStore( ScopeInfo& scopeInfo, MonoAllocator& monoAllocator );
180
181 /// Destructor
183 ~ScopeStore();
184
185 /// Initializes access methods #Store, #Get and #Remove and has to be invoked before
186 /// using them
187 /// @param scope Scope to use.
188 /// @param pathLevel Used only if parameter \p{scope} equals \alib{lox;Scope;Scope::Path}
189 /// to reference parent directories. Optional and defaults to \c 0.
190 /// @param threadID ID of the associated thread (for thread-related scopes only).
191 /// If \alib{threads::UNDEFINED} is given, the ID provided in
192 /// \p{scopeInfo} is used.
194 void InitAccess ( Scope scope, int pathLevel, threads::ThreadID threadID );
195
196 /// Stores a new value.
197 /// @param value The value to set.
198 /// @return Returns the previous value stored.
199 T Store ( T value ) { ALIB_ASSERT( value != nullptr, "ALOX" ) return access( 0, value ); }
200
201 /// Removes a value.
202 /// @param value The value to remove (must only be given for thread-related \e Scopes).
203 /// @return Returns the previous value stored.
204 T Remove ( T value ) { return access( 1, value ); }
205
206 /// Retrieves the value.
207 /// @return Returns the current value stored.
208 T Get () { return access( 2, nullptr ); }
209
210 /// Initializes a scope 'walk' by storing the given scope information and
211 /// setting fields of our walk 'state-machine' to proper start values.
212 ///
213 /// @param startScope The \e Scope to start searching for.
214 /// @param localObject The 'local object' returned by a walk after Scope::ThreadInner
215 /// and before Scope::Method
217 void InitWalk( Scope startScope, const T localObject );
218
219 /// Searches next value in the actual scope. While not found, moves walk state to next outer
220 /// state and continues there.
221 /// @return The next object found in the current or any next outer scope.
223
224 //###############################################################################################
225 // Internals
226 //###############################################################################################
227 protected:
228 /// Retrieves and optionally creates an entry in the map that stores language-related
229 /// scope information. The result is stored in field #actStringTreeNode.
230 /// @param create If \c true, a non-existing entry is created.
231 void initCursor( bool create );
232
233 /// Receives, inserts or removes a value.
234 /// @param cmd 0= insert, 1= remove, 2= get.
235 /// @param value The new value or the one to be removed.
236 /// @return Returns the previous value stored.
237 T access( int cmd, T value )
238 { return ScopeStoreHelper<T, TStackedThreadValues>().doAccess( *this, cmd, value ); }
239}; // ScopeStore
240
241
242#if !DOXYGEN
243
244extern template struct ScopeStoreHelper<NString , true>;
245extern template class ScopeStore <NString , true>;
246
247extern template struct ScopeStoreHelper<PrefixLogable* , true>;
248extern template class ScopeStore <PrefixLogable* , true>;
249
250
251extern template struct ScopeStoreHelper<SSMap<int>*, false>;
252extern template class ScopeStore <SSMap<int>*, false>;
253
254extern template struct ScopeStoreHelper<SSMap<Box>*, false>;
255extern template class ScopeStore <SSMap<Box>*, false>;
256
257#endif
258
259} // namespace [alib::lox::detail]
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).
T globalStore
The value of the global scope.
ALIB_DLL ScopeStore(ScopeInfo &scopeInfo, MonoAllocator &monoAllocator)
containers::StringTree< MonoAllocator, T, StringTreeNamesAlloc< character > > TLanguageStore
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.
ALIB_DLL ~ScopeStore()
Destructor.
ALIB_DLL void InitWalk(Scope startScope, const T localObject)
ALIB_DLL 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.
#define ALIB_DLL
Definition alib.inl:503
#define ALIB_ASSERT(cond, domain)
Definition alib.inl:1065
#define ALIB_EXPORT
Definition alib.inl:497
@ 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::TPoolAllocator< MonoAllocator > PoolAllocator
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2198
containers::HashMap< TAllocator, TKey, TMapped, THash, TEqual, THashCaching, TRecycling > HashMap
Type alias in namespace alib.
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
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