libpniio
formatter.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 <vector>
27 #include <iterator>
28 #include <pni/core/types.hpp>
29 #include <pni/core/type_erasures.hpp>
30 #include <boost/spirit/include/karma.hpp>
31 
32 #include "../container_io_config.hpp"
33 #include "get_generator.hpp"
35 
36 namespace pni{
37 namespace io{
38 
54  template<typename T>
55  class formatter
56  {
57  private:
59  typedef std::back_insert_iterator<pni::core::string> iterator_type;
62 
64  generator_type generator;
65 
66  public:
67 
77  pni::core::string operator()(const T &v) const
78  {
79  using namespace boost::spirit;
80  pni::core::string buffer;
81  iterator_type inserter(buffer);
82 
83  karma::generate(inserter,generator,v);
84 
85  return buffer;
86  }
87  };
88 
89  //-------------------------------------------------------------------------
98  template<> class formatter<pni::core::string>
99  {
100  public:
101  pni::core::string operator()(const pni::core::string &v) const
102  {
103  return v;
104  }
105  };
106 
107  //-------------------------------------------------------------------------
115  template<> class formatter<pni::core::value_ref>
116  {
117  public:
118  pni::core::string operator()(const pni::core::value_ref &v) const
119  {
120  return formatter<pni::core::value>()(pni::core::to_value(v));
121  }
122  };
123 
124  //-------------------------------------------------------------------------
136  template<typename CTYPE>
138  {
139  private:
140  typedef CTYPE container_type;
142  typedef std::back_insert_iterator<pni::core::string> iterator_type;
144  typedef typename container_type::value_type value_type;
149  generator_type generator;
151  public:
154  _config(config)
155  { }
156 
166  pni::core::string operator()(const container_type &v) const
167  {
168  using namespace boost::spirit;
169  core::string buffer;
170  iterator_type inserter(buffer);
171 
172  auto sep_rule = karma::char_(_config.separator());
173  auto cont_rule = generator % sep_rule;
174 
175  if(_config.start_symbol() && _config.stop_symbol())
176  {
177  karma::generate(inserter,_config.start_symbol()<<
178  cont_rule<<
179  _config.stop_symbol(),v);
180  }
181  else
182  {
183  karma::generate(inserter,cont_rule,v);
184  }
185 
186  return buffer;
187  }
188  };
189 
190 
191  //------------------------------------------------------------------------
201  template<typename T>
202  class formatter<std::vector<T>>
203  {
204  private:
206  formatter_type f;
207 
208  public:
211  f(config)
212  {}
213 
222  core::string operator()(const std::vector<T> &v) const
223  {
224  return f(v);
225  }
226  };
227 
228  //-------------------------------------------------------------------------
236  template<>
237  class formatter<pni::core::array>
238  {
239  private:
240  typedef std::vector<pni::core::value> container_type;
242  formatter_type f;
243 
244  public:
247  f(config)
248  {}
249 
250  //-----------------------------------------------------------------
251  core::string operator()(const pni::core::array &v) const
252  {
253  //there seems to be a crucial problem with the array iterator
254  //have not figured this out yet. However, we should use it
255  //with care!
256  container_type c(v.size());
257  std::copy(v.begin(),v.end(),c.begin());
258  return f(c);
259  }
260 
261  };
262 
263  //-------------------------------------------------------------------------
273  template<typename ...OTYPES>
274  class formatter<pni::core::mdarray<OTYPES...>>
275  {
276  private:
277  typedef pni::core::mdarray<OTYPES...> container_type;
279  formatter_type f;
280 
281  public:
284  f(config)
285  {}
286 
295  core::string operator()(const pni::core::mdarray<OTYPES...> &v) const
296  {
297  return f(v);
298  }
299 
300  };
301 
302  //-------------------------------------------------------------------------
309  template<>
310  class formatter<std::vector<pni::core::string>>
311  {
312  private:
314  typedef std::back_insert_iterator<pni::core::string> iterator_type;
315  public:
316 
325  pni::core::string operator()(const std::vector<pni::core::string> &v) const
326  {
327  using namespace boost::spirit;
328  pni::core::string buffer;
329  iterator_type inserter(buffer);
330 
331  karma::generate(inserter,karma::string % '\n',v);
332 
333  return buffer;
334  }
335  };
336 
337 
338 
339 //end of namespace
340 }
341 }
getter_type::type generator_type
Definition: formatter.hpp:147
generator_type generator
generator instance
Definition: formatter.hpp:64
formatter_type f
Definition: formatter.hpp:279
formatter for containers
Definition: formatter.hpp:137
core::string operator()(const pni::core::array &v) const
Definition: formatter.hpp:251
container IO configuration
Definition: container_io_config.hpp:66
STL namespace.
formatter(const container_io_config &config=container_io_config())
Definition: formatter.hpp:245
pni::core::string operator()(const pni::core::string &v) const
Definition: formatter.hpp:101
Definition: spirit_container_traits.hpp:33
container_formatter(const container_io_config &config=container_io_config())
Definition: formatter.hpp:152
scalar formatter
Definition: formatter.hpp:55
std::vector< pni::core::value > container_type
Definition: formatter.hpp:240
core::string operator()(const std::vector< T > &v) const
generate output
Definition: formatter.hpp:222
Definition: cbf_reader.hpp:41
generator_type generator
generator instance
Definition: formatter.hpp:149
formatter_type f
Definition: formatter.hpp:206
pni::core::string operator()(const std::vector< pni::core::string > &v) const
generate output
Definition: formatter.hpp:325
char separator() const
get separator symbol
Definition: container_io_config.cpp:60
char start_symbol() const
get start symbol
Definition: container_io_config.cpp:66
get generator for type
Definition: get_generator.hpp:44
formatter_type f
Definition: formatter.hpp:242
CTYPE container_type
Definition: formatter.hpp:140
formatter(const container_io_config &config=container_io_config())
Definition: formatter.hpp:282
get_generator< iterator_type, value_type > getter_type
generator type
Definition: formatter.hpp:146
get_generator< iterator_type, T >::type generator_type
generator type
Definition: formatter.hpp:61
formatter(const container_io_config &config=container_io_config())
Definition: formatter.hpp:209
std::back_insert_iterator< pni::core::string > iterator_type
output iterator type
Definition: formatter.hpp:142
pni::core::string operator()(const T &v) const
generate output
Definition: formatter.hpp:77
container_formatter< container_type > formatter_type
Definition: formatter.hpp:241
pni::core::mdarray< OTYPES...> container_type
Definition: formatter.hpp:277
pni::core::string operator()(const pni::core::value_ref &v) const
Definition: formatter.hpp:118
container_io_config _config
Definition: formatter.hpp:150
std::back_insert_iterator< pni::core::string > iterator_type
output iterator type
Definition: formatter.hpp:314
container_formatter< std::vector< T > > formatter_type
Definition: formatter.hpp:205
std::back_insert_iterator< pni::core::string > iterator_type
output iterator type
Definition: formatter.hpp:59
container_formatter< container_type > formatter_type
Definition: formatter.hpp:278
core::string operator()(const pni::core::mdarray< OTYPES...> &v) const
generate output
Definition: formatter.hpp:295
container_type::value_type value_type
element type of the container
Definition: formatter.hpp:144
char stop_symbol() const
get stop symbol
Definition: container_io_config.cpp:72
pni::core::string operator()(const container_type &v) const
generate output
Definition: formatter.hpp:166
boost::mpl::at< primitive_generators< ITERT >, T >::type type
generator type
Definition: get_generator.hpp:47