1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/*=============================================================================
Copyright (c) 2001-2011 Hartmut Kaiser
Copyright (c) 2001-2011 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include <boost/config/warning_disable.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <boost/spirit/include/phoenix_limits.hpp>
#include <boost/fusion/include/struct.hpp>
#include <boost/fusion/include/nview.hpp>
#include <boost/spirit/include/qi_char.hpp>
#include <boost/spirit/include/qi_string.hpp>
#include <boost/spirit/include/qi_numeric.hpp>
#include <boost/spirit/include/qi_operator.hpp>
#include <boost/spirit/include/qi_nonterminal.hpp>
#include <boost/spirit/include/qi_auxiliary.hpp>
#include <iostream>
#include <vector>
#include <string>
#include "test.hpp"
///////////////////////////////////////////////////////////////////////////////
struct test_data
{
std::string s1;
std::string s2;
int i1;
double d1;
std::string s3;
};
BOOST_FUSION_ADAPT_STRUCT(
test_data,
(int, i1)
(std::string, s1)
(std::string, s2)
(std::string, s3)
(double, d1)
)
///////////////////////////////////////////////////////////////////////////////
struct test_int_data1
{
int i;
};
// we provide a custom attribute transformation taking copy of the actual
// attribute value, simulating more complex type transformations
namespace boost { namespace spirit { namespace traits
{
template <>
struct transform_attribute<test_int_data1, int, qi::domain>
{
typedef int type;
static int pre(test_int_data1& d) { return d.i; }
static void post(test_int_data1& d, int i) { d.i = i; }
static void fail(test_int_data1&) {}
};
}}}
///////////////////////////////////////////////////////////////////////////////
struct test_int_data2
{
int i;
};
// we provide a simple custom attribute transformation utilizing passing the
// actual attribute by reference
namespace boost { namespace spirit { namespace traits
{
template <>
struct transform_attribute<test_int_data2, int, qi::domain>
{
typedef int& type;
static int& pre(test_int_data2& d) { return d.i; }
static void post(test_int_data2&, int const&) {}
static void fail(test_int_data2&) {}
};
}}}
///////////////////////////////////////////////////////////////////////////////
int
main()
{
using spirit_test::test_attr;
namespace qi = boost::spirit::qi;
namespace fusion = boost::fusion;
// testing attribute reordering in a fusion sequence as explicit attribute
{
typedef fusion::result_of::as_nview<test_data, 1, 0, 4>::type
test_view;
test_data d1 = { "", "", 0, 0.0, "" };
test_view v1 = fusion::as_nview<1, 0, 4>(d1);
BOOST_TEST(test_attr("s1,2,1.5",
*(qi::char_ - ',') >> ',' >> qi::int_ >> ',' >> qi::double_, v1));
BOOST_TEST(d1.i1 == 2 && d1.s1 == "s1" && d1.d1 == 1.5);
test_data d2 = { "", "", 0, 0.0, "" };
test_view v2 = fusion::as_nview<1, 0, 4>(d2);
BOOST_TEST(test_attr("s1, 2, 1.5 ",
*(qi::char_ - ',') >> ',' >> qi::int_ >> ',' >> qi::double_,
v2, qi::space));
BOOST_TEST(d2.i1 == 2 && d2.s1 == "s1" && d2.d1 == 1.5);
}
{
// this won't work without the second template argument as *digit
// exposes a vector<char> as its attribute
std::string str;
BOOST_TEST(test_attr("123"
, qi::attr_cast<std::string, std::string>(*qi::digit), str));
BOOST_TEST(str == "123");
}
// testing attribute reordering in a fusion sequence involving a rule
{
typedef fusion::result_of::as_nview<test_data, 1, 0, 4>::type
test_view;
std::vector<test_data> v;
qi::rule<char const*, test_view()> r1 =
*(qi::char_ - ',') >> ',' >> qi::int_ >> ',' >> qi::double_;
BOOST_TEST(test_attr("s1,2,1.5\ns2,4,3.5", r1 % qi::eol, v));
BOOST_TEST(v.size() == 2 &&
v[0].i1 == 2 && v[0].s1 == "s1" && v[0].d1 == 1.5 &&
v[1].i1 == 4 && v[1].s1 == "s2" && v[1].d1 == 3.5);
qi::rule<char const*, test_view(), qi::blank_type> r2 =
*(qi::char_ - ',') >> ',' >> qi::int_ >> ',' >> qi::double_;
v.clear();
BOOST_TEST(test_attr("s1, 2, 1.5 \n s2, 4, 3.5", r2 % qi::eol, v, qi::blank));
BOOST_TEST(v.size() == 2 &&
v[0].i1 == 2 && v[0].s1 == "s1" && v[0].d1 == 1.5 &&
v[1].i1 == 4 && v[1].s1 == "s2" && v[1].d1 == 3.5);
}
// testing explicit transformation if attribute needs to be copied
{
test_int_data1 d = { 0 };
BOOST_TEST(test_attr("1", qi::attr_cast(qi::int_), d));
BOOST_TEST(d.i == 1);
BOOST_TEST(test_attr("2", qi::attr_cast<test_int_data1>(qi::int_), d));
BOOST_TEST(d.i == 2);
BOOST_TEST(test_attr("3", qi::attr_cast<test_int_data1, int>(qi::int_), d));
BOOST_TEST(d.i == 3);
}
{
std::vector<test_int_data1> v;
BOOST_TEST(test_attr("1,2", qi::attr_cast(qi::int_) % ',', v));
BOOST_TEST(v.size() == 2 && v[0].i == 1 && v[1].i == 2);
v.clear();
BOOST_TEST(test_attr("1,2"
, qi::attr_cast<test_int_data1>(qi::int_) % ',', v));
BOOST_TEST(v.size() == 2 && v[0].i == 1 && v[1].i == 2);
v.clear();
BOOST_TEST(test_attr("1,2"
, qi::attr_cast<test_int_data1, int>(qi::int_) % ',', v));
BOOST_TEST(v.size() == 2 && v[0].i == 1 && v[1].i == 2);
}
return boost::report_errors();
}
|