summaryrefslogtreecommitdiff
path: root/boost/type_erasure/tuple.hpp
blob: 945dcdafe44b90480e22e97d3b20b03751a7db88 (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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// Boost.TypeErasure library
//
// Copyright 2011-2012 Steven Watanabe
//
// 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)
//
// $Id$

#if !defined(BOOST_PP_IS_ITERATING)

#ifndef BOOST_TYPE_ERASURE_TUPLE_HPP_INCLUDED
#define BOOST_TYPE_ERASURE_TUPLE_HPP_INCLUDED

#include <boost/config.hpp>


#ifdef BOOST_TYPE_ERASURE_DOXYGEN

namespace boost {
namespace type_erasure {

/**
 * @ref tuple is a Boost.Fusion Random Access Sequence containing
 * @ref any "anys". @c Concept specifies the \Concept for each
 * of the elements.  The remaining arguments must be (possibly const
 * and/or reference qualified) placeholders, which are the
 * @ref placeholder "placeholders" of the elements.
 */
template<class Concept, class... T>
class tuple
{
public:
    /**
     * Constructs a tuple.  Each element of @c args will
     * be used to initialize the corresponding @ref any member.
     * The @ref binding for the tuple elements is determined
     * by mapping the placeholders in @c T to the corresponding
     * types in @c U.
     */
    template<class... U>
    explicit tuple(U&&... args);
};

/**
 * Returns the Nth @ref any in the tuple.
 */
template<int N, class Concept, class... T>
any<Concept, TN>& get(tuple<Concept, T...>& arg);
/** \overload */
template<int N, class Concept, class... T>
const any<Concept, TN>& get(const tuple<Concept, T...>& arg);

}
}

#elif !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)

#include <boost/mpl/int.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/insert.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/fusion/include/category_of.hpp>
#include <boost/fusion/include/iterator_facade.hpp>
#include <boost/fusion/include/sequence_facade.hpp>
#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/static_binding.hpp>
#include <boost/type_erasure/config.hpp>

namespace boost {
namespace type_erasure {

template<class Concept, class... T>
struct cons;

template<class Concept>
struct cons<Concept>
{
    template<class Binding>
    cons(const Binding&) {}
};

template<class Concept, class T0, class... T>
struct cons<Concept, T0, T...>
{
    typedef any<Concept, T0> value_type;
    typedef cons<Concept, T...> rest_type;
    template<class Binding, class U0, class... U>
    cons(const Binding& b, U0&& u0, U&&... u)
      : value(std::forward<U0>(u0), b),
        rest(b, std::forward<U>(u)...)
    {}
    any<Concept, T0> value;
    cons<Concept, T...> rest;
};

namespace detail {

template<int N, class Cons>
struct cons_advance
{
    typedef typename cons_advance<N-1, Cons>::type::rest_type type;
    static const type& call(const Cons& c)
    {
        return cons_advance<N-1, Cons>::call(c).rest;
    }
};

template<class Cons>
struct cons_advance<0, Cons>
{
    typedef Cons type;
    static const type& call(const Cons& c)
    {
        return c;
    }
};

template<class... T>
struct make_map;

template<class T0, class... T>
struct make_map<T0, T...>
{
    typedef typename ::boost::mpl::insert<
        typename ::boost::type_erasure::detail::make_map<T...>::type,
        T0
    >::type type;
};

template<>
struct make_map<>
{
    typedef ::boost::mpl::map0<> type;
};

}

/** INTERNAL ONLY */
template<class Tuple, int N>
class tuple_iterator :
    public ::boost::fusion::iterator_facade<
        tuple_iterator<Tuple, N>,
        ::boost::fusion::random_access_traversal_tag
    >
{
public:
    typedef ::boost::mpl::int_<N> index;
    explicit tuple_iterator(Tuple& t_arg) : t(&t_arg) {}
    template<class It>
    struct value_of
    {
        typedef typename Tuple::template value_at<Tuple, mpl::int_<N> >::type type;
    };
    template<class It>
    struct deref
    {
        typedef typename Tuple::template at<Tuple, mpl::int_<N> >::type type;
        static type call(It it)
        {
            return Tuple::template at<Tuple, mpl::int_<N> >::call(*it.t);
        }
    };
    template<class It, class M>
    struct advance
    {
        typedef tuple_iterator<Tuple, (It::index::value+M::value)> type;
        static type call(It it) { return type(*it.t); }
    };
    template<class It>
    struct next : advance<It, ::boost::mpl::int_<1> > {};
    template<class It>
    struct prior : advance<It, ::boost::mpl::int_<-1> > {};
    template<class It1, class It2>
    struct distance
    {
        typedef typename ::boost::mpl::minus<
            typename It2::index,
            typename It1::index
        >::type type;
        static type call(It1, It2) { return type(); }
    };
private:
    Tuple* t;
};

template<class Concept, class... T>
class tuple :
    public ::boost::fusion::sequence_facade<
        ::boost::type_erasure::tuple<Concept, T...>,
        ::boost::fusion::forward_traversal_tag
    >
{
public:
    template<class... U>
    explicit tuple(U&&... args)
      : impl(
            ::boost::type_erasure::make_binding<
                typename ::boost::type_erasure::detail::make_map<
                    ::boost::mpl::pair<
                        typename ::boost::remove_const<
                            typename ::boost::remove_reference<T>::type
                        >::type,
                        typename ::boost::remove_const<
                            typename ::boost::remove_reference<U>::type
                        >::type
                    >...
                >::type
            >(),
            std::forward<U>(args)...)
    {}

    template<class Seq>
    struct begin
    {
        typedef ::boost::type_erasure::tuple_iterator<
            Seq,
            0
        > type;
        static type call(Seq& seq) { return type(seq); }
    };
    template<class Seq>
    struct end
    {
        typedef ::boost::type_erasure::tuple_iterator<
            Seq,
            sizeof...(T)
        > type;
        static type call(Seq& seq) { return type(seq); }
    };
    template<class Seq>
    struct size
    {
        typedef ::boost::mpl::int_<sizeof...(T)> type;
        static type call(Seq& seq) { return type(); }
    };
    template<class Seq>
    struct empty
    {
        typedef ::boost::mpl::bool_<sizeof...(T) == 0> type;
        static type call(Seq& seq) { return type(); }
    };
    template<class Seq, class N>
    struct at
    {
        typedef typename ::boost::type_erasure::detail::cons_advance<
            N::value,
            ::boost::type_erasure::cons<Concept, T...>
        >::type::value_type value_type;
        typedef typename ::boost::mpl::if_< ::boost::is_const<Seq>,
            const value_type&,
            value_type&
        >::type type;
        static type call(Seq& seq)
        {
            return const_cast<type>(
                ::boost::type_erasure::detail::cons_advance<
                    N::value,
                    ::boost::type_erasure::cons<Concept, T...>
                >::call(seq.impl).value
            );
        }
    };
    template<class Seq, class N>
    struct value_at
    {
        typedef typename ::boost::type_erasure::detail::cons_advance<
            N::value,
            ::boost::type_erasure::cons<Concept, T...>
        >::type::value_type value_type;
    };
    ::boost::type_erasure::cons<Concept, T...> impl;
};

template<int N, class Concept, class... T>
typename ::boost::type_erasure::detail::cons_advance<
    N,
    ::boost::type_erasure::cons<Concept, T...>
>::type::value_type& get(::boost::type_erasure::tuple<Concept, T...>& t)
{
    return const_cast<
        typename ::boost::type_erasure::detail::cons_advance<
            N,
            ::boost::type_erasure::cons<Concept, T...>
        >::type::value_type&
    >(
        ::boost::type_erasure::detail::cons_advance<N,
            ::boost::type_erasure::cons<Concept, T...>
        >::call(t.impl).value
    );
}

template<int N, class Concept, class... T>
const typename ::boost::type_erasure::detail::cons_advance<
    N,
    ::boost::type_erasure::cons<Concept, T...>
>::type::value_type& get(const ::boost::type_erasure::tuple<Concept, T...>& t)
{
    return ::boost::type_erasure::detail::cons_advance<
        N,
        ::boost::type_erasure::cons<Concept, T...>
    >::call(t.impl).value;
}

}
}

#else

#include <boost/mpl/int.hpp>
#include <boost/mpl/minus.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/map.hpp>
#include <boost/fusion/include/category_of.hpp>
#include <boost/fusion/include/iterator_facade.hpp>
#include <boost/fusion/include/sequence_facade.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/iteration/iterate.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/enum_trailing_params.hpp>
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
#include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
#include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/static_binding.hpp>
#include <boost/type_erasure/config.hpp>

namespace boost {
namespace type_erasure {

/** INTERNAL ONLY */
struct na {};

namespace detail {

template<int N, class Tuple>
struct get_impl;

template<class Concept,
    BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(
        BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, class T, ::boost::type_erasure::na)>
struct tuple_storage;

}

/** INTERNAL ONLY */
template<class Tuple, int N>
class tuple_iterator :
    public ::boost::fusion::iterator_facade<
        tuple_iterator<Tuple, N>,
        ::boost::fusion::random_access_traversal_tag
    >
{
public:
    typedef ::boost::mpl::int_<N> index;
    explicit tuple_iterator(Tuple& t_arg) : t(&t_arg) {}
    template<class It>
    struct value_of
    {
        typedef typename ::boost::type_erasure::detail::get_impl<
            It::index::value,
            Tuple
        >::value_type type;
    };
    template<class It>
    struct deref :
        ::boost::type_erasure::detail::get_impl<It::index::value, Tuple>
    {
        typedef typename ::boost::type_erasure::detail::get_impl<
            It::index::value,
            Tuple
        >::type type;
        static type call(It it)
        {
            return ::boost::type_erasure::detail::get_impl<
                It::index::value,
                Tuple
            >::call(*it.t);
        }
    };
    template<class It, class M>
    struct advance
    {
        typedef tuple_iterator<Tuple, (It::index::value+M::value)> type;
        static type call(It it) { return type(*it.t); }
    };
    template<class It>
    struct next : advance<It, ::boost::mpl::int_<1> > {};
    template<class It>
    struct prior : advance<It, ::boost::mpl::int_<-1> > {};
    template<class It1, class It2>
    struct distance
    {
        typedef typename ::boost::mpl::minus<
            typename It2::index,
            typename It1::index
        >::type type;
        static type call(It1, It2) { return type(); }
    };
private:
    Tuple* t;
};

/** INTERNAL ONLY */
template<class Derived>
struct tuple_base :
    ::boost::fusion::sequence_facade<
        Derived,
        ::boost::fusion::random_access_traversal_tag
    >
{
    template<class Seq>
    struct begin
    {
        typedef ::boost::type_erasure::tuple_iterator<Seq, 0> type;
        static type call(Seq& seq) { return type(seq); }
    };
    template<class Seq>
    struct end
    {
        typedef ::boost::type_erasure::tuple_iterator<
            Seq,
            Seq::tuple_size::value
        > type;
        static type call(Seq& seq) { return type(seq); }
    };
    template<class Seq>
    struct size
    {
        typedef typename Seq::tuple_size type;
        static type call(Seq& seq) { return type(); }
    };
    template<class Seq>
    struct empty
    {
        typedef typename boost::mpl::equal_to<
            typename Seq::tuple_size,
            boost::mpl::int_<0>
        >::type type;
        static type call(Seq& seq) { return type(); }
    };
    template<class Seq, class N>
    struct at : ::boost::type_erasure::detail::get_impl<N::value, Seq> {};
    template<class Seq, class N>
    struct value_at
    {
        typedef typename ::boost::type_erasure::detail::get_impl<
            N::value,
            Seq
        >::value_type type;
    };
};

template<class Concept,
    BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(
        BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, class T, ::boost::type_erasure::na)>
class tuple;

template<
    int N,
    class Concept
    BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, class T)
>
typename detail::get_impl<
    N,
    tuple<
        Concept
        BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, T)
    >
>::type get(
    tuple<
        Concept
        BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, T)
    >& arg)
{
    return detail::get_impl<
        N,
        tuple<
            Concept
            BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, T)
        >
    >::call(arg);
}

