OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
PredefinedString.cpp
Go to the documentation of this file.
1 //
2 // Class PredefinedString
3 // This class is used to parse attributes of type string that should
4 // be contained in set of predefined strings.
5 //
6 // Copyright (c) 2021, Christof Metzger-Kraus, Open Sourcerer
7 // All rights reserved
8 //
9 // This file is part of OPAL.
10 //
11 // OPAL is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
18 //
19 #include "Attributes/Attributes.h"
22 #include "Utilities/ParseError.h"
23 #include "Utilities/Util.h"
24 
25 using namespace Expressions;
26 
27 
28 namespace Attributes {
29 
30  PredefinedString::PredefinedString(const std::string &name,
31  const std::string &help,
32  const std::initializer_list<std::string>& predefinedStrings,
33  const std::string& defaultValue):
34  AttributeHandler(name, help, 0)
35  {
36  for (const std::string& value: predefinedStrings) {
37  predefinedStrings_m.insert(Util::toUpper(value));
38  }
40  }
41 
42 
44  {}
45 
46 
47  const std::string &PredefinedString::getType() const {
48  static const std::string type("predefined string");
49  return type;
50  }
51 
52  void PredefinedString::parse(Attribute &attr, Statement &stat, bool) const {
53  std::string value = Util::toUpper(parseStringValue(stat, "String value expected"));
54  if (predefinedStrings_m.count(value) == 0) {
55  throw ParseError("PredefinedString::parse", "Unsupported value '" + value + "'");
56  }
58  }
59 
60 };
const std::string name
Representation objects and parsers for attribute expressions.
Definition: Expressions.h:64
std::string parseStringValue(Statement &, const char msg[])
A collection of routines to construct object Attributes and retrieve.
Definition: Attributes.cpp:85
void setPredefinedString(Attribute &attr, const std::string &val)
Set predefined string value.
Definition: Attributes.cpp:426
std::string toUpper(const std::string &str)
Definition: Util.cpp:132
boost::function< boost::tuple< double, bool >arguments_t)> type
Definition: function.hpp:21
A representation of an Object attribute.
Definition: Attribute.h:52
Abstract base class for attribute parsers.
void setPredefinedValues(const std::set< std::string > &, const std::string &)
Add predefined strings values to the help.
virtual void parse(Attribute &, Statement &, bool) const
Parse the attribute.
virtual const std::string & getType() const
Return attribute type string `‘string’'.
std::set< std::string > predefinedStrings_m
Interface for statements.
Definition: Statement.h:38
Parse exception.
Definition: ParseError.h:32