libpniio
value_rule.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2013 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpniio.
5 //
6 // libpniio is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpniio is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpniio. If not, see <http://www.gnu.org/licenses/>.
18 // ===========================================================================
19 //
20 // Created on: May 6, 2013
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 //
24 
25 #pragma once
26 
27 #include <pni/core/types.hpp>
28 #include <pni/core/type_erasures.hpp>
29 #include <boost/variant.hpp>
30 #include <boost/spirit/include/qi.hpp>
31 #include <boost/spirit/include/phoenix.hpp>
32 
33 #include "spirit_rules.hpp"
34 #include "complex_rule.hpp"
35 
36 namespace pni{
37 namespace io{
38 
39 
70  template<typename ITERT>
71  struct value_rule : boost::spirit::qi::grammar<ITERT,pni::core::value()>
72  {
73  //--------------------------------------------------------------------
74  // definition of internal classes used by the value_rule
75  //--------------------------------------------------------------------
88  public boost::static_visitor<pni::core::value>
89  {
90  public:
94  pni::core::value operator()(const pni::core::int64 &v) const
95  {
96  return pni::core::value(v);
97  }
98 
102  pni::core::value operator()(const pni::core::complex64 &v) const
103  {
104  return pni::core::value(v);
105  }
106 
110  pni::core::value operator()(const pni::core::float64 &v) const
111  {
112  return pni::core::value(v);
113  }
114  };
115 
116  //--------------------------------------------------------------------
124  {
126  template<typename Sig> struct result
127  {
129  typedef pni::core::value type;
130  };
131 
132  //-----------------------------------------------------------------
142  template<typename Arg>
143  pni::core::value operator()(Arg const &n) const
144  {
145  return boost::apply_visitor(value_constructor_visitor(),n);
146  }
147 
148  };
149 
150  //--------------------------------------------------------------------
151  // here starts the implementation of the rule
152  //--------------------------------------------------------------------
154  typename boost::mpl::at<
155  spirit_rules,
156  pni::core::int64
157  >::type integer_rule_;
158 
160  typename boost::mpl::at<
161  spirit_rules,
162  pni::core::float64
163  >::type float_rule_;
164 
168  boost::spirit::qi::rule<ITERT,pni::core::value()> value_;
170  boost::spirit::qi::rule<ITERT> sign_rule;
172  boost::spirit::qi::rule<ITERT> float_signs;
174  boost::phoenix::function<value_constructor> construct_value;
175 
176  //--------------------------------------------------------------------
178  value_rule() : value_rule::base_type(value_)
179  {
180  //using namespace pni::core;
181  using namespace boost::spirit::qi;
182  //using namespace boost::fusion;
183  using namespace boost::phoenix;
184  using boost::spirit::qi::_1;
185 
186  sign_rule = char_('+')|char_('-');
187  float_signs = char_('.')|char_('e');
188 
189  value_ = ((integer_rule_ >> !(float_signs|sign_rule))
190  |
191  (float_rule_ >> !(sign_rule))
192  |
193  complex_rule_)[_val = construct_value(_1)];
194  }
195  };
196 
197 //end of namespace
198 }
199 }
visitor for value construction
Definition: value_rule.hpp:87
pni::core::value operator()(const pni::core::complex64 &v) const
complex64 value construction
Definition: value_rule.hpp:102
value_rule()
default constructor
Definition: value_rule.hpp:178
pni::core::value operator()(const pni::core::float64 &v) const
float64 value construction
Definition: value_rule.hpp:110
pni::core::value operator()(const pni::core::int64 &v) const
int64 value construction
Definition: value_rule.hpp:94
pni::core::value type
the return type
Definition: value_rule.hpp:129
complex_rule< ITERT, pni::core::complex64 > complex_rule_
rule for parsing a complex number
Definition: value_rule.hpp:166
Definition: cbf_reader.hpp:41
boost::spirit::qi::rule< ITERT > sign_rule
rule to match the + or - sign
Definition: value_rule.hpp:170
boost::mpl::at< spirit_rules, pni::core::float64 >::type float_rule_
rule for parsing a floating point number
Definition: value_rule.hpp:163
determine the return type of the function
Definition: value_rule.hpp:126
boost::mpl::at< spirit_rules, pni::core::int64 >::type integer_rule_
rule for parsing an integer
Definition: value_rule.hpp:157
boost::mpl::map< boost::mpl::pair< pni::core::uint8, boost::spirit::qi::uint_parser< pni::core::uint8, 10, 1, 4 > >, boost::mpl::pair< pni::core::uint16, >, boost::mpl::pair< pni::core::uint32, >, boost::mpl::pair< pni::core::uint64, >, boost::mpl::pair< pni::core::int8, boost::spirit::qi::uint_parser< pni::core::int8, 10, 1, 4 > >, boost::mpl::pair< pni::core::int16, >, boost::mpl::pair< pni::core::int32, >, boost::mpl::pair< pni::core::int64, >, boost::mpl::pair< core::float32, >, boost::mpl::pair< pni::core::float64, >, boost::mpl::pair< pni::core::float128, >, boost::mpl::pair< pni::core::bool_t, >, boost::mpl::pair< bool, > > spirit_rules
spirit rule map
Definition: spirit_rules.hpp:110
value construction lazy function
Definition: value_rule.hpp:123
value rule
Definition: value_rule.hpp:71
boost::spirit::qi::rule< ITERT, pni::core::value()> value_
value reading rule
Definition: value_rule.hpp:168
pni::core::value operator()(Arg const &n) const
lazy function implementation
Definition: value_rule.hpp:143
boost::phoenix::function< value_constructor > construct_value
lazy function to assemble a value instance
Definition: value_rule.hpp:174
boost::spirit::qi::rule< ITERT > float_signs
rule to match . or e used to denote a floating point number
Definition: value_rule.hpp:172