stream.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Copyright (c) 2011 Brian O'Kennedy
  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/config/warning_disable.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/spirit/include/qi_char.hpp>
  10. #include <boost/spirit/include/qi_numeric.hpp>
  11. #include <boost/spirit/include/qi_stream.hpp>
  12. #include <boost/spirit/include/qi_operator.hpp>
  13. #include "test.hpp"
  14. struct complex
  15. {
  16. complex (double a = 0.0, double b = 0.0) : a(a), b(b) {}
  17. double a, b;
  18. };
  19. std::istream& operator>> (std::istream& is, complex& z)
  20. {
  21. char lbrace = '\0', comma = '\0', rbrace = '\0';
  22. is >> lbrace >> z.a >> comma >> z.b >> rbrace;
  23. if (lbrace != '{' || comma != ',' || rbrace != '}')
  24. is.setstate(std::ios_base::failbit);
  25. return is;
  26. }
  27. int main()
  28. {
  29. using spirit_test::test_attr;
  30. {
  31. using boost::spirit::qi::blank;
  32. using boost::spirit::qi::double_;
  33. using boost::spirit::qi::stream;
  34. using boost::spirit::qi::stream_parser;
  35. using boost::fusion::at_c;
  36. complex c;
  37. BOOST_TEST(test_attr("{1.0,2.5}",
  38. stream_parser<char, complex>(), c, blank) &&
  39. c.a == 1.0 && c.b == 2.5);
  40. boost::fusion::vector<complex, double> d;
  41. BOOST_TEST(test_attr("{1.0,2.5},123.456",
  42. stream >> ',' >> double_, d, blank) &&
  43. at_c<0>(d).a == 1.0 && at_c<0>(d).b == 2.5 && at_c<1>(d) == 123.456);
  44. }
  45. return boost::report_errors();
  46. }