• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2009-2016 Vladimir Batov.
2 // Use, modification and distribution are subject to the Boost Software License,
3 // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
4 
5 #ifndef BOOST_CONVERT_SPIRIT_BASED_CONVERTER_HPP
6 #define BOOST_CONVERT_SPIRIT_BASED_CONVERTER_HPP
7 
8 #include <boost/convert/base.hpp>
9 #include <boost/convert/detail/config.hpp>
10 #include <boost/spirit/include/qi.hpp>
11 #include <boost/spirit/include/karma.hpp>
12 
13 namespace boost { namespace cnv
14 {
15     struct spirit;
16 }}
17 
18 struct boost::cnv::spirit : public boost::cnv::cnvbase<boost::cnv::spirit>
19 {
20     typedef boost::cnv::spirit             this_type;
21     typedef boost::cnv::cnvbase<this_type> base_type;
22 
23     using base_type::operator();
24 
25     template<typename string_type, typename out_type>
26     void
str_toboost::cnv::spirit27     str_to(cnv::range<string_type> range, optional<out_type>& result_out) const
28     {
29         typedef typename cnv::range<string_type>::iterator                  iterator;
30         typedef typename boost::spirit::traits::create_parser<out_type>::type parser;
31 
32         iterator    beg = range.begin();
33         iterator    end = range.end();
34         out_type result;
35 
36         if (boost::spirit::qi::parse(beg, end, parser(), result))
37             if (beg == end) // ensure the whole string has been parsed
38                 result_out = result;
39     }
40     template<typename in_type, typename char_type>
41     cnv::range<char_type*>
to_strboost::cnv::spirit42     to_str(in_type value_in, char_type* beg) const
43     {
44         typedef typename boost::spirit::traits::create_generator<in_type>::type generator;
45 
46         char_type* end = beg;
47         bool      good = boost::spirit::karma::generate(end, generator(), value_in);
48 
49         return cnv::range<char_type*>(beg, good ? end : beg);
50     }
51 };
52 
53 #endif // BOOST_CONVERT_SPIRIT_BASED_CONVERTER_HPP
54 
55