您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

427 行
12KB

  1. // class template tuple -*- C++ -*-
  2. // Copyright (C) 2004-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file tr1/tuple
  21. * This is a TR1 C++ Library header.
  22. */
  23. // Chris Jefferson <chris@bubblescope.net>
  24. // Variadic Templates support by Douglas Gregor <doug.gregor@gmail.com>
  25. #ifndef _GLIBCXX_TR1_TUPLE
  26. #define _GLIBCXX_TR1_TUPLE 1
  27. #pragma GCC system_header
  28. #include <utility>
  29. namespace std _GLIBCXX_VISIBILITY(default)
  30. {
  31. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  32. namespace tr1
  33. {
  34. // Adds a const reference to a non-reference type.
  35. template<typename _Tp>
  36. struct __add_c_ref
  37. { typedef const _Tp& type; };
  38. template<typename _Tp>
  39. struct __add_c_ref<_Tp&>
  40. { typedef _Tp& type; };
  41. // Adds a reference to a non-reference type.
  42. template<typename _Tp>
  43. struct __add_ref
  44. { typedef _Tp& type; };
  45. template<typename _Tp>
  46. struct __add_ref<_Tp&>
  47. { typedef _Tp& type; };
  48. /**
  49. * Contains the actual implementation of the @c tuple template, stored
  50. * as a recursive inheritance hierarchy from the first element (most
  51. * derived class) to the last (least derived class). The @c Idx
  52. * parameter gives the 0-based index of the element stored at this
  53. * point in the hierarchy; we use it to implement a constant-time
  54. * get() operation.
  55. */
  56. template<int _Idx, typename... _Elements>
  57. struct _Tuple_impl;
  58. /**
  59. * Zero-element tuple implementation. This is the basis case for the
  60. * inheritance recursion.
  61. */
  62. template<int _Idx>
  63. struct _Tuple_impl<_Idx> { };
  64. /**
  65. * Recursive tuple implementation. Here we store the @c Head element
  66. * and derive from a @c Tuple_impl containing the remaining elements
  67. * (which contains the @c Tail).
  68. */
  69. template<int _Idx, typename _Head, typename... _Tail>
  70. struct _Tuple_impl<_Idx, _Head, _Tail...>
  71. : public _Tuple_impl<_Idx + 1, _Tail...>
  72. {
  73. typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
  74. _Head _M_head;
  75. _Inherited& _M_tail() { return *this; }
  76. const _Inherited& _M_tail() const { return *this; }
  77. _Tuple_impl() : _Inherited(), _M_head() { }
  78. explicit
  79. _Tuple_impl(typename __add_c_ref<_Head>::type __head,
  80. typename __add_c_ref<_Tail>::type... __tail)
  81. : _Inherited(__tail...), _M_head(__head) { }
  82. template<typename... _UElements>
  83. _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
  84. : _Inherited(__in._M_tail()), _M_head(__in._M_head) { }
  85. _Tuple_impl(const _Tuple_impl& __in)
  86. : _Inherited(__in._M_tail()), _M_head(__in._M_head) { }
  87. template<typename... _UElements>
  88. _Tuple_impl&
  89. operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
  90. {
  91. _M_head = __in._M_head;
  92. _M_tail() = __in._M_tail();
  93. return *this;
  94. }
  95. _Tuple_impl&
  96. operator=(const _Tuple_impl& __in)
  97. {
  98. _M_head = __in._M_head;
  99. _M_tail() = __in._M_tail();
  100. return *this;
  101. }
  102. };
  103. template<typename... _Elements>
  104. class tuple : public _Tuple_impl<0, _Elements...>
  105. {
  106. typedef _Tuple_impl<0, _Elements...> _Inherited;
  107. public:
  108. tuple() : _Inherited() { }
  109. explicit
  110. tuple(typename __add_c_ref<_Elements>::type... __elements)
  111. : _Inherited(__elements...) { }
  112. template<typename... _UElements>
  113. tuple(const tuple<_UElements...>& __in)
  114. : _Inherited(__in) { }
  115. tuple(const tuple& __in)
  116. : _Inherited(__in) { }
  117. template<typename... _UElements>
  118. tuple&
  119. operator=(const tuple<_UElements...>& __in)
  120. {
  121. static_cast<_Inherited&>(*this) = __in;
  122. return *this;
  123. }
  124. tuple&
  125. operator=(const tuple& __in)
  126. {
  127. static_cast<_Inherited&>(*this) = __in;
  128. return *this;
  129. }
  130. };
  131. template<> class tuple<> { };
  132. // 2-element tuple, with construction and assignment from a pair.
  133. template<typename _T1, typename _T2>
  134. class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
  135. {
  136. typedef _Tuple_impl<0, _T1, _T2> _Inherited;
  137. public:
  138. tuple() : _Inherited() { }
  139. explicit
  140. tuple(typename __add_c_ref<_T1>::type __a1,
  141. typename __add_c_ref<_T2>::type __a2)
  142. : _Inherited(__a1, __a2) { }
  143. template<typename _U1, typename _U2>
  144. tuple(const tuple<_U1, _U2>& __in)
  145. : _Inherited(__in) { }
  146. tuple(const tuple& __in)
  147. : _Inherited(__in) { }
  148. template<typename _U1, typename _U2>
  149. tuple(const pair<_U1, _U2>& __in)
  150. : _Inherited(_Tuple_impl<0,
  151. typename __add_c_ref<_U1>::type,
  152. typename __add_c_ref<_U2>::type>(__in.first,
  153. __in.second))
  154. { }
  155. template<typename _U1, typename _U2>
  156. tuple&
  157. operator=(const tuple<_U1, _U2>& __in)
  158. {
  159. static_cast<_Inherited&>(*this) = __in;
  160. return *this;
  161. }
  162. tuple&
  163. operator=(const tuple& __in)
  164. {
  165. static_cast<_Inherited&>(*this) = __in;
  166. return *this;
  167. }
  168. template<typename _U1, typename _U2>
  169. tuple&
  170. operator=(const pair<_U1, _U2>& __in)
  171. {
  172. this->_M_head = __in.first;
  173. this->_M_tail()._M_head = __in.second;
  174. return *this;
  175. }
  176. };
  177. /// Gives the type of the ith element of a given tuple type.
  178. template<int __i, typename _Tp>
  179. struct tuple_element;
  180. /**
  181. * Recursive case for tuple_element: strip off the first element in
  182. * the tuple and retrieve the (i-1)th element of the remaining tuple.
  183. */
  184. template<int __i, typename _Head, typename... _Tail>
  185. struct tuple_element<__i, tuple<_Head, _Tail...> >
  186. : tuple_element<__i - 1, tuple<_Tail...> > { };
  187. /**
  188. * Basis case for tuple_element: The first element is the one we're seeking.
  189. */
  190. template<typename _Head, typename... _Tail>
  191. struct tuple_element<0, tuple<_Head, _Tail...> >
  192. {
  193. typedef _Head type;
  194. };
  195. /// Finds the size of a given tuple type.
  196. template<typename _Tp>
  197. struct tuple_size;
  198. /// class tuple_size
  199. template<typename... _Elements>
  200. struct tuple_size<tuple<_Elements...> >
  201. {
  202. static const int value = sizeof...(_Elements);
  203. };
  204. template<typename... _Elements>
  205. const int tuple_size<tuple<_Elements...> >::value;
  206. template<int __i, typename _Head, typename... _Tail>
  207. inline typename __add_ref<_Head>::type
  208. __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
  209. {
  210. return __t._M_head;
  211. }
  212. template<int __i, typename _Head, typename... _Tail>
  213. inline typename __add_c_ref<_Head>::type
  214. __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
  215. {
  216. return __t._M_head;
  217. }
  218. // Return a reference (const reference) to the ith element of a tuple.
  219. // Any const or non-const ref elements are returned with their original type.
  220. template<int __i, typename... _Elements>
  221. inline typename __add_ref<
  222. typename tuple_element<__i, tuple<_Elements...> >::type
  223. >::type
  224. get(tuple<_Elements...>& __t)
  225. {
  226. return __get_helper<__i>(__t);
  227. }
  228. template<int __i, typename... _Elements>
  229. inline typename __add_c_ref<
  230. typename tuple_element<__i, tuple<_Elements...> >::type
  231. >::type
  232. get(const tuple<_Elements...>& __t)
  233. {
  234. return __get_helper<__i>(__t);
  235. }
  236. // This class helps construct the various comparison operations on tuples
  237. template<int __check_equal_size, int __i, int __j,
  238. typename _Tp, typename _Up>
  239. struct __tuple_compare;
  240. template<int __i, int __j, typename _Tp, typename _Up>
  241. struct __tuple_compare<0, __i, __j, _Tp, _Up>
  242. {
  243. static bool __eq(const _Tp& __t, const _Up& __u)
  244. {
  245. return (get<__i>(__t) == get<__i>(__u) &&
  246. __tuple_compare<0, __i+1, __j, _Tp, _Up>::__eq(__t, __u));
  247. }
  248. static bool __less(const _Tp& __t, const _Up& __u)
  249. {
  250. return ((get<__i>(__t) < get<__i>(__u))
  251. || !(get<__i>(__u) < get<__i>(__t)) &&
  252. __tuple_compare<0, __i+1, __j, _Tp, _Up>::__less(__t, __u));
  253. }
  254. };
  255. template<int __i, typename _Tp, typename _Up>
  256. struct __tuple_compare<0, __i, __i, _Tp, _Up>
  257. {
  258. static bool __eq(const _Tp&, const _Up&)
  259. { return true; }
  260. static bool __less(const _Tp&, const _Up&)
  261. { return false; }
  262. };
  263. template<typename... _TElements, typename... _UElements>
  264. bool
  265. operator==(const tuple<_TElements...>& __t,
  266. const tuple<_UElements...>& __u)
  267. {
  268. typedef tuple<_TElements...> _Tp;
  269. typedef tuple<_UElements...> _Up;
  270. return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
  271. 0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
  272. }
  273. template<typename... _TElements, typename... _UElements>
  274. bool
  275. operator<(const tuple<_TElements...>& __t,
  276. const tuple<_UElements...>& __u)
  277. {
  278. typedef tuple<_TElements...> _Tp;
  279. typedef tuple<_UElements...> _Up;
  280. return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
  281. 0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
  282. }
  283. template<typename... _TElements, typename... _UElements>
  284. inline bool
  285. operator!=(const tuple<_TElements...>& __t,
  286. const tuple<_UElements...>& __u)
  287. { return !(__t == __u); }
  288. template<typename... _TElements, typename... _UElements>
  289. inline bool
  290. operator>(const tuple<_TElements...>& __t,
  291. const tuple<_UElements...>& __u)
  292. { return __u < __t; }
  293. template<typename... _TElements, typename... _UElements>
  294. inline bool
  295. operator<=(const tuple<_TElements...>& __t,
  296. const tuple<_UElements...>& __u)
  297. { return !(__u < __t); }
  298. template<typename... _TElements, typename... _UElements>
  299. inline bool
  300. operator>=(const tuple<_TElements...>& __t,
  301. const tuple<_UElements...>& __u)
  302. { return !(__t < __u); }
  303. template<typename _Tp>
  304. class reference_wrapper;
  305. // Helper which adds a reference to a type when given a reference_wrapper
  306. template<typename _Tp>
  307. struct __strip_reference_wrapper
  308. {
  309. typedef _Tp __type;
  310. };
  311. template<typename _Tp>
  312. struct __strip_reference_wrapper<reference_wrapper<_Tp> >
  313. {
  314. typedef _Tp& __type;
  315. };
  316. template<typename _Tp>
  317. struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
  318. {
  319. typedef _Tp& __type;
  320. };
  321. template<typename... _Elements>
  322. inline tuple<typename __strip_reference_wrapper<_Elements>::__type...>
  323. make_tuple(_Elements... __args)
  324. {
  325. typedef tuple<typename __strip_reference_wrapper<_Elements>::__type...>
  326. __result_type;
  327. return __result_type(__args...);
  328. }
  329. template<typename... _Elements>
  330. inline tuple<_Elements&...>
  331. tie(_Elements&... __args)
  332. {
  333. return tuple<_Elements&...>(__args...);
  334. }
  335. // A class (and instance) which can be used in 'tie' when an element
  336. // of a tuple is not required
  337. struct _Swallow_assign
  338. {
  339. template<class _Tp>
  340. _Swallow_assign&
  341. operator=(const _Tp&)
  342. { return *this; }
  343. };
  344. // TODO: Put this in some kind of shared file.
  345. namespace
  346. {
  347. _Swallow_assign ignore;
  348. }; // anonymous namespace
  349. }
  350. _GLIBCXX_END_NAMESPACE_VERSION
  351. }
  352. #endif // _GLIBCXX_TR1_TUPLE