]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/tuple
tuple: New.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / tuple
1 // <tuple> -*- C++ -*-
2
3 // Copyright (C) 2007 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /** @file include/tuple
31 * This is a Standard C++ Library header.
32 */
33
34 #ifndef _GLIBCXX_CXX0X_TUPLE
35 #define _GLIBCXX_CXX0X_TUPLE 1
36
37 #pragma GCC system_header
38
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
41 #endif
42
43 #include <utility>
44
45 namespace std
46 {
47 // Adds a const reference to a non-reference type.
48 template<typename _Tp>
49 struct __add_c_ref
50 { typedef const _Tp& type; };
51
52 template<typename _Tp>
53 struct __add_c_ref<_Tp&>
54 { typedef _Tp& type; };
55
56 // Adds a reference to a non-reference type.
57 template<typename _Tp>
58 struct __add_ref
59 { typedef _Tp& type; };
60
61 template<typename _Tp>
62 struct __add_ref<_Tp&>
63 { typedef _Tp& type; };
64
65 template<int _Idx, typename _Head, bool _IsEmpty>
66 struct _Head_base;
67
68 template<int _Idx, typename _Head>
69 struct _Head_base<_Idx, _Head, true>
70 : public _Head
71 {
72 _Head_base()
73 : _Head() { }
74
75 _Head_base(typename __add_c_ref<_Head>::type __h)
76 : _Head(__h) { }
77
78 _Head_base(typename std::remove_reference<_Head>::type&& __h)
79 : _Head(std::forward<_Head>(__h)) { }
80
81 _Head& _M_head() { return *this; }
82 const _Head& _M_head() const { return *this; }
83 };
84
85 template<int _Idx, typename _Head>
86 struct _Head_base<_Idx, _Head, false>
87 {
88 _Head_base()
89 : _M_head_impl() { }
90
91 _Head_base(typename __add_c_ref<_Head>::type __h)
92 : _M_head_impl(__h) { }
93
94 _Head_base(typename std::remove_reference<_Head>::type&& __h)
95 : _M_head_impl(std::move(__h)) { }
96
97 _Head& _M_head() { return _M_head_impl; }
98 const _Head& _M_head() const { return _M_head_impl; }
99
100 _Head _M_head_impl;
101 };
102
103 /**
104 * @if maint
105 * Contains the actual implementation of the @c tuple template, stored
106 * as a recursive inheritance hierarchy from the first element (most
107 * derived class) to the last (least derived class). The @c Idx
108 * parameter gives the 0-based index of the element stored at this
109 * point in the hierarchy; we use it to implement a constant-time
110 * get() operation.
111 * @endif
112 */
113 template<int _Idx, typename... _Elements>
114 struct _Tuple_impl;
115
116 /**
117 * @if maint
118 * Zero-element tuple implementation. This is the basis case for the
119 * inheritance recursion.
120 * @endif maint
121 */
122 template<int _Idx>
123 struct _Tuple_impl<_Idx> { };
124
125 /**
126 * @if maint
127 * Recursive tuple implementation. Here we store the @c Head element
128 * and derive from a @c Tuple_impl containing the remaining elements
129 * (which contains the @c Tail).
130 * @endif
131 */
132 template<int _Idx, typename _Head, typename... _Tail>
133 struct _Tuple_impl<_Idx, _Head, _Tail...>
134 : public _Tuple_impl<_Idx + 1, _Tail...>,
135 private _Head_base<_Idx, _Head, std::is_empty<_Head>::value>
136 {
137 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
138 typedef _Head_base<_Idx, _Head, std::is_empty<_Head>::value> _Base;
139
140 _Head& _M_head() { return _Base::_M_head(); }
141 const _Head& _M_head() const { return _Base::_M_head(); }
142
143 _Inherited& _M_tail() { return *this; }
144 const _Inherited& _M_tail() const { return *this; }
145
146 _Tuple_impl()
147 : _Inherited(), _Base() { }
148
149 explicit
150 _Tuple_impl(typename __add_c_ref<_Head>::type __head,
151 typename __add_c_ref<_Tail>::type... __tail)
152 : _Inherited(__tail...), _Base(__head) { }
153
154 template<typename... _UElements>
155 _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
156 : _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
157
158 _Tuple_impl(const _Tuple_impl& __in)
159 : _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
160
161 template<typename... _UElements>
162 _Tuple_impl&
163 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
164 {
165 _M_head() = __in._M_head();
166 _M_tail() = __in._M_tail();
167 return *this;
168 }
169
170 _Tuple_impl&
171 operator=(const _Tuple_impl& __in)
172 {
173 _M_head() = __in._M_head();
174 _M_tail() = __in._M_tail();
175 return *this;
176 }
177 };
178
179 template<typename... _Elements>
180 class tuple : public _Tuple_impl<0, _Elements...>
181 {
182 typedef _Tuple_impl<0, _Elements...> _Inherited;
183
184 public:
185 tuple()
186 : _Inherited() { }
187
188 explicit
189 tuple(typename __add_c_ref<_Elements>::type... __elements)
190 : _Inherited(__elements...) { }
191
192 template<typename... _UElements>
193 tuple(const tuple<_UElements...>& __in)
194 : _Inherited(__in) { }
195
196 tuple(const tuple& __in)
197 : _Inherited(__in) { }
198
199 template<typename... _UElements>
200 tuple&
201 operator=(const tuple<_UElements...>& __in)
202 {
203 static_cast<_Inherited&>(*this) = __in;
204 return *this;
205 }
206
207 tuple&
208 operator=(const tuple& __in)
209 {
210 static_cast<_Inherited&>(*this) = __in;
211 return *this;
212 }
213 };
214
215 template<> class tuple<> { };
216
217 // 2-element tuple, with construction and assignment from a pair.
218 template<typename _T1, typename _T2>
219 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
220 {
221 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
222
223 public:
224 tuple()
225 : _Inherited() { }
226
227 explicit
228 tuple(typename __add_c_ref<_T1>::type __a1,
229 typename __add_c_ref<_T2>::type __a2)
230 : _Inherited(__a1, __a2) { }
231
232 template<typename _U1, typename _U2>
233 tuple(const tuple<_U1, _U2>& __in)
234 : _Inherited(__in) { }
235
236 tuple(const tuple& __in)
237 : _Inherited(__in) { }
238
239 template<typename _U1, typename _U2>
240 tuple(const pair<_U1, _U2>& __in)
241 : _Inherited(_Tuple_impl<0,
242 typename __add_c_ref<_U1>::type,
243 typename __add_c_ref<_U2>::type>(__in.first,
244 __in.second))
245 { }
246
247 template<typename _U1, typename _U2>
248 tuple&
249 operator=(const tuple<_U1, _U2>& __in)
250 {
251 static_cast<_Inherited&>(*this) = __in;
252 return *this;
253 }
254
255 tuple&
256 operator=(const tuple& __in)
257 {
258 static_cast<_Inherited&>(*this) = __in;
259 return *this;
260 }
261
262 template<typename _U1, typename _U2>
263 tuple&
264 operator=(const pair<_U1, _U2>& __in)
265 {
266 this->_M_head() = __in.first;
267 this->_M_tail()._M_head() = __in.second;
268 return *this;
269 }
270 };
271
272
273 /// Gives the type of the ith element of a given tuple type.
274 template<int __i, typename _Tp>
275 struct tuple_element;
276
277 /**
278 * @if maint
279 * Recursive case for tuple_element: strip off the first element in
280 * the tuple and retrieve the (i-1)th element of the remaining tuple.
281 * @endif
282 */
283 template<int __i, typename _Head, typename... _Tail>
284 struct tuple_element<__i, tuple<_Head, _Tail...> >
285 : tuple_element<__i - 1, tuple<_Tail...> > { };
286
287 /**
288 * @if maint
289 * Basis case for tuple_element: The first element is the one we're seeking.
290 * @endif
291 */
292 template<typename _Head, typename... _Tail>
293 struct tuple_element<0, tuple<_Head, _Tail...> >
294 {
295 typedef _Head type;
296 };
297
298 /// Finds the size of a given tuple type.
299 template<typename _Tp>
300 struct tuple_size;
301
302 /// @brief class tuple_size
303 template<typename... _Elements>
304 struct tuple_size<tuple<_Elements...> >
305 {
306 static const int value = sizeof...(_Elements);
307 };
308
309 template<typename... _Elements>
310 const int tuple_size<tuple<_Elements...> >::value;
311
312 template<int __i, typename _Head, typename... _Tail>
313 inline typename __add_ref<_Head>::type
314 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
315 { return __t._M_head(); }
316
317 template<int __i, typename _Head, typename... _Tail>
318 inline typename __add_c_ref<_Head>::type
319 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
320 { return __t._M_head(); }
321
322 // Return a reference (const reference) to the ith element of a tuple.
323 // Any const or non-const ref elements are returned with their original type.
324 template<int __i, typename... _Elements>
325 inline typename __add_ref<
326 typename tuple_element<__i, tuple<_Elements...> >::type
327 >::type
328 get(tuple<_Elements...>& __t)
329 { return __get_helper<__i>(__t); }
330
331 template<int __i, typename... _Elements>
332 inline typename __add_c_ref<
333 typename tuple_element<__i, tuple<_Elements...> >::type
334 >::type
335 get(const tuple<_Elements...>& __t)
336 { return __get_helper<__i>(__t); }
337
338 // This class helps construct the various comparison operations on tuples
339 template<int __check_equal_size, int __i, int __j,
340 typename _Tp, typename _Up>
341 struct __tuple_compare;
342
343 template<int __i, int __j, typename _Tp, typename _Up>
344 struct __tuple_compare<0, __i, __j, _Tp, _Up>
345 {
346 static bool __eq(const _Tp& __t, const _Up& __u)
347 {
348 return (get<__i>(__t) == get<__i>(__u) &&
349 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__eq(__t, __u));
350 }
351
352 static bool __less(const _Tp& __t, const _Up& __u)
353 {
354 return ((get<__i>(__t) < get<__i>(__u))
355 || !(get<__i>(__u) < get<__i>(__t)) &&
356 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__less(__t, __u));
357 }
358 };
359
360 template<int __i, typename _Tp, typename _Up>
361 struct __tuple_compare<0, __i, __i, _Tp, _Up>
362 {
363 static bool __eq(const _Tp&, const _Up&)
364 { return true; }
365
366 static bool __less(const _Tp&, const _Up&)
367 { return false; }
368 };
369
370 template<typename... _TElements, typename... _UElements>
371 bool
372 operator==(const tuple<_TElements...>& __t,
373 const tuple<_UElements...>& __u)
374 {
375 typedef tuple<_TElements...> _Tp;
376 typedef tuple<_UElements...> _Up;
377 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Tp>::value,
378 0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
379 }
380
381 template<typename... _TElements, typename... _UElements>
382 bool
383 operator<(const tuple<_TElements...>& __t,
384 const tuple<_UElements...>& __u)
385 {
386 typedef tuple<_TElements...> _Tp;
387 typedef tuple<_UElements...> _Up;
388 return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Tp>::value,
389 0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
390 }
391
392 template<typename... _TElements, typename... _UElements>
393 inline bool
394 operator!=(const tuple<_TElements...>& __t,
395 const tuple<_UElements...>& __u)
396 { return !(__t == __u); }
397
398 template<typename... _TElements, typename... _UElements>
399 inline bool
400 operator>(const tuple<_TElements...>& __t,
401 const tuple<_UElements...>& __u)
402 { return __u < __t; }
403
404 template<typename... _TElements, typename... _UElements>
405 inline bool
406 operator<=(const tuple<_TElements...>& __t,
407 const tuple<_UElements...>& __u)
408 { return !(__u < __t); }
409
410 template<typename... _TElements, typename... _UElements>
411 inline bool
412 operator>=(const tuple<_TElements...>& __t,
413 const tuple<_UElements...>& __u)
414 { return !(__t < __u); }
415
416 // NB: DR 705.
417 template<typename... _Elements>
418 inline tuple<typename __decay_and_strip<_Elements>::__type...>
419 make_tuple(_Elements&&... __args)
420 {
421 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
422 __result_type;
423 return __result_type(std::forward<_Elements>(__args)...);
424 }
425
426 template<int...> struct __index_holder { };
427
428 template<int __i, typename _IdxHolder, typename... _Elements>
429 struct __index_holder_impl;
430
431 template<int __i, int... _Indexes, typename _IdxHolder, typename... _Elements>
432 struct __index_holder_impl<__i, __index_holder<_Indexes...>,
433 _IdxHolder, _Elements...>
434 {
435 typedef typename __index_holder_impl<__i + 1,
436 __index_holder<_Indexes..., __i>,
437 _Elements...>::type type;
438 };
439
440 template<int __i, int... _Indexes>
441 struct __index_holder_impl<__i, __index_holder<_Indexes...> >
442 { typedef __index_holder<_Indexes...> type; };
443
444 template<typename... _Elements>
445 struct __make_index_holder
446 : __index_holder_impl<0, __index_holder<>, _Elements...> { };
447
448 template<typename... _TElements, int... _TIdx,
449 typename... _UElements, int... _UIdx>
450 inline tuple<_TElements..., _UElements...>
451 __tuple_cat_helper(const tuple<_TElements...>& __t,
452 const __index_holder<_TIdx...>&,
453 const tuple<_UElements...>& __u,
454 const __index_holder<_UIdx...>&)
455 { return tuple<_TElements..., _UElements...>(get<_TIdx>(__t)...,
456 get<_UIdx>(__u)...); }
457
458 template<typename... _TElements, int... _TIdx,
459 typename... _UElements, int... _UIdx>
460 inline tuple<_TElements..., _UElements...>
461 __tuple_cat_helper(tuple<_TElements...>&& __t,
462 const __index_holder<_TIdx...>&,
463 const tuple<_UElements...>& __u,
464 const __index_holder<_UIdx...>&)
465 { return tuple<_TElements..., _UElements...>(std::move(get<_TIdx>(__t))...,
466 get<_UIdx>(__u)...); }
467
468 template<typename... _TElements, int... _TIdx,
469 typename... _UElements, int... _UIdx>
470 inline tuple<_TElements..., _UElements...>
471 __tuple_cat_helper(const tuple<_TElements...>& __t,
472 const __index_holder<_TIdx...>&,
473 tuple<_UElements...>&& __u,
474 const __index_holder<_UIdx...>&)
475 { return tuple<_TElements..., _UElements...>(get<_TIdx>(__t)...,
476 std::move(get<_UIdx>(__u))...); }
477
478 template<typename... _TElements, int... _TIdx,
479 typename... _UElements, int... _UIdx>
480 inline tuple<_TElements..., _UElements...>
481 __tuple_cat_helper(tuple<_TElements...>&& __t,
482 const __index_holder<_TIdx...>&,
483 tuple<_UElements...>&& __u,
484 const __index_holder<_UIdx...>&)
485 { return tuple<_TElements..., _UElements...>(std::move(get<_TIdx>(__t))...,
486 std::move(get<_UIdx>(__u))...); }
487
488 template<typename... _TElements, typename... _UElements>
489 inline tuple<_TElements..., _UElements...>
490 tuple_cat(const tuple<_TElements...>& __t, const tuple<_UElements...>& __u)
491 {
492 return __tuple_cat_helper(__t, typename
493 __make_index_holder<_TElements...>::type(),
494 __u, typename
495 __make_index_holder<_UElements...>::type());
496 }
497
498 template<typename... _TElements, typename... _UElements>
499 inline tuple<_TElements..., _UElements...>
500 tuple_cat(tuple<_TElements...>&& __t, const tuple<_UElements...>& __u)
501 {
502 return __tuple_cat_helper(std::move(__t), typename
503 __make_index_holder<_TElements...>::type(),
504 __u, typename
505 __make_index_holder<_UElements...>::type());
506 }
507
508 template<typename... _TElements, typename... _UElements>
509 inline tuple<_TElements..., _UElements...>
510 tuple_cat(const tuple<_TElements...>& __t, tuple<_UElements...>&& __u)
511 {
512 return __tuple_cat_helper(__t, typename
513 __make_index_holder<_TElements...>::type(),
514 std::move(__u), typename
515 __make_index_holder<_UElements...>::type());
516 }
517
518 template<typename... _TElements, typename... _UElements>
519 inline tuple<_TElements..., _UElements...>
520 tuple_cat(tuple<_TElements...>&& __t, tuple<_UElements...>&& __u)
521 {
522 return __tuple_cat_helper(std::move(__t), typename
523 __make_index_holder<_TElements...>::type(),
524 std::move(__u), typename
525 __make_index_holder<_UElements...>::type());
526 }
527
528 template<typename... _TElements, typename... _UElements>
529 tuple<_TElements..., _UElements...>
530 operator+(tuple<_TElements...>&& __t, tuple<_UElements...>&& __u)
531 { return tuple_cat(std::forward<decltype(__t)>(__t),
532 std::forward<decltype(__u)>(__u)); }
533
534 template<typename... _Elements>
535 inline tuple<_Elements&...>
536 tie(_Elements&... __args)
537 { return tuple<_Elements&...>(__args...); }
538
539 // A class (and instance) which can be used in 'tie' when an element
540 // of a tuple is not required
541 struct _Swallow_assign
542 {
543 template<class _Tp>
544 _Swallow_assign&
545 operator=(const _Tp&)
546 { return *this; }
547 };
548
549 // TODO: Put this in some kind of shared file.
550 namespace
551 {
552 _Swallow_assign ignore;
553 }; // anonymous namespace
554 }
555
556 #endif // _GLIBCXX_CXX0X_TUPLE