ALib C++ Library
Library Version: 2402 R1
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
11#ifndef HPP_ALIB_LANG_FORMAT_FORMATTER_STD
13#endif
14
15#if !defined (HPP_ALIB_MONOMEM_HASHMAP)
17#endif
18
19#if !defined (HPP_ALIB_MONOMEM_STDCONTAINERMA)
21#endif
22
23namespace alib { namespace expressions {
24
25class Compiler;
26class Expression;
27
28/** ************************************************************************************************
29 * This class acts as a simple virtual container to store custom resources in
30 * \alib{expressions;Scope} objects.
31 *
32 * To do so, custom derived types would simply add a custom member object together with a virtual
33 * destructor that assures that the member object(s) get rightfully deleted.
34 *
35 * Instances of this type allocated with operator \c new, may be stored in class \b %Scope in
36 * containers accessible with fields
37 * \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;a19232259f8695ae36c473d3689b574df;Resources;Scope::Resources} and
38 * \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;aaa60e006b065a3f22ed7f36981482ffa;CTScope;Scope::NamedResources}.
39 * All objects in these fields will be deleted with method \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;a372de693ad40b3f42839c8ec6ac845f4;Scope::Reset;expressions::Scope::Reset}.
40 **************************************************************************************************/
42{
43 /** The virtual destructor needed for virtual types. */
44 virtual ~ScopeResource() {}
45};
46
47/** ************************************************************************************************
48 * This type is used as the default class to provide access to program data when evaluating
49 * \alib expressions.
50 * Usually a derived type which contains references to necessary application data is passed to
51 * method \alib{expressions;Expression::Evaluate}.
52 * Then, custom callback functions may cast this object back to its original type and access such
53 * application data.
54 *
55 * Also, scope objects are used to store intermediate results and of-course the final one,
56 * in the case that such results are not of a simple type that can be boxed
57 * \alib{boxing;Box} "by value".
58 *
59 * For this, different simple allocator objects are provided. A custom, derived type may add own
60 * storage facilities, for example vectors of custom objects which are deleted when the vector is
61 * deleted.
62 *
63 * A scope object can be reused for evaluating the same expression several times. Prior to
64 * evaluation, the custom "scoped data" has to be set.
65 * With each reuse, method #Reset will be invoked internally.
66 * Hence, if custom 'allocators' are added in derived types, this method has
67 * to be overwritten to a) invoke the original method and b) clean such custom types.
68 *
69 * One singleton of this type, used to store compile-time data is created with virtual
70 * method \alib{expressions;Compiler::getCompileTimeScope}. If compile-time invokable custom
71 * callback methods use custom storage allocators, this method has to be overridden to return
72 * the proper custom version of this class. (Note, this is not needed for the evaluation-time
73 * instances, as this is created in the custom code unit anyhow and passed to method
74 * \alib{expressions;Expression::Evaluate}.
75 *
76 * \see
77 * For details on the use of scopes see manual chapter
78 * \ref alib_expressions_scopes "8 Scopes".
79 *
80 **************************************************************************************************/
81struct Scope
82{
83 /**
84 * Block-allocator used to store temporary data and results. The allocated data within
85 * this object becomes cleared automatically by method #Reset, in the moment
86 * an expression is evaluated a next time (usually with different custom scope data).
87 *
88 * Note that this allocator is \b not cleared for the compile-time scope object #CTScope.
89 */
91
92 /**
93 * This is a pointer to the compile-time scope. This is available only at evaluation time
94 * and primarily is used to access field #NamedResources. This allows to create resources
95 * at compile-time, which can be used for evaluation.<br>
96 *
97 * A sample use case is implemented with built-in compiler plug-in
98 * \alib{expressions::plugins;Strings}. When wildcard or regex matching is performed on
99 * constant pattern strings, the matching class (which itself "compiles" the pattern once) is
100 * created once and reused during evaluation.
101 */
103
104 /**
105 * This is the argument stack used by class \alib{expressions;detail::VirtualMachine} when
106 * evaluating expressions.
107 */
108 std::vector<alib::Box> Stack;
109
110 /** Stack of nested expressions called during evaluation. Used to detect cyclic expressions. */
111 std::vector<Expression*, StdContMA<Expression*>> NestedExpressions;
112
113 /**
114 * Simple list of user-defined, virtually deletable objects.
115 * The objects in this vector will be deleted with #Reset.
116 */
117 std::vector<ScopeResource*,StdContMA<ScopeResource*>> Resources;
118
119 /**
120 * A list of user-defined, named resources. Named resources may be allocated at compile-time
121 * and used at evaluation-time.
122 */
124
125 /**
126 * Used to convert numbers to strings and vice versa. In addition expression function
127 * \b %Format of built-in compiler plugin \alib{expressions::plugins;Strings} uses this object
128 * to perform the formatting of arbitrary objects according to a given format string.
129 *
130 * Hence, to support customized format strings, a different formatter is to be passed here.
131 * Default format string conventions provided with \alib are
132 * \alib{lang::format;FormatterPythonStyle;python style} and
133 * \alib{lang::format;FormatterJavaStyle;java/printf-like style}.
134 *
135 * The default implementation of method \alib{expressions;Compiler::getCompileTimeScope}
136 * provides field \alib{expressions;Compiler::CfgFormatter} with the constructor of the
137 * default compile-time scope.
138 */
140
141
142 /** ********************************************************************************************
143 * Constructor.<br>
144 * Usually, for parameter \p formatter field \alib{expressions;Compiler::CfgFormatter} should
145 * be provided.
146 *
147 * @param formatter A reference to a \c std::shared_ptr holding a formatter.
148 * Usually \alib{expressions;Compiler::CfgFormatter}.
149 **********************************************************************************************/
150 ALIB_API Scope( SPFormatter& formatter );
151
152 /** ********************************************************************************************
153 * Virtual destructor
154 **********************************************************************************************/
155 virtual ~Scope()
156 {
157 Reset();
158 }
159
160 /** ********************************************************************************************
161 * This function may be called (respectively provides reliable results) only from within
162 * callback functions that this scope is passed to.
163 *
164 * \note
165 * Usually, this method is not needed to be checked. It is useful and provided to support
166 * more complicated management of resources, i.e. allocation of resources at compile-time
167 * which are later used for evaluation.
168 *
169 * @return \c true if this is a compile-time invocation, \c false otherwise.
170 **********************************************************************************************/
172 {
173 return CTScope == nullptr;
174 }
175
176 /** ********************************************************************************************
177 * Scope objects usually are reused, either for evaluating the same expression using
178 * different scoped data (attached to derived versions of this class), or for evaluating
179 * different expression.<br>
180 * Such reuse is internally detected and if so, this method is invoked.
181 *
182 * Instances of this class used as compilation scope, are not reset during the life-cycle
183 * of an expression.
184 *
185 * Derived versions of this class need to free allocations performed by callback functions.
186 **********************************************************************************************/
188 virtual void Reset();
189
190};
191
192} // namespace alib[::expressions]
193
194/// Type alias in namespace \b alib. Renamed to not collide with #alib::lox::Scope.
196
197
198} // namespace [alib]
199
200
201#endif // HPP_ALIB_EXPRESSIONS_SCOPE
#define ALIB_API
Definition alib.hpp:538
Definition alib.cpp:57
lox::Scope Scope
Type alias in namespace alib.
std::shared_ptr< lang::format::Formatter > SPFormatter
expressions::Compiler Compiler
Type alias in namespace alib.
Definition compiler.hpp:596
std::vector< ScopeResource *, StdContMA< ScopeResource * > > Resources
Definition scope.hpp:117
virtual ALIB_API void Reset()
Definition compiler.cpp:67
MonoAllocator Allocator
Definition scope.hpp:90
HashMap< NString, ScopeResource * > NamedResources
Definition scope.hpp:123
SPFormatter Formatter
Definition scope.hpp:139
std::vector< alib::Box > Stack
Definition scope.hpp:108
std::vector< Expression *, StdContMA< Expression * > > NestedExpressions
Definition scope.hpp:111