OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
column.hpp
Go to the documentation of this file.
1 //
2 // Copyright & License: See Copyright.readme in src directory
3 //
4 
9 #ifndef COLUMN_HPP_
10 #define COLUMN_HPP_
11 
12 #include "ast.hpp"
13 #include "skipper.hpp"
14 #include "error_handler.hpp"
15 
16 #include <boost/config/warning_disable.hpp>
17 #include <boost/spirit/include/qi.hpp>
18 #include <boost/fusion/include/adapt_struct.hpp>
19 #include <boost/optional.hpp>
20 
21 #include <vector>
22 
23 #define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
24 #define BOOST_SPIRIT_QI_DEBUG
25 
26 namespace SDDS {
27  struct column
28  {
34  , TYPE
36  };
37 
38  unsigned int order_m;
39  boost::optional<std::string> name_m;
40  boost::optional<std::string> units_m;
41  boost::optional<std::string> description_m;
42  boost::optional<ast::datatype> type_m;
44  static unsigned int count_m;
45 
46  bool checkMandatories() const
47  {
48  return name_m && type_m;
49  }
50 
51  template <attributes A>
53  {
54  static bool apply()
55  {
56  std::string attributeString;
57  switch(A)
58  {
59  case SYMBOL:
60  attributeString = "symbol";
61  break;
62  case FORMAT_STRING:
63  attributeString = "format_string";
64  break;
65  case FIELD_LENGTH:
66  attributeString = "field_length";
67  break;
68  default:
69  return true;
70  }
71  std::cerr << attributeString << " not supported yet" << std::endl;
72  return false;
73  }
74  };
75 
76  template <typename Iterator, typename Skipper>
77  bool parse(
78  Iterator& first
79  , Iterator last
80  , Skipper const& skipper)
81  {
82  switch(*this->type_m) {
83  case ast::FLOAT:
84  {
85  float f = 0.0;
86  boost::spirit::qi::float_type float_;
87  if (phrase_parse(first, last, float_, skipper, f)) {
88  this->values_m.push_back(f);
89  return true;
90  }
91  break;
92  }
93  case ast::DOUBLE:
94  {
95  double d = 0.0;
96  boost::spirit::qi::double_type double_;
97  if (phrase_parse(first, last, double_, skipper, d)) {
98  this->values_m.push_back(d);
99  return true;
100  }
101  break;
102  }
103  case ast::SHORT:
104  {
105  short s = 0;
106  boost::spirit::qi::short_type short_;
107  if (phrase_parse(first, last, short_, skipper, s)) {
108  this->values_m.push_back(s);
109  return true;
110  }
111  break;
112  }
113  case ast::LONG:
114  {
115  long l = 0;
116  boost::spirit::qi::long_type long_;
117  if (phrase_parse(first, last, long_, skipper, l)) {
118  this->values_m.push_back(l);
119  return true;
120  }
121  break;
122  }
123  case ast::CHARACTER:
124  {
125  char c = '\0';
126  boost::spirit::qi::char_type char_;
127  if (phrase_parse(first, last, char_, skipper, c)) {
128  this->values_m.push_back(c);
129  return true;
130  }
131  break;
132  }
133  case ast::STRING:
134  {
135  std::string s("");
137  if (phrase_parse(first, last, qstring, skipper, s)) {
138  this->values_m.push_back(s);
139  return true;
140  }
141  break;
142  }
143  }
144  return false;
145  }
146  };
147 
148  struct columnList: std::vector<column> {};
149 
150  template <typename Iterator>
151  struct columnOrder
152  {
153  template <typename, typename>
154  struct result { typedef void type; };
155 
156  void operator()(column& col, Iterator) const
157  {
158  col.order_m = column::count_m ++;
159  }
160  };
161 
162  inline std::ostream& operator<<(std::ostream& out, const column& col) {
163  if (col.name_m) out << "name = " << *col.name_m << ", ";
164  if (col.type_m) out << "type = " << *col.type_m << ", ";
165  if (col.units_m) out << "units = " << *col.units_m << ", ";
166  if (col.description_m) out << "description = " << *col.description_m << ", ";
167  out << "order = " << col.order_m;
168 
169  return out;
170  }
171 }
172 
174  SDDS::column,
175  (boost::optional<std::string>, name_m)
176  (boost::optional<SDDS::ast::datatype>, type_m)
177  (boost::optional<std::string>, units_m)
178  (boost::optional<std::string>, description_m)
179  (SDDS::ast::variant_t, value_m)
180 )
181 
182 namespace SDDS { namespace parser
183 {
184  namespace qi = boost::spirit::qi;
185  namespace ascii = boost::spirit::ascii;
186  namespace phx = boost::phoenix;
187 
189  // The expression grammar
191  template <typename Iterator>
192  struct column_parser: qi::grammar<Iterator, column(), skipper<Iterator> >
193  {
194  column_parser(error_handler<Iterator> & _error_handler);
195 
196  qi::rule<Iterator, std::string(), skipper<Iterator> > string, quoted_string, units;
197  qi::rule<Iterator, std::string(), skipper<Iterator> > column_name, column_units,
198  column_description, column_symbol, column_format;
199  qi::rule<Iterator, ast::datatype(), skipper<Iterator> > column_type;
200  qi::rule<Iterator, column(), skipper<Iterator> > start;
201  qi::rule<Iterator, long(), skipper<Iterator> > column_field;
202  qi::rule<Iterator, ast::nil(), skipper<Iterator> > column_unsupported_pre,
203  column_unsupported_post;
204  qi::symbols<char, ast::datatype> datatype;
205  };
206 }}
207 #endif /* COLUMN_HPP_ */
boost::optional< std::string > name_m
Definition: column.hpp:39
ast::columnData_t values_m
Definition: column.hpp:43
bool checkMandatories() const
Definition: column.hpp:46
bool parse(Iterator &first, Iterator last, Skipper const &skipper)
Definition: column.hpp:77
static unsigned int count_m
Definition: column.hpp:44
boost::optional< std::string > units_m
Definition: column.hpp:40
boost::optional< ast::datatype > type_m
Definition: column.hpp:42
boost::optional< std::string > description_m
Definition: column.hpp:41
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
void operator()(column &col, Iterator) const
Definition: column.hpp:156
boost::variant< float, double, short, long, char, std::string > variant_t
Definition: ast.hpp:40
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:173
std::vector< variant_t > columnData_t
Definition: ast.hpp:42
unsigned int order_m
Definition: column.hpp:38
std::ostream & operator<<(std::ostream &out, const array &)
Definition: array.hpp:89
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
datatype
Definition: ast.hpp:20