1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___TUPLE
12#define _LIBCPP___TUPLE
13
14#include <__config>
15#include <cstddef>
16#include <type_traits>
17
18#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19#pragma GCC system_header
20#endif
21
22
23_LIBCPP_BEGIN_NAMESPACE_STD
24
25template <class _Tp> class _LIBCPP_TYPE_VIS_ONLY tuple_size;
26
27template <class _Tp>
28class _LIBCPP_TYPE_VIS_ONLY tuple_size<const _Tp>
29    : public tuple_size<_Tp> {};
30
31template <class _Tp>
32class _LIBCPP_TYPE_VIS_ONLY tuple_size<volatile _Tp>
33    : public tuple_size<_Tp> {};
34
35template <class _Tp>
36class _LIBCPP_TYPE_VIS_ONLY tuple_size<const volatile _Tp>
37    : public tuple_size<_Tp> {};
38
39template <size_t _Ip, class _Tp> class _LIBCPP_TYPE_VIS_ONLY tuple_element;
40
41template <size_t _Ip, class _Tp>
42class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, const _Tp>
43{
44public:
45    typedef typename add_const<typename tuple_element<_Ip, _Tp>::type>::type type;
46};
47
48template <size_t _Ip, class _Tp>
49class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, volatile _Tp>
50{
51public:
52    typedef typename add_volatile<typename tuple_element<_Ip, _Tp>::type>::type type;
53};
54
55template <size_t _Ip, class _Tp>
56class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, const volatile _Tp>
57{
58public:
59    typedef typename add_cv<typename tuple_element<_Ip, _Tp>::type>::type type;
60};
61
62template <class _Tp> struct __tuple_like : false_type {};
63
64template <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
68// tuple specializations
69
70#if !defined(_LIBCPP_HAS_NO_VARIADICS)
71template <class ..._Tp> class _LIBCPP_TYPE_VIS_ONLY tuple;
72
73template <class... _Tp> struct __tuple_like<tuple<_Tp...> > : true_type {};
74
75template <size_t _Ip, class ..._Tp>
76_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
77typename tuple_element<_Ip, tuple<_Tp...> >::type&
78get(tuple<_Tp...>&) _NOEXCEPT;
79
80template <size_t _Ip, class ..._Tp>
81_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
82const typename tuple_element<_Ip, tuple<_Tp...> >::type&
83get(const tuple<_Tp...>&) _NOEXCEPT;
84
85template <size_t _Ip, class ..._Tp>
86_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
87typename tuple_element<_Ip, tuple<_Tp...> >::type&&
88get(tuple<_Tp...>&&) _NOEXCEPT;
89#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 {};
96
97template <size_t _Ip, class _T1, class _T2>
98_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
99typename tuple_element<_Ip, pair<_T1, _T2> >::type&
100get(pair<_T1, _T2>&) _NOEXCEPT;
101
102template <size_t _Ip, class _T1, class _T2>
103_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
104const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
105get(const pair<_T1, _T2>&) _NOEXCEPT;
106
107#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
108template <size_t _Ip, class _T1, class _T2>
109_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
110typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
111get(pair<_T1, _T2>&&) _NOEXCEPT;
112#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 {};
119
120template <size_t _Ip, class _Tp, size_t _Size>
121_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
122_Tp&
123get(array<_Tp, _Size>&) _NOEXCEPT;
124
125template <size_t _Ip, class _Tp, size_t _Size>
126_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
127const _Tp&
128get(const array<_Tp, _Size>&) _NOEXCEPT;
129
130#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
131template <size_t _Ip, class _Tp, size_t _Size>
132_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
133_Tp&&
134get(array<_Tp, _Size>&&) _NOEXCEPT;
135#endif
136
137#if !defined(_LIBCPP_HAS_NO_VARIADICS)
138
139// __lazy_and
140
141template <bool _Last, class ..._Preds>
142struct __lazy_and_impl;
143
144template <class ..._Preds>
145struct __lazy_and_impl<false, _Preds...> : false_type {};
146
147template <>
148struct __lazy_and_impl<true> : true_type {};
149
150template <class _Pred>
151struct __lazy_and_impl<true, _Pred> : integral_constant<bool, _Pred::type::value> {};
152
153template <class _Hp, class ..._Tp>
154struct __lazy_and_impl<true, _Hp, _Tp...> : __lazy_and_impl<_Hp::type::value, _Tp...> {};
155
156template <class _P1, class ..._Pr>
157struct __lazy_and : __lazy_and_impl<_P1::type::value, _Pr...> {};
158
159// __lazy_not
160
161template <class _Pred>
162struct __lazy_not : integral_constant<bool, !_Pred::type::value> {};
163
164// __make_tuple_indices
165
166template <size_t...> struct __tuple_indices {};
167
168template <size_t _Sp, class _IntTuple, size_t _Ep>
169struct __make_indices_imp;
170
171template <size_t _Sp, size_t ..._Indices, size_t _Ep>
172struct __make_indices_imp<_Sp, __tuple_indices<_Indices...>, _Ep>
173{
174    typedef typename __make_indices_imp<_Sp+1, __tuple_indices<_Indices..., _Sp>, _Ep>::type type;
175};
176
177template <size_t _Ep, size_t ..._Indices>
178struct __make_indices_imp<_Ep, __tuple_indices<_Indices...>, _Ep>
179{
180    typedef __tuple_indices<_Indices...> type;
181};
182
183template <size_t _Ep, size_t _Sp = 0>
184struct __make_tuple_indices
185{
186    static_assert(_Sp <= _Ep, "__make_tuple_indices input error");
187    typedef typename __make_indices_imp<_Sp, __tuple_indices<>, _Ep>::type type;
188};
189
190// __tuple_types
191
192template <class ..._Tp> struct __tuple_types {};
193
194template <size_t _Ip>
195class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, __tuple_types<> >
196{
197public:
198    static_assert(_Ip == 0, "tuple_element index out of range");
199    static_assert(_Ip != 0, "tuple_element index out of range");
200};
201
202template <class _Hp, class ..._Tp>
203class _LIBCPP_TYPE_VIS_ONLY tuple_element<0, __tuple_types<_Hp, _Tp...> >
204{
205public:
206    typedef _Hp type;
207};
208
209template <size_t _Ip, class _Hp, class ..._Tp>
210class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, __tuple_types<_Hp, _Tp...> >
211{
212public:
213    typedef typename tuple_element<_Ip-1, __tuple_types<_Tp...> >::type type;
214};
215
216template <class ..._Tp>
217class _LIBCPP_TYPE_VIS_ONLY tuple_size<__tuple_types<_Tp...> >
218    : public integral_constant<size_t, sizeof...(_Tp)>
219{
220};
221
222template <class... _Tp> struct __tuple_like<__tuple_types<_Tp...> > : true_type {};
223
224// __make_tuple_types
225
226// __make_tuple_types<_Tuple<_Types...>, _Ep, _Sp>::type is a
227// __tuple_types<_Types...> using only those _Types in the range [_Sp, _Ep).
228// _Sp defaults to 0 and _Ep defaults to tuple_size<_Tuple>.  If _Tuple is a
229// lvalue_reference type, then __tuple_types<_Types&...> is the result.
230
231template <class _TupleTypes, class _Tp, size_t _Sp, size_t _Ep>
232struct __make_tuple_types_imp;
233
234template <class ..._Types, class _Tp, size_t _Sp, size_t _Ep>
235struct __make_tuple_types_imp<__tuple_types<_Types...>, _Tp, _Sp, _Ep>
236{
237    typedef typename remove_reference<_Tp>::type _Tpr;
238    typedef typename __make_tuple_types_imp<__tuple_types<_Types...,
239                                            typename conditional<is_lvalue_reference<_Tp>::value,
240                                                typename tuple_element<_Sp, _Tpr>::type&,
241                                                typename tuple_element<_Sp, _Tpr>::type>::type>,
242                                            _Tp, _Sp+1, _Ep>::type type;
243};
244
245template <class ..._Types, class _Tp, size_t _Ep>
246struct __make_tuple_types_imp<__tuple_types<_Types...>, _Tp, _Ep, _Ep>
247{
248    typedef __tuple_types<_Types...> type;
249};
250
251template <class _Tp, size_t _Ep = tuple_size<typename remove_reference<_Tp>::type>::value, size_t _Sp = 0>
252struct __make_tuple_types
253{
254    static_assert(_Sp <= _Ep, "__make_tuple_types input error");
255    typedef typename __make_tuple_types_imp<__tuple_types<>, _Tp, _Sp, _Ep>::type type;
256};
257
258// __tuple_convertible
259
260template <class, class>
261struct __tuple_convertible_imp : public false_type {};
262
263template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
264struct __tuple_convertible_imp<__tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
265    : public integral_constant<bool,
266                               is_convertible<_Tp0, _Up0>::value &&
267                               __tuple_convertible_imp<__tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
268
269template <>
270struct __tuple_convertible_imp<__tuple_types<>, __tuple_types<> >
271    : public true_type {};
272
273template <bool, class, class>
274struct __tuple_convertible_apply : public false_type {};
275
276template <class _Tp, class _Up>
277struct __tuple_convertible_apply<true, _Tp, _Up>
278  : public __tuple_convertible_imp<
279      typename __make_tuple_types<_Tp>::type
280    , typename __make_tuple_types<_Up>::type
281    >
282{};
283
284template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
285                                bool = __tuple_like<_Up>::value>
286struct __tuple_convertible
287    : public false_type {};
288
289template <class _Tp, class _Up>
290struct __tuple_convertible<_Tp, _Up, true, true>
291    : public __tuple_convertible_apply<tuple_size<typename remove_reference<_Tp>::type>::value ==
292                                     tuple_size<_Up>::value, _Tp, _Up>
293{};
294
295// __tuple_constructible
296
297template <class, class>
298struct __tuple_constructible_imp : public false_type {};
299
300template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
301struct __tuple_constructible_imp<__tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
302    : public integral_constant<bool,
303                               is_constructible<_Up0, _Tp0>::value &&
304                               __tuple_constructible_imp<__tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
305
306template <>
307struct __tuple_constructible_imp<__tuple_types<>, __tuple_types<> >
308    : public true_type {};
309
310template <bool _SameSize, class, class>
311struct __tuple_constructible_apply : public false_type {};
312
313template <class _Tp, class _Up>
314struct __tuple_constructible_apply<true, _Tp, _Up>
315  : public __tuple_constructible_imp<
316      typename __make_tuple_types<_Tp>::type
317    , typename __make_tuple_types<_Up>::type
318    >
319{};
320
321template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
322                                bool = __tuple_like<_Up>::value>
323struct __tuple_constructible
324    : public false_type {};
325
326template <class _Tp, class _Up>
327struct __tuple_constructible<_Tp, _Up, true, true>
328    : public __tuple_constructible_apply<tuple_size<typename remove_reference<_Tp>::type>::value ==
329                                     tuple_size<_Up>::value, _Tp, _Up>
330{};
331
332// __tuple_assignable
333
334template <class, class>
335struct __tuple_assignable_imp : public false_type {};
336
337template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
338struct __tuple_assignable_imp<__tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
339    : public integral_constant<bool,
340                               is_assignable<_Up0&, _Tp0>::value &&
341                               __tuple_assignable_imp<__tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
342
343template <>
344struct __tuple_assignable_imp<__tuple_types<>, __tuple_types<> >
345    : public true_type {};
346
347template <bool, class, class>
348struct __tuple_assignable_apply : public false_type {};
349
350template <class _Tp, class _Up>
351struct __tuple_assignable_apply<true, _Tp, _Up>
352  : __tuple_assignable_imp<
353      typename __make_tuple_types<_Tp>::type
354    , typename __make_tuple_types<_Up>::type
355    >
356{};
357
358template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
359                                bool = __tuple_like<_Up>::value>
360struct __tuple_assignable
361    : public false_type {};
362
363template <class _Tp, class _Up>
364struct __tuple_assignable<_Tp, _Up, true, true>
365    : public __tuple_assignable_apply<tuple_size<typename remove_reference<_Tp>::type>::value ==
366                                    tuple_size<_Up>::value, _Tp, _Up>
367{};
368
369#endif  // _LIBCPP_HAS_NO_VARIADICS
370
371_LIBCPP_END_NAMESPACE_STD
372
373#endif  // _LIBCPP___TUPLE
374