template<
    int N,
    class Concept
    BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, class T)
>
typename detail::get_impl<
    N,
    const tuple<
        Concept
        BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, T)
    >
>::type get(
    const tuple<
        Concept
        BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, T)
    >& arg)
{
    return detail::get_impl<
        N,
        const tuple<
            Concept
            BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, T)
        >
    >::call(arg);
}
    
/** INTERNAL ONLY */
#define BOOST_PP_FILENAME_1 <boost/type_erasure/tuple.hpp>
/** INTERNAL ONLY */
#define BOOST_PP_ITERATION_LIMITS (0, BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE)
#include BOOST_PP_ITERATE()

}
}

#endif

#endif

#else

#define N BOOST_PP_ITERATION()

#define BOOST_TYPE_ERASURE_TAG_TYPEDEF(z, n, data)                          \
    typedef BOOST_PP_CAT(T, n) BOOST_PP_CAT(tag_type, n);                   \
    typedef typename ::boost::remove_reference<BOOST_PP_CAT(T, n)>::type    \
        BOOST_PP_CAT(tag, n);

#define BOOST_TYPE_ERASURE_PAIR(z, n, data) \
    ::boost::mpl::pair<BOOST_PP_CAT(tag, n), BOOST_PP_CAT(U, n)>

#define BOOST_TYPE_ERASURE_CONSTRUCT(z, n, data)\
    BOOST_PP_CAT(t, n)(BOOST_PP_CAT(u, n), table)

