1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2008-2013. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_DETAIL_VARIADIC_TEMPLATES_TOOLS_HPP
12 #define BOOST_CONTAINER_DETAIL_VARIADIC_TEMPLATES_TOOLS_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
24 #include <boost/move/utility_core.hpp>
26 #include <boost/container/detail/type_traits.hpp>
27 #include <cstddef> //std::size_t
31 namespace container_detail {
33 template<typename... Values>
36 template<> class tuple<>
39 template<typename Head, typename... Tail>
40 class tuple<Head, Tail...>
41 : private tuple<Tail...>
43 typedef tuple<Tail...> inherited;
47 : inherited(), m_head()
50 template<class U, class ...Args>
51 tuple(U &&u, Args && ...args)
52 : inherited(::boost::forward<Args>(args)...), m_head(::boost::forward<U>(u))
55 // Construct tuple from another tuple.
56 template<typename... VValues>
57 tuple(const tuple<VValues...>& other)
58 : inherited(other.tail()), m_head(other.head())
61 template<typename... VValues>
62 tuple& operator=(const tuple<VValues...>& other)
64 m_head = other.head();
65 tail() = other.tail();
69 typename add_reference<Head>::type head() { return m_head; }
70 typename add_reference<const Head>::type head() const { return m_head; }
72 inherited& tail() { return *this; }
73 const inherited& tail() const { return *this; }
80 template<typename... Values>
81 tuple<Values&&...> forward_as_tuple(Values&&... values)
82 { return tuple<Values&&...>(::boost::forward<Values>(values)...); }
84 template<int I, typename Tuple>
87 template<int I, typename Head, typename... Tail>
88 struct tuple_element<I, tuple<Head, Tail...> >
90 typedef typename tuple_element<I-1, tuple<Tail...> >::type type;
93 template<typename Head, typename... Tail>
94 struct tuple_element<0, tuple<Head, Tail...> >
99 template<int I, typename Tuple>
102 template<int I, typename Head, typename... Values>
103 class get_impl<I, tuple<Head, Values...> >
105 typedef typename tuple_element<I-1, tuple<Values...> >::type Element;
106 typedef get_impl<I-1, tuple<Values...> > Next;
109 typedef typename add_reference<Element>::type type;
110 typedef typename add_const_reference<Element>::type const_type;
111 static type get(tuple<Head, Values...>& t) { return Next::get(t.tail()); }
112 static const_type get(const tuple<Head, Values...>& t) { return Next::get(t.tail()); }
115 template<typename Head, typename... Values>
116 class get_impl<0, tuple<Head, Values...> >
119 typedef typename add_reference<Head>::type type;
120 typedef typename add_const_reference<Head>::type const_type;
121 static type get(tuple<Head, Values...>& t) { return t.head(); }
122 static const_type get(const tuple<Head, Values...>& t){ return t.head(); }
125 template<int I, typename... Values>
126 typename get_impl<I, tuple<Values...> >::type get(tuple<Values...>& t)
127 { return get_impl<I, tuple<Values...> >::get(t); }
129 template<int I, typename... Values>
130 typename get_impl<I, tuple<Values...> >::const_type get(const tuple<Values...>& t)
131 { return get_impl<I, tuple<Values...> >::get(t); }
133 ////////////////////////////////////////////////////
134 // Builds an index_tuple<0, 1, 2, ..., Num-1>, that will
135 // be used to "unpack" into comma-separated values
136 // in a function call.
137 ////////////////////////////////////////////////////
139 template<std::size_t ... Indexes>
140 struct index_tuple{};
142 template<std::size_t Num, typename Tuple = index_tuple<> >
143 struct build_number_seq;
145 template<std::size_t Num, std::size_t ... Indexes>
146 struct build_number_seq<Num, index_tuple<Indexes...> >
147 : build_number_seq<Num - 1, index_tuple<Indexes..., sizeof...(Indexes)> >
150 template<std::size_t ... Indexes>
151 struct build_number_seq<0, index_tuple<Indexes...> >
152 { typedef index_tuple<Indexes...> type; };
155 }}} //namespace boost { namespace container { namespace container_detail {
157 #include <boost/container/detail/config_end.hpp>
159 #endif //#ifndef BOOST_CONTAINER_DETAIL_VARIADIC_TEMPLATES_TOOLS_HPP