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