summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/fusion/example/extension/triple.cpp
blob: ac8f18e08e2cfc4d83177e233603c979b02c4d8b (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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*=============================================================================
    Copyright (c) 2001-2011 Joel de Guzman
    Copyright (c) 2011 Nathan Ridge
    Copyright (c) 2006 Dan Marsden

    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)
==============================================================================*/

/*=============================================================================
    An implementation of a std::pair like triple<T0, T1, T2>
    We use fusion::sequence_facade and fusion::iterator_facade
    to make our triple a fully conforming Boost.Fusion random
    traversal sequence.
==============================================================================*/

#include <boost/detail/lightweight_test.hpp>

#include <boost/fusion/sequence/sequence_facade.hpp>
#include <boost/fusion/iterator/iterator_facade.hpp>
#include <boost/fusion/sequence/intrinsic.hpp>
#include <boost/fusion/iterator.hpp>
#include <boost/fusion/support/category_of.hpp>
#include <boost/fusion/algorithm/iteration/fold.hpp>

#include <boost/mpl/int.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/minus.hpp>
#include <boost/mpl/assert.hpp>

#include <boost/type_traits/is_const.hpp>
#include <boost/type_traits/is_same.hpp>

#include <string>

namespace mpl = boost::mpl;
namespace fusion = boost::fusion;

namespace demo
{
    template<typename Seq, int N>
    struct triple_iterator
        : fusion::iterator_facade<triple_iterator<Seq, N>,
          fusion::random_access_traversal_tag>
    {
        typedef mpl::int_<N> index;
        typedef Seq sequence_type;

        triple_iterator(Seq& seq)
            : seq_(seq) {}

        Seq& seq_;

        template<typename T>
        struct value_of;

        template<typename Sq>
        struct value_of<triple_iterator<Sq, 0> >
            : mpl::identity<typename Sq::t0_type>
        {};

        template<typename Sq>
        struct value_of<triple_iterator<Sq, 1> >
            : mpl::identity<typename Sq::t1_type>
        {};

        template<typename Sq>
        struct value_of<triple_iterator<Sq, 2> >
            : mpl::identity<typename Sq::t2_type>
        {};

        template<typename T>
        struct deref;

        template <typename Sq>
        struct deref<triple_iterator<Sq, 0> >
        {
            typedef typename Sq::t0_type& type;

            static type
            call(triple_iterator<Sq, 0> const& iter)
            {
                return iter.seq_.t0;
            }
        };

        template <typename Sq>
        struct deref<triple_iterator<Sq, 0> const>
        {
            typedef typename Sq::t0_type const& type;

            static type
            call(triple_iterator<Sq, 0> const& iter)
            {
                return iter.seq_.t0;
            }
        };

        template <typename Sq>
        struct deref<triple_iterator<Sq, 1> >
        {
            typedef typename Sq::t1_type& type;

            static type
            call(triple_iterator<Sq, 1> const& iter)
            {
                return iter.seq_.t1;
            }
        };

        template <typename Sq>
        struct deref<triple_iterator<Sq, 1> const>
        {
            typedef typename Sq::t1_type const& type;

            static type
            call(triple_iterator<Sq, 1> const& iter)
            {
                return iter.seq_.t1;
            }
        };

        template <typename Sq>
        struct deref<triple_iterator<Sq, 2> >
        {
            typedef typename Sq::t2_type& type;

            static type
            call(triple_iterator<Sq, 2> const& iter)
            {
                return iter.seq_.t2;
            }
        };

        template <typename Sq>
        struct deref<triple_iterator<Sq, 2> const>
        {
            typedef typename Sq::t2_type const& type;

            static type
            call(triple_iterator<Sq, 2> const& iter)
            {
                return iter.seq_.t2;
            }
        };

        template<typename It>
        struct next
        {
            typedef triple_iterator<
                typename It::sequence_type, It::index::value + 1>
            type;

            static type call(It const& it)
            {
                return type(it.seq_);
            }
        };

        template<typename It>
        struct prior
        {
            typedef triple_iterator<
                typename It::sequence_type, It::index::value - 1>
            type;

            static type call(It const& it)
            {
                return type(it.seq_);
            }
        };

        template<typename It1, typename It2>
        struct distance
        {
            typedef typename mpl::minus<
              typename It2::index, typename It1::index>::type
            type;

            static type call(It1 const& it1, It2 const& it2)
            {
                return type();
            }
        };

        template<typename It, typename M>
        struct advance
        {
            typedef triple_iterator<
                typename It::sequence_type,
                It::index::value + M::value>
            type;

            static type call(It const& it)
            {
                return type(it.seq_);
            }
        };
    };

