OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
parameter.hpp
Go to the documentation of this file.
1 //
2 // Struct parameter
3 //
4 // Copyright (c) 2015, Christof Metzger-Kraus, Helmholtz-Zentrum Berlin
5 // All rights reserved
6 //
7 // This file is part of OPAL.
8 //
9 // OPAL is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
16 //
17 #ifndef PARAMETER_HPP_
18 #define PARAMETER_HPP_
19 
20 #include "ast.hpp"
21 #include "skipper.hpp"
22 #include "error_handler.hpp"
23 
24 #include <boost/config/warning_disable.hpp>
25 #include <boost/spirit/include/qi.hpp>
26 #include <boost/fusion/include/adapt_struct.hpp>
27 #include <boost/optional.hpp>
28 
29 #include <list>
30 
31 #define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
32 #define BOOST_SPIRIT_QI_DEBUG
33 
34 namespace SDDS {
35  struct parameter
36  {
42  , TYPE
44  };
45 
46  unsigned int order_m;
47  boost::optional<std::string> name_m;
48  boost::optional<std::string> units_m;
49  boost::optional<std::string> description_m;
50  boost::optional<ast::datatype> type_m;
52  static unsigned int count_m;
53 
54  bool checkMandatories() const
55  {
56  return name_m && type_m;
57  }
58 
59  template <attributes A>
61  {
62  static bool apply()
63  {
64  std::string attributeString;
65  switch(A)
66  {
67  case SYMBOL:
68  attributeString = "symbol";
69  break;
70  case FORMAT_STRING:
71  attributeString = "format_string";
72  break;
73  case FIXED_VALUE:
74  attributeString = "fixed_value";
75  break;
76  default:
77  return true;
78  }
79  std::cerr << attributeString << " not supported yet" << std::endl;
80  return false;
81  }
82  };
83 
84  template <typename Iterator, typename Skipper>
85  bool parse(
86  Iterator& first
87  , Iterator last
88  , Skipper const& skipper)
89  {
90  switch(*this->type_m) {
91  case ast::FLOAT:
92  {
93  boost::spirit::qi::float_type float_;
94  return phrase_parse(first, last, float_, skipper, this->value_m);
95  }
96  case ast::DOUBLE:
97  {
98  boost::spirit::qi::double_type double_;
99  return phrase_parse(first, last, double_, skipper, this->value_m);
100  }
101  case ast::SHORT:
102  {
103  boost::spirit::qi::short_type short_;
104  return phrase_parse(first, last, short_, skipper, this->value_m);
105  }
106  case ast::LONG:
107  {
108  boost::spirit::qi::long_type long_;
109  return phrase_parse(first, last, long_, skipper, this->value_m);
110  }
111  case ast::CHARACTER:
112  {
113  boost::spirit::qi::char_type char_;
114  return phrase_parse(first, last, char_, skipper, this->value_m);
115  }
116  case ast::STRING:
117  {
119  return phrase_parse(first, last, string, skipper, this->value_m);
120  }
121  }
122  return false;
123  }
124  };
125 
126  struct parameterList : std::vector<parameter> {};
127 
128  template <typename Iterator>
130  {
131  template <typename, typename>
132  struct result { typedef void type; };
133 
134  void operator()(parameter& param, Iterator) const
135  {
136  param.order_m = parameter::count_m ++;
137  }
138  };
139 
140  inline std::ostream& operator<<(std::ostream& out, const parameter& param) {
141  if (param.name_m) out << "name = " << *param.name_m << ", ";
142  if (param.type_m) out << "type = " << *param.type_m << ", ";
143  if (param.units_m) out << "units = " << *param.units_m << ", ";
144  if (param.description_m) out << "description = " << *param.description_m << ", ";
145  out << "order = " << param.order_m;
146 
147  return out;
148  }
149 }
150 
153  (boost::optional<std::string>, name_m)
154  (boost::optional<SDDS::ast::datatype>, type_m)
155  (boost::optional<std::string>, units_m)
156  (boost::optional<std::string>, description_m)
157  (SDDS::ast::variant_t, value_m)
158 )
159 
160 namespace SDDS { namespace parser
161 {
162  namespace qi = boost::spirit::qi;
163  namespace ascii = boost::spirit::ascii;
164  namespace phx = boost::phoenix;
165 
166  template <typename Iterator>
167  struct parameter_parser: qi::grammar<Iterator, parameter(), skipper<Iterator> >
168  {
169  parameter_parser(error_handler<Iterator> & _error_handler);
170 
171  qi::rule<Iterator, std::string(), skipper<Iterator> > string, quoted_string, units;
172  qi::rule<Iterator, std::string(), skipper<Iterator> > parameter_name, parameter_units,
173  parameter_description, parameter_symbol, parameter_format;
174  qi::rule<Iterator, ast::datatype(), skipper<Iterator> > parameter_type;
175  qi::rule<Iterator, long(), skipper<Iterator> > parameter_fixed;
176  qi::rule<Iterator, parameter(), skipper<Iterator> > start;
177  qi::rule<Iterator, ast::nil(), skipper<Iterator> > parameter_unsupported_pre,
178  parameter_unsupported_post;
179  qi::symbols<char, ast::datatype> datatype;
180  };
181 }}
182 #endif /* PARAMETER_HPP_ */
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
BOOST_FUSION_ADAPT_STRUCT(SDDS::parameter,(boost::optional< std::string >, name_m)(boost::optional< SDDS::ast::datatype >, type_m)(boost::optional< std::string >, units_m)(boost::optional< std::string >, description_m)(SDDS::ast::variant_t, value_m)) namespace SDDS
Definition: parameter.hpp:151
Definition: array.hpp:33
std::ostream & operator<<(std::ostream &out, const array &)
Definition: array.hpp:97
datatype
Definition: ast.hpp:28
@ STRING
Definition: ast.hpp:33
@ DOUBLE
Definition: ast.hpp:29
@ LONG
Definition: ast.hpp:31
@ FLOAT
Definition: ast.hpp:28
@ CHARACTER
Definition: ast.hpp:32
@ SHORT
Definition: ast.hpp:30
boost::variant< float, double, short, long, char, std::string > variant_t
Definition: ast.hpp:48
boost::optional< std::string > name_m
Definition: parameter.hpp:47
ast::variant_t value_m
Definition: parameter.hpp:51
boost::optional< ast::datatype > type_m
Definition: parameter.hpp:50
unsigned int order_m
Definition: parameter.hpp:46
bool parse(Iterator &first, Iterator last, Skipper const &skipper)
Definition: parameter.hpp:85
boost::optional< std::string > units_m
Definition: parameter.hpp:48
boost::optional< std::string > description_m
Definition: parameter.hpp:49
static unsigned int count_m
Definition: parameter.hpp:52
bool checkMandatories() const
Definition: parameter.hpp:54
void operator()(parameter &param, Iterator) const
Definition: parameter.hpp:134