OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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
28#include <list>
29#include <optional>
30#include <ostream>
31#include <string>
32
33#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
34#define BOOST_SPIRIT_QI_DEBUG
35
36namespace SDDS {
37 struct parameter
38 {
47
48 unsigned int order_m;
49 std::optional<std::string> name_m;
50 std::optional<std::string> units_m;
51 std::optional<std::string> description_m;
52 std::optional<ast::datatype> type_m;
54 static unsigned int count_m;
55
56 bool checkMandatories() const
57 {
58 return name_m && type_m;
59 }
60
61 template <attributes A>
63 {
64 static bool apply()
65 {
66 std::string attributeString;
67 switch(A)
68 {
69 case SYMBOL:
70 attributeString = "symbol";
71 break;
72 case FORMAT_STRING:
73 attributeString = "format_string";
74 break;
75 case FIXED_VALUE:
76 attributeString = "fixed_value";
77 break;
78 default:
79 return true;
80 }
81 std::cerr << attributeString << " not supported yet" << std::endl;
82 return false;
83 }
84 };
85
86 template <typename Iterator, typename Skipper>
87 bool parse(
88 Iterator& first
89 , Iterator last
90 , Skipper const& skipper)
91 {
92 switch(*this->type_m) {
93 case ast::FLOAT:
94 {
95 boost::spirit::qi::float_type float_;
96 return phrase_parse(first, last, float_, skipper, this->value_m);
97 }
98 case ast::DOUBLE:
99 {
100 boost::spirit::qi::double_type double_;
101 return phrase_parse(first, last, double_, skipper, this->value_m);
102 }
103 case ast::SHORT:
104 {
105 boost::spirit::qi::short_type short_;
106 return phrase_parse(first, last, short_, skipper, this->value_m);
107 }
108 case ast::LONG:
109 {
110 boost::spirit::qi::long_type long_;
111 return phrase_parse(first, last, long_, skipper, this->value_m);
112 }
113 case ast::CHARACTER:
114 {
115 boost::spirit::qi::char_type char_;
116 return phrase_parse(first, last, char_, skipper, this->value_m);
117 }
118 case ast::STRING:
119 {
121 return phrase_parse(first, last, string, skipper, this->value_m);
122 }
123 }
124 return false;
125 }
126 };
127
128 struct parameterList : std::vector<parameter> {};
129
130 template <typename Iterator>
132 {
133 template <typename, typename>
134 struct result { typedef void type; };
135
136 void operator()(parameter& param, Iterator) const
137 {
138 param.order_m = parameter::count_m ++;
139 }
140 };
141
142 inline std::ostream& operator<<(std::ostream& out, const parameter& param) {
143 if (param.name_m) out << "name = " << *param.name_m << ", ";
144 if (param.type_m) out << "type = " << *param.type_m << ", ";
145 if (param.units_m) out << "units = " << *param.units_m << ", ";
146 if (param.description_m) out << "description = " << *param.description_m << ", ";
147 out << "order = " << param.order_m;
148
149 return out;
150 }
151}
152
155 (std::optional<std::string>, name_m)
156 (std::optional<SDDS::ast::datatype>, type_m)
157 (std::optional<std::string>, units_m)
158 (std::optional<std::string>, description_m)
159 (SDDS::ast::variant_t, value_m)
160)
161
162namespace SDDS { namespace parser
163{
164 namespace qi = boost::spirit::qi;
165 namespace ascii = boost::spirit::ascii;
166 namespace phx = boost::phoenix;
167
168 template <typename Iterator>
169 struct parameter_parser: qi::grammar<Iterator, parameter(), skipper<Iterator> >
170 {
171 parameter_parser(error_handler<Iterator> & _error_handler);
172
173 qi::rule<Iterator, std::string(), skipper<Iterator> > string, quoted_string, units;
174 qi::rule<Iterator, std::string(), skipper<Iterator> > parameter_name, parameter_units,
175 parameter_description, parameter_symbol, parameter_format;
176 qi::rule<Iterator, ast::datatype(), skipper<Iterator> > parameter_type;
177 qi::rule<Iterator, long(), skipper<Iterator> > parameter_fixed;
178 qi::rule<Iterator, parameter(), skipper<Iterator> > start;
179 qi::rule<Iterator, ast::nil(), skipper<Iterator> > parameter_unsupported_pre,
180 parameter_unsupported_post;
181 qi::symbols<char, ast::datatype> datatype;
182 };
183}}
184#endif /* PARAMETER_HPP_ */
BOOST_FUSION_ADAPT_STRUCT(SDDS::parameter,(std::optional< std::string >, name_m)(std::optional< SDDS::ast::datatype >, type_m)(std::optional< std::string >, units_m)(std::optional< std::string >, description_m)(SDDS::ast::variant_t, value_m)) namespace SDDS
std::ostream & operator<<(std::ostream &out, const array &)
Definition array.hpp:97
@ 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
std::optional< std::string > name_m
Definition parameter.hpp:49
std::optional< std::string > description_m
Definition parameter.hpp:51
ast::variant_t value_m
Definition parameter.hpp:53
std::optional< std::string > units_m
Definition parameter.hpp:50
unsigned int order_m
Definition parameter.hpp:48
bool parse(Iterator &first, Iterator last, Skipper const &skipper)
Definition parameter.hpp:87
std::optional< ast::datatype > type_m
Definition parameter.hpp:52
static unsigned int count_m
Definition parameter.hpp:54
bool checkMandatories() const
Definition parameter.hpp:56
void operator()(parameter &param, Iterator) const