45 #include <boost/regex.hpp>
50 std::string stringifyVariable(
Object *obj) {
54 std::ostringstream valueStream;
58 return valueStream.str();
64 return valueStream.str();
69 bool boolean = value->
getBool();
70 valueStream << std::boolalpha << boolean;
77 "The variable '" + obj->
getOpalName() +
"' isn't of type REAL, STRING or BOOL");
110 throw OpalException(
"Attributes::getBool()",
"Attribute \"" +
111 attr.
getName() +
"\" is not logical.");
128 throw OpalException(
"Attributes::setBool()",
"Attribute \"" +
129 attr.
getName() +
"\" is not logical.");
146 return value->evaluate();
148 throw OpalException(
"Attributes::getBoolArray()",
"Attribute \"" +
149 attr.
getName() +
"\" is not a logical array.");
152 return std::vector<bool>();
163 throw OpalException(
"Attributes::setBoolArray()",
"Attribute \"" +
164 attr.
getName() +
"\" is not a logical array");
182 return place->evaluate();
184 throw OpalException(
"Attributes::getPlace()",
"Attribute \"" +
185 attr.
getName() +
"\" is not a place reference.");
197 throw OpalException(
"Attributes::setPlace()",
"Attribute \"" +
198 attr.
getName() +
"\" is not a place reference.");
216 return range->evaluate();
218 throw OpalException(
"Attributes::getRange()",
"Attribute \"" +
219 attr.
getName() +
"\" is not a range reference.");
231 throw OpalException(
"Attributes::setRange()",
"Attribute \"" +
232 attr.
getName() +
"\" is not a range reference.");
246 makeReal(
const std::string &
name,
const std::string &help,
double initial) {
262 throw OpalException(
"Attributes::getReal()",
"Attribute \"" +
263 attr.
getName() +
"\" is not real.");
280 throw OpalException(
"Attributes::setReal()",
"Attribute \"" +
281 attr.
getName() +
"\" is not real.");
300 throw OpalException(
"Attributes::getRealArray()",
"Attribute \"" +
301 attr.
getName() +
"\" is not a real array.");
304 return std::vector<double>();
315 throw OpalException(
"Attributes::setRealArray()",
"Attribute \"" +
316 attr.
getName() +
"\" is not a real array.");
338 makeString(
const std::string &
name,
const std::string &help,
const std::string &initial) {
356 throw OpalException(
"Attributes::getString()",
"Attribute \"" +
357 attr.
getName() +
"\" is not string.");
362 boost::regex variableRE(
"\\$\\{(.*?)\\}");
365 std::string exprDeref;
366 std::string::const_iterator start = expr.begin();
367 std::string::const_iterator
end = expr.end();
369 while (boost::regex_search(start,
end, what, variableRE, boost::match_default)) {
370 exprDeref += std::string(start, what[0].first);
371 std::string variable =
Util::toUpper(std::string(what[1].first, what[1].second));
373 if (
Object *obj = opal->find(variable)) {
374 exprDeref += ::stringifyVariable(obj);
377 "Can't find variable '" + variable +
"' in string \"" + expr +
"\"");
380 start = what[0].second;
382 exprDeref += std::string(start,
end);
386 return std::string();
400 throw OpalException(
"Attributes::setString()",
"Attribute \"" +
401 attr.
getName() +
"\" is not a string.");
410 const std::string &help,
411 const std::initializer_list<std::string>& predefinedStrings) {
418 const std::string &help,
419 const std::initializer_list<std::string>& predefinedStrings,
420 const std::string &initial) {
434 value.
set(upperCaseVal);
436 throw OpalException(
"Attributes::setPredefinedString()",
"Attribute \"" +
437 attr.
getName() +
"\" is not a supported string.");
465 throw OpalException(
"Attributes::setUpperCaseString()",
"Attribute \"" +
466 attr.
getName() +
"\" is not an upper case string.");
485 boost::regex variableRE(
"\\$\\{(.*?)\\}");
489 for (
auto expr: value) {
490 std::string exprDeref;
491 std::string::const_iterator start = expr.begin();
492 std::string::const_iterator
end = expr.end();
494 while (boost::regex_search(start,
end, what, variableRE, boost::match_default)) {
495 exprDeref += std::string(start, what[0].first);
496 std::string variable =
Util::toUpper(std::string(what[1].first, what[1].second));
498 if (
Object *obj = opal->find(variable)) {
499 std::ostringstream value;
502 real->printValue(value);
503 exprDeref += value.str();
505 exprDeref += std::string(what[0].first, what[0].second);
508 start = what[0].second;
510 expr = exprDeref + std::string(start,
end);
515 throw OpalException(
"Attributes::getStringArray()",
"Attribute \"" +
516 attr.
getName() +
"\" is not a string array.");
519 return std::vector<std::string>();
529 throw OpalException(
"Attributes::setStringArray()",
"Attribute \"" +
530 attr.
getName() +
"\" is not a string array.");
543 std::vector<std::string> uppercase(value.size());
544 std::transform(value.begin(), value.end(), uppercase.begin(),
545 [](std::string val) -> std::string { return Util::toUpper(val); });
548 throw OpalException(
"Attributes::setUpperCaseStringArray()",
"Attribute \"" +
549 attr.
getName() +
"\" is not an upper case string array.");
566 return row->evaluate();
568 throw OpalException(
"Attributes::getTableRow()",
"Attribute \"" +
570 "\" is not a table row reference.");
582 throw OpalException(
"Attributes::setTableRow()",
"Attribute \"" +
584 "\" is not a table row reference.");
603 throw OpalException(
"Attributes::getTokenList()",
"Attribute \"" +
604 attr.
getName() +
"\" is not a token list.");
607 return std::list<Token>();
614 attr.
set(
new SValue<std::list<Token> >(val));
617 "\" is not a token list.");
636 throw OpalException(
"Attributes::getTokenListArray()",
"Attribute \"" +
637 attr.
getName() +
"\" is not a token list array.");
640 return std::vector<std::list<Token> >();
647 const std::vector<std::list<Token> > &value) {
649 attr.
set(
new AValue<std::list<Token> >(value));
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
FLieGenerator< T, N > real(const FLieGenerator< std::complex< T >, N > &)
Take real part of a complex generator.
Representation objects and parsers for attribute expressions.
A collection of routines to construct object Attributes and retrieve.
Attribute makePlace(const std::string &name, const std::string &help)
Create a place attribute.
double getReal(const Attribute &attr)
Return real value.
void setBoolArray(Attribute &attr, const std::vector< bool > &value)
Set logical array value.
void setUpperCaseStringArray(Attribute &attr, const std::vector< std::string > &value)
Set upper case string array value.
Attribute makeUpperCaseStringArray(const std::string &name, const std::string &help)
Make uppercase string array attribute.
void setRealArray(Attribute &attr, const std::vector< double > &value)
Set array value.
void setBool(Attribute &attr, bool val)
Set logical value.
Attribute makeBool(const std::string &name, const std::string &help, bool ini)
Make logical attribute.
Attribute makeUpperCaseString(const std::string &name, const std::string &help, const std::string &initial)
Make uppercase string attribute.
std::list< Token > getTokenList(const Attribute &attr)
Return token list value.
void setTableRow(Attribute &attr, const TableRowRep &rep)
Set table row value.
Attribute makePredefinedString(const std::string &name, const std::string &help, const std::initializer_list< std::string > &predefinedStrings, const std::string &initial)
Make predefined string attribute.
Attribute makeStringArray(const std::string &name, const std::string &help)
Create a string array attribute.
void setUpperCaseString(Attribute &attr, const std::string &val)
Set uppercase string value.
Attribute makeTableRow(const std::string &name, const std::string &help)
Create a table row attribute.
Attribute makeReference(const std::string &name, const std::string &help)
Create a reference attribute.
void setRange(Attribute &attr, const RangeRep &rep)
Set range value.
void setString(Attribute &attr, const std::string &val)
Set string value.
Attribute makeRange(const std::string &name, const std::string &help)
Create a range attribute.
Attribute makeTokenListArray(const std::string &name, const std::string &help)
Make token list attribute.
std::vector< bool > getBoolArray(const Attribute &attr)
Get logical array value.
bool getBool(const Attribute &attr)
Return logical value.
TableRowRep getTableRow(const Attribute &attr)
Get table row value.
Attribute makeTokenList(const std::string &name, const std::string &help)
Make token list attribute.
void setTokenListArray(Attribute &attr, const std::vector< std::list< Token > > &value)
Set token list array value.
void setReal(Attribute &attr, double val)
Set real value.
Attribute makeRealArray(const std::string &name, const std::string &help)
Create real array attribute.
void setStringArray(Attribute &attr, const std::vector< std::string > &value)
Set string array value.
PlaceRep getPlace(const Attribute &attr)
Get place value.
std::vector< double > getRealArray(const Attribute &attr)
Get array value.
Attribute makeString(const std::string &name, const std::string &help, const std::string &initial)
Make string attribute.
std::vector< std::string > getStringArray(const Attribute &attr)
Get string array value.
void setPredefinedString(Attribute &attr, const std::string &val)
Set predefined string value.
std::string getString(const Attribute &attr)
Get string value.
Attribute makeBoolArray(const std::string &name, const std::string &help)
Create a logical array attribute.
std::vector< std::list< Token > > getTokenListArray(const Attribute &attr)
Return token list array value.
void setTokenList(Attribute &attr, const std::list< Token > &val)
Set token list value.
RangeRep getRange(const Attribute &attr)
Get range value.
void setPlace(Attribute &attr, const PlaceRep &rep)
Set place value.
Attribute makeReal(const std::string &name, const std::string &help, double initial)
Make real attribute.
constexpr double e
The value of.
std::string toUpper(const std::string &str)
A representation of an Object attribute.
AttributeBase & getBase() const
Return reference to polymorphic value.
const std::string & getName() const
Return the attribute name.
void set(AttributeBase *newBase)
Define new value.
bool isBaseAllocated() const
AttributeHandler & getHandler() const
Return a reference to the parser.
Abstract base class for attribute values of different types.
An attribute defined as a reference to a scalar.
virtual void set(const T &) const
Store new value.
virtual T evaluate() const
Evaluate.
The base class for all OPAL objects.
const std::string & getOpalName() const
Return object name.
static OpalData * getInstance()
Representation of a place within a beam line or sequence.
Representation of a range within a beam line or sequence.
Representation of a table row reference.
The base class for all OPAL value definitions.
virtual double getReal() const
Return real value.
virtual bool getBool() const
Return logical value.
virtual std::string getString() const
Return string value.
Parser for attribute of type logical.
Parser for an attribute of type logical array.
Parser for an attribute of type place reference.
Parser for an attribute of type string.
Parser for an attribute of type range definition.
Parser for an attribute of type real.
Parser for an attribute of type real array.
Parser for an attribute of type attribute reference.
Parser for an attribute of type string.
Parser for an attribute of type string array.
Parser for an attribute of type table row reference.
Parser for an attribute of type token list.
Parser for an attribute of type token list array.
Parser for an attribute of type string.
Parser for an attribute of type string array.
Object attribute with a `‘deferred’' array value.
Object attribute with a constant array value.
Object attribute with a constant scalar value.
The base class for all OPAL exceptions.