| 1 | // Copyright (c) 2010 Olaf Peter |
| 2 | // Copyright (c) 2001-2010 Hartmut Kaiser |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #include <boost/spirit/include/karma.hpp> |
| 8 | |
| 9 | #include <boost/core/lightweight_test.hpp> |
| 10 | |
| 11 | namespace client |
| 12 | { |
| 13 | namespace karma = boost::spirit::karma; |
| 14 | |
| 15 | template <typename OutputIterator> |
| 16 | struct grammar |
| 17 | : karma::grammar<OutputIterator, boost::optional<double>()> |
| 18 | { |
| 19 | grammar() |
| 20 | : grammar::base_type(start) |
| 21 | { |
| 22 | using karma::double_; |
| 23 | |
| 24 | u = double_ << "U" ; |
| 25 | start = ( !double_ << "NA" ) | u; |
| 26 | |
| 27 | start.name("start" ); |
| 28 | u.name("u" ); |
| 29 | } |
| 30 | |
| 31 | karma::rule<OutputIterator, double()> u; |
| 32 | karma::rule<OutputIterator, boost::optional<double>()> start; |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | int main() |
| 37 | { |
| 38 | namespace karma = boost::spirit::karma; |
| 39 | |
| 40 | typedef std::back_insert_iterator<std::string> sink_type; |
| 41 | |
| 42 | boost::optional<double> d1, d2; |
| 43 | d2 = 1.0; |
| 44 | |
| 45 | std::string generated1, generated2; |
| 46 | client::grammar<sink_type> g; |
| 47 | |
| 48 | BOOST_TEST(karma::generate(sink_type(generated1), g, d1) && generated1 == "NA" ); |
| 49 | BOOST_TEST(karma::generate(sink_type(generated2), g, d2) && generated2 == "1.0U" ); |
| 50 | |
| 51 | return boost::report_errors(); |
| 52 | } |
| 53 | |