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