summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/test/qi/attribute2.cpp
blob: d8167d89e8348a2510029e9e0251c120edfa66c5 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
/*=============================================================================
    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;

    {
        std::vector<test_int_data1> v;
        qi::rule<char const*, int()> r = qi::int_;

        BOOST_TEST(test_attr("1,2", r % ',', v));
        BOOST_TEST(v.size() == 2 && v[0].i == 1 && v[1].i == 2);
    }

    {
        std::vector<double> v;
        qi::rule<char const*, int()> r = qi::int_;

        BOOST_TEST(test_attr("1,2", r % ',', v));
        BOOST_TEST(v.size() == 2 && v[0] == 1.0 && v[1] == 2.0);
    }

    {
        std::vector<test_int_data1> v;

// this won't compile as there is no defined transformation for
// test_int_data1 and double
//      BOOST_TEST(test_attr("1.0,2.2", qi::attr_cast(qi::double_) % ',', v));
//      BOOST_TEST(test_attr("1.0,2.2"
//        , qi::attr_cast<test_int_data1>(qi::double_) % ',', v));

        BOOST_TEST(test_attr("1.0,2.2"
          , qi::attr_cast<test_int_data1, int>(qi::double_) % ',', v));
        BOOST_TEST(v.size() == 2 && v[0].i == 1 && v[1].i == 2);

        qi::rule<char const*, int()> r = qi::double_;

        v.clear();
        BOOST_TEST(test_attr("1.0,2.0", r % ',', v));
        BOOST_TEST(v.size() == 2 && v[0].i == 1 && v[1].i == 2);
    }

    // testing explicit transformation if attribute is taken by reference
    {
        test_int_data2 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_data2>(qi::int_), d));
        BOOST_TEST(d.i == 2);
        BOOST_TEST(test_attr("3", qi::attr_cast<test_int_data2, int>(qi::int_), d));
        BOOST_TEST(d.i == 3);
    }

    {
        std::vector<test_int_data2> 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_data2>(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_data2, int>(qi::int_) % ',', v));
        BOOST_TEST(v.size() == 2 && v[0].i == 1 && v[1].i == 2);
    }

    {
        std::vector<test_int_data2> v;
        qi::rule<char const*, int()> r = qi::int_;

        BOOST_TEST(test_attr("1,2", r % ',', v));
        BOOST_TEST(v.size() == 2 && v[0].i == 1 && v[1].i == 2);
    }

    {
        std::vector<test_int_data2> v;

// this won't compile as there is no defined transformation for
// test_int_data2 and double
//      BOOST_TEST(test_attr("1.0,2.2", qi::attr_cast(qi::double_) % ',', v));
//      BOOST_TEST(test_attr("1.0,2.2"
//        , qi::attr_cast<test_int_data2>(qi::double_) % ',', v));

        BOOST_TEST(test_attr("1.0,2.2"
          , qi::attr_cast<test_int_data2, int>(qi::double_) % ',', v));
        BOOST_TEST(v.size() == 2 && v[0].i == 1 && v[1].i == 2);

        qi::rule<char const*, int()> r = qi::double_;

        v.clear();
        BOOST_TEST(test_attr("1.0,2.0", r % ',', v));
        BOOST_TEST(v.size() == 2 && v[0].i == 1 && v[1].i == 2);
    }

    return boost::report_errors();
}