OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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
28#include <iostream>
29#include <optional>
30#include <ostream>
31#include <string>
32#include <vector>
33
34#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
35#define BOOST_SPIRIT_QI_DEBUG
36
37namespace SDDS {
38 struct column
39 {
48
49 unsigned int order_m;
50 std::optional<std::string> name_m;
51 std::optional<std::string> units_m;
52 std::optional<std::string> description_m;
53 std::optional<ast::datatype> type_m;
55 static unsigned int count_m;
56
57 bool checkMandatories() const
58 {
59 return name_m && type_m;
60 }
61
62 template <attributes A>
64 {
65 static bool apply()
66 {
67 std::string attributeString;
68 switch(A)
69 {
70 case SYMBOL:
71 attributeString = "symbol";
72 break;
73 case FORMAT_STRING:
74 attributeString = "format_string";
75 break;
76 case FIELD_LENGTH:
77 attributeString = "field_length";
78 break;
79 default:
80 return true;
81 }
82 std::cerr << attributeString << " not supported yet" << std::endl;
83 return false;
84 }
85 };
86
87 template <typename Iterator, typename Skipper>
88 bool parse(
89 Iterator& first
90 , Iterator last
91 , Skipper const& skipper)
92 {
93 switch(*this->type_m) {
94 case ast::FLOAT:
95 {
96 float f = 0.0;
97 boost::spirit::qi::float_type float_;
98 if (phrase_parse(first, last, float_, skipper, f)) {
99 this->values_m.push_back(f);
100 return true;
101 }
102 break;
103 }
104 case ast::DOUBLE:
105 {
106 double d = 0.0;
107 boost::spirit::qi::double_type double_;
108 if (phrase_parse(first, last, double_, skipper, d)) {
109 this->values_m.push_back(d);
110 return true;
111 }
112 break;
113 }
114 case ast::SHORT:
115 {
116 short s = 0;
117 boost::spirit::qi::short_type short_;
118 if (phrase_parse(first, last, short_, skipper, s)) {
119 this->values_m.push_back(s);
120 return true;
121 }
122 break;
123 }
124 case ast::LONG:
125 {
126 long l = 0;
127 boost::spirit::qi::long_type long_;
128 if (phrase_parse(first, last, long_, skipper, l)) {
129 this->values_m.push_back(l);
130 return true;
131 }
132 break;
133 }
134 case ast::CHARACTER:
135 {
136 char c = '\0';
137 boost::spirit::qi::char_type char_;
138 if (phrase_parse(first, last, char_, skipper, c)) {
139 this->values_m.push_back(c);
140 return true;
141 }
142 break;
143 }
144 case ast::STRING:
145 {
146 std::string s("");
148 if (phrase_parse(first, last, qstring, skipper, s)) {
149 this->values_m.push_back(s);
150 return true;
151 }
152 break;
153 }
154 }
155 return false;
156 }
157 };
158
159 struct columnList: std::vector<column> {};
160
161 template <typename Iterator>
163 {
164 template <typename, typename>
165 struct result { typedef void type; };
166
167 void operator()(column& col, Iterator) const
168 {
169 col.order_m = column::count_m ++;
170 }
171 };
172
173 inline std::ostream& operator<<(std::ostream& out, const column& col) {
174 if (col.name_m) out << "name = " << *col.name_m << ", ";
175 if (col.type_m) out << "type = " << *col.type_m << ", ";
176 if (col.units_m) out << "units = " << *col.units_m << ", ";
177 if (col.description_m) out << "description = " << *col.description_m << ", ";
178 out << "order = " << col.order_m;
179
180 return out;
181 }
182}
183
186 (std::optional<std::string>, name_m)
187 (std::optional<SDDS::ast::datatype>, type_m)
188 (std::optional<std::string>, units_m)
189 (std::optional<std::string>, description_m)
190 (SDDS::ast::variant_t, value_m)
191)
192
193namespace SDDS { namespace parser
194{
195 namespace qi = boost::spirit::qi;
196 namespace ascii = boost::spirit::ascii;
197 namespace phx = boost::phoenix;
198
200 // The expression grammar
202 template <typename Iterator>
203 struct column_parser: qi::grammar<Iterator, column(), skipper<Iterator> >
204 {
205 column_parser(error_handler<Iterator> & _error_handler);
206
207 qi::rule<Iterator, std::string(), skipper<Iterator> > string, quoted_string, units;
208 qi::rule<Iterator, std::string(), skipper<Iterator> > column_name, column_units,
209 column_description, column_symbol, column_format;
210 qi::rule<Iterator, ast::datatype(), skipper<Iterator> > column_type;
211 qi::rule<Iterator, column(), skipper<Iterator> > start;
212 qi::rule<Iterator, long(), skipper<Iterator> > column_field;
213 qi::rule<Iterator, ast::nil(), skipper<Iterator> > column_unsupported_pre,
214 column_unsupported_post;
215 qi::symbols<char, ast::datatype> datatype;
216 };
217}}
218#endif /* COLUMN_HPP_ */
BOOST_FUSION_ADAPT_STRUCT(SDDS::column,(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
Definition column.hpp:184
std::ostream & operator<<(std::ostream &out, const array &)
Definition array.hpp:97
std::vector< variant_t > columnData_t
Definition ast.hpp:50
@ 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 column.hpp:50
std::optional< ast::datatype > type_m
Definition column.hpp:53
unsigned int order_m
Definition column.hpp:49
ast::columnData_t values_m
Definition column.hpp:54
bool checkMandatories() const
Definition column.hpp:57
bool parse(Iterator &first, Iterator last, Skipper const &skipper)
Definition column.hpp:88
std::optional< std::string > units_m
Definition column.hpp:51
std::optional< std::string > description_m
Definition column.hpp:52
static unsigned int count_m
Definition column.hpp:55
void operator()(column &col, Iterator) const
Definition column.hpp:167