regression_optional_double.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #include <boost/config/warning_disable.hpp>
  7. #include <boost/spirit/include/karma.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. namespace client
  10. {
  11. namespace karma = boost::spirit::karma;
  12. template <typename OutputIterator>
  13. struct grammar
  14. : karma::grammar<OutputIterator, boost::optional<double>()>
  15. {
  16. grammar()
  17. : grammar::base_type(start)
  18. {
  19. using karma::double_;
  20. u = double_ << "U";
  21. start = ( !double_ << "NA" ) | u;
  22. start.name("start");
  23. u.name("u");
  24. }
  25. karma::rule<OutputIterator, double()> u;
  26. karma::rule<OutputIterator, boost::optional<double>()> start;
  27. };
  28. }
  29. int main()
  30. {
  31. namespace karma = boost::spirit::karma;
  32. typedef std::back_insert_iterator<std::string> sink_type;
  33. boost::optional<double> d1, d2;
  34. d2 = 1.0;
  35. std::string generated1, generated2;
  36. client::grammar<sink_type> g;
  37. BOOST_TEST(karma::generate(sink_type(generated1), g, d1) && generated1 == "NA");
  38. BOOST_TEST(karma::generate(sink_type(generated2), g, d2) && generated2 == "1.0U");
  39. return boost::report_errors();
  40. }