1// Copyright (c) 2001-2011 Hartmut Kaiser
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6#include <boost/spirit/include/karma_omit.hpp>
7
8#include <boost/spirit/include/karma.hpp>
9#include <boost/fusion/include/std_pair.hpp>
10#include <boost/phoenix/core.hpp>
11#include <boost/phoenix/operator.hpp>
12
13#include <iostream>
14#include "test.hpp"
15
16using namespace spirit_test;
17
18int main()
19{
20 using boost::spirit::karma::_1;
21 using boost::spirit::karma::_a;
22 using boost::spirit::karma::double_;
23 using boost::spirit::karma::int_;
24 using boost::spirit::karma::omit;
25 using boost::spirit::karma::skip;
26 using boost::spirit::karma::rule;
27 using boost::spirit::karma::locals;
28
29 typedef spirit_test::output_iterator<char>::type outiter_type;
30
31 // even if omit[] never fails, it has to honor the result of the
32 // embedded generator
33 {
34 typedef std::pair<double, double> attribute_type;
35 rule<outiter_type, attribute_type()> r;
36
37 r %= omit[double_(1.0) << double_] | "42";
38
39 attribute_type p1 (1.0, 2.0);
40 BOOST_TEST(test("", r, p1));
41
42 attribute_type p2 (10.0, 2.0);
43 BOOST_TEST(test("42", r, p2));
44 }
45
46 typedef std::pair<std::vector<int>, int> attribute_type;
47 rule<outiter_type, attribute_type(), locals<int> > r;
48
49 attribute_type a;
50 a.first.push_back(x: 0x01);
51 a.first.push_back(x: 0x02);
52 a.first.push_back(x: 0x04);
53 a.first.push_back(x: 0x08);
54 a.second = 0;
55
56 // omit[] is supposed to execute the embedded generator
57 {
58 std::pair<double, double> p (1.0, 2.0);
59 BOOST_TEST(test("2.0", omit[double_] << double_, p));
60
61 r %= omit[ *(int_[_a = _a + _1]) ] << int_[_1 = _a];
62 BOOST_TEST(test("15", r, a));
63 }
64
65 // skip[] is not supposed to execute the embedded generator
66 {
67 using boost::spirit::karma::double_;
68 using boost::spirit::karma::skip;
69
70 std::pair<double, double> p (1.0, 2.0);
71 BOOST_TEST(test("2.0", skip[double_] << double_, p));
72
73 r %= skip[ *(int_[_a = _a + _1]) ] << int_[_1 = _a];
74 BOOST_TEST(test("0", r, a));
75 }
76
77 return boost::report_errors();
78}
79

source code of boost/libs/spirit/test/karma/omit.cpp