]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2001-2011 Joel de Guzman | |
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 | =============================================================================*/ | |
1e59de90 TL |
7 | #include <boost/spirit/include/qi_optional.hpp> |
8 | ||
7c673cae FG |
9 | #include <boost/spirit/include/qi_char.hpp> |
10 | #include <boost/spirit/include/qi_numeric.hpp> | |
11 | #include <boost/spirit/include/qi_operator.hpp> | |
12 | #include <boost/spirit/include/qi_action.hpp> | |
13 | #include <boost/spirit/include/qi_directive.hpp> | |
14 | #include <boost/spirit/include/support_argument.hpp> | |
1e59de90 TL |
15 | #include <boost/phoenix/core.hpp> |
16 | #include <boost/phoenix/operator.hpp> | |
7c673cae FG |
17 | #include <boost/fusion/adapted/struct.hpp> |
18 | ||
19 | #include <iostream> | |
20 | #include "test.hpp" | |
21 | ||
1e59de90 TL |
22 | #ifdef _MSC_VER |
23 | // bogus https://developercommunity.visualstudio.com/t/buggy-warning-c4709/471956 | |
24 | # pragma warning(disable: 4709) // comma operator within array index expression | |
25 | #endif | |
26 | ||
7c673cae FG |
27 | struct adata |
28 | { | |
29 | int a; | |
30 | boost::optional<int> b; | |
31 | }; | |
32 | ||
33 | BOOST_FUSION_ADAPT_STRUCT( | |
34 | adata, | |
35 | (int, a) | |
36 | (boost::optional<int>, b) | |
37 | ) | |
38 | ||
39 | struct test_attribute_type | |
40 | { | |
41 | template <typename Attribute, typename Context> | |
42 | void operator()(Attribute&, Context&, bool&) const | |
43 | { | |
44 | BOOST_TEST(typeid(Attribute).name() == typeid(boost::optional<int>).name()); | |
45 | } | |
46 | }; | |
47 | ||
48 | int | |
49 | main() | |
50 | { | |
51 | using spirit_test::test; | |
52 | using spirit_test::test_attr; | |
53 | ||
54 | using boost::spirit::qi::_1; | |
55 | using boost::spirit::qi::int_; | |
56 | using boost::spirit::qi::omit; | |
57 | using boost::spirit::ascii::char_; | |
58 | ||
59 | { | |
60 | BOOST_TEST((test("1234", -int_))); | |
61 | BOOST_TEST((test("abcd", -int_, false))); | |
20effc67 TL |
62 | |
63 | boost::optional<int> n; | |
64 | BOOST_TEST(test_attr("", -int_, n)) | |
65 | && BOOST_TEST(!n); | |
66 | BOOST_TEST(test_attr("123", -int_, n)) | |
67 | && BOOST_TEST(n) && BOOST_TEST_EQ(*n, 123); | |
68 | ||
69 | boost::optional<std::string> s; | |
70 | BOOST_TEST(test_attr("", -+char_, s)) | |
71 | && BOOST_TEST(!s); | |
72 | BOOST_TEST(test_attr("abc", -+char_, s)) | |
73 | && BOOST_TEST(s) && BOOST_TEST_EQ(*s, "abc"); | |
7c673cae FG |
74 | } |
75 | ||
76 | { // test propagation of unused | |
77 | using boost::fusion::at_c; | |
78 | using boost::fusion::vector; | |
79 | ||
80 | vector<char, char> v; | |
81 | BOOST_TEST((test_attr("a1234c", char_ >> -omit[int_] >> char_, v))); | |
82 | BOOST_TEST((at_c<0>(v) == 'a')); | |
83 | BOOST_TEST((at_c<1>(v) == 'c')); | |
84 | ||
85 | v = boost::fusion::vector<char, char>(); | |
86 | BOOST_TEST((test_attr("a1234c", char_ >> omit[-int_] >> char_, v))); | |
87 | BOOST_TEST((at_c<0>(v) == 'a')); | |
88 | BOOST_TEST((at_c<1>(v) == 'c')); | |
89 | ||
90 | char ch; | |
91 | BOOST_TEST((test_attr(",c", -(',' >> char_), ch))); | |
92 | BOOST_TEST((ch == 'c')); | |
93 | } | |
94 | ||
95 | { // test action | |
96 | boost::optional<int> n = 0; | |
97 | BOOST_TEST((test_attr("1234", (-int_)[test_attribute_type()], n))); | |
98 | BOOST_TEST((n.get() == 1234)); | |
99 | } | |
100 | ||
101 | { | |
102 | std::string s; | |
103 | BOOST_TEST((test_attr("abc", char_ >> -(char_ >> char_), s))); | |
104 | BOOST_TEST(s == "abc"); | |
105 | } | |
106 | ||
107 | { | |
108 | namespace phx = boost::phoenix; | |
109 | ||
110 | boost::optional<int> n = 0; | |
111 | BOOST_TEST((test("1234", (-int_)[phx::ref(n) = _1]))); | |
112 | BOOST_TEST(n.get() == 1234); | |
113 | ||
114 | n = boost::optional<int>(); | |
115 | BOOST_TEST((test("abcd", (-int_)[phx::ref(n) = _1], false))); | |
116 | BOOST_TEST(!n); | |
117 | } | |
118 | ||
119 | { | |
120 | std::vector<adata> v; | |
121 | BOOST_TEST((test_attr("a 1 2 a 2", *("a" >> int_ >> -int_), v | |
122 | , char_(' ')))); | |
123 | BOOST_TEST(2 == v.size() && | |
124 | 1 == v[0].a && v[0].b && 2 == *(v[0].b) && | |
125 | 2 == v[1].a && !v[1].b); | |
126 | } | |
127 | ||
128 | return boost::report_errors(); | |
129 | } |