File: nest.hpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (321 lines) | stat: -rw-r--r-- 10,967 bytes parent folder | download | duplicates (13)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*=============================================================================
    Copyright (C) 2015 Kohei Takahshi

    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 <utility>
#include <boost/config.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/as_deque.hpp>
#include <boost/fusion/include/as_list.hpp>
#include <boost/fusion/include/as_vector.hpp>
#include <boost/fusion/include/begin.hpp>
#include <boost/fusion/include/is_sequence.hpp>
#include <boost/fusion/include/size.hpp>
#include <boost/fusion/include/value_of.hpp>
#include <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/remove_reference.hpp>

#include "fixture.hpp"

namespace test_detail
{
    struct adapted_sequence
    {
        adapted_sequence() : value_() {}
        explicit adapted_sequence(int value) : value_(value) {}
        int value_;
    };

    bool operator==(adapted_sequence const& lhs, adapted_sequence const& rhs)
    {
        return lhs.value_ == rhs.value_;
    }

    bool operator!=(adapted_sequence const& lhs, adapted_sequence const& rhs)
    {
        return lhs.value_ != rhs.value_;
    }

    template <template <typename> class Scenario>
    struct can_convert_using
    {
        template <typename T>
        struct to
        {
            static bool can_convert_(boost::true_type /* skip */)
            {
                return true;
            }

            static bool can_convert_(boost::false_type /* skip */)
            {
                using namespace boost::fusion;
                return
                    run<Scenario<T> >(typename result_of::as_deque<T>::type()) &&
                    run<Scenario<T> >(typename result_of::as_list<T>::type()) &&
                    run<Scenario<T> >(typename result_of::as_vector<T>::type());
            }

            template <typename Source, typename Expected>
            bool operator()(Source const&, Expected const&) const
            {
                // bug when converting single element sequences in C++03 and
                // C++11... 
                // not_<not_<is_convertible<sequence<sequence<int>>, int >
                // is invalid check
                typedef typename ::boost::fusion::result_of::size<T>::type seq_size;
                return can_convert_(
                    boost::integral_constant<bool, seq_size::value == 1>()
                );
            }
        };
    };

    template <typename T>
    struct can_construct_from_elements
    {
        template <typename Source, typename Expected>
        bool operator()(Source const&, Expected const&) const
        {
            // constructing a nested sequence of one is the complicated case to
            // disambiguate from a conversion-copy, so focus on that
            typedef typename boost::fusion::result_of::size<T>::type seq_size;
            return can_construct_(
                boost::integral_constant<int, seq_size::value>()
            );
        }

        template <int Size>
        static bool can_construct_(boost::integral_constant<int, Size>)
        {
            return Size == 0 || Size == 2 || Size == 3;
        }

        static bool can_construct_(boost::integral_constant<int, 1>)
        {
            typedef typename ::boost::remove_reference<
                typename ::boost::remove_const<
                    typename ::boost::fusion::result_of::value_of<
                        typename ::boost::fusion::result_of::begin<T>::type
                    >::type
                >::type
            >::type element;

            return run< can_construct<T> >(element(), T());
        }
    };

    template <typename T>
    struct can_nest
    {
        template <typename Source, typename Expected>
        bool operator()(Source const& source, Expected const& expected)
        {
            return
                run< can_copy<T> >(source, expected) &&
                run< can_convert_using<can_copy>::to<T> >(source, expected) &&
                run< can_construct_from_elements<T> >(source, expected);
        }
    };
} // test_detail


BOOST_FUSION_ADAPT_STRUCT(test_detail::adapted_sequence, (int, data))