#define BOOST_TYPE_ERASURE_TUPLE_MEMBER(z, n, data)\
    ::boost::type_erasure::any<Concept, BOOST_PP_CAT(T, n)> BOOST_PP_CAT(t, n);

#if N == 1
#define BOOST_TYPE_ERASURE_EXPLICIT explicit
#else
#define BOOST_TYPE_ERASURE_EXPLICIT
#endif

namespace detail {
    
template<class Concept BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
struct tuple_storage
#if N != BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE
    <Concept BOOST_PP_ENUM_TRAILING_PARAMS(N, T)>
#endif
{
#if N
    template<class Table BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)>
    tuple_storage(
        const Table& table BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, &u))
        :
        BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_CONSTRUCT, ~) {}
#else
    template<class Table>
    explicit tuple_storage(const Table&) {}
#endif
    BOOST_PP_REPEAT(N, BOOST_TYPE_ERASURE_TUPLE_MEMBER, `)
};

#if N != BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE

template<class Tuple>
struct get_impl<N, Tuple>
{
    typedef any<
        typename Tuple::concept_type,
        typename Tuple::BOOST_PP_CAT(tag_type, N)
    > value_type;
    typedef value_type& type;
    static type call(Tuple& arg)
    { return arg.impl.BOOST_PP_CAT(t, N); }
};

template<class Tuple>
struct get_impl<N, const Tuple>
{
    typedef any<
        typename Tuple::concept_type,
        typename Tuple::BOOST_PP_CAT(tag_type, N)
    > value_type;
    typedef const value_type& type;
    static type call(const Tuple& arg)
    { return arg.impl.BOOST_PP_CAT(t, N); }
};

#endif

}

template<class Concept BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
class tuple 
#if N != BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE
    <Concept BOOST_PP_ENUM_TRAILING_PARAMS(N, T)>
#endif
    : public tuple_base<tuple<Concept BOOST_PP_ENUM_TRAILING_PARAMS(N, T)> >
{
    typedef Concept concept_type;
    BOOST_PP_REPEAT(N, BOOST_TYPE_ERASURE_TAG_TYPEDEF, ~)
public:
    typedef ::boost::mpl::int_<N> tuple_size;
#if N
    template<BOOST_PP_ENUM_PARAMS(N, class U)>
#endif
    BOOST_TYPE_ERASURE_EXPLICIT
    tuple(BOOST_PP_ENUM_BINARY_PARAMS(N, U, &u)) :
        impl(
            ::boost::type_erasure::make_binding<
                ::boost::mpl::map<
                    BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_PAIR, ~)
                >
            >()
            BOOST_PP_ENUM_TRAILING_PARAMS(N, u)
        )
    {}
#if N
    template<BOOST_PP_ENUM_PARAMS(N, class U)>
    BOOST_TYPE_ERASURE_EXPLICIT 
    tuple(BOOST_PP_ENUM_BINARY_PARAMS(N, const U, &u)) :
        impl(
            ::boost::type_erasure::make_binding<
                ::boost::mpl::map<
                    BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_PAIR, ~)
                >
            >()
            BOOST_PP_ENUM_TRAILING_PARAMS(N, u)
        )
    {}
#endif
private:
    template<int M, class Tuple>
    friend struct ::boost::type_erasure::detail::get_impl;
    ::boost::type_erasure::detail::tuple_storage<
        Concept
        BOOST_PP_ENUM_TRAILING_PARAMS(N, T)
    > impl;
};

#undef BOOST_TYPE_ERASURE_EXPLICIT
#undef BOOST_TYPE_ERASURE_TUPLE_MEMBER
#undef BOOST_TYPE_ERASURE_CONSTRUCT
#undef BOOST_TYPE_ERASURE_PAIR
#undef BOOST_TYPE_ERASURE_TAG_TYPEDEF
#undef N

#endif