Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP___TUPLE |
| 12 | #define _LIBCPP___TUPLE |
| 13 | |
| 14 | #include <__config> |
| 15 | #include <cstddef> |
| 16 | #include <type_traits> |
| 17 | |
| 18 | #pragma GCC system_header |
| 19 | |
| 20 | #ifdef _LIBCPP_HAS_NO_VARIADICS |
| 21 | |
| 22 | #include <__tuple_03> |
| 23 | |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 24 | #else // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 25 | |
| 26 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 27 | |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 28 | template <class _Tp> class _LIBCPP_VISIBLE tuple_size; |
Howard Hinnant | 86ed8ae | 2010-11-17 19:22:43 +0000 | [diff] [blame] | 29 | |
| 30 | template <class _Tp> |
| 31 | class _LIBCPP_VISIBLE tuple_size<const _Tp> |
| 32 | : public tuple_size<_Tp> {}; |
| 33 | |
| 34 | template <class _Tp> |
| 35 | class _LIBCPP_VISIBLE tuple_size<volatile _Tp> |
| 36 | : public tuple_size<_Tp> {}; |
| 37 | |
| 38 | template <class _Tp> |
| 39 | class _LIBCPP_VISIBLE tuple_size<const volatile _Tp> |
| 40 | : public tuple_size<_Tp> {}; |
| 41 | |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 42 | template <size_t _Ip, class _Tp> class _LIBCPP_VISIBLE tuple_element; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 43 | |
Howard Hinnant | 86ed8ae | 2010-11-17 19:22:43 +0000 | [diff] [blame] | 44 | template <size_t _Ip, class _Tp> |
| 45 | class _LIBCPP_VISIBLE tuple_element<_Ip, const _Tp> |
| 46 | { |
| 47 | public: |
| 48 | typedef typename add_const<typename tuple_element<_Ip, _Tp>::type>::type type; |
| 49 | }; |
| 50 | |
| 51 | template <size_t _Ip, class _Tp> |
| 52 | class _LIBCPP_VISIBLE tuple_element<_Ip, volatile _Tp> |
| 53 | { |
| 54 | public: |
| 55 | typedef typename add_volatile<typename tuple_element<_Ip, _Tp>::type>::type type; |
| 56 | }; |
| 57 | |
| 58 | template <size_t _Ip, class _Tp> |
| 59 | class _LIBCPP_VISIBLE tuple_element<_Ip, const volatile _Tp> |
| 60 | { |
| 61 | public: |
| 62 | typedef typename add_cv<typename tuple_element<_Ip, _Tp>::type>::type type; |
| 63 | }; |
| 64 | |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 65 | template <class ..._Tp> class _LIBCPP_VISIBLE tuple; |
| 66 | template <class _T1, class _T2> class _LIBCPP_VISIBLE pair; |
| 67 | template <class _Tp, size_t _Size> struct _LIBCPP_VISIBLE array; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 68 | |
| 69 | template <class _Tp> struct __tuple_like : false_type {}; |
| 70 | |
Howard Hinnant | 0e1493e | 2010-12-11 20:47:50 +0000 | [diff] [blame] | 71 | template <class _Tp> struct __tuple_like<const _Tp> : public __tuple_like<_Tp> {}; |
| 72 | template <class _Tp> struct __tuple_like<volatile _Tp> : public __tuple_like<_Tp> {}; |
| 73 | template <class _Tp> struct __tuple_like<const volatile _Tp> : public __tuple_like<_Tp> {}; |
| 74 | |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 75 | template <class... _Tp> struct __tuple_like<tuple<_Tp...> > : true_type {}; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 76 | template <class _T1, class _T2> struct __tuple_like<pair<_T1, _T2> > : true_type {}; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 77 | template <class _Tp, size_t _Size> struct __tuple_like<array<_Tp, _Size> > : true_type {}; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 78 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 79 | template <size_t _Ip, class ..._Tp> |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 80 | typename tuple_element<_Ip, tuple<_Tp...> >::type& |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 81 | get(tuple<_Tp...>&); |
| 82 | |
| 83 | template <size_t _Ip, class ..._Tp> |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 84 | const typename tuple_element<_Ip, tuple<_Tp...> >::type& |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 85 | get(const tuple<_Tp...>&); |
| 86 | |
Howard Hinnant | cd2254b | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 87 | template <size_t _Ip, class ..._Tp> |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 88 | typename tuple_element<_Ip, tuple<_Tp...> >::type&& |
Howard Hinnant | cd2254b | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 89 | get(tuple<_Tp...>&&); |
| 90 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 91 | template <size_t _Ip, class _T1, class _T2> |
| 92 | typename tuple_element<_Ip, pair<_T1, _T2> >::type& |
| 93 | get(pair<_T1, _T2>&); |
| 94 | |
| 95 | template <size_t _Ip, class _T1, class _T2> |
| 96 | const typename tuple_element<_Ip, pair<_T1, _T2> >::type& |
| 97 | get(const pair<_T1, _T2>&); |
| 98 | |
Howard Hinnant | cd2254b | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 99 | template <size_t _Ip, class _T1, class _T2> |
| 100 | typename tuple_element<_Ip, pair<_T1, _T2> >::type&& |
| 101 | get(pair<_T1, _T2>&&); |
| 102 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 103 | template <size_t _Ip, class _Tp, size_t _Size> |
| 104 | _Tp& |
| 105 | get(array<_Tp, _Size>&); |
| 106 | |
| 107 | template <size_t _Ip, class _Tp, size_t _Size> |
| 108 | const _Tp& |
| 109 | get(const array<_Tp, _Size>&); |
| 110 | |
Howard Hinnant | cd2254b | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 111 | template <size_t _Ip, class _Tp, size_t _Size> |
| 112 | _Tp&& |
| 113 | get(array<_Tp, _Size>&&); |
| 114 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 115 | // __make_tuple_indices |
| 116 | |
| 117 | template <size_t...> struct __tuple_indices {}; |
| 118 | |
| 119 | template <size_t _Sp, class _IntTuple, size_t _Ep> |
| 120 | struct __make_indices_imp; |
| 121 | |
| 122 | template <size_t _Sp, size_t ..._Indices, size_t _Ep> |
| 123 | struct __make_indices_imp<_Sp, __tuple_indices<_Indices...>, _Ep> |
| 124 | { |
| 125 | typedef typename __make_indices_imp<_Sp+1, __tuple_indices<_Indices..., _Sp>, _Ep>::type type; |
| 126 | }; |
| 127 | |
| 128 | template <size_t _Ep, size_t ..._Indices> |
| 129 | struct __make_indices_imp<_Ep, __tuple_indices<_Indices...>, _Ep> |
| 130 | { |
| 131 | typedef __tuple_indices<_Indices...> type; |
| 132 | }; |
| 133 | |
| 134 | template <size_t _Ep, size_t _Sp = 0> |
| 135 | struct __make_tuple_indices |
| 136 | { |
| 137 | static_assert(_Sp <= _Ep, "__make_tuple_indices input error"); |
| 138 | typedef typename __make_indices_imp<_Sp, __tuple_indices<>, _Ep>::type type; |
| 139 | }; |
| 140 | |
| 141 | // __tuple_types |
| 142 | |
| 143 | template <class ..._Tp> struct __tuple_types {}; |
| 144 | |
| 145 | template <size_t _Ip> |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 146 | class _LIBCPP_VISIBLE tuple_element<_Ip, __tuple_types<> > |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 147 | { |
| 148 | public: |
| 149 | static_assert(_Ip == 0, "tuple_element index out of range"); |
| 150 | static_assert(_Ip != 0, "tuple_element index out of range"); |
| 151 | }; |
| 152 | |
| 153 | template <class _Hp, class ..._Tp> |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 154 | class _LIBCPP_VISIBLE tuple_element<0, __tuple_types<_Hp, _Tp...> > |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 155 | { |
| 156 | public: |
| 157 | typedef _Hp type; |
| 158 | }; |
| 159 | |
| 160 | template <size_t _Ip, class _Hp, class ..._Tp> |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 161 | class _LIBCPP_VISIBLE tuple_element<_Ip, __tuple_types<_Hp, _Tp...> > |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 162 | { |
| 163 | public: |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 164 | typedef typename tuple_element<_Ip-1, __tuple_types<_Tp...> >::type type; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | template <class ..._Tp> |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 168 | class _LIBCPP_VISIBLE tuple_size<__tuple_types<_Tp...> > |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 169 | : public integral_constant<size_t, sizeof...(_Tp)> |
| 170 | { |
| 171 | }; |
| 172 | |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 173 | template <class... _Tp> struct __tuple_like<__tuple_types<_Tp...> > : true_type {}; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 174 | |
| 175 | // __make_tuple_types |
| 176 | |
Howard Hinnant | e049cc5 | 2010-09-27 17:54:17 +0000 | [diff] [blame] | 177 | // __make_tuple_types<_Tuple<_Types...>, _Ep, _Sp>::type is a |
| 178 | // __tuple_types<_Types...> using only those _Types in the range [_Sp, _Ep). |
| 179 | // _Sp defaults to 0 and _Ep defaults to tuple_size<_Tuple>. If _Tuple is a |
| 180 | // lvalue_reference type, then __tuple_types<_Types&...> is the result. |
| 181 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 182 | template <class _TupleTypes, class _Tp, size_t _Sp, size_t _Ep> |
| 183 | struct __make_tuple_types_imp; |
| 184 | |
| 185 | template <class ..._Types, class _Tp, size_t _Sp, size_t _Ep> |
| 186 | struct __make_tuple_types_imp<__tuple_types<_Types...>, _Tp, _Sp, _Ep> |
| 187 | { |
| 188 | typedef typename remove_reference<_Tp>::type _Tpr; |
| 189 | typedef typename __make_tuple_types_imp<__tuple_types<_Types..., |
Howard Hinnant | e049cc5 | 2010-09-27 17:54:17 +0000 | [diff] [blame] | 190 | typename conditional<is_lvalue_reference<_Tp>::value, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 191 | typename tuple_element<_Sp, _Tpr>::type&, |
| 192 | typename tuple_element<_Sp, _Tpr>::type>::type>, |
| 193 | _Tp, _Sp+1, _Ep>::type type; |
| 194 | }; |
| 195 | |
| 196 | template <class ..._Types, class _Tp, size_t _Ep> |
| 197 | struct __make_tuple_types_imp<__tuple_types<_Types...>, _Tp, _Ep, _Ep> |
| 198 | { |
| 199 | typedef __tuple_types<_Types...> type; |
| 200 | }; |
| 201 | |
| 202 | template <class _Tp, size_t _Ep = tuple_size<typename remove_reference<_Tp>::type>::value, size_t _Sp = 0> |
| 203 | struct __make_tuple_types |
| 204 | { |
| 205 | static_assert(_Sp <= _Ep, "__make_tuple_types input error"); |
| 206 | typedef typename __make_tuple_types_imp<__tuple_types<>, _Tp, _Sp, _Ep>::type type; |
| 207 | }; |
| 208 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 209 | // __tuple_convertible |
| 210 | |
| 211 | template <bool, class _Tp, class _Up> |
| 212 | struct __tuple_convertible_imp : public false_type {}; |
| 213 | |
| 214 | template <class _Tp0, class ..._Tp, class _Up0, class ..._Up> |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 215 | struct __tuple_convertible_imp<true, __tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> > |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 216 | : public integral_constant<bool, |
| 217 | is_convertible<_Tp0, _Up0>::value && |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 218 | __tuple_convertible_imp<true, __tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {}; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 219 | |
| 220 | template <> |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 221 | struct __tuple_convertible_imp<true, __tuple_types<>, __tuple_types<> > |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 222 | : public true_type {}; |
| 223 | |
| 224 | template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value, |
| 225 | bool = __tuple_like<_Up>::value> |
| 226 | struct __tuple_convertible |
| 227 | : public false_type {}; |
| 228 | |
| 229 | template <class _Tp, class _Up> |
| 230 | struct __tuple_convertible<_Tp, _Up, true, true> |
| 231 | : public __tuple_convertible_imp<tuple_size<typename remove_reference<_Tp>::type>::value == |
| 232 | tuple_size<_Up>::value, |
| 233 | typename __make_tuple_types<_Tp>::type, typename __make_tuple_types<_Up>::type> |
| 234 | {}; |
| 235 | |
| 236 | // __tuple_assignable |
| 237 | |
Howard Hinnant | e049cc5 | 2010-09-27 17:54:17 +0000 | [diff] [blame] | 238 | template <bool, class _Tp, class _Up> |
| 239 | struct __tuple_assignable_imp : public false_type {}; |
| 240 | |
| 241 | template <class _Tp0, class ..._Tp, class _Up0, class ..._Up> |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 242 | struct __tuple_assignable_imp<true, __tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> > |
Howard Hinnant | e049cc5 | 2010-09-27 17:54:17 +0000 | [diff] [blame] | 243 | : public integral_constant<bool, |
Howard Hinnant | f048fe3 | 2010-11-20 18:25:22 +0000 | [diff] [blame] | 244 | is_assignable<_Up0&, _Tp0>::value && |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 245 | __tuple_assignable_imp<true, __tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {}; |
Howard Hinnant | e049cc5 | 2010-09-27 17:54:17 +0000 | [diff] [blame] | 246 | |
| 247 | template <> |
Howard Hinnant | f83417b | 2011-01-24 16:07:25 +0000 | [diff] [blame^] | 248 | struct __tuple_assignable_imp<true, __tuple_types<>, __tuple_types<> > |
Howard Hinnant | e049cc5 | 2010-09-27 17:54:17 +0000 | [diff] [blame] | 249 | : public true_type {}; |
| 250 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 251 | template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value, |
| 252 | bool = __tuple_like<_Up>::value> |
| 253 | struct __tuple_assignable |
| 254 | : public false_type {}; |
| 255 | |
| 256 | template <class _Tp, class _Up> |
| 257 | struct __tuple_assignable<_Tp, _Up, true, true> |
Howard Hinnant | e049cc5 | 2010-09-27 17:54:17 +0000 | [diff] [blame] | 258 | : public __tuple_assignable_imp<tuple_size<typename remove_reference<_Tp>::type>::value == |
| 259 | tuple_size<_Up>::value, |
| 260 | typename __make_tuple_types<_Tp>::type, typename __make_tuple_types<_Up>::type> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 261 | {}; |
| 262 | |
| 263 | _LIBCPP_END_NAMESPACE_STD |
| 264 | |
| 265 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 266 | |
| 267 | #endif // _LIBCPP___TUPLE |