__tuple 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. #ifndef _LIBCPP___TUPLE
  11. #define _LIBCPP___TUPLE
  12. #include <__config>
  13. #include <cstddef>
  14. #include <type_traits>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. #pragma GCC system_header
  17. #endif
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. template <class _Tp> class _LIBCPP_TYPE_VIS_ONLY tuple_size;
  20. template <class _Tp>
  21. class _LIBCPP_TYPE_VIS_ONLY tuple_size<const _Tp>
  22. : public tuple_size<_Tp> {};
  23. template <class _Tp>
  24. class _LIBCPP_TYPE_VIS_ONLY tuple_size<volatile _Tp>
  25. : public tuple_size<_Tp> {};
  26. template <class _Tp>
  27. class _LIBCPP_TYPE_VIS_ONLY tuple_size<const volatile _Tp>
  28. : public tuple_size<_Tp> {};
  29. template <size_t _Ip, class _Tp> class _LIBCPP_TYPE_VIS_ONLY tuple_element;
  30. template <size_t _Ip, class _Tp>
  31. class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, const _Tp>
  32. {
  33. public:
  34. typedef typename add_const<typename tuple_element<_Ip, _Tp>::type>::type type;
  35. };
  36. template <size_t _Ip, class _Tp>
  37. class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, volatile _Tp>
  38. {
  39. public:
  40. typedef typename add_volatile<typename tuple_element<_Ip, _Tp>::type>::type type;
  41. };
  42. template <size_t _Ip, class _Tp>
  43. class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, const volatile _Tp>
  44. {
  45. public:
  46. typedef typename add_cv<typename tuple_element<_Ip, _Tp>::type>::type type;
  47. };
  48. template <class _Tp> struct __tuple_like : false_type {};
  49. template <class _Tp> struct __tuple_like<const _Tp> : public __tuple_like<_Tp> {};
  50. template <class _Tp> struct __tuple_like<volatile _Tp> : public __tuple_like<_Tp> {};
  51. template <class _Tp> struct __tuple_like<const volatile _Tp> : public __tuple_like<_Tp> {};
  52. // tuple specializations
  53. #if !defined(_LIBCPP_HAS_NO_VARIADICS)
  54. template <size_t...> struct __tuple_indices {};
  55. template <class _IdxType, _IdxType... _Values>
  56. struct __integer_sequence {
  57. template <template <class _OIdxType, _OIdxType...> class _ToIndexSeq, class _ToIndexType>
  58. using __convert = _ToIndexSeq<_ToIndexType, _Values...>;
  59. template <size_t _Sp>
  60. using __to_tuple_indices = __tuple_indices<(_Values + _Sp)...>;
  61. };
  62. #if !__has_builtin(__make_integer_seq) || defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE)
  63. namespace __detail {
  64. template<typename _Tp, size_t ..._Extra> struct __repeat;
  65. template<typename _Tp, _Tp ..._Np, size_t ..._Extra> struct __repeat<__integer_sequence<_Tp, _Np...>, _Extra...> {
  66. typedef __integer_sequence<_Tp,
  67. _Np...,
  68. sizeof...(_Np) + _Np...,
  69. 2 * sizeof...(_Np) + _Np...,
  70. 3 * sizeof...(_Np) + _Np...,
  71. 4 * sizeof...(_Np) + _Np...,
  72. 5 * sizeof...(_Np) + _Np...,
  73. 6 * sizeof...(_Np) + _Np...,
  74. 7 * sizeof...(_Np) + _Np...,
  75. _Extra...> type;
  76. };
  77. template<size_t _Np> struct __parity;
  78. template<size_t _Np> struct __make : __parity<_Np % 8>::template __pmake<_Np> {};
  79. template<> struct __make<0> { typedef __integer_sequence<size_t> type; };
  80. template<> struct __make<1> { typedef __integer_sequence<size_t, 0> type; };
  81. template<> struct __make<2> { typedef __integer_sequence<size_t, 0, 1> type; };
  82. template<> struct __make<3> { typedef __integer_sequence<size_t, 0, 1, 2> type; };
  83. template<> struct __make<4> { typedef __integer_sequence<size_t, 0, 1, 2, 3> type; };
  84. template<> struct __make<5> { typedef __integer_sequence<size_t, 0, 1, 2, 3, 4> type; };
  85. template<> struct __make<6> { typedef __integer_sequence<size_t, 0, 1, 2, 3, 4, 5> type; };
  86. template<> struct __make<7> { typedef __integer_sequence<size_t, 0, 1, 2, 3, 4, 5, 6> type; };
  87. template<> struct __parity<0> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type> {}; };
  88. template<> struct __parity<1> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 1> {}; };
  89. template<> struct __parity<2> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 2, _Np - 1> {}; };
  90. template<> struct __parity<3> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 3, _Np - 2, _Np - 1> {}; };
  91. template<> struct __parity<4> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
  92. template<> struct __parity<5> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
  93. template<> struct __parity<6> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 6, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
  94. template<> struct __parity<7> { template<size_t _Np> struct __pmake : __repeat<typename __make<_Np / 8>::type, _Np - 7, _Np - 6, _Np - 5, _Np - 4, _Np - 3, _Np - 2, _Np - 1> {}; };
  95. } // namespace detail
  96. #endif // !__has_builtin(__make_integer_seq) || defined(_LIBCPP_TESTING_FALLBACK_MAKE_INTEGER_SEQUENCE)
  97. #if __has_builtin(__make_integer_seq)
  98. template <size_t _Ep, size_t _Sp>
  99. using __make_indices_imp =
  100. typename __make_integer_seq<__integer_sequence, size_t, _Ep - _Sp>::template
  101. __to_tuple_indices<_Sp>;
  102. #else
  103. template <size_t _Ep, size_t _Sp>
  104. using __make_indices_imp =
  105. typename __detail::__make<_Ep - _Sp>::type::template __to_tuple_indices<_Sp>;
  106. #endif
  107. template <size_t _Ep, size_t _Sp = 0>
  108. struct __make_tuple_indices
  109. {
  110. static_assert(_Sp <= _Ep, "__make_tuple_indices input error");
  111. typedef __make_indices_imp<_Ep, _Sp> type;
  112. };
  113. template <class ..._Tp> class _LIBCPP_TYPE_VIS_ONLY tuple;
  114. template <class... _Tp> struct __tuple_like<tuple<_Tp...> > : true_type {};
  115. template <size_t _Ip, class ..._Tp>
  116. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  117. typename tuple_element<_Ip, tuple<_Tp...> >::type&
  118. get(tuple<_Tp...>&) _NOEXCEPT;
  119. template <size_t _Ip, class ..._Tp>
  120. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  121. const typename tuple_element<_Ip, tuple<_Tp...> >::type&
  122. get(const tuple<_Tp...>&) _NOEXCEPT;
  123. template <size_t _Ip, class ..._Tp>
  124. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  125. typename tuple_element<_Ip, tuple<_Tp...> >::type&&
  126. get(tuple<_Tp...>&&) _NOEXCEPT;
  127. template <size_t _Ip, class ..._Tp>
  128. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  129. const typename tuple_element<_Ip, tuple<_Tp...> >::type&&
  130. get(const tuple<_Tp...>&&) _NOEXCEPT;
  131. #endif
  132. // pair specializations
  133. template <class _T1, class _T2> struct __tuple_like<pair<_T1, _T2> > : true_type {};
  134. template <size_t _Ip, class _T1, class _T2>
  135. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  136. typename tuple_element<_Ip, pair<_T1, _T2> >::type&
  137. get(pair<_T1, _T2>&) _NOEXCEPT;
  138. template <size_t _Ip, class _T1, class _T2>
  139. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  140. const typename tuple_element<_Ip, pair<_T1, _T2> >::type&
  141. get(const pair<_T1, _T2>&) _NOEXCEPT;
  142. #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
  143. template <size_t _Ip, class _T1, class _T2>
  144. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  145. typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
  146. get(pair<_T1, _T2>&&) _NOEXCEPT;
  147. template <size_t _Ip, class _T1, class _T2>
  148. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  149. const typename tuple_element<_Ip, pair<_T1, _T2> >::type&&
  150. get(const pair<_T1, _T2>&&) _NOEXCEPT;
  151. #endif
  152. // array specializations
  153. template <class _Tp, size_t _Size> struct _LIBCPP_TYPE_VIS_ONLY array;
  154. template <class _Tp, size_t _Size> struct __tuple_like<array<_Tp, _Size> > : true_type {};
  155. template <size_t _Ip, class _Tp, size_t _Size>
  156. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  157. _Tp&
  158. get(array<_Tp, _Size>&) _NOEXCEPT;
  159. template <size_t _Ip, class _Tp, size_t _Size>
  160. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  161. const _Tp&
  162. get(const array<_Tp, _Size>&) _NOEXCEPT;
  163. #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
  164. template <size_t _Ip, class _Tp, size_t _Size>
  165. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  166. _Tp&&
  167. get(array<_Tp, _Size>&&) _NOEXCEPT;
  168. template <size_t _Ip, class _Tp, size_t _Size>
  169. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  170. const _Tp&&
  171. get(const array<_Tp, _Size>&&) _NOEXCEPT;
  172. #endif
  173. #if !defined(_LIBCPP_HAS_NO_VARIADICS)
  174. // __tuple_types
  175. template <class ..._Tp> struct __tuple_types {};
  176. #if !__has_builtin(__type_pack_element)
  177. namespace __indexer_detail {
  178. template <size_t _Idx, class _Tp>
  179. struct __indexed { using type = _Tp; };
  180. template <class _Types, class _Indexes> struct __indexer;
  181. template <class ..._Types, size_t ..._Idx>
  182. struct __indexer<__tuple_types<_Types...>, __tuple_indices<_Idx...>>
  183. : __indexed<_Idx, _Types>...
  184. {};
  185. template <size_t _Idx, class _Tp>
  186. __indexed<_Idx, _Tp> __at_index(__indexed<_Idx, _Tp> const&);
  187. } // namespace __indexer_detail
  188. template <size_t _Idx, class ..._Types>
  189. using __type_pack_element = typename decltype(
  190. __indexer_detail::__at_index<_Idx>(
  191. __indexer_detail::__indexer<
  192. __tuple_types<_Types...>,
  193. typename __make_tuple_indices<sizeof...(_Types)>::type
  194. >{})
  195. )::type;
  196. #endif
  197. template <size_t _Ip, class ..._Types>
  198. class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, __tuple_types<_Types...>>
  199. {
  200. public:
  201. static_assert(_Ip < sizeof...(_Types), "tuple_element index out of range");
  202. typedef __type_pack_element<_Ip, _Types...> type;
  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. template <class... _Tp> struct __tuple_like<__tuple_types<_Tp...> > : true_type {};
  210. template <bool _ApplyLV, bool _ApplyConst, bool _ApplyVolatile>
  211. struct __apply_cv_mf;
  212. template <>
  213. struct __apply_cv_mf<false, false, false> {
  214. template <class _Tp> using __apply = _Tp;
  215. };
  216. template <>
  217. struct __apply_cv_mf<false, true, false> {
  218. template <class _Tp> using __apply = const _Tp;
  219. };
  220. template <>
  221. struct __apply_cv_mf<false, false, true> {
  222. template <class _Tp> using __apply = volatile _Tp;
  223. };
  224. template <>
  225. struct __apply_cv_mf<false, true, true> {
  226. template <class _Tp> using __apply = const volatile _Tp;
  227. };
  228. template <>
  229. struct __apply_cv_mf<true, false, false> {
  230. template <class _Tp> using __apply = _Tp&;
  231. };
  232. template <>
  233. struct __apply_cv_mf<true, true, false> {
  234. template <class _Tp> using __apply = const _Tp&;
  235. };
  236. template <>
  237. struct __apply_cv_mf<true, false, true> {
  238. template <class _Tp> using __apply = volatile _Tp&;
  239. };
  240. template <>
  241. struct __apply_cv_mf<true, true, true> {
  242. template <class _Tp> using __apply = const volatile _Tp&;
  243. };
  244. template <class _Tp, class _RawTp = typename remove_reference<_Tp>::type>
  245. using __apply_cv_t = __apply_cv_mf<
  246. is_lvalue_reference<_Tp>::value,
  247. is_const<_RawTp>::value,
  248. is_volatile<_RawTp>::value>;
  249. // __make_tuple_types
  250. // __make_tuple_types<_Tuple<_Types...>, _Ep, _Sp>::type is a
  251. // __tuple_types<_Types...> using only those _Types in the range [_Sp, _Ep).
  252. // _Sp defaults to 0 and _Ep defaults to tuple_size<_Tuple>. If _Tuple is a
  253. // lvalue_reference type, then __tuple_types<_Types&...> is the result.
  254. template <class _TupleTypes, class _TupleIndices>
  255. struct __make_tuple_types_flat;
  256. template <template <class...> class _Tuple, class ..._Types, size_t ..._Idx>
  257. struct __make_tuple_types_flat<_Tuple<_Types...>, __tuple_indices<_Idx...>> {
  258. // Specialization for pair, tuple, and __tuple_types
  259. template <class _Tp, class _ApplyFn = __apply_cv_t<_Tp>>
  260. using __apply_quals = __tuple_types<
  261. typename _ApplyFn::template __apply<__type_pack_element<_Idx, _Types...>>...
  262. >;
  263. };
  264. template <class _Vt, size_t _Np, size_t ..._Idx>
  265. struct __make_tuple_types_flat<array<_Vt, _Np>, __tuple_indices<_Idx...>> {
  266. template <size_t>
  267. using __value_type = _Vt;
  268. template <class _Tp, class _ApplyFn = __apply_cv_t<_Tp>>
  269. using __apply_quals = __tuple_types<
  270. typename _ApplyFn::template __apply<__value_type<_Idx>>...
  271. >;
  272. };
  273. template <class _Tp, size_t _Ep = tuple_size<typename remove_reference<_Tp>::type>::value,
  274. size_t _Sp = 0,
  275. bool _SameSize = (_Ep == tuple_size<typename remove_reference<_Tp>::type>::value)>
  276. struct __make_tuple_types
  277. {
  278. static_assert(_Sp <= _Ep, "__make_tuple_types input error");
  279. using _RawTp = typename remove_cv<typename remove_reference<_Tp>::type>::type;
  280. using _Maker = __make_tuple_types_flat<_RawTp, typename __make_tuple_indices<_Ep, _Sp>::type>;
  281. using type = typename _Maker::template __apply_quals<_Tp>;
  282. };
  283. template <class ..._Types, size_t _Ep>
  284. struct __make_tuple_types<tuple<_Types...>, _Ep, 0, true> {
  285. typedef __tuple_types<_Types...> type;
  286. };
  287. template <class ..._Types, size_t _Ep>
  288. struct __make_tuple_types<__tuple_types<_Types...>, _Ep, 0, true> {
  289. typedef __tuple_types<_Types...> type;
  290. };
  291. template <bool ..._Preds>
  292. struct __all_dummy;
  293. template <bool ..._Pred>
  294. using __all = is_same<__all_dummy<_Pred...>, __all_dummy<(_Pred, true)...>>;
  295. struct __tuple_sfinae_base {
  296. template <template <class, class...> class _Trait,
  297. class ..._LArgs, class ..._RArgs>
  298. static auto __do_test(__tuple_types<_LArgs...>, __tuple_types<_RArgs...>)
  299. -> __all<typename enable_if<_Trait<_LArgs, _RArgs>::value, bool>::type{true}...>;
  300. template <template <class...> class>
  301. static auto __do_test(...) -> false_type;
  302. template <class _FromArgs, class _ToArgs>
  303. using __constructible = decltype(__do_test<is_constructible>(_ToArgs{}, _FromArgs{}));
  304. template <class _FromArgs, class _ToArgs>
  305. using __convertible = decltype(__do_test<is_convertible>(_FromArgs{}, _ToArgs{}));
  306. template <class _FromArgs, class _ToArgs>
  307. using __assignable = decltype(__do_test<is_assignable>(_ToArgs{}, _FromArgs{}));
  308. };
  309. // __tuple_convertible
  310. template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
  311. bool = __tuple_like<_Up>::value>
  312. struct __tuple_convertible
  313. : public false_type {};
  314. template <class _Tp, class _Up>
  315. struct __tuple_convertible<_Tp, _Up, true, true>
  316. : public __tuple_sfinae_base::__convertible<
  317. typename __make_tuple_types<_Tp>::type
  318. , typename __make_tuple_types<_Up>::type
  319. >
  320. {};
  321. // __tuple_constructible
  322. template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
  323. bool = __tuple_like<_Up>::value>
  324. struct __tuple_constructible
  325. : public false_type {};
  326. template <class _Tp, class _Up>
  327. struct __tuple_constructible<_Tp, _Up, true, true>
  328. : public __tuple_sfinae_base::__constructible<
  329. typename __make_tuple_types<_Tp>::type
  330. , typename __make_tuple_types<_Up>::type
  331. >
  332. {};
  333. // __tuple_assignable
  334. template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
  335. bool = __tuple_like<_Up>::value>
  336. struct __tuple_assignable
  337. : public false_type {};
  338. template <class _Tp, class _Up>
  339. struct __tuple_assignable<_Tp, _Up, true, true>
  340. : public __tuple_sfinae_base::__assignable<
  341. typename __make_tuple_types<_Tp>::type
  342. , typename __make_tuple_types<_Up&>::type
  343. >
  344. {};
  345. #endif // _LIBCPP_HAS_NO_VARIADICS
  346. _LIBCPP_END_NAMESPACE_STD
  347. #endif // _LIBCPP___TUPLE