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.
5 #ifndef BOOST_CONVERT_SPIRIT_BASED_CONVERTER_HPP
6 #define BOOST_CONVERT_SPIRIT_BASED_CONVERTER_HPP
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>
13 namespace boost { namespace cnv
18 struct boost::cnv::spirit : public boost::cnv::cnvbase<boost::cnv::spirit>
20 typedef boost::cnv::spirit this_type;
21 typedef boost::cnv::cnvbase<this_type> base_type;
23 using base_type::operator();
25 template<typename string_type, typename out_type>
27 str_to(cnv::range<string_type> range, optional<out_type>& result_out) const
29 typedef typename cnv::range<string_type>::iterator iterator;
30 typedef typename boost::spirit::traits::create_parser<out_type>::type parser;
32 iterator beg = range.begin();
33 iterator end = range.end();
36 if (boost::spirit::qi::parse(beg, end, parser(), result))
37 if (beg == end) // ensure the whole string has been parsed
40 template<typename in_type, typename char_type>
41 cnv::range<char_type*>
42 to_str(in_type value_in, char_type* beg) const
44 typedef typename boost::spirit::traits::create_generator<in_type>::type generator;
47 bool good = boost::spirit::karma::generate(end, generator(), value_in);
49 return cnv::range<char_type*>(beg, good ? end : beg);
53 #endif // BOOST_CONVERT_SPIRIT_BASED_CONVERTER_HPP