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