ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
scope.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_expressions of the \aliblong.
4///
5/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace expressions {
9
10class Compiler;
11class ExpressionVal;
12namespace detail { struct VirtualMachineBase; }
13
14//==================================================================================================
15/// This class acts as a simple virtual container to store custom resources in
16/// #"expressions::Scope" objects.
17///
18/// To do so, custom derived types would simply add a custom member object together with a virtual
19/// destructor that ensures that the member object(s) get rightfully deleted.
20///
21/// Instances of this type have to be created using the scope's allocator and are to be stored in
22/// container #"Scope::NamedResources".
23//==================================================================================================
25 /// The virtual destructor needed for virtual types.
26 virtual ~ScopeResource() {}
27};
28
29//==================================================================================================
30/// This type is used as the default class to provide access to program data when evaluating
31/// \alib expressions.
32/// Usually a derived type which contains references to necessary application data is passed to
33/// the method #"ExpressionVal::Evaluate;*".
34/// Then, custom callback functions may cast instances of this type that they receive back to
35/// the derived type and access such application-specific data.
36///
37/// Also, scope objects are used to store intermediate results as well as the final one,
38/// in the case that such results are not of a simple type that can be #"Box;boxed by value".
39///
40/// For this, two different allocator objects are provided. One for compile-time results and
41/// one for those needed at evaluation-time.
42///
43/// A scope object can be reused for evaluating the same expression several times.
44/// Before the evaluation, the custom "scoped data" has to be set.
45/// With each reuse, the method #"Scope::Reset" will be invoked internally.
46/// Hence, if custom storage members are added in derived types, this method has
47/// to be overwritten to a) invoke the original method and b) clean such custom types.
48///
49/// One singleton of this type, which is used to store compile-time data, is created with the
50/// virtual method #"Compiler::createCompileTimeScope;*".
51/// If compile-time invokable custom callback methods use custom storage allocators, this method
52/// has to be overridden to return the proper custom version of this class.
53/// (Note, this is not needed for the evaluation-time instances, as this is created in the custom
54/// code unit anyhow and passed to method #"ExpressionVal::Evaluate;*").
55///
56/// \see
57/// - For details on the use of scopes see the manual chapter #"alib_expressions_scopes".
58/// - This is an almost completely public struct.
59/// The design rationale behind this is explained in the manual chapter
60/// #"alib_expressions_prereq_bauhaus"
61//==================================================================================================
62struct Scope {
63 /// Members used by the virtual machine. This is constructed only with evaluation-time scopes.
64 struct VMMembers {
65 /// Constructor.
66 /// @param allocator The allocator of the evaluation scope.b
68 : CTScope(nullptr)
69 , NestedExpressions(allocator) {}
70
71 /// This is a pointer to the compile-time scope, primarily is used to access field
72 /// #"Scope::NamedResources".
73 /// which is only created with compile time scopes.
74 /// This concept allows creating resources at compile-time which can be used for evaluation.
75 ///
76 /// A sample use case is implemented with the built-in compiler plug-in
77 /// #"plugins::Strings". When wildcard or regex matching is performed on
78 /// constant pattern strings, the matching class (which itself "compiles" the pattern once)
79 /// is created once and reused during evaluation.
81
82 /// Stack of nested expressions called during evaluation. Used to detect cyclic expressions.
84 };
85
86 /// Evaluation-scope allocator. With compile-time scopes, this is allocator will not be
87 /// initialized.
89
90 /// Monotonic allocator used to store temporary data and results.
91 /// The allocated data within this object becomes cleared automatically by method
92 /// #"Scope::Reset", at the moment an expression is evaluated the next time (usually with
93 /// different custom scope data).
94 ///
95 /// Note that this allocator is \b not cleared for the compile-time scope instance.
97
98 /// This is the argument stack used by class #"detail::VirtualMachine;*" when
99 /// evaluating expressions.<br>
100 /// With compilation, it used to "simulate" evaluation calls at compile-time.
102
103 /// Used to convert numbers to strings and vice versa. In addition, the expression function
104 /// \b Format of the built-in compiler plugin #"plugins::Strings" uses this object
105 /// to perform the formatting of arbitrary objects according to a given format string.
106 ///
107 /// Hence, to support customized format strings, a different formatter is to be passed here.
108 /// Default format string conventions provided with \alib are
109 /// #"FormatterPythonStyle;python style" and
110 /// #"FormatterJavaStyle;java/printf-like style".
111 ///
112 /// The default implementation of method #"Compiler::createCompileTimeScope;*"
113 /// provides the field #"Compiler::CfgFormatter;*" with the constructor of the
114 /// default compile-time scope.
116
117 /// A list of user-defined, named resources.
118 /// Named resources may be allocated at compile-time and used at evaluation-time.<br>
119 /// This pointer is only set with compile-time scopes.
122
123 /// The members used for the virtual machine. Available only with evaluation-time instances.
125
126 #if ALIB_DEBUG_CRITICAL_SECTIONS
127 /// Debug-tool to detect usage of evaluation scope from within multiple threads
128 /// (which is not allowed). It is set by the virtual machine when running programs.
130 #endif
131
132 /// Constructor used with evaluation scopes. Creates a mono allocator.<br>
133 /// Usually, for parameter \p{formatter} field #"Compiler::CfgFormatter;*" should
134 /// be provided.
135 ///
136 /// @param formatter A reference to a \c std::shared_ptr holding a formatter.
137 /// Usually #"Compiler::CfgFormatter;*".
138 ALIB_DLL Scope( SPFormatter& formatter );
139
140 /// Constructor used with compile-time scopes. Receives the allocator from the expression
141 /// instance.<br>
142 /// Usually, for parameter \p{formatter} field #"Compiler::CfgFormatter;*" should
143 /// be provided.
144 ///
145 /// @param allocator The allocator of the expression.
146 /// @param formatter A reference to a \c std::shared_ptr holding a formatter.
147 /// Usually #"Compiler::CfgFormatter;*".
148 ALIB_DLL Scope( MonoAllocator& allocator, SPFormatter& formatter );
149
150 /// Virtual destructor.
151 ALIB_DLL virtual ~Scope();
152
153 /// Deleted copy constructor.
154 Scope(const Scope&) =delete;
155
156 /// Deleted copy assignment.
157 void operator=(const Scope&) =delete;
158
159 /// Usually, this method is unnecessary to be checked.
160 /// It is useful and provided to support more complicated management of resources, i.e.
161 /// allocation of resources at compile-time which are later used for evaluation.
162 ///
163 /// @return \c true if this is a compile-time invocation, \c false otherwise.
164 bool IsCompileTime() { return EvalScopeVMMembers == nullptr; }
165
166 /// Scope objects usually are reused, either for evaluating the same expression using
167 /// different scoped data (attached to derived versions of this class), or for evaluating
168 /// different expression.<br>
169 /// Such a reuse is internally detected, and if so, this method is invoked.
170 ///
171 /// Instances of this class used as compilation scope, are not reset during the life-cycle
172 /// of an expression.
173 ///
174 /// Derived versions of this class need to free allocations performed by callback functions.
176 virtual void Reset();
177
178 protected:
179 /// This method is called in the destructor, as well as in method #"Scope::Reset".
181 virtual void freeResources();
182}; // Scope
183
184} // namespace alib[::expressions]
185
186/// Type alias in namespace #"%alib". Renamed to not collide with #"alib::lox::Scope;3".
188
189} // namespace [alib]
#define ALIB_DLL
#define ALIB_EXPORT
Definition alox.cpp:14
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
strings::TString< nchar > NString
Type alias in namespace #"%alib".
Definition string.hpp:2174
containers::SharedPtr< format::Formatter > SPFormatter
Definition formatter.hpp:41
expressions::Scope ExpressionScope
Type alias in namespace #"%alib". Renamed to not collide with #"alib::lox::Scope;3".
Definition scope.hpp:187
containers::HashMap< TAllocator, TKey, TMapped, THash, TEqual, THashCaching, TRecycling > HashMap
Type alias in namespace #"%alib".
std::vector< T, StdMA< T > > StdVectorMA
Type alias in namespace #"%alib".
lox::Scope Scope
Type alias in namespace #"%alib".
virtual ~ScopeResource()
The virtual destructor needed for virtual types.
Definition scope.hpp:26
Members used by the virtual machine. This is constructed only with evaluation-time scopes.
Definition scope.hpp:64
StdVectorMA< ExpressionVal * > NestedExpressions
Stack of nested expressions called during evaluation. Used to detect cyclic expressions.
Definition scope.hpp:83
VMMembers(MonoAllocator &allocator)
Definition scope.hpp:67
VMMembers * EvalScopeVMMembers
The members used for the virtual machine. Available only with evaluation-time instances.
Definition scope.hpp:124
virtual ~Scope()
Virtual destructor.
Definition compiler.cpp:34
void operator=(const Scope &)=delete
Deleted copy assignment.
StdVectorMA< Box > * Stack
Definition scope.hpp:101
virtual void freeResources()
This method is called in the destructor, as well as in method #"Scope::Reset".
Definition compiler.cpp:44
Scope(SPFormatter &formatter)
Definition compiler.cpp:8
MonoAllocator & Allocator
Definition scope.hpp:96
HashMap< MonoAllocator, NString, ScopeResource * > * NamedResources
Definition scope.hpp:121
SPFormatter Formatter
Definition scope.hpp:115
lang::DbgCriticalSections DCS
Definition scope.hpp:129
Scope(const Scope &)=delete
Deleted copy constructor.
virtual void Reset()
Definition compiler.cpp:52
MonoAllocator * EvalScopeAllocator
Definition scope.hpp:88
Base class exported by the main module #"F;ALib.Expressions.H" for technical reasons.
Definition compiler.hpp:520