OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Attributes.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: Attributes.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Namespace Attributes
10 //
11 // ------------------------------------------------------------------------
12 //
13 // $Date: 2002/01/17 22:18:36 $
14 // $Author: jsberg $
15 //
16 // ------------------------------------------------------------------------
17 
18 #include "Attributes/Attributes.h"
19 
21 #include "Attributes/Bool.h"
22 #include "Attributes/BoolArray.h"
23 #include "Attributes/Place.h"
24 #include "Attributes/Range.h"
25 #include "Attributes/Real.h"
26 #include "Attributes/RealArray.h"
27 #include "Attributes/Reference.h"
28 #include "Attributes/opalstr.h"
29 #include "Attributes/StringArray.h"
30 #include "Attributes/TableRow.h"
31 #include "Attributes/TokenList.h"
33 #include "Expressions/AValue.h"
34 #include "Expressions/SRefAttr.h"
35 #include "Expressions/SValue.h"
37 
40 #include "Utilities/Util.h"
41 
42 #include <boost/regex.hpp>
43 
44 using std::string;
45 using namespace Expressions;
46 
47 
48 // Namespace Attributes.
49 // ------------------------------------------------------------------------
50 
51 namespace Attributes {
52 
53  // ----------------------------------------------------------------------
54  // Boolean value.
55 
56  Attribute makeBool(const std::string &name, const std::string &help) {
57  return Attribute(new Bool(name, help), nullptr);
58  }
59 
60 
61  Attribute makeBool(const std::string &name, const std::string &help, bool ini) {
62  return Attribute(new Bool(name, help), new SValue<bool>(ini));
63  }
64 
65 
66  bool getBool(const Attribute &attr) {
67  if(attr.isBaseAllocated()) {
68  AttributeBase *base = &attr.getBase();
69  if(dynamic_cast<Bool *>(&attr.getHandler())) {
70  return dynamic_cast<SValue<bool> *>(base)->evaluate();
71  } else if(SValue<SRefAttr<bool> > *ref =
72  dynamic_cast<SValue<SRefAttr<bool> > *>(base)) {
73  const SRefAttr<bool> &value = ref->evaluate();
74  return value.evaluate();
75  } else {
76  throw OpalException("Attributes::get()", "Attribute \"" +
77  attr.getName() + "\" is not logical.");
78  }
79  } else {
80  return false;
81  }
82  }
83 
84 
85  void setBool(Attribute &attr, bool val) {
86  if(dynamic_cast<const Bool *>(&attr.getHandler())) {
87  attr.set(new SValue<bool>(val));
88  } else if(SValue<SRefAttr<bool> > *ref =
89  dynamic_cast<SValue<SRefAttr<bool> >*>(&attr.getBase())) {
90  const SRefAttr<bool> &value = ref->evaluate();
91  value.set(val);
92  } else {
93  throw OpalException("Attributes::setBool()", "Attribute \"" +
94  attr.getName() + "\" is not logical.");
95  }
96  }
97 
98 
99  // ----------------------------------------------------------------------
100  // Boolean array value.
101  Attribute makeBoolArray(const std::string &name, const std::string &help) {
102  return Attribute(new BoolArray(name, help), nullptr);
103  }
104 
105 
106  std::vector<bool> getBoolArray(const Attribute &attr) {
107  if(attr.isBaseAllocated()) {
108  AttributeBase *base = &attr.getBase();
109  if(AValue<bool> *value =
110  dynamic_cast<AValue<bool>*>(base)) {
111  return value->evaluate();
112  } else {
113  throw OpalException("Attributes::getBoolArray()", "Attribute \"" +
114  attr.getName() + "\" is not a logical array.");
115  }
116  } else {
117  return std::vector<bool>();
118  }
119  }
120 
121 
122  void setBoolArray(Attribute &attr, const std::vector<bool> &value) {
123  if(dynamic_cast<const BoolArray *>(&attr.getHandler())) {
124  // Use ADeferred here, since a component may be overridden later
125  // by an expression.
126  attr.set(new ADeferred<bool>(value));
127  } else {
128  throw OpalException("Attributes::setBoolArray()", "Attribute \"" +
129  attr.getName() + "\" is not a logical array");
130  }
131  }
132 
133 
134  // ----------------------------------------------------------------------
135  // Place value.
136 
137  Attribute makePlace(const std::string &name, const std::string &help) {
138  return Attribute(new Place(name, help),
139  new SValue<PlaceRep>(PlaceRep("#S")));
140  }
141 
142 
143  PlaceRep getPlace(const Attribute &attr) {
144  if(attr.isBaseAllocated()) {
145  if(SValue<PlaceRep> *place =
146  dynamic_cast<SValue<PlaceRep> *>(&attr.getBase())) {
147  return place->evaluate();
148  } else {
149  throw OpalException("Attributes::getPlace()", "Attribute \"" +
150  attr.getName() + "\" is not a place reference.");
151  }
152  } else {
153  return PlaceRep();
154  }
155  }
156 
157 
158  void setPlace(Attribute &attr, const PlaceRep &rep) {
159  if(dynamic_cast<const Place *>(&attr.getHandler())) {
160  attr.set(new SValue<PlaceRep>(rep));
161  } else {
162  throw OpalException("Attributes::getPlace()", "Attribute \"" +
163  attr.getName() + "\" is not a place reference.");
164  }
165  }
166 
167 
168  // ----------------------------------------------------------------------
169  // Range value.
170 
171  Attribute makeRange(const std::string &name, const std::string &help) {
172  return Attribute(new Range(name, help),
173  new SValue<RangeRep>(RangeRep()));
174  }
175 
176 
177  RangeRep getRange(const Attribute &attr) {
178  if(attr.isBaseAllocated()) {
179  if(SValue<RangeRep> *range =
180  dynamic_cast<SValue<RangeRep> *>(&attr.getBase())) {
181  return range->evaluate();
182  } else {
183  throw OpalException("Attributes::get()", "Attribute \"" +
184  attr.getName() + "\" is not a range reference.");
185  }
186  } else {
187  return RangeRep();
188  }
189  }
190 
191 
192  void setRange(Attribute &attr, const RangeRep &rep) {
193  if(dynamic_cast<const Range *>(&attr.getHandler())) {
194  attr.set(new SValue<RangeRep>(rep));
195  } else {
196  throw OpalException("Attributes::get()", "Attribute \"" +
197  attr.getName() + "\" is not a range reference.");
198  }
199  }
200 
201 
202  // ----------------------------------------------------------------------
203  // Real value.
204 
205  Attribute makeReal(const std::string &name, const std::string &help) {
206  return Attribute(new Real(name, help), nullptr);
207  }
208 
209 
210  Attribute
211  makeReal(const std::string &name, const std::string &help, double initial) {
212  return Attribute(new Real(name, help),
213  new SValue<double>(initial));
214  }
215 
216 
217  double getReal(const Attribute &attr) {
218  if(attr.isBaseAllocated()) {
219  AttributeBase *base = &attr.getBase();
220  if(dynamic_cast<Real *>(&attr.getHandler())) {
221  return dynamic_cast<SValue<double> *>(base)->evaluate();
222  } else if(SValue<SRefAttr<double> > *ref =
223  dynamic_cast<SValue<SRefAttr<double> > *>(base)) {
224  const SRefAttr<double> &value = ref->evaluate();
225  return value.evaluate();
226  } else {
227  throw OpalException("Attributes::getReal()", "Attribute \"" +
228  attr.getName() + "\" is not real.");
229  }
230  } else {
231  return 0.0;
232  }
233  }
234 
235 
236  void setReal(Attribute &attr, double val) {
237  if(dynamic_cast<const Real *>(&attr.getHandler())) {
238  attr.set(new SValue<double>(val));
239  } else if(SValue<SRefAttr<double> > *ref =
240  dynamic_cast<SValue<SRefAttr<double> >*>(&attr.getBase())) {
241  const SRefAttr<double> &value = ref->evaluate();
242  value.set(val);
243  } else {
244  throw OpalException("Attributes::setReal()", "Attribute \"" +
245  attr.getName() + "\" is not real.");
246  }
247  }
248 
249 
250  // ----------------------------------------------------------------------
251  // Real array value.
252 
253  Attribute makeRealArray(const std::string &name, const std::string &help) {
254  return Attribute(new RealArray(name, help), nullptr);
255  }
256 
257 
258  std::vector<double> getRealArray(const Attribute &attr) {
259  if(attr.isBaseAllocated()) {
260  if(dynamic_cast<RealArray *>(&attr.getHandler())) {
261  AttributeBase *base = &attr.getBase();
262  return dynamic_cast<AValue<double>*>(base)->evaluate();
263  } else {
264  throw OpalException("Attributes::getRealArray()", "Attribute \"" +
265  attr.getName() + "\" is not a real array.");
266  }
267  } else {
268  return std::vector<double>();
269  }
270  }
271 
272 
273  void setRealArray(Attribute &attr, const std::vector<double> &value) {
274  if(dynamic_cast<const RealArray *>(&attr.getHandler())) {
275  // Use ADeferred here, since a component may be overridden later
276  // by an expression.
277  attr.set(new ADeferred<double>(value));
278  } else {
279  throw OpalException("Attributes::setRealArray()", "Attribute \"" +
280  attr.getName() + "\" is not a real array.");
281  }
282  }
283 
284 
285  // ----------------------------------------------------------------------
286  // Reference value.
287 
288  Attribute makeReference(const std::string &name, const std::string &help) {
289  return Attribute(new Reference(name, help), nullptr);
290  }
291 
292 
293  // ----------------------------------------------------------------------
294  // String value.
295 
296  Attribute makeString(const std::string &name, const std::string &help) {
297  return Attribute(new String(name, help), nullptr);
298  }
299 
300 
301  Attribute
302  makeString(const std::string &name, const std::string &help, const std::string &initial) {
303  return Attribute(new String(name, help), new SValue<std::string>(initial));
304  }
305 
306 
307  std::string getString(const Attribute &attr) {
308  if(attr.isBaseAllocated()) {
309  AttributeBase *base = &attr.getBase();
310  std::string expr;
311  if(dynamic_cast<String *>(&attr.getHandler())) {
312  expr = dynamic_cast<SValue<std::string> *>(base)->evaluate();
313  } else if(SValue<SRefAttr<std::string> > *ref =
314  dynamic_cast<SValue<SRefAttr<std::string> > *>(base)) {
315  const SRefAttr<std::string> &value = ref->evaluate();
316  expr = value.evaluate();
317  } else {
318  throw OpalException("Attributes::getString()", "Attribute \"" +
319  attr.getName() + "\" is not string.");
320  }
321 
322  auto opal = OpalData::getInstance();
323 
324  boost::regex variableRE("\\$\\{(.*?)\\}");
325  boost::smatch what;
326 
327  std::string exprDeref;
328  std::string::const_iterator start = expr.begin();
329  std::string::const_iterator end = expr.end();
330 
331  while (boost::regex_search(start, end, what, variableRE, boost::match_default)) {
332  exprDeref += std::string(start, what[0].first);
333  std::string variable = Util::toUpper(std::string(what[1].first, what[1].second));
334 
335  if (Object *obj = opal->find(variable)) {
336  std::ostringstream value;
337 
338  RealVariable *real = static_cast<RealVariable*>(obj);
339  real->printValue(value);
340  exprDeref += value.str();
341  } else {
342  exprDeref += std::string(what[0].first, what[0].second);
343  }
344 
345  start = what[0].second;
346  }
347  exprDeref += std::string(start, end);
348 
349  return exprDeref;
350  } else {
351  return std::string();
352  }
353  }
354 
355 
356  void setString(Attribute &attr, const std::string &val) {
357  if(dynamic_cast<const String *>(&attr.getHandler())) {
358  attr.set(new SValue<std::string>(val));
359  } else if(SValue<SRefAttr<std::string> > *ref =
360  dynamic_cast<SValue<SRefAttr<std::string> >*>(&attr.getBase())) {
361  const SRefAttr<std::string> &value = ref->evaluate();
362  value.set(val);
363  } else {
364  throw OpalException("Attributes::setString()", "Attribute \"" +
365  attr.getName() + "\" is not a string.");
366  }
367  }
368 
369 
370  // ----------------------------------------------------------------------
371  // String array value.
372 
373  Attribute makeStringArray(const std::string &name, const std::string &help) {
374  return Attribute(new StringArray(name, help), nullptr);
375  }
376 
377 
378  std::vector<std::string> getStringArray(const Attribute &attr) {
379  if(attr.isBaseAllocated()) {
380  AttributeBase *base = &attr.getBase();
381  if(dynamic_cast<StringArray *>(&attr.getHandler())) {
382  auto opal = OpalData::getInstance();
383 
384  boost::regex variableRE("\\$\\{(.*?)\\}");
385  boost::smatch what;
386 
387  std::vector<std::string> value = dynamic_cast<AValue<std::string>*>(base)->evaluate();
388  for (auto expr: value) {
389  std::string exprDeref;
390  std::string::const_iterator start = expr.begin();
391  std::string::const_iterator end = expr.end();
392 
393  while (boost::regex_search(start, end, what, variableRE, boost::match_default)) {
394  exprDeref += std::string(start, what[0].first);
395  std::string variable = Util::toUpper(std::string(what[1].first, what[1].second));
396 
397  if (Object *obj = opal->find(variable)) {
398  std::ostringstream value;
399 
400  RealVariable *real = static_cast<RealVariable*>(obj);
401  real->printValue(value);
402  exprDeref += value.str();
403  } else {
404  exprDeref += std::string(what[0].first, what[0].second);
405  }
406 
407  start = what[0].second;
408  }
409  expr = exprDeref + std::string(start, end);
410  }
411 
412  return value;
413  } else {
414  throw OpalException("Attributes::getStringArray()", "Attribute \"" +
415  attr.getName() + "\" is not a string array.");
416  }
417  } else {
418  return std::vector<std::string>();
419  }
420  }
421 
422 
423  void setStringArray(Attribute &attr, const std::vector<std::string> &value) {
424  if(dynamic_cast<const StringArray *>(&attr.getHandler())) {
425  // Strings are never expressions, so AValue will do here.
426  attr.set(new AValue<std::string>(value));
427  } else {
428  throw OpalException("Attributes::setStringArray()", "Attribute \"" +
429  attr.getName() + "\" is not a string array.");
430  }
431  }
432 
433 
434  // ----------------------------------------------------------------------
435  // Table row reference value.
436 
437  Attribute makeTableRow(const std::string &name, const std::string &help) {
438  return Attribute(new TableRow(name, help), nullptr);
439  }
440 
441 
443  if(attr.isBaseAllocated()) {
444  if(SValue<TableRowRep> *row =
445  dynamic_cast<SValue<TableRowRep> *>(&attr.getBase())) {
446  return row->evaluate();
447  } else {
448  throw OpalException("Attributes::get()", "Attribute \"" +
449  attr.getName() +
450  "\" is not a table row reference.");
451  }
452  } else {
453  return TableRowRep();
454  }
455  }
456 
457 
458  void setTableRow(Attribute &attr, const TableRowRep &rep) {
459  if(dynamic_cast<const TableRow *>(&attr.getHandler())) {
460  attr.set(new SValue<TableRowRep>(rep));
461  } else {
462  throw OpalException("Attributes::get()", "Attribute \"" +
463  attr.getName() +
464  "\" is not a table row reference.");
465  }
466  }
467 
468 
469  // ----------------------------------------------------------------------
470  // Token list value.
471 
472  Attribute makeTokenList(const std::string &name, const std::string &help) {
473  return Attribute(new TokenList(name, help), nullptr);
474  }
475 
476 
477  std::list<Token> getTokenList(const Attribute &attr) {
478  if(attr.isBaseAllocated()) {
479  AttributeBase *base = &attr.getBase();
480  if(dynamic_cast<TokenList *>(&attr.getHandler())) {
481  return dynamic_cast<SValue<std::list<Token> > *>(base)->evaluate();
482  } else {
483  throw OpalException("Attributes::getTokenList()", "Attribute \"" +
484  attr.getName() + "\" is not a token list.");
485  }
486  } else {
487  return std::list<Token>();
488  }
489  }
490 
491 
492  void setTokenList(Attribute &attr, const std::list<Token> &val) {
493  if(dynamic_cast<const TokenList *>(&attr.getHandler())) {
494  attr.set(new SValue<std::list<Token> >(val));
495  } else {
496  throw OpalException("Attributes::set()", "Attribute \"" + attr.getName() +
497  "\" is not a token list.");
498  }
499  }
500 
501 
502  // ----------------------------------------------------------------------
503  // Token list array value.
504 
505  Attribute makeTokenListArray(const std::string &name, const std::string &help) {
506  return Attribute(new TokenListArray(name, help), nullptr);
507  }
508 
509 
510  std::vector<std::list<Token> > getTokenListArray(const Attribute &attr) {
511  if(attr.isBaseAllocated()) {
512  AttributeBase *base = &attr.getBase();
513  if(dynamic_cast<TokenListArray *>(&attr.getHandler())) {
514  return dynamic_cast<AValue<std::list<Token> > *>(base)->evaluate();
515  } else {
516  throw OpalException("Attributes::getTokenListArray()", "Attribute \"" +
517  attr.getName() + "\" is not a token list array.");
518  }
519  } else {
520  return std::vector<std::list<Token> >();
521  }
522  }
523 
524 
525  void
527  const std::vector<std::list<Token> > &value) {
528  // Token lists are never expressions, so AValue will do here.
529  attr.set(new AValue<std::list<Token> >(value));
530  }
531 }
void setReal(Attribute &attr, double val)
Set real value.
Definition: Attributes.cpp:236
bool isBaseAllocated() const
Definition: Attribute.cpp:71
void setStringArray(Attribute &attr, const std::vector< std::string > &value)
Set string array value.
Definition: Attributes.cpp:423
virtual void printValue(std::ostream &os) const
Print its value.
void set(AttributeBase *newBase)
Define new value.
Definition: Attribute.cpp:137
Abstract base class for attribute values of different types.
Definition: AttributeBase.h:32
RangeRep getRange(const Attribute &attr)
Get range value.
Definition: Attributes.cpp:177
void setRange(Attribute &attr, const RangeRep &rep)
Set range value.
Definition: Attributes.cpp:192
The base class for all OPAL exceptions.
Definition: OpalException.h:28
Parser for an attribute of type token list.
Definition: TokenList.h:36
void setTokenList(Attribute &attr, const std::list< Token > &val)
Set token list value.
Definition: Attributes.cpp:492
std::list< Token > getTokenList(const Attribute &attr)
Return token list value.
Definition: Attributes.cpp:477
Parser for an attribute of type attribute reference.
Definition: Reference.h:33
The REAL VARIABLE definition.
Definition: RealVariable.h:26
std::string toUpper(const std::string &str)
Definition: Util.cpp:130
std::vector< bool > getBoolArray(const Attribute &attr)
Get logical array value.
Definition: Attributes.cpp:106
Attribute makeRange(const std::string &name, const std::string &help)
Create a range attribute.
Definition: Attributes.cpp:171
void setString(Attribute &attr, const std::string &val)
Set string value.
Definition: Attributes.cpp:356
Representation of a table row reference.
Definition: TableRowRep.h:36
TableRowRep getTableRow(const Attribute &attr)
Get table row value.
Definition: Attributes.cpp:442
Parser for attribute of type logical.
Definition: Bool.h:31
FLieGenerator< T, N > real(const FLieGenerator< std::complex< T >, N > &)
Take real part of a complex generator.
An attribute defined as a reference to a scalar.
Definition: Expressions.h:248
bool getBool(const Attribute &attr)
Return logical value.
Definition: Attributes.cpp:66
PlaceRep getPlace(const Attribute &attr)
Get place value.
Definition: Attributes.cpp:143
Representation of a place within a beam line or sequence.
Definition: PlaceRep.h:41
Attribute makeStringArray(const std::string &name, const std::string &help)
Create a string array attribute.
Definition: Attributes.cpp:373
static OpalData * getInstance()
Definition: OpalData.cpp:209
Attribute makeBoolArray(const std::string &name, const std::string &help)
Create a logical array attribute.
Definition: Attributes.cpp:101
A representation of an Object attribute.
Definition: Attribute.h:55
Parser for an attribute of type string.
Definition: opalstr.h:31
void setTableRow(Attribute &attr, const TableRowRep &rep)
Set table row value.
Definition: Attributes.cpp:458
std::vector< std::string > getStringArray(const Attribute &attr)
Get string array value.
Definition: Attributes.cpp:378
virtual void set(const T &) const
Store new value.
Definition: SRefAttr.h:205
Parser for an attribute of type table row reference.
Definition: TableRow.h:34
void setBoolArray(Attribute &attr, const std::vector< bool > &value)
Set logical array value.
Definition: Attributes.cpp:122
Object attribute with a constant array value.
Definition: AValue.h:35
std::vector< double > getRealArray(const Attribute &attr)
Get array value.
Definition: Attributes.cpp:258
Object attribute with a ``deferred&#39;&#39; array value.
Definition: ADeferred.h:40
void setRealArray(Attribute &attr, const std::vector< double > &value)
Set array value.
Definition: Attributes.cpp:273
Parser for an attribute of type real.
Definition: Real.h:31
Representation of a range within a beam line or sequence.
Definition: RangeRep.h:34
void setPlace(Attribute &attr, const PlaceRep &rep)
Set place value.
Definition: Attributes.cpp:158
Attribute makeTableRow(const std::string &name, const std::string &help)
Create a table row attribute.
Definition: Attributes.cpp:437
std::vector< std::list< Token > > getTokenListArray(const Attribute &attr)
Return token list array value.
Definition: Attributes.cpp:510
Attribute makePlace(const std::string &name, const std::string &help)
Create a place attribute.
Definition: Attributes.cpp:137
Attribute makeTokenListArray(const std::string &name, const std::string &help)
Make token list attribute.
Definition: Attributes.cpp:505
Parser for an attribute of type place reference.
Definition: Place.h:32
Object attribute with a constant scalar value.
Definition: SValue.h:34
The base class for all OPAL objects.
Definition: Object.h:48
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Definition: Attributes.cpp:56
void setBool(Attribute &attr, bool val)
Set logical value.
Definition: Attributes.cpp:85
const std::string name
Parser for an attribute of type real array.
Definition: RealArray.h:32
virtual T evaluate() const
Evaluate.
Definition: SRefAttr.h:144
Attribute makeRealArray(const std::string &name, const std::string &help)
Create real array attribute.
Definition: Attributes.cpp:253
AttributeHandler & getHandler() const
Return a reference to the parser.
Definition: Attribute.cpp:75
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:217
Parser for an attribute of type string array.
Definition: StringArray.h:32
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
Attribute makeReference(const std::string &name, const std::string &help)
Create a reference attribute.
Definition: Attributes.cpp:288
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:205
void setTokenListArray(Attribute &attr, const std::vector< std::list< Token > > &value)
Set token list array value.
Definition: Attributes.cpp:526
Attribute makeTokenList(const std::string &name, const std::string &help)
Make token list attribute.
Definition: Attributes.cpp:472
const std::string & getName() const
Return the attribute name.
Definition: Attribute.cpp:90
Parser for an attribute of type token list array.
AttributeBase & getBase() const
Return reference to polymorphic value.
Definition: Attribute.cpp:67
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307
Parser for an attribute of type range definition.
Definition: Range.h:32
Parser for an attribute of type logical array.
Definition: BoolArray.h:32