OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
column.hpp
Go to the documentation of this file.
1//
2// Struct column
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 COLUMN_HPP_
18#define COLUMN_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 <vector>
30
31#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
32#define BOOST_SPIRIT_QI_DEBUG
33
34namespace SDDS {
35 struct column
36 {
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 FIELD_LENGTH:
74 attributeString = "field_length";
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 float f = 0.0;
94 boost::spirit::qi::float_type float_;
95 if (phrase_parse(first, last, float_, skipper, f)) {
96 this->values_m.push_back(f);
97 return true;
98 }
99 break;
100 }
101 case ast::DOUBLE:
102 {
103 double d = 0.0;
104 boost::spirit::qi::double_type double_;
105 if (phrase_parse(first, last, double_, skipper, d)) {
106 this->values_m.push_back(d);
107 return true;
108 }
109 break;
110 }
111 case ast::SHORT:
112 {
113 short s = 0;
114 boost::spirit::qi::short_type short_;
115 if (phrase_parse(first, last, short_, skipper, s)) {
116 this->values_m.push_back(s);
117 return true;
118 }
119 break;
120 }
121 case ast::LONG:
122 {
123 long l = 0;
124 boost::spirit::qi::long_type long_;
125 if (phrase_parse(first, last, long_, skipper, l)) {
126 this->values_m.push_back(l);
127 return true;
128 }
129 break;
130 }
131 case ast::CHARACTER:
132 {
133 char c = '\0';
134 boost::spirit::qi::char_type char_;
135 if (phrase_parse(first, last, char_, skipper, c)) {
136 this->values_m.push_back(c);
137 return true;
138 }
139 break;
140 }
141 case ast::STRING:
142 {
143 std::string s("");
145 if (phrase_parse(first, last, qstring, skipper, s)) {
146 this->values_m.push_back(s);
147 return true;
148 }
149 break;
150 }
151 }
152 return false;
153 }
154 };
155
156 struct columnList: std::vector<column> {};
157
158 template <typename Iterator>
160 {
161 template <typename, typename>
162 struct result { typedef void type; };
163
164 void operator()(column& col, Iterator) const
165 {
166 col.order_m = column::count_m ++;
167 }
168 };
169
170 inline std::ostream& operator<<(std::ostream& out, const column& col) {
171 if (col.name_m) out << "name = " << *col.name_m << ", ";
172 if (col.type_m) out << "type = " << *col.type_m << ", ";
173 if (col.units_m) out << "units = " << *col.units_m << ", ";
174 if (col.description_m) out << "description = " << *col.description_m << ", ";
175 out << "order = " << col.order_m;
176
177 return out;
178 }
179}
180
183 (boost::optional<std::string>, name_m)
184 (boost::optional<SDDS::ast::datatype>, type_m)
185 (boost::optional<std::string>, units_m)
186 (boost::optional<std::string>, description_m)
187 (SDDS::ast::variant_t, value_m)
188)
189
190namespace SDDS { namespace parser
191{
192 namespace qi = boost::spirit::qi;
193 namespace ascii = boost::spirit::ascii;
194 namespace phx = boost::phoenix;
195
197 // The expression grammar
199 template <typename Iterator>
200 struct column_parser: qi::grammar<Iterator, column(), skipper<Iterator> >
201 {
202 column_parser(error_handler<Iterator> & _error_handler);
203
204 qi::rule<Iterator, std::string(), skipper<Iterator> > string, quoted_string, units;
205 qi::rule<Iterator, std::string(), skipper<Iterator> > column_name, column_units,
206 column_description, column_symbol, column_format;
207 qi::rule<Iterator, ast::datatype(), skipper<Iterator> > column_type;
208 qi::rule<Iterator, column(), skipper<Iterator> > start;
209 qi::rule<Iterator, long(), skipper<Iterator> > column_field;
210 qi::rule<Iterator, ast::nil(), skipper<Iterator> > column_unsupported_pre,
211 column_unsupported_post;
212 qi::symbols<char, ast::datatype> datatype;
213 };
214}}
215#endif /* COLUMN_HPP_ */
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
BOOST_FUSION_ADAPT_STRUCT(SDDS::column,(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: column.hpp:181
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:45
std::ostream & operator<<(std::ostream &out, const array &)
Definition: array.hpp:97
std::vector< variant_t > columnData_t
Definition: ast.hpp:50
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: column.hpp:47
boost::optional< std::string > units_m
Definition: column.hpp:48
boost::optional< ast::datatype > type_m
Definition: column.hpp:50
@ FIELD_LENGTH
Definition: column.hpp:43
@ FORMAT_STRING
Definition: column.hpp:41
unsigned int order_m
Definition: column.hpp:46
ast::columnData_t values_m
Definition: column.hpp:51
bool checkMandatories() const
Definition: column.hpp:54
bool parse(Iterator &first, Iterator last, Skipper const &skipper)
Definition: column.hpp:85
boost::optional< std::string > description_m
Definition: column.hpp:49
static unsigned int count_m
Definition: column.hpp:52
void operator()(column &col, Iterator) const
Definition: column.hpp:164