ALib C++ Library
Library Version: 2510 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, StdVectorMono<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 {
120 return src.first ? std::size_t( src.second * 282312799l )
121 : std::size_t( src.second * 573292817l ) ^ std::size_t(-1);
122 }
123 };
124
125 /// The inner/outer thread map of values. The boolean value of the key is \c true for
126 /// the inner store and \c false for the outer.
130#endif
131
132
133 // #############################################################################################
134 // Protected fields
135 // #############################################################################################
136 protected:
137
138 /// ScopeInfo of 'our' lox.
140
141 /// Flag used to lazily create the key to language-related scope values.
143
144 /// Indicates if currently a scope walk is active.
146
147 /// The actual scope of a walk.
149
150 /// The actual language related scope's map node.
152
153 /// The path level when using access methods.
155
156#if !ALIB_SINGLE_THREADED
157 /// Actual thread ID
159#endif
160
161 /// The 'local object' returned by a walk after Scope::ThreadInner and before Scope::Method.
163
164 /// The next value of a walk during \e Scope::ThreadInner/Outer.
166
167 /// The list of values of \e Scope::ThreadOuter/Inner during a walk.
169
170 // #############################################################################################
171 // Public interface
172 // #############################################################################################
173 public:
174
175 /// Constructor
176 /// @param scopeInfo The ScopeInfo singleton of 'our' Lox.
177 /// @param monoAllocator The monotonic allocator used for the
178 /// \alib{containers;StringTree} needed by member #languageStore.
180 ScopeStore( ScopeInfo& scopeInfo, MonoAllocator& monoAllocator );
181
182 /// Destructor
184 ~ScopeStore();
185
186 /// Initializes access methods #Store, #Get and #Remove and has to be invoked before
187 /// using them
188 /// @param scope Scope to use.
189 /// @param pathLevel Used only if parameter \p{scope} equals \alib{lox;Scope;Scope::Path}
190 /// to reference parent directories. Optional and defaults to \c 0.
191 /// @param threadID ID of the associated thread (for thread-related scopes only).
192 /// If \alib{threads::UNDEFINED} is given, the ID provided in
193 /// \p{scopeInfo} is used.
195 void InitAccess ( Scope scope, int pathLevel, threads::ThreadID threadID );
196
197 /// Stores a new value.
198 /// @param value The value to set.
199 /// @return Returns the previous value stored.
200 T Store ( T value )
201 {
202 ALIB_ASSERT( value != nullptr, "ALOX" )
203 return access( 0, value );
204 }
205
206 /// Removes a value.
207 /// @param value The value to remove (must only be given for thread-related \e Scopes).
208 /// @return Returns the previous value stored.
209 T Remove ( T value ) { return access( 1, value ); }
210
211 /// Retrieves the value.
212 /// @return Returns the current value stored.
213 T Get ()
214 {
215 return access( 2, nullptr );
216 }
217
218 /// Initializes a scope 'walk' by storing the given scope information and
219 /// setting fields of our walk 'state-machine' to proper start values.
220 ///
221 /// @param startScope The \e Scope to start searching for.
222 /// @param localObject The 'local object' returned by a walk after Scope::ThreadInner
223 /// and before Scope::Method
225 void InitWalk( Scope startScope, const T localObject );
226
227 /// Searches next value in the actual scope. While not found, moves walk state to next outer
228 /// state and continues there.
229 /// @return The next object found in the current or any next outer scope.
231
232 // #############################################################################################
233 // Internals
234 // #############################################################################################
235 protected:
236 /// Retrieves and optionally creates an entry in the map that stores language-related
237 /// scope information. The result is stored in field #actStringTreeNode.
238 /// @param create If \c true, a non-existing entry is created.
239 void initCursor( bool create );
240
241 /// Receives, inserts or removes a value.
242 /// @param cmd 0= insert, 1= remove, 2= get.
243 /// @param value The new value or the one to be removed.
244 /// @return Returns the previous value stored.
245 T access( int cmd, T value ) { return ScopeStoreHelper<T, TStackedThreadValues>()
246 .doAccess( *this, cmd, value ); }
247}; // ScopeStore
248
249
250#if !DOXYGEN
251
252extern template struct ScopeStoreHelper<NString , true>;
253extern template class ScopeStore <NString , true>;
254
255extern template struct ScopeStoreHelper<PrefixLogable* , true>;
256extern template class ScopeStore <PrefixLogable* , true>;
257
258
259extern template struct ScopeStoreHelper<SSMap<int>*, false>;
260extern template class ScopeStore <SSMap<int>*, false>;
261
262extern template struct ScopeStoreHelper<SSMap<Box>*, false>;
263extern template class ScopeStore <SSMap<Box>*, false>;
264
265#endif
266
267} // namespace [alib::lox::detail]
threads::ThreadID actThreadID
Actual thread ID.
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.
std::conditional_t< TStackedThreadValues, StdVectorMono< T >, T > TThreadMapValue
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:496
#define ALIB_ASSERT(cond, domain)
Definition alib.inl:1048
#define ALIB_EXPORT
Definition alib.inl:488
@ 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:2390
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