// Copyright (c) 2009-2020 Vladimir Batov. // Use, modification and distribution are subject to the Boost Software License, // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt. #ifndef BOOST_CONVERT_SPIRIT_BASED_CONVERTER_HPP #define BOOST_CONVERT_SPIRIT_BASED_CONVERTER_HPP #include #include #include #include namespace boost { namespace cnv { struct spirit; }} struct boost::cnv::spirit : boost::cnv::cnvbase { using this_type = boost::cnv::spirit; using base_type = boost::cnv::cnvbase; using base_type::operator(); template void str_to(cnv::range range, optional& result_out) const { using parser = typename boost::spirit::traits::create_parser::type; auto beg = range.begin(); auto end = range.end(); auto result = out_type(); if (boost::spirit::qi::parse(beg, end, parser(), result)) if (beg == end) // ensure the whole string has been parsed result_out = result; } template cnv::range to_str(in_type value_in, char_type* beg) const { using generator = typename boost::spirit::traits::create_generator::type; auto end = beg; bool good = boost::spirit::karma::generate(end, generator(), value_in); return cnv::range(beg, good ? end : beg); } }; #endif // BOOST_CONVERT_SPIRIT_BASED_CONVERTER_HPP