1/*=============================================================================
2 Copyright (c) 2001-2011 Hartmut Kaiser
3 Copyright (c) 2011 Brian O'Kennedy
4
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7=============================================================================*/
8#include <boost/spirit/include/qi_stream.hpp>
9
10#include <boost/spirit/include/qi_char.hpp>
11#include <boost/spirit/include/qi_numeric.hpp>
12#include <boost/spirit/include/qi_operator.hpp>
13
14#include "test.hpp"
15
16struct complex
17{
18 complex (double a = 0.0, double b = 0.0) : a(a), b(b) {}
19 double a, b;
20};
21
22std::istream& operator>> (std::istream& is, complex& z)
23{
24 char lbrace = '\0', comma = '\0', rbrace = '\0';
25 is >> lbrace >> z.a >> comma >> z.b >> rbrace;
26 if (lbrace != '{' || comma != ',' || rbrace != '}')
27 is.setstate(std::ios_base::failbit);
28
29 return is;
30}
31
32int main()
33{
34 using spirit_test::test_attr;
35
36 {
37 using boost::spirit::qi::blank;
38 using boost::spirit::qi::double_;
39 using boost::spirit::qi::stream;
40 using boost::spirit::qi::stream_parser;
41 using boost::fusion::at_c;
42
43 complex c;
44 BOOST_TEST(test_attr("{1.0,2.5}",
45 stream_parser<char, complex>(), c, blank) &&
46 c.a == 1.0 && c.b == 2.5);
47
48 boost::variant<complex, double> cd;
49 BOOST_TEST(test_attr("{1.0",
50 stream_parser<char, complex>() | "{" >> double_, cd, blank) &&
51 boost::get<double>(cd) == 1.0);
52
53 boost::fusion::vector<complex, double> d;
54 BOOST_TEST(test_attr("{1.0,2.5},123.456",
55 stream >> ',' >> double_, d, blank) &&
56 at_c<0>(d).a == 1.0 && at_c<0>(d).b == 2.5 && at_c<1>(d) == 123.456);
57 }
58
59 return boost::report_errors();
60}
61
62
63

source code of boost/libs/spirit/test/qi/stream.cpp