1 // Boost.TypeErasure library
3 // Copyright 2011-2012 Steven Watanabe
5 // Distributed under the Boost Software License Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
11 #if !defined(BOOST_PP_IS_ITERATING)
13 #ifndef BOOST_TYPE_ERASURE_TUPLE_HPP_INCLUDED
14 #define BOOST_TYPE_ERASURE_TUPLE_HPP_INCLUDED
16 #include <boost/config.hpp>
19 #ifdef BOOST_TYPE_ERASURE_DOXYGEN
22 namespace type_erasure {
25 * @ref tuple is a Boost.Fusion Random Access Sequence containing
26 * @ref any "anys". @c Concept specifies the \Concept for each
27 * of the elements. The remaining arguments must be (possibly const
28 * and/or reference qualified) placeholders, which are the
29 * @ref placeholder "placeholders" of the elements.
31 template<class Concept, class... T>
36 * Constructs a tuple. Each element of @c args will
37 * be used to initialize the corresponding @ref any member.
38 * The @ref binding for the tuple elements is determined
39 * by mapping the placeholders in @c T to the corresponding
43 explicit tuple(U&&... args);
47 * Returns the Nth @ref any in the tuple.
49 template<int N, class Concept, class... T>
50 any<Concept, TN>& get(tuple<Concept, T...>& arg);
52 template<int N, class Concept, class... T>
53 const any<Concept, TN>& get(const tuple<Concept, T...>& arg);
58 #elif !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
60 #include <boost/mpl/int.hpp>
61 #include <boost/mpl/bool.hpp>
62 #include <boost/mpl/map.hpp>
63 #include <boost/mpl/insert.hpp>
64 #include <boost/type_traits/remove_reference.hpp>
65 #include <boost/type_traits/remove_const.hpp>
66 #include <boost/fusion/include/category_of.hpp>
67 #include <boost/fusion/include/iterator_facade.hpp>
68 #include <boost/fusion/include/sequence_facade.hpp>
69 #include <boost/type_erasure/any.hpp>
70 #include <boost/type_erasure/static_binding.hpp>
71 #include <boost/type_erasure/config.hpp>
74 namespace type_erasure {
76 template<class Concept, class... T>
79 template<class Concept>
82 template<class Binding>
83 cons(const Binding&) {}
86 template<class Concept, class T0, class... T>
87 struct cons<Concept, T0, T...>
89 typedef any<Concept, T0> value_type;
90 typedef cons<Concept, T...> rest_type;
91 template<class Binding, class U0, class... U>
92 cons(const Binding& b, U0&& u0, U&&... u)
93 : value(std::forward<U0>(u0), b),
94 rest(b, std::forward<U>(u)...)
96 any<Concept, T0> value;
97 cons<Concept, T...> rest;
102 template<int N, class Cons>
105 typedef typename cons_advance<N-1, Cons>::type::rest_type type;
106 static const type& call(const Cons& c)
108 return cons_advance<N-1, Cons>::call(c).rest;
113 struct cons_advance<0, Cons>
116 static const type& call(const Cons& c)
125 template<class T0, class... T>
126 struct make_map<T0, T...>
128 typedef typename ::boost::mpl::insert<
129 typename ::boost::type_erasure::detail::make_map<T...>::type,
137 typedef ::boost::mpl::map0<> type;
143 template<class Tuple, int N>
144 class tuple_iterator :
145 public ::boost::fusion::iterator_facade<
146 tuple_iterator<Tuple, N>,
147 ::boost::fusion::random_access_traversal_tag
151 typedef ::boost::mpl::int_<N> index;
152 explicit tuple_iterator(Tuple& t_arg) : t(&t_arg) {}
156 typedef typename Tuple::template value_at<Tuple, mpl::int_<N> >::type type;
161 typedef typename Tuple::template at<Tuple, mpl::int_<N> >::type type;
162 static type call(It it)
164 return Tuple::template at<Tuple, mpl::int_<N> >::call(*it.t);
167 template<class It, class M>
170 typedef tuple_iterator<Tuple, (It::index::value+M::value)> type;
171 static type call(It it) { return type(*it.t); }
174 struct next : advance<It, ::boost::mpl::int_<1> > {};
176 struct prior : advance<It, ::boost::mpl::int_<-1> > {};
177 template<class It1, class It2>
180 typedef typename ::boost::mpl::minus<
184 static type call(It1, It2) { return type(); }
190 template<class Concept, class... T>
192 public ::boost::fusion::sequence_facade<
193 ::boost::type_erasure::tuple<Concept, T...>,
194 ::boost::fusion::forward_traversal_tag
199 explicit tuple(U&&... args)
201 ::boost::type_erasure::make_binding<
202 typename ::boost::type_erasure::detail::make_map<
204 typename ::boost::remove_const<
205 typename ::boost::remove_reference<T>::type
207 typename ::boost::remove_const<
208 typename ::boost::remove_reference<U>::type
213 std::forward<U>(args)...)
219 typedef ::boost::type_erasure::tuple_iterator<
223 static type call(Seq& seq) { return type(seq); }
228 typedef ::boost::type_erasure::tuple_iterator<
232 static type call(Seq& seq) { return type(seq); }
237 typedef ::boost::mpl::int_<sizeof...(T)> type;
238 static type call(Seq& seq) { return type(); }
243 typedef ::boost::mpl::bool_<sizeof...(T) == 0> type;
244 static type call(Seq& seq) { return type(); }
246 template<class Seq, class N>
249 typedef typename ::boost::type_erasure::detail::cons_advance<
251 ::boost::type_erasure::cons<Concept, T...>
252 >::type::value_type value_type;
253 typedef typename ::boost::mpl::if_< ::boost::is_const<Seq>,
257 static type call(Seq& seq)
259 return const_cast<type>(
260 ::boost::type_erasure::detail::cons_advance<
262 ::boost::type_erasure::cons<Concept, T...>
263 >::call(seq.impl).value
267 template<class Seq, class N>
270 typedef typename ::boost::type_erasure::detail::cons_advance<
272 ::boost::type_erasure::cons<Concept, T...>
273 >::type::value_type value_type;
275 ::boost::type_erasure::cons<Concept, T...> impl;
278 template<int N, class Concept, class... T>
279 typename ::boost::type_erasure::detail::cons_advance<
281 ::boost::type_erasure::cons<Concept, T...>
282 >::type::value_type& get(::boost::type_erasure::tuple<Concept, T...>& t)
285 typename ::boost::type_erasure::detail::cons_advance<
287 ::boost::type_erasure::cons<Concept, T...>
290 ::boost::type_erasure::detail::cons_advance<N,
291 ::boost::type_erasure::cons<Concept, T...>
292 >::call(t.impl).value
296 template<int N, class Concept, class... T>
297 const typename ::boost::type_erasure::detail::cons_advance<
299 ::boost::type_erasure::cons<Concept, T...>
300 >::type::value_type& get(const ::boost::type_erasure::tuple<Concept, T...>& t)
302 return ::boost::type_erasure::detail::cons_advance<
304 ::boost::type_erasure::cons<Concept, T...>
305 >::call(t.impl).value;
313 #include <boost/mpl/int.hpp>
314 #include <boost/mpl/minus.hpp>
315 #include <boost/mpl/equal_to.hpp>
316 #include <boost/mpl/map.hpp>
317 #include <boost/fusion/include/category_of.hpp>
318 #include <boost/fusion/include/iterator_facade.hpp>
319 #include <boost/fusion/include/sequence_facade.hpp>
320 #include <boost/preprocessor/cat.hpp>
321 #include <boost/preprocessor/iteration/iterate.hpp>
322 #include <boost/preprocessor/repetition/repeat.hpp>
323 #include <boost/preprocessor/repetition/enum.hpp>
324 #include <boost/preprocessor/repetition/enum_params.hpp>
325 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
326 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
327 #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
328 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
329 #include <boost/type_erasure/any.hpp>
330 #include <boost/type_erasure/static_binding.hpp>
331 #include <boost/type_erasure/config.hpp>
334 namespace type_erasure {
341 template<int N, class Tuple>
344 template<class Concept,
345 BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(
346 BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, class T, ::boost::type_erasure::na)>
347 struct tuple_storage;
352 template<class Tuple, int N>
353 class tuple_iterator :
354 public ::boost::fusion::iterator_facade<
355 tuple_iterator<Tuple, N>,
356 ::boost::fusion::random_access_traversal_tag
360 typedef ::boost::mpl::int_<N> index;
361 explicit tuple_iterator(Tuple& t_arg) : t(&t_arg) {}
365 typedef typename ::boost::type_erasure::detail::get_impl<
372 ::boost::type_erasure::detail::get_impl<It::index::value, Tuple>
374 typedef typename ::boost::type_erasure::detail::get_impl<
378 static type call(It it)
380 return ::boost::type_erasure::detail::get_impl<
386 template<class It, class M>
389 typedef tuple_iterator<Tuple, (It::index::value+M::value)> type;
390 static type call(It it) { return type(*it.t); }
393 struct next : advance<It, ::boost::mpl::int_<1> > {};
395 struct prior : advance<It, ::boost::mpl::int_<-1> > {};
396 template<class It1, class It2>
399 typedef typename ::boost::mpl::minus<
403 static type call(It1, It2) { return type(); }
410 template<class Derived>
412 ::boost::fusion::sequence_facade<
414 ::boost::fusion::random_access_traversal_tag
420 typedef ::boost::type_erasure::tuple_iterator<Seq, 0> type;
421 static type call(Seq& seq) { return type(seq); }
426 typedef ::boost::type_erasure::tuple_iterator<
428 Seq::tuple_size::value
430 static type call(Seq& seq) { return type(seq); }
435 typedef typename Seq::tuple_size type;
436 static type call(Seq& seq) { return type(); }
441 typedef typename boost::mpl::equal_to<
442 typename Seq::tuple_size,
445 static type call(Seq& seq) { return type(); }
447 template<class Seq, class N>
448 struct at : ::boost::type_erasure::detail::get_impl<N::value, Seq> {};
449 template<class Seq, class N>
452 typedef typename ::boost::type_erasure::detail::get_impl<
459 template<class Concept,
460 BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(
461 BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, class T, ::boost::type_erasure::na)>
467 BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, class T)
469 typename detail::get_impl<
473 BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, T)
478 BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, T)
481 return detail::get_impl<
485 BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, T)
493 BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, class T)
495 typename detail::get_impl<
499 BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, T)
504 BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, T)
507 return detail::get_impl<
511 BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE, T)
517 #define BOOST_PP_FILENAME_1 <boost/type_erasure/tuple.hpp>
519 #define BOOST_PP_ITERATION_LIMITS (0, BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE)
520 #include BOOST_PP_ITERATE()
531 #define N BOOST_PP_ITERATION()
533 #define BOOST_TYPE_ERASURE_TAG_TYPEDEF(z, n, data) \
534 typedef BOOST_PP_CAT(T, n) BOOST_PP_CAT(tag_type, n); \
535 typedef typename ::boost::remove_reference<BOOST_PP_CAT(T, n)>::type \
536 BOOST_PP_CAT(tag, n);
538 #define BOOST_TYPE_ERASURE_PAIR(z, n, data) \
539 ::boost::mpl::pair<BOOST_PP_CAT(tag, n), BOOST_PP_CAT(U, n)>
541 #define BOOST_TYPE_ERASURE_CONSTRUCT(z, n, data)\
542 BOOST_PP_CAT(t, n)(BOOST_PP_CAT(u, n), table)
544 #define BOOST_TYPE_ERASURE_TUPLE_MEMBER(z, n, data)\
545 ::boost::type_erasure::any<Concept, BOOST_PP_CAT(T, n)> BOOST_PP_CAT(t, n);
548 #define BOOST_TYPE_ERASURE_EXPLICIT explicit
550 #define BOOST_TYPE_ERASURE_EXPLICIT
555 template<class Concept BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
557 #if N != BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE
558 <Concept BOOST_PP_ENUM_TRAILING_PARAMS(N, T)>
562 template<class Table BOOST_PP_ENUM_TRAILING_PARAMS(N, class U)>
564 const Table& table BOOST_PP_ENUM_TRAILING_BINARY_PARAMS(N, U, &u))
566 BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_CONSTRUCT, ~) {}
568 template<class Table>
569 explicit tuple_storage(const Table&) {}
571 BOOST_PP_REPEAT(N, BOOST_TYPE_ERASURE_TUPLE_MEMBER, `)
574 #if N != BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE
576 template<class Tuple>
577 struct get_impl<N, Tuple>
580 typename Tuple::concept_type,
581 typename Tuple::BOOST_PP_CAT(tag_type, N)
583 typedef value_type& type;
584 static type call(Tuple& arg)
585 { return arg.impl.BOOST_PP_CAT(t, N); }
588 template<class Tuple>
589 struct get_impl<N, const Tuple>
592 typename Tuple::concept_type,
593 typename Tuple::BOOST_PP_CAT(tag_type, N)
595 typedef const value_type& type;
596 static type call(const Tuple& arg)
597 { return arg.impl.BOOST_PP_CAT(t, N); }
604 template<class Concept BOOST_PP_ENUM_TRAILING_PARAMS(N, class T)>
606 #if N != BOOST_TYPE_ERASURE_MAX_TUPLE_SIZE
607 <Concept BOOST_PP_ENUM_TRAILING_PARAMS(N, T)>
609 : public tuple_base<tuple<Concept BOOST_PP_ENUM_TRAILING_PARAMS(N, T)> >
611 typedef Concept concept_type;
612 BOOST_PP_REPEAT(N, BOOST_TYPE_ERASURE_TAG_TYPEDEF, ~)
614 typedef ::boost::mpl::int_<N> tuple_size;
616 template<BOOST_PP_ENUM_PARAMS(N, class U)>
618 BOOST_TYPE_ERASURE_EXPLICIT
619 tuple(BOOST_PP_ENUM_BINARY_PARAMS(N, U, &u)) :
621 ::boost::type_erasure::make_binding<
623 BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_PAIR, ~)
626 BOOST_PP_ENUM_TRAILING_PARAMS(N, u)
630 template<BOOST_PP_ENUM_PARAMS(N, class U)>
631 BOOST_TYPE_ERASURE_EXPLICIT
632 tuple(BOOST_PP_ENUM_BINARY_PARAMS(N, const U, &u)) :
634 ::boost::type_erasure::make_binding<
636 BOOST_PP_ENUM(N, BOOST_TYPE_ERASURE_PAIR, ~)
639 BOOST_PP_ENUM_TRAILING_PARAMS(N, u)
644 template<int M, class Tuple>
645 friend struct ::boost::type_erasure::detail::get_impl;
646 ::boost::type_erasure::detail::tuple_storage<
648 BOOST_PP_ENUM_TRAILING_PARAMS(N, T)
652 #undef BOOST_TYPE_ERASURE_EXPLICIT
653 #undef BOOST_TYPE_ERASURE_TUPLE_MEMBER
654 #undef BOOST_TYPE_ERASURE_CONSTRUCT
655 #undef BOOST_TYPE_ERASURE_PAIR
656 #undef BOOST_TYPE_ERASURE_TAG_TYPEDEF