tuple 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // class template tuple -*- C++ -*-
  2. // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 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
  30. {
  31. namespace tr1
  32. {
  33. // Adds a const reference to a non-reference type.
  34. template<typename _Tp>
  35. struct __add_c_ref
  36. { typedef const _Tp& type; };
  37. template<typename _Tp>
  38. struct __add_c_ref<_Tp&>
  39. { typedef _Tp& type; };
  40. // Adds a reference to a non-reference type.
  41. template<typename _Tp>
  42. struct __add_ref
  43. { typedef _Tp& type; };
  44. template<typename _Tp>
  45. struct __add_ref<_Tp&>
  46. { typedef _Tp& type; };
  47. /**
  48. * Contains the actual implementation of the @c tuple template, stored
  49. * as a recursive inheritance hierarchy from the first element (most
  50. * derived class) to the last (least derived class). The @c Idx
  51. * parameter gives the 0-based index of the element stored at this
  52. * point in the hierarchy; we use it to implement a constant-time
  53. * get() operation.
  54. */
  55. template<int _Idx, typename... _Elements>
  56. struct _Tuple_impl;
  57. /**
  58. * Zero-element tuple implementation. This is the basis case for the
  59. * inheritance recursion.
  60. */
  61. template<int _Idx>
  62. struct _Tuple_impl<_Idx> { };
  63. /**
  64. * Recursive tuple implementation. Here we store the @c Head element
  65. * and derive from a @c Tuple_impl containing the remaining elements
  66. * (which contains the @c Tail).
  67. */
  68. template<int _Idx, typename _Head, typename... _Tail>
  69. struct _Tuple_impl<_Idx, _Head, _Tail...>
  70. : public _Tuple_impl<_Idx + 1, _Tail...>
  71. {
  72. typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
  73. _Head _M_head;
  74. _Inherited& _M_tail() { return *this; }
  75. const _Inherited& _M_tail() const { return *this; }
  76. _Tuple_impl() : _Inherited(), _M_head() { }
  77. explicit
  78. _Tuple_impl(typename __add_c_ref<_Head>::type __head,
  79. typename __add_c_ref<_Tail>::type... __tail)
  80. : _Inherited(__tail...), _M_head(__head) { }
  81. template<typename... _UElements>
  82. _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
  83. : _Inherited(__in._M_tail()), _M_head(__in._M_head) { }
  84. _Tuple_impl(const _Tuple_impl& __in)
  85. : _Inherited(__in._M_tail()), _M_head(__in._M_head) { }
  86. template<typename... _UElements>
  87. _Tuple_impl&
  88. operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
  89. {
  90. _M_head = __in._M_head;
  91. _M_tail() = __in._M_tail();
  92. return *this;
  93. }
  94. _Tuple_impl&
  95. operator=(const _Tuple_impl& __in)
  96. {
  97. _M_head = __in._M_head;
  98. _M_tail() = __in._M_tail();
  99. return *this;
  100. }
  101. };
  102. template<typename... _Elements>
  103. class tuple : public _Tuple_impl<0, _Elements...>
  104. {
  105. typedef _Tuple_impl<0, _Elements...> _Inherited;
  106. public:
  107. tuple() : _Inherited() { }
  108. explicit
  109. tuple(typename __add_c_ref<_Elements>::type... __elements)
  110. : _Inherited(__elements...) { }
  111. template<typename... _UElements>
  112. tuple(const tuple<_UElements...>& __in)
  113. : _Inherited(__in) { }
  114. tuple(const tuple& __in)
  115. : _Inherited(__in) { }
  116. template<typename... _UElements>
  117. tuple&
  118. operator=(const tuple<_UElements...>& __in)
  119. {
  120. static_cast<_Inherited&>(*this) = __in;
  121. return *this;
  122. }
  123. tuple&
  124. operator=(const tuple& __in)
  125. {
  126. static_cast<_Inherited&>(*this) = __in;
  127. return *this;
  128. }
  129. };
  130. template<> class tuple<> { };
  131. // 2-element tuple, with construction and assignment from a pair.
  132. template<typename _T1, typename _T2>
  133. class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
  134. {
  135. typedef _Tuple_impl<0, _T1, _T2> _Inherited;
  136. public:
  137. tuple() : _Inherited() { }
  138. explicit
  139. tuple(typename __add_c_ref<_T1>::type __a1,
  140. typename __add_c_ref<_T2>::type __a2)
  141. : _Inherited(__a1, __a2) { }
  142. template<typename _U1, typename _U2>
  143. tuple(const tuple<_U1, _U2>& __in)
  144. : _Inherited(__in) { }
  145. tuple(const tuple& __in)
  146. : _Inherited(__in) { }
  147. template<typename _U1, typename _U2>
  148. tuple(const pair<_U1, _U2>& __in)
  149. : _Inherited(_Tuple_impl<0,
  150. typename __add_c_ref<_U1>::type,
  151. typename __add_c_ref<_U2>::type>(__in.first,
  152. __in.second))
  153. { }
  154. template<typename _U1, typename _U2>
  155. tuple&
  156. operator=(const tuple<_U1, _U2>& __in)
  157. {
  158. static_cast<_Inherited&>(*this) = __in;
  159. return *this;
  160. }
  161. tuple&
  162. operator=(const tuple& __in)
  163. {
  164. static_cast<_Inherited&>(*this) = __in;
  165. return *this;
  166. }
  167. template<typename _U1, typename _U2>
  168. tuple&
  169. operator=(const pair<_U1, _U2>& __in)
  170. {
  171. this->_M_head = __in.first;
  172. this->_M_tail()._M_head = __in.second;
  173. return *this;
  174. }
  175. };
  176. /// Gives the type of the ith element of a given tuple type.
  177. template<int __i, typename _Tp>
  178. struct tuple_element;
  179. /**
  180. * Recursive case for tuple_element: strip off the first element in
  181. * the tuple and retrieve the (i-1)th element of the remaining tuple.
  182. */
  183. template<int __i, typename _Head, typename... _Tail>
  184. struct tuple_element<__i, tuple<_Head, _Tail...> >
  185. : tuple_element<__i - 1, tuple<_Tail...> > { };
  186. /**
  187. * Basis case for tuple_element: The first element is the one we're seeking.
  188. */
  189. template<typename _Head, typename... _Tail>
  190. struct tuple_element<0, tuple<_Head, _Tail...> >
  191. {
  192. typedef _Head type;
  193. };
  194. /// Finds the size of a given tuple type.
  195. template<typename _Tp>
  196. struct tuple_size;
  197. /// class tuple_size
  198. template<typename... _Elements>
  199. struct tuple_size<tuple<_Elements...> >
  200. {
  201. static const int value = sizeof...(_Elements);
  202. };
  203. template<typename... _Elements>
  204. const int tuple_size<tuple<_Elements...> >::value;
  205. template<int __i, typename _Head, typename... _Tail>
  206. inline typename __add_ref<_Head>::type
  207. __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
  208. {
  209. return __t._M_head;
  210. }
  211. template<int __i, typename _Head, typename... _Tail>
  212. inline typename __add_c_ref<_Head>::type
  213. __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
  214. {
  215. return __t._M_head;
  216. }
  217. // Return a reference (const reference) to the ith element of a tuple.
  218. // Any const or non-const ref elements are returned with their original type.
  219. template<int __i, typename... _Elements>
  220. inline typename __add_ref<
  221. typename tuple_element<__i, tuple<_Elements...> >::type
  222. >::type
  223. get(tuple<_Elements...>& __t)
  224. {
  225. return __get_helper<__i>(__t);
  226. }
  227. template<int __i, typename... _Elements>
  228. inline typename __add_c_ref<
  229. typename tuple_element<__i, tuple<_Elements...> >::type
  230. >::type
  231. get(const tuple<_Elements...>& __t)
  232. {
  233. return __get_helper<__i>(__t);
  234. }
  235. // This class helps construct the various comparison operations on tuples
  236. template<int __check_equal_size, int __i, int __j,
  237. typename _Tp, typename _Up>
  238. struct __tuple_compare;
  239. template<int __i, int __j, typename _Tp, typename _Up>
  240. struct __tuple_compare<0, __i, __j, _Tp, _Up>
  241. {
  242. static bool __eq(const _Tp& __t, const _Up& __u)
  243. {
  244. return (get<__i>(__t) == get<__i>(__u) &&
  245. __tuple_compare<0, __i+1, __j, _Tp, _Up>::__eq(__t, __u));
  246. }
  247. static bool __less(const _Tp& __t, const _Up& __u)
  248. {
  249. return ((get<__i>(__t) < get<__i>(__u))
  250. || !(get<__i>(__u) < get<__i>(__t)) &&
  251. __tuple_compare<0, __i+1, __j, _Tp, _Up>::__less(__t, __u));
  252. }
  253. };
  254. template<int __i, typename _Tp, typename _Up>
  255. struct __tuple_compare<0, __i, __i, _Tp, _Up>
  256. {
  257. static bool __eq(const _Tp&, const _Up&)
  258. { return true; }
  259. static bool __less(const _Tp&, const _Up&)
  260. { return false; }
  261. };
  262. template<typename... _TElements, typename... _UElements>
  263. bool
  264. operator==(const tuple<_TElements...>& __t,
  265. const tuple<_UElements...>& __u)
  266. {
  267. typedef tuple<_TElements...> _Tp;
  268. typedef tuple<_UElements...> _Up;
  269. return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
  270. 0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
  271. }
  272. template<typename... _TElements, typename... _UElements>
  273. bool
  274. operator<(const tuple<_TElements...>& __t,
  275. const tuple<_UElements...>& __u)
  276. {
  277. typedef tuple<_TElements...> _Tp;
  278. typedef tuple<_UElements...> _Up;
  279. return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
  280. 0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
  281. }
  282. template<typename... _TElements, typename... _UElements>
  283. inline bool
  284. operator!=(const tuple<_TElements...>& __t,
  285. const tuple<_UElements...>& __u)
  286. { return !(__t == __u); }
  287. template<typename... _TElements, typename... _UElements>
  288. inline bool
  289. operator>(const tuple<_TElements...>& __t,
  290. const tuple<_UElements...>& __u)
  291. { return __u < __t; }
  292. template<typename... _TElements, typename... _UElements>
  293. inline bool
  294. operator<=(const tuple<_TElements...>& __t,
  295. const tuple<_UElements...>& __u)
  296. { return !(__u < __t); }
  297. template<typename... _TElements, typename... _UElements>
  298. inline bool
  299. operator>=(const tuple<_TElements...>& __t,
  300. const tuple<_UElements...>& __u)
  301. { return !(__t < __u); }
  302. template<typename _Tp>
  303. class reference_wrapper;
  304. // Helper which adds a reference to a type when given a reference_wrapper
  305. template<typename _Tp>
  306. struct __strip_reference_wrapper
  307. {
  308. typedef _Tp __type;
  309. };
  310. template<typename _Tp>
  311. struct __strip_reference_wrapper<reference_wrapper<_Tp> >
  312. {
  313. typedef _Tp& __type;
  314. };
  315. template<typename _Tp>
  316. struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
  317. {
  318. typedef _Tp& __type;
  319. };
  320. template<typename... _Elements>
  321. inline tuple<typename __strip_reference_wrapper<_Elements>::__type...>
  322. make_tuple(_Elements... __args)
  323. {
  324. typedef tuple<typename __strip_reference_wrapper<_Elements>::__type...>
  325. __result_type;
  326. return __result_type(__args...);
  327. }
  328. template<typename... _Elements>
  329. inline tuple<_Elements&...>
  330. tie(_Elements&... __args)
  331. {
  332. return tuple<_Elements&...>(__args...);
  333. }
  334. // A class (and instance) which can be used in 'tie' when an element
  335. // of a tuple is not required
  336. struct _Swallow_assign
  337. {
  338. template<class _Tp>
  339. _Swallow_assign&
  340. operator=(const _Tp&)
  341. { return *this; }
  342. };
  343. // TODO: Put this in some kind of shared file.
  344. namespace
  345. {
  346. _Swallow_assign ignore;
  347. }; // anonymous namespace
  348. }
  349. }
  350. #endif // _GLIBCXX_TR1_TUPLE