blob: 57581e842862b26a0d204c888a8ef0fac54171fd [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___TUPLE
12#define _LIBCPP___TUPLE
13
14#include <__config>
15#include <cstddef>
16#include <type_traits>
17
Howard Hinnant08e17472011-10-17 20:05:10 +000018#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000019#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000020#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000022
23_LIBCPP_BEGIN_NAMESPACE_STD
24
Howard Hinnant0f678bd2013-08-12 18:38:34 +000025template <class _Tp> class _LIBCPP_TYPE_VIS_ONLY tuple_size;
Howard Hinnant86ed8ae2010-11-17 19:22:43 +000026
27template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000028class _LIBCPP_TYPE_VIS_ONLY tuple_size<const _Tp>
Howard Hinnant86ed8ae2010-11-17 19:22:43 +000029 : public tuple_size<_Tp> {};
30
31template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000032class _LIBCPP_TYPE_VIS_ONLY tuple_size<volatile _Tp>
Howard Hinnant86ed8ae2010-11-17 19:22:43 +000033 : public tuple_size<_Tp> {};
34
35template <class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000036class _LIBCPP_TYPE_VIS_ONLY tuple_size<const volatile _Tp>
Howard Hinnant86ed8ae2010-11-17 19:22:43 +000037 : public tuple_size<_Tp> {};
38
Howard Hinnant0f678bd2013-08-12 18:38:34 +000039template <size_t _Ip, class _Tp> class _LIBCPP_TYPE_VIS_ONLY tuple_element;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000040
Howard Hinnant86ed8ae2010-11-17 19:22:43 +000041template <size_t _Ip, class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000042class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, const _Tp>
Howard Hinnant86ed8ae2010-11-17 19:22:43 +000043{
44public:
45 typedef typename add_const<typename tuple_element<_Ip, _Tp>::type>::type type;
46};
47
48template <size_t _Ip, class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000049class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, volatile _Tp>
Howard Hinnant86ed8ae2010-11-17 19:22:43 +000050{
51public:
52 typedef typename add_volatile<typename tuple_element<_Ip, _Tp>::type>::type type;
53};
54
55template <size_t _Ip, class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000056class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, const volatile _Tp>
Howard Hinnant86ed8ae2010-11-17 19:22:43 +000057{
58public:
59 typedef typename add_cv<typename tuple_element<_Ip, _Tp>::type>::type type;
60};
61
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000062template <class _Tp> struct __tuple_like : false_type {};
63
Howard Hinnant0e1493e2010-12-11 20:47:50 +000064template <class _Tp> struct __tuple_like<const _Tp> : public __tuple_like<_Tp> {};
65template <class _Tp> struct __tuple_like<volatile _Tp> : public __tuple_like<_Tp> {};
66template <class _Tp> struct __tuple_like<const volatile _Tp> : public __tuple_like<_Tp> {};
67
Eric Fiselier8e706d22015-03-17 15:08:03 +000068// tuple specializations
69
70#if !defined(_LIBCPP_HAS_NO_VARIADICS)
71template <class ..._Tp> class _LIBCPP_TYPE_VIS_ONLY tuple;
72
Howard Hinnantf83417b2011-01-24 16:07:25 +000073template <class... _Tp> struct __tuple_like<tuple<_Tp...> > : true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000074
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075template <size_t _Ip, class ..._Tp>
Marshall Clow8fc4f5a2013-07-17 18:25:36 +000076_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantf83417b2011-01-24 16:07:25 +000077typename tuple_element<_Ip, tuple<_Tp...> >::type&
Howard Hinnanta5e01212011-05-27 19:08:18 +000078get(tuple<_Tp...>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079
80template <size_t _Ip, class ..._Tp>
Marshall Clow8fc4f5a2013-07-17 18:25:36 +000081_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantf83417b2011-01-24 16:07:25 +000082const typename tuple_element<_Ip, tuple<_Tp...> >::type&
Howard Hinnanta5e01212011-05-27 19:08:18 +000083get(const tuple<_Tp...>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084
Howard Hinnantcd2254b2010-11-17 19:52:17 +000085template <size_t _Ip, class ..._Tp>
Marshall Clow8fc4f5a2013-07-17 18:25:36 +000086_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantf83417b2011-01-24 16:07:25 +000087typename tuple_element<_Ip, tuple<_Tp...> >::type&&
Howard Hinnanta5e01212011-05-27 19:08:18 +000088get(tuple<_Tp...>&&) _NOEXCEPT;
Eric Fiselier8e706d22015-03-17 15:08:03 +000089#endif
90
91// pair specializations
92
93template <class _T1, class _T2> struct _LIBCPP_TYPE_VIS_ONLY pair;
94
95template <class _T1, class _T2> struct __tuple_like<pair<_T1, _T2> > : true_type {};
Howard Hinnantcd2254b2010-11-17 19:52:17 +000096
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000097template <size_t _Ip, class _T1, class _T2>
Marshall Clow8fc4f5a2013-07-17 18:25:36 +000098_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000099typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000100get(pair<_T1, _T2>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000101
102template <size_t _Ip, class _T1, class _T2>
Marshall Clow8fc4f5a2013-07-17 18:25:36 +0000103_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000104const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000105get(const pair<_T1, _T2>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106
Eric Fiselier8e706d22015-03-17 15:08:03 +0000107#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000108template <size_t _Ip, class _T1, class _T2>
Marshall Clow8fc4f5a2013-07-17 18:25:36 +0000109_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000110typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
Howard Hinnanta5e01212011-05-27 19:08:18 +0000111get(pair<_T1, _T2>&&) _NOEXCEPT;
Eric Fiselier8e706d22015-03-17 15:08:03 +0000112#endif
113
114// array specializations
115
116template <class _Tp, size_t _Size> struct _LIBCPP_TYPE_VIS_ONLY array;
117
118template <class _Tp, size_t _Size> struct __tuple_like<array<_Tp, _Size> > : true_type {};
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000119
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000120template <size_t _Ip, class _Tp, size_t _Size>
Marshall Clow8fc4f5a2013-07-17 18:25:36 +0000121_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000122_Tp&
Howard Hinnantf0562af2011-05-31 21:06:33 +0000123get(array<_Tp, _Size>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000124
125template <size_t _Ip, class _Tp, size_t _Size>
Marshall Clow8fc4f5a2013-07-17 18:25:36 +0000126_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000127const _Tp&
Howard Hinnantf0562af2011-05-31 21:06:33 +0000128get(const array<_Tp, _Size>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129
Eric Fiselier8e706d22015-03-17 15:08:03 +0000130#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000131template <size_t _Ip, class _Tp, size_t _Size>
Marshall Clow8fc4f5a2013-07-17 18:25:36 +0000132_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000133_Tp&&
Howard Hinnantf0562af2011-05-31 21:06:33 +0000134get(array<_Tp, _Size>&&) _NOEXCEPT;
Eric Fiselier8e706d22015-03-17 15:08:03 +0000135#endif
136
137#if !defined(_LIBCPP_HAS_NO_VARIADICS)
138
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000139// __make_tuple_indices
140
141template <size_t...> struct __tuple_indices {};
142
143template <size_t _Sp, class _IntTuple, size_t _Ep>
144struct __make_indices_imp;
145
146template <size_t _Sp, size_t ..._Indices, size_t _Ep>
147struct __make_indices_imp<_Sp, __tuple_indices<_Indices...>, _Ep>
148{
149 typedef typename __make_indices_imp<_Sp+1, __tuple_indices<_Indices..., _Sp>, _Ep>::type type;
150};
151
152template <size_t _Ep, size_t ..._Indices>
153struct __make_indices_imp<_Ep, __tuple_indices<_Indices...>, _Ep>
154{
155 typedef __tuple_indices<_Indices...> type;
156};
157
158template <size_t _Ep, size_t _Sp = 0>
159struct __make_tuple_indices
160{
161 static_assert(_Sp <= _Ep, "__make_tuple_indices input error");
162 typedef typename __make_indices_imp<_Sp, __tuple_indices<>, _Ep>::type type;
163};
164
165// __tuple_types
166
167template <class ..._Tp> struct __tuple_types {};
168
169template <size_t _Ip>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000170class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, __tuple_types<> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000171{
172public:
173 static_assert(_Ip == 0, "tuple_element index out of range");
174 static_assert(_Ip != 0, "tuple_element index out of range");
175};
176
177template <class _Hp, class ..._Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000178class _LIBCPP_TYPE_VIS_ONLY tuple_element<0, __tuple_types<_Hp, _Tp...> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179{
180public:
181 typedef _Hp type;
182};
183
184template <size_t _Ip, class _Hp, class ..._Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000185class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, __tuple_types<_Hp, _Tp...> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000186{
187public:
Howard Hinnantf83417b2011-01-24 16:07:25 +0000188 typedef typename tuple_element<_Ip-1, __tuple_types<_Tp...> >::type type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189};
190
191template <class ..._Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000192class _LIBCPP_TYPE_VIS_ONLY tuple_size<__tuple_types<_Tp...> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000193 : public integral_constant<size_t, sizeof...(_Tp)>
194{
195};
196
Howard Hinnantf83417b2011-01-24 16:07:25 +0000197template <class... _Tp> struct __tuple_like<__tuple_types<_Tp...> > : true_type {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000198
199// __make_tuple_types
200
Howard Hinnante049cc52010-09-27 17:54:17 +0000201// __make_tuple_types<_Tuple<_Types...>, _Ep, _Sp>::type is a
202// __tuple_types<_Types...> using only those _Types in the range [_Sp, _Ep).
203// _Sp defaults to 0 and _Ep defaults to tuple_size<_Tuple>. If _Tuple is a
204// lvalue_reference type, then __tuple_types<_Types&...> is the result.
205
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000206template <class _TupleTypes, class _Tp, size_t _Sp, size_t _Ep>
207struct __make_tuple_types_imp;
208
209template <class ..._Types, class _Tp, size_t _Sp, size_t _Ep>
210struct __make_tuple_types_imp<__tuple_types<_Types...>, _Tp, _Sp, _Ep>
211{
212 typedef typename remove_reference<_Tp>::type _Tpr;
213 typedef typename __make_tuple_types_imp<__tuple_types<_Types...,
Howard Hinnante049cc52010-09-27 17:54:17 +0000214 typename conditional<is_lvalue_reference<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000215 typename tuple_element<_Sp, _Tpr>::type&,
216 typename tuple_element<_Sp, _Tpr>::type>::type>,
217 _Tp, _Sp+1, _Ep>::type type;
218};
219
220template <class ..._Types, class _Tp, size_t _Ep>
221struct __make_tuple_types_imp<__tuple_types<_Types...>, _Tp, _Ep, _Ep>
222{
223 typedef __tuple_types<_Types...> type;
224};
225
226template <class _Tp, size_t _Ep = tuple_size<typename remove_reference<_Tp>::type>::value, size_t _Sp = 0>
227struct __make_tuple_types
228{
229 static_assert(_Sp <= _Ep, "__make_tuple_types input error");
230 typedef typename __make_tuple_types_imp<__tuple_types<>, _Tp, _Sp, _Ep>::type type;
231};
232
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000233// __tuple_convertible
234
Eric Fiselier522aaf62014-10-28 06:31:22 +0000235template <class, class>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000236struct __tuple_convertible_imp : public false_type {};
237
238template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
Eric Fiselier522aaf62014-10-28 06:31:22 +0000239struct __tuple_convertible_imp<__tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000240 : public integral_constant<bool,
Howard Hinnantdc1345f2012-04-01 23:10:42 +0000241 is_convertible<_Tp0, _Up0>::value &&
Eric Fiselier522aaf62014-10-28 06:31:22 +0000242 __tuple_convertible_imp<__tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243
244template <>
Eric Fiselier522aaf62014-10-28 06:31:22 +0000245struct __tuple_convertible_imp<__tuple_types<>, __tuple_types<> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000246 : public true_type {};
247
Eric Fiselier522aaf62014-10-28 06:31:22 +0000248template <bool, class, class>
249struct __tuple_convertible_apply : public false_type {};
250
251template <class _Tp, class _Up>
252struct __tuple_convertible_apply<true, _Tp, _Up>
253 : public __tuple_convertible_imp<
254 typename __make_tuple_types<_Tp>::type
255 , typename __make_tuple_types<_Up>::type
256 >
257{};
258
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
260 bool = __tuple_like<_Up>::value>
261struct __tuple_convertible
262 : public false_type {};
263
264template <class _Tp, class _Up>
265struct __tuple_convertible<_Tp, _Up, true, true>
Eric Fiselier522aaf62014-10-28 06:31:22 +0000266 : public __tuple_convertible_apply<tuple_size<typename remove_reference<_Tp>::type>::value ==
267 tuple_size<_Up>::value, _Tp, _Up>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268{};
269
Howard Hinnantdc1345f2012-04-01 23:10:42 +0000270// __tuple_constructible
271
Eric Fiselier522aaf62014-10-28 06:31:22 +0000272template <class, class>
Howard Hinnantdc1345f2012-04-01 23:10:42 +0000273struct __tuple_constructible_imp : public false_type {};
274
275template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
Eric Fiselier522aaf62014-10-28 06:31:22 +0000276struct __tuple_constructible_imp<__tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
Howard Hinnantdc1345f2012-04-01 23:10:42 +0000277 : public integral_constant<bool,
278 is_constructible<_Up0, _Tp0>::value &&
Eric Fiselier522aaf62014-10-28 06:31:22 +0000279 __tuple_constructible_imp<__tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
Howard Hinnantdc1345f2012-04-01 23:10:42 +0000280
281template <>
Eric Fiselier522aaf62014-10-28 06:31:22 +0000282struct __tuple_constructible_imp<__tuple_types<>, __tuple_types<> >
Howard Hinnantdc1345f2012-04-01 23:10:42 +0000283 : public true_type {};
284
Eric Fiselier522aaf62014-10-28 06:31:22 +0000285template <bool _SameSize, class, class>
286struct __tuple_constructible_apply : public false_type {};
287
288template <class _Tp, class _Up>
289struct __tuple_constructible_apply<true, _Tp, _Up>
290 : public __tuple_constructible_imp<
291 typename __make_tuple_types<_Tp>::type
292 , typename __make_tuple_types<_Up>::type
293 >
294{};
295
Howard Hinnantdc1345f2012-04-01 23:10:42 +0000296template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
297 bool = __tuple_like<_Up>::value>
298struct __tuple_constructible
299 : public false_type {};
300
301template <class _Tp, class _Up>
302struct __tuple_constructible<_Tp, _Up, true, true>
Eric Fiselier522aaf62014-10-28 06:31:22 +0000303 : public __tuple_constructible_apply<tuple_size<typename remove_reference<_Tp>::type>::value ==
304 tuple_size<_Up>::value, _Tp, _Up>
Howard Hinnantdc1345f2012-04-01 23:10:42 +0000305{};
306
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000307// __tuple_assignable
308
Eric Fiselier522aaf62014-10-28 06:31:22 +0000309template <class, class>
Howard Hinnante049cc52010-09-27 17:54:17 +0000310struct __tuple_assignable_imp : public false_type {};
311
312template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
Eric Fiselier522aaf62014-10-28 06:31:22 +0000313struct __tuple_assignable_imp<__tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
Howard Hinnante049cc52010-09-27 17:54:17 +0000314 : public integral_constant<bool,
Howard Hinnantf048fe32010-11-20 18:25:22 +0000315 is_assignable<_Up0&, _Tp0>::value &&
Eric Fiselier522aaf62014-10-28 06:31:22 +0000316 __tuple_assignable_imp<__tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
Howard Hinnante049cc52010-09-27 17:54:17 +0000317
318template <>
Eric Fiselier522aaf62014-10-28 06:31:22 +0000319struct __tuple_assignable_imp<__tuple_types<>, __tuple_types<> >
Howard Hinnante049cc52010-09-27 17:54:17 +0000320 : public true_type {};
321
Eric Fiselier522aaf62014-10-28 06:31:22 +0000322template <bool, class, class>
323struct __tuple_assignable_apply : public false_type {};
324
325template <class _Tp, class _Up>
326struct __tuple_assignable_apply<true, _Tp, _Up>
327 : __tuple_assignable_imp<
328 typename __make_tuple_types<_Tp>::type
329 , typename __make_tuple_types<_Up>::type
330 >
331{};
332
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
334 bool = __tuple_like<_Up>::value>
335struct __tuple_assignable
336 : public false_type {};
337
338template <class _Tp, class _Up>
339struct __tuple_assignable<_Tp, _Up, true, true>
Eric Fiselier522aaf62014-10-28 06:31:22 +0000340 : public __tuple_assignable_apply<tuple_size<typename remove_reference<_Tp>::type>::value ==
341 tuple_size<_Up>::value, _Tp, _Up>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000342{};
343
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000344#endif // _LIBCPP_HAS_NO_VARIADICS
345
Eric Fiselier8e706d22015-03-17 15:08:03 +0000346_LIBCPP_END_NAMESPACE_STD
347
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000348#endif // _LIBCPP___TUPLE