ALib C++ Library
Library Version: 2412 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-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_LOX_DETAIL_SCOPE
9#define HPP_ALIB_LOX_DETAIL_SCOPE 1
10#pragma once
11#if !defined(HPP_ALIB_LOX_PROPPERINCLUDE)
12# error "ALib sources with ending '.inl' must not be included from outside."
13#endif
14
19
20namespace alib { namespace lox { namespace detail {
21
22// forward declarations
23class ScopeInfo;
24class PrefixLogable;
25
26
27/// Shortcut to the ScopeStore's hashmap.
28template <typename T> using SSMap = HashMap< PoolAllocator ,
29 NString ,// key
30 T ,// map
31 std::hash <NString> ,
32 std::equal_to<NString> ,
34 Recycling::None >;
35
36
37// forwards
38template<typename T, bool TStackedThreadValues>
39class ScopeStore;
40
41/// A helper-class that has two specializations to implement different versions of methods
42/// \alib{lox::detail;ScopeStore::Walk} and
43/// \alib{lox::detail;ScopeStore::access} for each boolean value of template argument
44/// \p{TStackedThreadValues}.<br>
45///
46/// @tparam T The stored object type.
47/// @tparam TStackedThreadValues If true, values stored for thread scopes will be always replaced
48/// instead of appended.
49/// This is for example \c false for <em>Log Data</em> and
50/// <em>Log Once</em> and \c true for <em>Scope Domains</em>
51/// and <em>Prefix Logables</em>.
52template<typename T, bool TStackedThreadValues>
54{
55 /// Implements \alib{lox::detail;ScopeStore::Walk} with two specializations.
56 /// @param self The \b ScopeStore that invoked us.
57 /// @return The result as specified in \alib{lox::detail;ScopeStore::Walk}.
59
60 /// Implements \alib{lox::detail;ScopeStore::access} with two specializations.
61 /// @param self The \b ScopeStore that invoked us.
62 /// @param cmd Parameter \p{cmd} of the original method.
63 /// @param value Parameter \p{value} of the original method.
64 /// @return The result as specified in \alib{lox::detail;ScopeStore::access}.
65 T doAccess( ScopeStore<T, TStackedThreadValues>& self, int cmd, T value );
66};
67
68#if !DOXYGEN
69// specializations for true/false of TStackedThreadValues
70template<typename T > struct ScopeStoreHelper<T, false>
71{
72 T doWalk ( ScopeStore<T, false>& self );
73 T doAccess( ScopeStore<T, false>& self, int cmd, T value );
74};
75
76template<typename T > struct ScopeStoreHelper<T, true>
77{
78 T doWalk ( ScopeStore<T, true>& self );
79 T doAccess( ScopeStore<T, true>& self, int cmd, T value );
80};
81#endif
82
83//==================================================================================================
84/// This class is responsible for scope-related functionality of class Lox.
85/// \note This is a pure internal helper-class. Documentation may be limited.
86/// @tparam T The stored object type.
87/// @tparam TStackedThreadValues If true, values stored for thread scopes will be always replaced
88/// instead of appended.
89/// This is for example \c false for <em>Log Data</em> and
90/// <em>Log Once</em> and \c true for <em>Scope Domains</em>
91/// and <em>Prefix Logables</em>.
92//==================================================================================================
93template<typename T, bool TStackedThreadValues>
95{
96 #if !DOXYGEN
97 friend struct ScopeStoreHelper<T, TStackedThreadValues>;
98 #endif
99
100 public:
101 /// Alias name for the string tree template used for storing language-related data.
102 /// The language store uses a \c StringTree with a monotonic allocator.
103 /// This does not lead to memory leaks, because during the life-time of a \b %Lox objects
104 /// are only added, but never deleted. If a value is unset, the node is not deleted but
105 /// set to a \e nulled value. This makes the language store very memory efficient (and fast).
107 T,
109
110 /// The type of object stored for the thread values. This depends on whether multiple
111 /// (a stack of) values can be stored, which is not true for log data and log once, as
112 /// those operate with hash maps.
113 using TThreadMapValue= ATMP_IF_T_F(TStackedThreadValues, StdVectorMono<T>, T );
114
115 /// The value of the global scope.
117
118 /// \b %StringTree to store data for language-related scopes (path,source,method).
120
121#if ALIB_THREADS
122 /// Key type for the thread store.
123 using ThreadMapKey = std::pair<bool,threads::ThreadID>;
124
125 /// Hash functor for <c>std::pair<bool,ThreadID></c>.
127 {
128 /// Calculates a hash code.
129 /// @param src The object to hash.
130 /// @return The hash code.
131 std::size_t operator()(const std::pair<bool, threads::ThreadID>& src) const
132 {
133 return src.first ? std::size_t( src.second * 282312799l )
134 : std::size_t( src.second * 573292817l ) ^ std::size_t(-1);
135 }
136 };
137
138 /// The inner/outer thread map of values. The boolean value of the key is \c true for
139 /// the inner store and \c false for the outer.
143#endif
144
145
146 // #############################################################################################
147 // Protected fields
148 // #############################################################################################
149 protected:
150
151 /// ScopeInfo of 'our' lox.
153
154 /// Flag used to lazily create the key to language-related scope values.
156
157 /// Indicates if currently a scope walk is active.
159
160 /// The actual scope of a walk.
162
163 /// The actual language related scope's map node.
165
166 /// The path level when using access methods.
168
169#if ALIB_THREADS
170 /// Actual thread ID
172#endif
173
174 /// The 'local object' returned by a walk after Scope::ThreadInner and before Scope::Method.
176
177 /// The next value of a walk during \e Scope::ThreadInner/Outer.
179
180 /// The list of values of \e Scope::ThreadOuter/Inner during a walk.
182
183 // #############################################################################################
184 // Public interface
185 // #############################################################################################
186 public:
187
188 //==========================================================================================
189 /// Constructor
190 /// @param scopeInfo The ScopeInfo singleton of 'our' Lox.
191 /// @param monoAllocator The monotonic allocator used for the
192 /// \alib{containers;StringTree} needed by member #languageStore.
193 //==========================================================================================
195 ScopeStore( ScopeInfo& scopeInfo, MonoAllocator& monoAllocator );
196
197 //==========================================================================================
198 /// Destructor
199 //==========================================================================================
201 ~ScopeStore();
202
203 //==========================================================================================
204 /// Initializes access methods #Store, #Get and #Remove and has to be invoked before
205 /// using them
206 /// @param scope Scope to use.
207 /// @param pathLevel Used only if parameter \p{scope} equals \alib{lox;Scope;Scope::Path}
208 /// to reference parent directories. Optional and defaults to \c 0.
209 /// @param threadID ID of the associated thread (for thread-related scopes only).
210 /// If \alib{threads::UNDEFINED} is given, the ID provided in
211 /// \p{scopeInfo} is used.
212 //==========================================================================================
214 void InitAccess ( Scope scope, int pathLevel, threads::ThreadID threadID );
215
216 //==========================================================================================
217 /// Stores a new value.
218 /// @param value The value to set.
219 /// @return Returns the previous value stored.
220 //==========================================================================================
221 T Store ( T value )
222 {
223 ALIB_ASSERT( value != nullptr )
224 return access( 0, value );
225 }
226
227 //==========================================================================================
228 /// Removes a value.
229 /// @param value The value to remove (must only be given for thread-related \e Scopes).
230 /// @return Returns the previous value stored.
231 //==========================================================================================
232 T Remove ( T value ) { return access( 1, value ); }
233
234 //==========================================================================================
235 /// Retrieves the value.
236 /// @return Returns the current value stored.
237 //==========================================================================================
238 T Get ()
239 {
240 return access( 2, nullptr );
241 }
242
243 //==========================================================================================
244 /// Initializes a scope 'walk' by storing the given scope information and
245 /// setting fields of our walk 'state-machine' to proper start values.
246 ///
247 /// @param startScope The \e Scope to start searching for.
248 /// @param localObject The 'local object' returned by a walk after Scope::ThreadInner
249 /// and before Scope::Method
250 //==========================================================================================
252 void InitWalk( Scope startScope, const T localObject );
253
254 //==========================================================================================
255 /// Searches next value in the actual scope. While not found, moves walk state to next outer
256 /// state and continues there.
257 /// @return The next object found in the current or any next outer scope.
258 //==========================================================================================
260
261
262
263
264 // #############################################################################################
265 // Internals
266 // #############################################################################################
267 protected:
268 //==========================================================================================
269 /// Retrieves and optionally creates an entry in the map that stores language-related
270 /// scope information. The result is stored in field #actStringTreeNode.
271 /// @param create If \c true, a non-existing entry is created.
272 //==========================================================================================
273 void initCursor( bool create );
274
275 //==========================================================================================
276 /// Receives, inserts or removes a value.
277 /// @param cmd 0= insert, 1= remove, 2= get.
278 /// @param value The new value or the one to be removed.
279 /// @return Returns the previous value stored.
280 //==========================================================================================
281 T access( int cmd, T value ) { return ScopeStoreHelper<T, TStackedThreadValues>()
282 .doAccess( *this, cmd, value ); }
283}; // ScopeStore
284
285
286#if !DOXYGEN
287
288extern template struct ScopeStoreHelper<NString , true>;
289extern template class ScopeStore <NString , true>;
290
291extern template struct ScopeStoreHelper<PrefixLogable* , true>;
292extern template class ScopeStore <PrefixLogable* , true>;
293
294
295extern template struct ScopeStoreHelper<SSMap<int>*, false>;
296extern template class ScopeStore <SSMap<int>*, false>;
297
298extern template struct ScopeStoreHelper<SSMap<Box>*, false>;
299extern template class ScopeStore <SSMap<Box>*, false>;
300
301#endif
302
303
304}}} // namespace [alib::lox::detail]
305
306
307
308#endif // HPP_ALIB_LOX_DETAIL_SCOPE
309
TLanguageStore::Cursor actStringTreeNode
The actual language related scope's map node.
ALIB_API void InitWalk(Scope startScope, const T localObject)
T access(int cmd, T value)
bool lazyLanguageNode
Flag used to lazily create the key to language-related scope values.
ALIB_API void InitAccess(Scope scope, int pathLevel, threads::ThreadID threadID)
TThreadMapValue * walkThreadValues
The list of values of Scope::ThreadOuter/Inner during a walk.
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.
TLanguageStore languageStore
StringTree to store data for language-related scopes (path,source,method).
int actPathLevel
The path level when using access methods.
bool walking
Indicates if currently a scope walk is active.
threads::ThreadID actThreadID
Actual thread ID.
Scope actScope
The actual scope of a walk.
ATMP_IF_T_F(TStackedThreadValues, StdVectorMono< T >, T) TThreadMapValue
std::pair< bool, threads::ThreadID > ThreadMapKey
Key type for the thread store.
ALIB_API ScopeStore(ScopeInfo &scopeInfo, MonoAllocator &monoAllocator)
ScopeInfo & scopeInfo
ScopeInfo of 'our' lox.
HashMap< MonoAllocator, ThreadMapKey, TThreadMapValue, BoolThreadIDHash > threadStore
T globalStore
The value of the global scope.
ALIB_API ~ScopeStore()
Destructor.
#define ATMP_IF_T_F( Cond, T, F)
Definition tmp.hpp:50
#define ALIB_API
Definition alib.hpp:639
#define ALIB_ASSERT(cond)
Definition alib.hpp:1270
@ Enabled
Caching is enabled.
integer ThreadID
The ALib thread identifier type.
Definition loxpimpl.inl:28
Definition alib.cpp:69
std::vector< T, SCAMono< T > > StdVectorMono
Type alias in namespace alib.
Definition stdvector.hpp:21
strings::TString< nchar > NString
Type alias in namespace alib.
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
monomem::TPoolAllocator< MonoAllocator, ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT > PoolAllocator
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