libpniio
value_generator.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2015 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: Feb 11, 2015
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 //
24 #pragma once
25 
26 #include <iterator>
27 #include <complex>
28 #include <pni/core/types.hpp>
29 #include <pni/core/type_erasures.hpp>
30 #include <boost/spirit/include/karma.hpp>
31 #include <boost/spirit/include/phoenix.hpp>
32 #include <boost/mpl/map.hpp>
33 
34 #include "primitive_generators.hpp"
35 
36 namespace pni{
37 namespace io{
38 
39 
51  template<
52  typename OITER,
53  typename VTYPE
54  >
55  struct value_generator : boost::spirit::karma::grammar<OITER,VTYPE()>
56  {
72  {
73  typedef std::back_insert_iterator<pni::core::string> iterator_type;
75 
79  template<typename Sig> struct result
80  {
81  typedef pni::core::string type;
82  };
83 
96  template<typename T>
97  static pni::core::string _to_string(const VTYPE &v)
98  {
99  typedef boost::mpl::at<generator_map,T> at_type;
100  typedef typename at_type::type generator_type;
101 
102  pni::core::string formatter_result;
103  iterator_type inserter(formatter_result);
104 
105  boost::spirit::karma::generate(inserter,
106  generator_type(),
107  v.template as<T>());
108 
109  return formatter_result;
110  }
111 
126  template<typename Arg>
127  pni::core::string operator()(const Arg &n) const
128  {
129  using namespace pni::core;
130 
131  switch(type_id(n))
132  {
133  case type_id_t::UINT8:
134  return _to_string<uint8>(n);
135  case type_id_t::INT8:
136  return _to_string<int8>(n);
137  case type_id_t::UINT16:
138  return _to_string<uint16>(n);
139  case type_id_t::INT16:
140  return _to_string<int16>(n);
141  case type_id_t::UINT32:
142  return _to_string<uint32>(n);
143  case type_id_t::INT32:
144  return _to_string<int32>(n);
145  case type_id_t::UINT64:
146  return _to_string<uint64>(n);
147  case type_id_t::INT64:
148  return _to_string<int64>(n);
149  case type_id_t::FLOAT32:
150  return _to_string<float32>(n);
151  case type_id_t::FLOAT64:
152  return _to_string<float64>(n);
153  case type_id_t::FLOAT128:
154  return _to_string<float128>(n);
155  case type_id_t::COMPLEX32:
156  return _to_string<complex32>(n);
157  case type_id_t::COMPLEX64:
158  return _to_string<complex64>(n);
159  case type_id_t::COMPLEX128:
160  return _to_string<complex128>(n);
161  case type_id_t::BOOL:
162  return _to_string<bool_t>(n);
163  case type_id_t::STRING:
164  return n.template as<string>();
165  case type_id_t::NONE:
166  return "NONE";
167  throw value_error(EXCEPTION_RECORD,
168  "Passed an uninitialized value!");
169  default:
170  throw type_error(EXCEPTION_RECORD,
171  "The value instances holds an unkown type!");
172 
173  }
174  }
175  };
176 
177 
178  //-------------------------------------------------------------------
180  boost::phoenix::function<lazy_to_string> to_string;
181 
183  boost::spirit::karma::rule<OITER,VTYPE()> value_rule;
184 
190  value_generator(): value_generator::base_type(value_rule)
191  {
192  using boost::spirit::karma::_1;
193  using boost::spirit::karma::_val;
194 
195  value_rule = boost::spirit::karma::string[_1 = to_string(_val)];
196  }
197 
198  };
199 
200 }
201 }
std::back_insert_iterator< pni::core::string > iterator_type
Definition: value_generator.hpp:73
generator for scalar type erasures
Definition: value_generator.hpp:55
pni::core::string operator()(const Arg &n) const
get real part
Definition: value_generator.hpp:127
boost::mpl::map< boost::mpl::pair< pni::core::uint8, pni_io_uint_generator< ITERT, pni::core::uint8 >>, boost::mpl::pair< pni::core::uint16, pni_io_uint_generator< ITERT, pni::core::uint16 >>, boost::mpl::pair< pni::core::uint32, pni_io_uint_generator< ITERT, pni::core::uint32 >>, boost::mpl::pair< pni::core::uint64, pni_io_uint_generator< ITERT, pni::core::uint64 >>, boost::mpl::pair< pni::core::int8, pni_io_int_generator< ITERT, pni::core::int8 >>, boost::mpl::pair< pni::core::int16, pni_io_int_generator< ITERT, pni::core::int16 >>, boost::mpl::pair< pni::core::int32, pni_io_int_generator< ITERT, pni::core::int32 >>, boost::mpl::pair< pni::core::int64, pni_io_int_generator< ITERT, pni::core::int64 >>, boost::mpl::pair< pni::core::float32, pni_io_real_generator< ITERT, pni::core::float32 >>, boost::mpl::pair< pni::core::float64, pni_io_real_generator< ITERT, pni::core::float64 >>, boost::mpl::pair< pni::core::float128, pni_io_real_generator< ITERT, pni::core::float128 >>, boost::mpl::pair< pni::core::bool_t, pni_io_bool_generator< ITERT, pni::core::bool_t >>, boost::mpl::pair< pni::core::complex32, pni_io_complex_generator< ITERT, pni::core::float32 >>, boost::mpl::pair< pni::core::complex64, pni_io_complex_generator< ITERT, pni::core::float64 >>, boost::mpl::pair< pni::core::complex128, pni_io_complex_generator< ITERT, pni::core::float128 >> > primitive_generators
Definition: primitive_generators.hpp:89
pni::core::string type
result type
Definition: value_generator.hpp:81
pni::core::type_id_t type_id(const h5datatype &o)
get type code
Definition: h5datatype.cpp:240
Definition: cbf_reader.hpp:41
result type of the lazy function
Definition: value_generator.hpp:79
boost::phoenix::function< lazy_to_string > to_string
instance of the get_real lazy function
Definition: value_generator.hpp:180
value_generator()
default constructor
Definition: value_generator.hpp:190
static pni::core::string _to_string(const VTYPE &v)
typed string conversion
Definition: value_generator.hpp:97
convert value type to a string
Definition: value_generator.hpp:71
primitive_generators< iterator_type > generator_map
Definition: value_generator.hpp:74
boost::spirit::karma::rule< OITER, VTYPE()> value_rule
total rule for complex numbers
Definition: value_generator.hpp:183