libpniio
vector_parser.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 #pragma once
25 
26 #include <sstream>
27 
28 #include<pni/core/types.hpp>
29 #include<pni/core/error.hpp>
30 #include<vector>
31 
32 #include "../exceptions.hpp"
33 #include "conversion_trait.hpp"
34 #include "delimiter_rule.hpp"
35 #include "parser.hpp"
36 #include "get_sequence_rule.hpp"
37 #include "../container_io_config.hpp"
38 
39 
40 namespace pni{
41 namespace io{
42 
43  //------------------------------------------------------------------------
62  template<
63  typename T,
64  typename ITERT
65  >
66  class parser<std::vector<T>,ITERT>
67  {
68  public:
70  typedef std::vector<T> result_type;
72  typedef ITERT iterator_type;
74  typedef boost::spirit::qi::expectation_failure<iterator_type>
76  private:
80  typedef typename trait_type::read_type read_type;
82  typedef std::vector<read_type> buffer_type;
86  typedef std::unique_ptr<rule_type> rule_ptr;
88  rule_ptr sequence_;
89 
90  static rule_ptr get_rule_from_config(const container_io_config &c)
91  {
92  if(c.separator() && c.start_symbol() && c.stop_symbol())
93  return rule_ptr(new rule_type(c.start_symbol(),
94  c.stop_symbol(),
95  c.separator()));
96  else if(c.start_symbol() && c.stop_symbol() && !c.separator())
97  return rule_ptr(new rule_type(c.start_symbol(),
98  c.stop_symbol()));
99  else if(c.separator() && !c.start_symbol() && !c.stop_symbol())
100  return rule_ptr(new rule_type(c.separator()));
101  else
102  return rule_ptr(new rule_type());
103  }
104 
105  public:
106  //-----------------------------------------------------------------
110  parser(const container_io_config &config =
112  : sequence_(get_rule_from_config(config))
113  {}
114 
115  //-----------------------------------------------------------------
127  result_type operator()(const core::string &s) const
128  {
129  using namespace pni::core;
130  using namespace boost::spirit;
131 
132  buffer_type container;
133 
134  try
135  {
136  if(!qi::parse(s.begin(),s.end(),(*sequence_)>qi::eoi,container))
137  {
138  throw parser_error(EXCEPTION_RECORD,
139  "Error parsing sequence!");
140  }
141  }
142  catch(...)
143  {
144  throw parser_error(EXCEPTION_RECORD,
145  "Cannot parse array data from string: "
146  "\""+s+"\"!");
147  }
148 
149  //perform the conversion to the requested type - maybe not the
150  //best solution - but should be sufficient for now.
151  //Remove this code when we found a better way to handle (u)int8
152  //values.
153  try
154  {
155  result_type result;
156  for(auto e: container)
157  result.push_back(trait_type::convert(e));
158 
159  return result;
160 
161  }
162  catch(...)
163  {
164  throw parser_error(EXCEPTION_RECORD,
165  "Error during type conversion!");
166  }
167  }
168  };
169 
170 
171 //end of namespace
172 }
173 }
default conversion trait
Definition: conversion_trait.hpp:46
std::vector< read_type > buffer_type
buffer type used for reading
Definition: vector_parser.hpp:82
conversion_trait< T > trait_type
conversion trait used during parsing
Definition: vector_parser.hpp:78
container IO configuration
Definition: container_io_config.hpp:66
general parser related error
Definition: exceptions.hpp:99
static result_type convert(read_type &&v)
conversion function
Definition: conversion_trait.hpp:61
STL namespace.
ITERT iterator_type
input iterator type
Definition: vector_parser.hpp:72
Definition: spirit_container_traits.hpp:33
parser for primitive types
Definition: parsers/parser.hpp:62
container parser
Definition: sequence_rule.hpp:59
static rule_ptr get_rule_from_config(const container_io_config &c)
Definition: vector_parser.hpp:90
Definition: cbf_reader.hpp:41
std::unique_ptr< rule_type > rule_ptr
Definition: vector_parser.hpp:86
char separator() const
get separator symbol
Definition: container_io_config.cpp:60
get_sequence_rule< ITERT, buffer_type >::type rule_type
rule type
Definition: vector_parser.hpp:85
std::vector< T > result_type
result type for the parser
Definition: vector_parser.hpp:70
char start_symbol() const
get start symbol
Definition: container_io_config.cpp:66
boost::spirit::qi::expectation_failure< iterator_type > expectation_error
parser exception type
Definition: vector_parser.hpp:75
rule_ptr sequence_
rule type to parse the sequence
Definition: vector_parser.hpp:88
T read_type
the type to read
Definition: conversion_trait.hpp:51
result_type operator()(const core::string &s) const
parse a string
Definition: vector_parser.hpp:127
parser(const container_io_config &config=container_io_config())
default constructor
Definition: vector_parser.hpp:110
trait_type::read_type read_type
data type to read
Definition: vector_parser.hpp:80
char stop_symbol() const
get stop symbol
Definition: container_io_config.cpp:72