template <template <typename> class Scenario>
void
test()
{
    using namespace test_detail;

    BOOST_TEST(boost::fusion::traits::is_sequence<adapted_sequence>::value);
    BOOST_TEST(boost::fusion::size(adapted_sequence()) == 1);

    BOOST_TEST((
        run< Scenario< FUSION_SEQUENCE< FUSION_SEQUENCE<> > > >(
            FUSION_SEQUENCE< FUSION_SEQUENCE<> >()
        )
    ));
    BOOST_TEST((
        run< Scenario<FUSION_SEQUENCE<FUSION_SEQUENCE<>, int> > >(
            FUSION_SEQUENCE< FUSION_SEQUENCE<>, int>(FUSION_SEQUENCE<>(), 325)
        )
    ));
    BOOST_TEST((
        run< Scenario< FUSION_SEQUENCE<int, FUSION_SEQUENCE<> > > >(
            FUSION_SEQUENCE< int, FUSION_SEQUENCE<> >(325, FUSION_SEQUENCE<>())
        )
    ));
    BOOST_TEST((
        run< Scenario<FUSION_SEQUENCE<int, FUSION_SEQUENCE<>, float> > >(
            FUSION_SEQUENCE<int, FUSION_SEQUENCE<> , float>(
                325, FUSION_SEQUENCE<>(), 2.0f
            )
        )
    ));

    BOOST_TEST((
        run< Scenario< FUSION_SEQUENCE< FUSION_SEQUENCE<int> > > >(
            FUSION_SEQUENCE< FUSION_SEQUENCE<int> >(FUSION_SEQUENCE<int>(400))
        )
    ));
    BOOST_TEST((
        run< Scenario< FUSION_SEQUENCE<adapted_sequence> > >(
            FUSION_SEQUENCE<adapted_sequence>(adapted_sequence(400))
        )
    ));
    BOOST_TEST((
        run< Scenario<FUSION_SEQUENCE<FUSION_SEQUENCE<int>, int> > >(
            FUSION_SEQUENCE<FUSION_SEQUENCE<int>, int>(
                FUSION_SEQUENCE<int>(325), 400
            )
        )
    ));
    BOOST_TEST((
        run< Scenario<FUSION_SEQUENCE<adapted_sequence, int> > >(
           FUSION_SEQUENCE<adapted_sequence, int>(adapted_sequence(325), 400)
        )
    ));
    BOOST_TEST((
        run< Scenario< FUSION_SEQUENCE< int, FUSION_SEQUENCE<int> > > >(
            FUSION_SEQUENCE< int, FUSION_SEQUENCE<int> >(
                325, FUSION_SEQUENCE<int>(400)
            )
        )
    ));
    BOOST_TEST((
        run< Scenario< FUSION_SEQUENCE<int, adapted_sequence> > >(
            FUSION_SEQUENCE<int, adapted_sequence>(325, adapted_sequence(450))
        )
    ));
    BOOST_TEST((
        run< Scenario< FUSION_SEQUENCE<int, FUSION_SEQUENCE<int>, int> > >(
            FUSION_SEQUENCE<int, FUSION_SEQUENCE<int>, int>(
                500, FUSION_SEQUENCE<int>(350), 200
            )
        )
    ));
    BOOST_TEST((
        run< Scenario< FUSION_SEQUENCE<int, adapted_sequence, int> > >(
            FUSION_SEQUENCE<int, adapted_sequence, int>(
                300, adapted_sequence(500), 400)
        )
    ));

    BOOST_TEST((
        run< Scenario< FUSION_SEQUENCE< FUSION_SEQUENCE<int, int> > > >(
            FUSION_SEQUENCE< FUSION_SEQUENCE<int, int> >(
                FUSION_SEQUENCE<int, int>(450, 500)
            )
        )
    ));
    BOOST_TEST((
        run< Scenario< FUSION_SEQUENCE<FUSION_SEQUENCE<int, int>, int> > >(
            FUSION_SEQUENCE<FUSION_SEQUENCE<int, int>, int>(
                FUSION_SEQUENCE<int, int>(450, 500), 150
            )
        )
    ));
    BOOST_TEST((
        run< Scenario< FUSION_SEQUENCE< int, FUSION_SEQUENCE<int, int> > > >(
            FUSION_SEQUENCE< int, FUSION_SEQUENCE<int, int> >(
                450, FUSION_SEQUENCE<int, int>(500, 150)
            )
        )
    ));
    BOOST_TEST((
        run<Scenario< FUSION_SEQUENCE<int, FUSION_SEQUENCE<int, int>, int> > >(
            FUSION_SEQUENCE<int, FUSION_SEQUENCE<int, int>, int>(
                150, FUSION_SEQUENCE<int, int>(250, 350), 450
            )
        )
    ));

    BOOST_TEST((
        run<Scenario<FUSION_SEQUENCE<FUSION_SEQUENCE<>, FUSION_SEQUENCE<> > > >(
            FUSION_SEQUENCE< FUSION_SEQUENCE<>, FUSION_SEQUENCE<> >(
                FUSION_SEQUENCE<>(), FUSION_SEQUENCE<>()
            )
        )
    ));
    BOOST_TEST((
        run<Scenario<FUSION_SEQUENCE<FUSION_SEQUENCE<int>, FUSION_SEQUENCE<> > > >(
            FUSION_SEQUENCE< FUSION_SEQUENCE<int>, FUSION_SEQUENCE<> >(
                FUSION_SEQUENCE<int>(150), FUSION_SEQUENCE<>()
            )
        )
    ));
    BOOST_TEST((
        run<Scenario<FUSION_SEQUENCE<FUSION_SEQUENCE<>, FUSION_SEQUENCE<int> > > >(
            FUSION_SEQUENCE< FUSION_SEQUENCE<>, FUSION_SEQUENCE<int> >(
                FUSION_SEQUENCE<>(), FUSION_SEQUENCE<int>(500)
            )
        )
    ));
    BOOST_TEST((
        run<Scenario<FUSION_SEQUENCE<FUSION_SEQUENCE<int>, FUSION_SEQUENCE<int> > > >(
            FUSION_SEQUENCE< FUSION_SEQUENCE<int>, FUSION_SEQUENCE<int> >(
                FUSION_SEQUENCE<int>(155), FUSION_SEQUENCE<int>(255)
            )
        )
    ));
    BOOST_TEST((
        run< Scenario<
            FUSION_SEQUENCE< FUSION_SEQUENCE<int, int>, FUSION_SEQUENCE<int> >
        > >(
            FUSION_SEQUENCE< FUSION_SEQUENCE<int, int>, FUSION_SEQUENCE<int> >(
                FUSION_SEQUENCE<int, int>(222, 333), FUSION_SEQUENCE<int>(444)
            )
        )
    ));
    BOOST_TEST((
        run< Scenario<
            FUSION_SEQUENCE< FUSION_SEQUENCE<int>, FUSION_SEQUENCE<int, int> >
        > >(
            FUSION_SEQUENCE< FUSION_SEQUENCE<int>, FUSION_SEQUENCE<int, int> >(
                FUSION_SEQUENCE<int>(100), FUSION_SEQUENCE<int, int>(300, 400)
            )
        )
    ));
    BOOST_TEST((
        run< Scenario<
            FUSION_SEQUENCE< FUSION_SEQUENCE<int, int>, FUSION_SEQUENCE<int, int> >
        > >(
            FUSION_SEQUENCE< FUSION_SEQUENCE<int, int>, FUSION_SEQUENCE<int, int> >(
                FUSION_SEQUENCE<int, int>(600, 700)
              , FUSION_SEQUENCE<int, int>(800, 900)
            )
        )
    ));


    // Ignore desired scenario, and cheat to make these work
    BOOST_TEST((
        run< can_lvalue_construct< FUSION_SEQUENCE<FUSION_SEQUENCE<>&> > >(
            FUSION_SEQUENCE<>()
          , FUSION_SEQUENCE< FUSION_SEQUENCE<> >()
        )
    ));
    BOOST_TEST((
        run< can_construct< FUSION_SEQUENCE<const FUSION_SEQUENCE<>&> > >(
            FUSION_SEQUENCE<>()
          , FUSION_SEQUENCE< FUSION_SEQUENCE<> >()
        )
    ));
    BOOST_TEST((
        run< can_lvalue_construct< FUSION_SEQUENCE<FUSION_SEQUENCE<int>&> > >(
            FUSION_SEQUENCE<int>(300)
          , FUSION_SEQUENCE< FUSION_SEQUENCE<int> >(FUSION_SEQUENCE<int>(300))
        )
    ));
    BOOST_TEST((
        run< can_construct< FUSION_SEQUENCE<const FUSION_SEQUENCE<int>&> > >(
            FUSION_SEQUENCE<int>(400)
          , FUSION_SEQUENCE< FUSION_SEQUENCE<int> >(FUSION_SEQUENCE<int>(400))
        )
    ));
}