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