    template<typename T0, typename T1, typename T2>
    struct triple
        : fusion::sequence_facade<triple<T0, T1, T2>,
          fusion::random_access_traversal_tag>
    {
        triple(T0 const& t0, T1 const& t1, T2 const& t2)
            : t0(t0), t1(t1), t2(t2)
        {}

        template<typename Sq>
        struct begin
        {
            typedef demo::triple_iterator<Sq, 0> type;

            static type call(Sq& sq)
            {
                return type(sq);
            }
        };

        template<typename Sq>
        struct end
        {
            typedef demo::triple_iterator<Sq, 3> type;

            static type call(Sq& sq)
            {
                return type(sq);
            }
        };

        template<typename Sq>
        struct size
            : mpl::int_<3>
        {};

        template<typename Sq, typename N>
        struct value_at
            : value_at<Sq, mpl::int_<N::value> >
        {};

        template<typename Sq>
        struct value_at<Sq, mpl::int_<0> >
        {
            typedef typename Sq::t0_type type;
        };

        template<typename Sq>
        struct value_at<Sq, mpl::int_<1> >
        {
            typedef typename Sq::t1_type type;
        };

        template<typename Sq>
        struct value_at<Sq, mpl::int_<2> >
        {
            typedef typename Sq::t2_type type;
        };

        template<typename Sq, typename N>
        struct at
            : at<Sq, mpl::int_<N::value> >
        {};

        template<typename Sq>
        struct at<Sq, mpl::int_<0> >
        {
            typedef typename
                mpl::if_<
                    boost::is_const<Sq>
                  , typename Sq::t0_type const&
                  , typename Sq::t0_type&
                >::type
            type;

            static type call(Sq& sq)
            {
                return sq.t0;
            }
        };

        template<typename Sq>
        struct at<Sq, mpl::int_<1> >
        {
            typedef typename
                mpl::if_<
                    boost::is_const<Sq>
                  , typename Sq::t1_type const&
                  , typename Sq::t1_type&
                >::type
            type;

            static type call(Sq& sq)
            {
                return sq.t1;
            }
        };

        template<typename Sq>
        struct at<Sq, mpl::int_<2> >
        {
            typedef typename
                mpl::if_<
                    boost::is_const<Sq>
                  , typename Sq::t2_type const&
                  , typename Sq::t2_type&
                >::type
            type;

            static type call(Sq& sq)
            {
                return sq.t2;
            }
        };

        typedef T0 t0_type;
        typedef T1 t1_type;
        typedef T2 t2_type;

        T0 t0;
        T1 t1;
        T2 t2;
    };
}

struct modifying_fold_functor
{
    template <typename T>
    struct result
    {
        typedef bool type;
    };

    template <typename T>
    bool operator()(bool b, T&)
    {
        return b;
    }
};

struct nonmodifying_fold_functor
{
    template <typename T>
    struct result
    {
        typedef bool type;
    };

    template <typename T>
    bool operator()(bool b, const T&)
    {
        return b;
    }
};

int main()
{
    typedef demo::triple<int, char, std::string> my_triple;
    my_triple t(101, 'a', "hello");
    BOOST_TEST(*fusion::begin(t) == 101);
    BOOST_TEST(*fusion::next(fusion::begin(t)) == 'a');
    BOOST_TEST(*fusion::prior(fusion::end(t)) == "hello");
    BOOST_TEST(fusion::distance(fusion::begin(t), fusion::end(t)) == 3);
    BOOST_TEST(fusion::size(t) == 3);
    BOOST_MPL_ASSERT((boost::is_same<
        int, fusion::result_of::value_at_c<my_triple, 0>::type>));
    BOOST_MPL_ASSERT((boost::is_same<
      char, fusion::result_of::value_at_c<my_triple, 1>::type>));
    BOOST_MPL_ASSERT((boost::is_same<
        std::string, fusion::result_of::value_at_c<my_triple, 2>::type>));
    BOOST_TEST(fusion::at_c<0>(t) == 101);
    BOOST_TEST(fusion::at_c<1>(t) == 'a');
    BOOST_TEST(fusion::at_c<2>(t) == "hello");
    BOOST_TEST(fusion::fold(t, true, modifying_fold_functor()) == true);
    BOOST_TEST(fusion::fold(t, true, nonmodifying_fold_functor()) == true);
    return boost::report_errors();
}