OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
AbstractObjects/Expressions.h
Go to the documentation of this file.
1//
2// Namespace Expressions
3// This namespace contains the representations for all OPAL expressions
4// (scalar and array). It also defines the entire user interface for
5// the expression parser. All classes derived from these classes are
6// declared in this same namespace in the module "Expressions".
7//
8// The expression parser is a recursive-descent parser. When a parse
9// routine recognizes an expression, it returns its internal represenation
10// and skips the corresponding input tokens. In case of error, a
11// ParseError exception is thrown.
12//
13// Template class: Expression::Scalar<T>
14// This abstract base class defines the interface for OPAL scalar
15// expressions whose values have type T.
16//
17// Template class: Expression::Array<T>
18// This abstract base class defines the interface for OPAL array
19// expressions whose components have type T.
20//
21// Template class: Expression::PtrToScalar<T>
22// This class implements a pointer which owns a scalar expression
23// of type Expression::Scalar<T>.
24//
25// Template class: Expression::PtrToArray<T>
26// This class implements a pointer which owns an array expression
27// of type Expression::Array<T>.
28//
29// Template class: Expression::ArrayOfPtrs<T>
30// This class implements an array of pointers, each of which owns
31// an expression of type Expression::Scalar<T>.
32//
33// Declared functions:
34// A collection of functions implementing a recursive descent parser.
35//
36//
37// Copyright (c) 2008-2020
38// Paul Scherrer Institut, Villigen PSI, Switzerland
39// All rights reserved.
40//
41// OPAL is licensed under GNU GPL version 3.
42//
43
44#ifndef OPAL_Expressions_HH
45#define OPAL_Expressions_HH 1
46
47#include <iosfwd>
48#include <list>
49#include <memory>
50#include <string>
51#include <vector>
52#include "OpalParser/Token.h"
53
54class PlaceRep;
55class RangeRep;
56class SFunction;
57class Statement;
58class Table;
59class TableRowRep;
60
61// ========================================================================
63
64namespace Expressions {
65
66 // Template class Expression::Scalar<T>.
67 // ----------------------------------------------------------------------
69
70 template <class T>
71 class Scalar {
72 public:
74 Scalar(const Scalar&);
75 virtual ~Scalar();
76
78 // Deep copy of the expression.
79 virtual Scalar<T>* clone() const = 0;
80
82 // Recursively evaluate the expression and return the result.
83 // Any non-deferred values are cached.
84 virtual T evaluate() const = 0;
85
87 // Default action assumes non-constant.
88 virtual bool isConstant() const;
89
91 // Print the expression on the specified stream.
92 // The [b]precedence[/b] parameter controls printing of parentheses.
93 virtual void print(std::ostream&, int precedence = 99) const = 0;
94
95 private:
96 // Not implemented.
97 void operator=(const Scalar&);
98 };
99
100 // Template class Expression::PtrToScalar<T>.
101 // ----------------------------------------------------------------------
103
104 template <class T>
106 public:
109
112 PtrToScalar(PtrToScalar&& rhs) noexcept = default;
115 PtrToScalar& operator=(PtrToScalar&&) noexcept = default;
116 PtrToScalar& operator=(Scalar<T>* rhs);
117
118 Scalar<T>* operator->() const { return ptr_m.get(); }
119 Scalar<T>& operator*() const { return *ptr_m; }
120 explicit operator bool() const { return ptr_m != nullptr; }
121 bool isValid() const { return ptr_m != nullptr; }
122 Scalar<T>* release() { return ptr_m.release(); }
123 Scalar<T>* get() const { return ptr_m.get(); }
124
125 private:
126 // Mutable to preserve transfer-on-copy semantics from OwnPtr.
127 mutable std::unique_ptr<Scalar<T>> ptr_m;
128 };
129
130 // Template class Expression::ArrayOfPtrs<T>.
131 // ------------------------------------------------------------------------
133
134 template <class T>
135 class ArrayOfPtrs : public std::vector<PtrToScalar<T>> {
136 public:
137 ArrayOfPtrs();
138 ArrayOfPtrs(int);
139 ArrayOfPtrs(const std::vector<PtrToScalar<T>>& rhs);
140 ~ArrayOfPtrs();
141 };
142
143 // Template class Expression::Array<T>.
144 // ----------------------------------------------------------------------
146
147 template <class T>
148 class OArray {
149 public:
151 OArray(const OArray&);
152 virtual ~OArray();
153
155 // Deep copy.
156 virtual OArray<T>* clone() const = 0;
157
159 // Recursively evaluate the expression and return the result.
160 // Any non-deferred expression is cached.
161 virtual std::vector<T> evaluate() const = 0;
162
164 // Default action assumes non-constant.
165 virtual bool isConstant() const;
166
168 // Print the expression on the specified stream.
169 // The [b]precedence[/b] parameter controls printing of parentheses.
170 virtual void print(std::ostream&, int precedence = 99) const = 0;
171
172 private:
173 // Not implemented.
174 void operator=(const OArray&);
175 };
176
177 // Template class Expression::PtrToArray<T>.
178 // ----------------------------------------------------------------------
180
181 template <class T>
183 public:
186
189 PtrToArray(PtrToArray&& rhs) noexcept = default;
192 PtrToArray& operator=(PtrToArray&&) noexcept = default;
193 PtrToArray& operator=(OArray<T>* rhs);
194
195 OArray<T>* operator->() const { return ptr_m.get(); }
196 OArray<T>& operator*() const { return *ptr_m; }
197 explicit operator bool() const { return ptr_m != nullptr; }
198 bool isValid() const { return ptr_m != nullptr; }
199 OArray<T>* release() { return ptr_m.release(); }
200 OArray<T>* get() const { return ptr_m.get(); }
201
202 private:
203 // Mutable to preserve transfer-on-copy semantics from OwnPtr.
204 mutable std::unique_ptr<OArray<T>> ptr_m;
205 };
206
207 // ----------------------------------------------------------------------
208 // SCALAR EXPRESSION PARSERS.
209
212
215
217 extern double parseRealConst(Statement&);
218
220 // When no string is seen, a ParseError is thrown with the message
221 // given as the second argument.
222 extern std::string parseString(Statement&, const char msg[]);
223 extern std::string parseStringValue(Statement&, const char msg[]);
224
225 // ARRAY EXPRESSION PARSERS.
226
229
232
235
237 extern std::vector<std::string> parseStringArray(Statement&);
238
239 // SPECIAL VALUE PARSERS.
240
242 extern void parseDelimiter(Statement& stat, char delim);
243
245 extern void parseDelimiter(Statement& stat, const char delim[2]);
246
249
252
253 // Forward declaration.
254 template <class T>
255 class SRefAttr;
256
259
262
264 extern std::list<Token> parseTokenList(Statement&);
265
267 extern std::vector<std::list<Token>> parseTokenListArray(Statement&);
268
269 // SPECIAL PARSER.
270
273
274 // Implementation of class Expression::Scalar<T>.
275 // ----------------------------------------------------------------------
276
277 template <class T>
279
280 template <class T>
281 inline Scalar<T>::Scalar(const Scalar<T>&) {}
282
283 template <class T>
285
286 template <class T>
287 inline bool Scalar<T>::isConstant() const {
288 return false;
289 }
290
291 // Implementation of class Expression::OArray<T>.
292 // ----------------------------------------------------------------------
293
294 template <class T>
296
297 template <class T>
298 inline OArray<T>::OArray(const OArray<T>&) {}
299
300 template <class T>
302
303 template <class T>
304 inline bool OArray<T>::isConstant() const {
305 return false;
306 }
307
308 // Implementation of class Expression::PtrToScalar<T>.
309 // ----------------------------------------------------------------------
310
311 template <class T>
312 inline PtrToScalar<T>::PtrToScalar() : ptr_m() {}
313
314 template <class T>
315 inline PtrToScalar<T>::PtrToScalar(const PtrToScalar& rhs) : ptr_m(std::move(rhs.ptr_m)) {}
316
317 template <class T>
318 inline PtrToScalar<T>::PtrToScalar(Scalar<T>* rhs) : ptr_m(rhs) {}
319
320 template <class T>
322
323 template <class T>
325 if (this != &rhs) {
326 ptr_m = std::move(rhs.ptr_m);
327 }
328 return *this;
329 }
330
331 template <class T>
333 ptr_m.reset(rhs);
334 return *this;
335 }
336
337 // Implementation of class Expression::ArrayOfPtrs<T>.
338 // ----------------------------------------------------------------------
339
340 template <class T>
342
343 template <class T>
344 inline ArrayOfPtrs<T>::ArrayOfPtrs(int n) : std::vector<PtrToScalar<T>>(n, PtrToScalar<T>()) {}
345
346 template <class T>
347 inline ArrayOfPtrs<T>::ArrayOfPtrs(const std::vector<PtrToScalar<T>>& rhs)
348 : std::vector<PtrToScalar<T>>(rhs) {}
349
350 template <class T>
352
353 // Implementation of class Expression::PtrToArray<T>.
354 // ----------------------------------------------------------------------
355
356 template <class T>
357 inline PtrToArray<T>::PtrToArray() : ptr_m() {}
358
359 template <class T>
360 inline PtrToArray<T>::PtrToArray(const PtrToArray& rhs) : ptr_m(std::move(rhs.ptr_m)) {}
361
362 template <class T>
363 inline PtrToArray<T>::PtrToArray(OArray<T>* rhs) : ptr_m(rhs) {}
364
365 template <class T>
367
368 template <class T>
370 if (this != &rhs) {
371 ptr_m = std::move(rhs.ptr_m);
372 }
373 return *this;
374 }
375
376 template <class T>
378 ptr_m.reset(rhs);
379 return *this;
380 }
381
382 // End of namespace Expressions.
383 // ======================================================================
384}; // namespace Expressions
385
386#endif // OPAL_Expressions_HH
double T
Definition OPALTypes.h:8
An array of pointers to scalar expressions.
virtual bool isConstant() const
Test for constant.
virtual OArray< T > * clone() const =0
Copy expression.
virtual std::vector< T > evaluate() const =0
Evaluate.
virtual void print(std::ostream &, int precedence=99) const =0
Print expression.
OArray(const OArray &)
void operator=(const OArray &)
A pointer to an array expression.
PtrToArray(OArray< T > *rhs)
Constructor from object just created.
PtrToArray & operator=(const PtrToArray< T > &)
PtrToArray(PtrToArray &&rhs) noexcept=default
std::unique_ptr< OArray< T > > ptr_m
PtrToArray & operator=(PtrToArray &&) noexcept=default
A pointer to a scalar expression.
PtrToScalar & operator=(PtrToScalar &&) noexcept=default
std::unique_ptr< Scalar< T > > ptr_m
PtrToScalar(PtrToScalar &&rhs) noexcept=default
PtrToScalar(Scalar< T > *rhs)
Constructor from an object just created.
PtrToScalar & operator=(const PtrToScalar &)
An attribute defined as a reference to a scalar.
Definition SRefAttr.h:47
void operator=(const Scalar &)
virtual T evaluate() const =0
Evaluate.
Scalar(const Scalar &)
virtual Scalar< T > * clone() const =0
Copy scalar expression.
virtual bool isConstant() const
Test for constant.
virtual void print(std::ostream &, int precedence=99) const =0
Print expression.
Representation of a place within a beam line or sequence.
Definition PlaceRep.h:40
Representation of a range within a beam line or sequence.
Definition RangeRep.h:33
Functions of arc length.
Definition SFunction.h:30
Interface for statements.
Definition Statement.h:37
Representation of a table row reference.
Definition TableRowRep.h:35
The base class for all OPAL tables.
Definition Table.h:41
Representation objects and parsers for attribute expressions.
std::string parseStringValue(Statement &, const char msg[])
PtrToScalar< bool > parseBool(Statement &)
Parse boolean expression.
PtrToScalar< double > parseReal(Statement &)
Parse real expression.
PtrToArray< double > parseRealConstArray(Statement &)
Parse real array constant.
std::vector< std::string > parseStringArray(Statement &)
Parse string array.
RangeRep parseRange(Statement &)
Parse range specification.
PlaceRep parsePlace(Statement &)
Parse place specification.
SRefAttr< double > * parseReference(Statement &)
Parse variable reference.
std::vector< std::list< Token > > parseTokenListArray(Statement &)
Parse a token list array (for LIST commands).
PtrToArray< double > parseRealArray(Statement &)
Parse real array expression.
PtrToScalar< double > parseTableExpression(Statement &, const Table *)
Parse table expression (depends on a table's rows).
std::list< Token > parseTokenList(Statement &)
Parse a token list (for macro argument and the like).
TableRowRep parseTableRow(Statement &)
Parse a token list (for macro argument and the like).
std::string parseString(Statement &, const char msg[])
Parse string value.
void parseDelimiter(Statement &stat, char delim)
Test for one-character delimiter.
double parseRealConst(Statement &)
Parse real constant.
PtrToArray< bool > parseBoolArray(Statement &)
Parse boolean array expression.
STL namespace.