OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Expressions.h
Go to the documentation of this file.
1 #ifndef OPAL_Expressions_HH
2 #define OPAL_Expressions_HH 1
3 
4 // ------------------------------------------------------------------------
5 // $RCSfile: Expressions.h,v $
6 // ------------------------------------------------------------------------
7 // $Revision: 1.1.1.1 $
8 // ------------------------------------------------------------------------
9 // Copyright: see Copyright.readme
10 // ------------------------------------------------------------------------
11 //
12 // Namespace Expressions
13 // This namespace contains the representations for all OPAL expressions
14 // (scalar and array). It also defines the entire user interface for
15 // the expression parser. All classes derived from these classes are
16 // declared in this same namespace in the module "Expressions".
17 //
18 // The expression parser is a recursive-descent parser. When a parse
19 // routine recognizes an expression, it returns its internal represenation
20 // and skips the corresponding input tokens. In case of error, a
21 // ParseError exception is thrown.
22 //
23 // Template class: Expression::Scalar<T>
24 // This abstract base class defines the interface for OPAL scalar
25 // expressions whose values have type T.
26 //
27 // Template class: Expression::Array<T>
28 // This abstract base class defines the interface for OPAL array
29 // expressions whose components have type T.
30 //
31 // Template class: Expression::PtrToScalar<T>
32 // This class implements a pointer which owns a scalar expression
33 // of type Expression::Scalar<T>.
34 //
35 // Template class: Expression::PtrToArray<T>
36 // This class implements a pointer which owns an array expression
37 // of type Expression::Array<T>.
38 //
39 // Template class: Expression::ArrayOfPtrs<T>
40 // This class implements an array of pointers, each of which owns
41 // an expression of type Expression::Scalar<T>.
42 //
43 // Declared functions:
44 // A collection of functions implementing a recursive descent parser.
45 //
46 // ------------------------------------------------------------------------
47 //
48 // $Date: 2000/03/27 09:33:34 $
49 // $Author: Andreas Adelmann $
50 //
51 // ------------------------------------------------------------------------
52 
54 #include "Parser/Token.h"
55 #include <list>
56 #include <iosfwd>
57 #include <string>
58 #include <vector>
59 
60 class PlaceRep;
61 class RangeRep;
62 class SFunction;
63 class Statement;
64 class Table;
65 class TableRowRep;
66 
67 
68 // Begin namespace Expression.
69 // ========================================================================
71 
72 namespace Expressions {
73 
74 
75  // Template class Expression::Scalar<T>.
76  // ----------------------------------------------------------------------
78 
79  template <class T> class Scalar {
80 
81  public:
82 
83  Scalar();
84  Scalar(const Scalar &);
85  virtual ~Scalar();
86 
88  // Deep copy of the expression.
89  virtual Scalar<T> *clone() const = 0;
90 
92  // Recursively evaluate the expression and return the result.
93  // Any non-deferred values are cached.
94  virtual T evaluate() const = 0;
95 
97  // Default action assumes non-constant.
98  virtual bool isConstant() const;
99 
101  // Print the expression on the specified stream.
102  // The [b]precedence[/b] parameter controls printing of parentheses.
103  virtual void print(std::ostream &, int precedence = 99) const = 0;
104 
105  private:
106 
107  // Not implemented.
108  void operator=(const Scalar &);
109  };
110 
111 
112  // Template class Expression::PtrToScalar<T>.
113  // ----------------------------------------------------------------------
115 
116  template <class T> class PtrToScalar:
117  public OwnPtr<Scalar<T> > {
118 
119  public:
120 
122  PtrToScalar(Scalar<T> *rhs);
123 
124  PtrToScalar();
125  PtrToScalar(const PtrToScalar &rhs);
126  ~PtrToScalar();
127  };
128 
129 
130  // Template class Expression::ArrayOfPtrs<T>.
131  // ------------------------------------------------------------------------
133 
134  template <class T> class ArrayOfPtrs: public std::vector<PtrToScalar<T> > {
135 
136  public:
137 
138  ArrayOfPtrs();
139  ArrayOfPtrs(int);
140  ArrayOfPtrs(const std::vector<PtrToScalar<T> > &rhs);
141  ~ArrayOfPtrs();
142  };
143 
144 
145  // Template class Expression::Array<T>.
146  // ----------------------------------------------------------------------
148 
149  template <class T> class OArray {
150 
151  public:
152 
153  OArray();
154  OArray(const OArray &);
155  virtual ~OArray();
156 
158  // Deep copy.
159  virtual OArray<T> *clone() const = 0;
160 
162  // Recursively evaluate the expression and return the result.
163  // Any non-deferred expression is cached.
164  virtual std::vector<T> evaluate() const = 0;
165 
167  // Default action assumes non-constant.
168  virtual bool isConstant() const;
169 
171  // Print the expression on the specified stream.
172  // The [b]precedence[/b] parameter controls printing of parentheses.
173  virtual void print(std::ostream &, int precedence = 99) const = 0;
174 
175  private:
176 
177  // Not implemented.
178  void operator=(const OArray &);
179  };
180 
181 
182  // Template class Expression::PtrToArray<T>.
183  // ----------------------------------------------------------------------
185 
186  template <class T> class PtrToArray:
187  public OwnPtr<OArray<T> > {
188 
189  public:
190 
192  PtrToArray(OArray<T> *rhs);
193 
194  PtrToArray();
195  PtrToArray(const PtrToArray &rhs);
196  ~PtrToArray();
197  };
198 
199 
200  // ----------------------------------------------------------------------
201  // SCALAR EXPRESSION PARSERS.
202 
205 
208 
210  extern double parseRealConst(Statement &);
211 
213  // When no string is seen, a ParseError is thrown with the message
214  // given as the second argument.
215  extern std::string parseString(Statement &, const char msg[]);
216 
217 
218  // ARRAY EXPRESSION PARSERS.
219 
222 
225 
228 
230  extern std::vector<std::string> parseStringArray(Statement &);
231 
232 
233  // SPECIAL VALUE PARSERS.
234 
236  extern void parseDelimiter(Statement &stat, char delim);
237 
239  extern void parseDelimiter(Statement &stat, const char delim[2]);
240 
242  extern PlaceRep parsePlace(Statement &);
243 
245  extern RangeRep parseRange(Statement &);
246 
247  // Forward declaration.
248  template <class T> class SRefAttr;
249 
252 
255 
257  extern std::list<Token> parseTokenList(Statement &);
258 
260  extern std::vector<std::list<Token> > parseTokenListArray(Statement &);
261 
262 
263  // SPECIAL PARSER.
264 
267 
268 
269  // Implementation of class Expression::Scalar<T>.
270  // ----------------------------------------------------------------------
271 
272  template <class T> inline
274  {}
275 
276 
277  template <class T> inline
279  {}
280 
281 
282  template <class T> inline
284  {}
285 
286 
287  template <class T> inline
288  bool Scalar<T>::isConstant() const {
289  return false;
290  }
291 
292 
293  // Implementation of class Expression::OArray<T>.
294  // ----------------------------------------------------------------------
295 
296  template <class T> inline
298  {}
299 
300 
301  template <class T> inline
303  {}
304 
305 
306  template <class T> inline
308  {}
309 
310 
311  template <class T> inline
312  bool OArray<T>::isConstant() const {
313  return false;
314  }
315 
316 
317  // Implementation of class Expression::PtrToScalar<T>.
318  // ----------------------------------------------------------------------
319 
320 
321  template <class T> inline
323  OwnPtr<Scalar<T> >()
324  {}
325 
326 
327  template <class T> inline
329  OwnPtr<Scalar<T> >(rhs)
330  {}
331 
332 
333  template <class T> inline
335  OwnPtr<Scalar<T> >(rhs)
336  {}
337 
338 
339  template <class T> inline
341  {}
342 
343 
344  // Implementation of class Expression::ArrayOfPtrs<T>.
345  // ----------------------------------------------------------------------
346 
347  template <class T> inline
349  std::vector<PtrToScalar<T> >()
350  {}
351 
352 
353  template <class T> inline
355  std::vector<PtrToScalar<T> >(n, PtrToScalar<T>())
356  {}
357 
358 
359  template <class T> inline
360  ArrayOfPtrs<T>::ArrayOfPtrs(const std::vector<PtrToScalar<T> > &rhs):
361  std::vector<PtrToScalar<T> >(rhs)
362  {}
363 
364 
365  template <class T> inline
367  {}
368 
369 
370  // Implementation of class Expression::PtrToArray<T>.
371  // ----------------------------------------------------------------------
372 
373 
374  template <class T> inline
376  OwnPtr<OArray<T> >()
377  {}
378 
379 
380  template <class T> inline
382  OwnPtr<OArray<T> >(rhs)
383  {}
384 
385 
386  template <class T> inline
388  OwnPtr<OArray<T> >(rhs)
389  {}
390 
391 
392  template <class T> inline
394  {}
395 
396 
397  // End of namespace Expressions.
398  // ======================================================================
399 };
400 
401 #endif // OPAL_Expressions_HH
A scalar expression.
Definition: Expressions.h:79
virtual bool isConstant() const
Test for constant.
Definition: Expressions.h:312
double parseRealConst(Statement &)
Parse real constant.
virtual void print(std::ostream &, int precedence=99) const =0
Print expression.
PlaceRep parsePlace(Statement &)
Parse place specification.
void operator=(const Scalar &)
Functions of arc length.
Definition: SFunction.h:31
virtual bool isConstant() const
Test for constant.
Definition: Expressions.h:288
Definition: rbendmap.h:8
PtrToScalar< double > parseReal(Statement &)
Parse real expression.
A pointer to a scalar expression.
Definition: Expressions.h:116
RangeRep parseRange(Statement &)
Parse range specification.
A pointer which owns the object pointed at.
Definition: OwnPtr.h:31
Representation of a table row reference.
Definition: TableRowRep.h:36
SRefAttr< double > * parseReference(Statement &)
Parse variable reference.
An attribute defined as a reference to a scalar.
Definition: Expressions.h:248
virtual OArray< T > * clone() const =0
Copy expression.
void parseDelimiter(Statement &stat, char delim)
Test for one-character delimiter.
Representation of a place within a beam line or sequence.
Definition: PlaceRep.h:41
PtrToScalar< bool > parseBool(Statement &)
Parse boolean expression.
virtual void print(std::ostream &, int precedence=99) const =0
Print expression.
Interface for statements.
Definition: Statement.h:38
virtual Scalar< T > * clone() const =0
Copy scalar expression.
TableRowRep parseTableRow(Statement &)
Parse a token list (for macro argument and the like).
Representation of a range within a beam line or sequence.
Definition: RangeRep.h:34
std::vector< std::string > parseStringArray(Statement &)
Parse string array.
PtrToArray< double > parseRealArray(Statement &)
Parse real array expression.
PtrToScalar< double > parseTableExpression(Statement &, const Table *)
Parse table expression (depends on a table&#39;s rows).
An array of pointers to scalar expressions.
Definition: Expressions.h:134
std::list< Token > parseTokenList(Statement &)
Parse a token list (for macro argument and the like).
virtual std::vector< T > evaluate() const =0
Evaluate.
virtual T evaluate() const =0
Evaluate.
void operator=(const OArray &)
std::string parseString(Statement &, const char msg[])
Parse string value.
PtrToArray< double > parseRealConstArray(Statement &)
Parse real array constant.
The base class for all OPAL tables.
Definition: Table.h:42
PtrToArray< bool > parseBoolArray(Statement &)
Parse boolean array expression.
A pointer to an array expression.
Definition: Expressions.h:186
std::vector< std::list< Token > > parseTokenListArray(Statement &)
Parse a token list array (for LIST commands).
An array expression.
Definition: Expressions.h:149