OSDN Git Service

2010-02-25 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / tuple
1 // <tuple> -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009, 2010 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file include/tuple
26  *  This is a Standard C++ Library header.
27  */
28
29 #ifndef _GLIBCXX_TUPLE
30 #define _GLIBCXX_TUPLE 1
31
32 #pragma GCC system_header
33
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
36 #else
37
38 #include <utility>
39
40 namespace std
41 {
42   // Adds a const reference to a non-reference type.
43   template<typename _Tp>
44     struct __add_c_ref
45     { typedef const _Tp& type; };
46
47   template<typename _Tp>
48     struct __add_c_ref<_Tp&>
49     { typedef _Tp& type; };
50
51   // Adds a reference to a non-reference type.
52   template<typename _Tp>
53     struct __add_ref
54     { typedef _Tp& type; };
55
56   template<typename _Tp>
57     struct __add_ref<_Tp&>
58     { typedef _Tp& type; };
59
60   template<std::size_t _Idx, typename _Head, bool _IsEmpty>
61     struct _Head_base;
62
63   template<std::size_t _Idx, typename _Head>
64     struct _Head_base<_Idx, _Head, true>
65     : public _Head
66     {
67       _Head_base()
68       : _Head() { }
69
70       _Head_base(const _Head& __h)
71       : _Head(__h) { }
72
73       template<typename _UHead>
74         _Head_base(_UHead&& __h)
75         : _Head(std::forward<_UHead>(__h)) { }
76
77       _Head&       _M_head()       { return *this; }
78       const _Head& _M_head() const { return *this; }
79     
80       void _M_swap_impl(_Head&) { /* no-op */ }
81     };
82
83   template<std::size_t _Idx, typename _Head>
84     struct _Head_base<_Idx, _Head, false>
85     {
86       _Head_base()
87       : _M_head_impl() { }
88
89       _Head_base(const _Head& __h)
90       : _M_head_impl(__h) { }
91
92       template<typename _UHead>
93         _Head_base(_UHead&& __h)
94         : _M_head_impl(std::forward<_UHead>(__h)) { }
95
96       _Head&       _M_head()       { return _M_head_impl; }
97       const _Head& _M_head() const { return _M_head_impl; }        
98
99       void
100       _M_swap_impl(_Head& __h)
101       { 
102         using std::swap;
103         swap(__h, _M_head_impl);
104       }
105
106       _Head _M_head_impl; 
107     };
108
109   /**
110    * Contains the actual implementation of the @c tuple template, stored
111    * as a recursive inheritance hierarchy from the first element (most
112    * derived class) to the last (least derived class). The @c Idx
113    * parameter gives the 0-based index of the element stored at this
114    * point in the hierarchy; we use it to implement a constant-time
115    * get() operation.
116    */
117   template<std::size_t _Idx, typename... _Elements>
118     struct _Tuple_impl; 
119
120   /**
121    * Zero-element tuple implementation. This is the basis case for the 
122    * inheritance recursion.
123    */
124   template<std::size_t _Idx>
125     struct _Tuple_impl<_Idx>
126     { 
127     protected:
128       void _M_swap_impl(_Tuple_impl&) { /* no-op */ }
129     };
130
131   /**
132    * Recursive tuple implementation. Here we store the @c Head element
133    * and derive from a @c Tuple_impl containing the remaining elements
134    * (which contains the @c Tail).
135    */
136   template<std::size_t _Idx, typename _Head, typename... _Tail>
137     struct _Tuple_impl<_Idx, _Head, _Tail...>
138     : public _Tuple_impl<_Idx + 1, _Tail...>,
139       private _Head_base<_Idx, _Head, std::is_empty<_Head>::value>
140     {
141       typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
142       typedef _Head_base<_Idx, _Head, std::is_empty<_Head>::value> _Base;
143
144       _Head&            _M_head()       { return _Base::_M_head(); }
145       const _Head&      _M_head() const { return _Base::_M_head(); }
146
147       _Inherited&       _M_tail()       { return *this; }
148       const _Inherited& _M_tail() const { return *this; }
149
150       _Tuple_impl()
151       : _Inherited(), _Base() { }
152
153       explicit 
154       _Tuple_impl(const _Head& __head, const _Tail&... __tail)
155       : _Inherited(__tail...), _Base(__head) { }
156
157       template<typename _UHead, typename... _UTail> 
158         explicit
159         _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
160         : _Inherited(std::forward<_UTail>(__tail)...),
161           _Base(std::forward<_UHead>(__head)) { }
162
163       _Tuple_impl(const _Tuple_impl& __in)
164       : _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
165
166       _Tuple_impl(_Tuple_impl&& __in)
167       : _Inherited(std::move(__in._M_tail())),
168         _Base(std::forward<_Head>(__in._M_head())) { }
169
170       template<typename... _UElements>
171         _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
172         : _Inherited(__in._M_tail()), _Base(__in._M_head()) { }
173
174       template<typename... _UElements>
175         _Tuple_impl(_Tuple_impl<_Idx, _UElements...>&& __in)
176         : _Inherited(std::move(__in._M_tail())),
177           _Base(std::move(__in._M_head())) { }
178
179       _Tuple_impl&
180       operator=(const _Tuple_impl& __in)
181       {
182         _M_head() = __in._M_head();
183         _M_tail() = __in._M_tail();
184         return *this;
185       }
186
187       _Tuple_impl&
188       operator=(_Tuple_impl&& __in)
189       {
190         _M_head() = std::move(__in._M_head());
191         _M_tail() = std::move(__in._M_tail());
192         return *this;
193       }
194
195       template<typename... _UElements>
196         _Tuple_impl&
197         operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
198         {
199           _M_head() = __in._M_head();
200           _M_tail() = __in._M_tail();
201           return *this;
202         }
203
204       template<typename... _UElements>
205         _Tuple_impl&
206         operator=(_Tuple_impl<_Idx, _UElements...>&& __in)
207         {
208           _M_head() = std::move(__in._M_head());
209           _M_tail() = std::move(__in._M_tail());
210           return *this;
211         }
212
213     protected:
214       void
215       _M_swap_impl(_Tuple_impl& __in)
216       {
217         _Base::_M_swap_impl(__in._M_head());
218         _Inherited::_M_swap_impl(__in._M_tail());
219       }
220     };
221
222   /// tuple
223   template<typename... _Elements> 
224     class tuple : public _Tuple_impl<0, _Elements...>
225     {
226       typedef _Tuple_impl<0, _Elements...> _Inherited;
227
228     public:
229       tuple()
230       : _Inherited() { }
231
232       explicit
233       tuple(const _Elements&... __elements)
234       : _Inherited(__elements...) { }
235
236       template<typename... _UElements>
237         explicit
238         tuple(_UElements&&... __elements)
239         : _Inherited(std::forward<_UElements>(__elements)...) { }
240
241       tuple(const tuple& __in)
242       : _Inherited(static_cast<const _Inherited&>(__in)) { }
243
244       tuple(tuple&& __in)
245       : _Inherited(static_cast<_Inherited&&>(__in)) { }
246
247       template<typename... _UElements>
248         tuple(const tuple<_UElements...>& __in)
249         : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
250         { }
251
252       template<typename... _UElements>
253         tuple(tuple<_UElements...>&& __in)
254         : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
255
256       // XXX http://gcc.gnu.org/ml/libstdc++/2008-02/msg00047.html
257       template<typename... _UElements>
258         tuple(tuple<_UElements...>& __in)
259         : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
260         { }
261
262       tuple&
263       operator=(const tuple& __in)
264       {
265         static_cast<_Inherited&>(*this) = __in;
266         return *this;
267       }
268
269       tuple&
270       operator=(tuple&& __in)
271       {
272         static_cast<_Inherited&>(*this) = std::move(__in);
273         return *this;
274       }
275
276       template<typename... _UElements>
277         tuple&
278         operator=(const tuple<_UElements...>& __in)
279         {
280           static_cast<_Inherited&>(*this) = __in;
281           return *this;
282         }
283
284       template<typename... _UElements>
285         tuple&
286         operator=(tuple<_UElements...>&& __in)
287         {
288           static_cast<_Inherited&>(*this) = std::move(__in);
289           return *this;
290         }
291
292       void
293       swap(tuple& __in)
294       { _Inherited::_M_swap_impl(__in); }
295     };
296
297
298   template<>  
299     class tuple<>
300     {
301     public:
302       void swap(tuple&) { /* no-op */ }
303     };
304
305   /// tuple (2-element), with construction and assignment from a pair.
306   template<typename _T1, typename _T2>
307     class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
308     {
309       typedef _Tuple_impl<0, _T1, _T2> _Inherited;
310
311     public:
312       tuple()
313       : _Inherited() { }
314
315       explicit
316       tuple(const _T1& __a1, const _T2& __a2)
317       : _Inherited(__a1, __a2) { }
318
319       template<typename _U1, typename _U2>
320         explicit
321         tuple(_U1&& __a1, _U2&& __a2)
322         : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
323
324       tuple(const tuple& __in)
325       : _Inherited(static_cast<const _Inherited&>(__in)) { }
326
327       tuple(tuple&& __in)
328       : _Inherited(static_cast<_Inherited&&>(__in)) { }
329
330       template<typename _U1, typename _U2>
331         tuple(const tuple<_U1, _U2>& __in)
332         : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
333
334       template<typename _U1, typename _U2>
335         tuple(tuple<_U1, _U2>&& __in)
336         : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
337
338       template<typename _U1, typename _U2>
339         tuple(const pair<_U1, _U2>& __in)
340         : _Inherited(__in.first, __in.second) { }
341
342       template<typename _U1, typename _U2>
343         tuple(pair<_U1, _U2>&& __in)
344         : _Inherited(std::move(__in.first), std::move(__in.second)) { }
345
346       tuple&
347       operator=(const tuple& __in)
348       {
349         static_cast<_Inherited&>(*this) = __in;
350         return *this;
351       }
352
353       tuple&
354       operator=(tuple&& __in)
355       {
356         static_cast<_Inherited&>(*this) = std::move(__in);
357         return *this;
358       }
359
360       template<typename _U1, typename _U2>
361         tuple&
362         operator=(const tuple<_U1, _U2>& __in)
363         {
364           static_cast<_Inherited&>(*this) = __in;
365           return *this;
366         }
367
368       template<typename _U1, typename _U2>
369         tuple&
370         operator=(tuple<_U1, _U2>&& __in)
371         {
372           static_cast<_Inherited&>(*this) = std::move(__in);
373           return *this;
374         }
375
376       template<typename _U1, typename _U2>
377         tuple&
378         operator=(const pair<_U1, _U2>& __in)
379         {
380           this->_M_head() = __in.first;
381           this->_M_tail()._M_head() = __in.second;
382           return *this;
383         }
384
385       template<typename _U1, typename _U2>
386         tuple&
387         operator=(pair<_U1, _U2>&& __in)
388         {
389           this->_M_head() = std::move(__in.first);
390           this->_M_tail()._M_head() = std::move(__in.second);
391           return *this;
392         }
393
394       void
395       swap(tuple& __in)
396       { 
397         using std::swap;
398         swap(this->_M_head(), __in._M_head());
399         swap(this->_M_tail()._M_head(), __in._M_tail()._M_head());      
400       }
401     };
402
403
404   /// Gives the type of the ith element of a given tuple type.
405   template<std::size_t __i, typename _Tp>
406     struct tuple_element;
407
408   /**
409    * Recursive case for tuple_element: strip off the first element in
410    * the tuple and retrieve the (i-1)th element of the remaining tuple.
411    */
412   template<std::size_t __i, typename _Head, typename... _Tail>
413     struct tuple_element<__i, tuple<_Head, _Tail...> >
414     : tuple_element<__i - 1, tuple<_Tail...> > { };
415
416   /**
417    * Basis case for tuple_element: The first element is the one we're seeking.
418    */
419   template<typename _Head, typename... _Tail>
420     struct tuple_element<0, tuple<_Head, _Tail...> >
421     {
422       typedef _Head type;
423     };
424
425   /// Finds the size of a given tuple type.
426   template<typename _Tp>
427     struct tuple_size;
428
429   /// class tuple_size
430   template<typename... _Elements>
431     struct tuple_size<tuple<_Elements...> >
432     {
433       static const std::size_t value = sizeof...(_Elements);
434     };
435
436   template<typename... _Elements>
437     const std::size_t tuple_size<tuple<_Elements...> >::value;
438
439   template<std::size_t __i, typename _Head, typename... _Tail>
440     inline typename __add_ref<_Head>::type
441     __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
442     { return __t._M_head(); }
443
444   template<std::size_t __i, typename _Head, typename... _Tail>
445     inline typename __add_c_ref<_Head>::type
446     __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
447     { return __t._M_head(); }
448
449   // Return a reference (const reference) to the ith element of a tuple.
450   // Any const or non-const ref elements are returned with their original type.
451   template<std::size_t __i, typename... _Elements>
452     inline typename __add_ref<
453                       typename tuple_element<__i, tuple<_Elements...> >::type
454                     >::type
455     get(tuple<_Elements...>& __t)
456     { return __get_helper<__i>(__t); }
457
458   template<std::size_t __i, typename... _Elements>
459     inline typename __add_c_ref<
460                       typename tuple_element<__i, tuple<_Elements...> >::type
461                     >::type
462     get(const tuple<_Elements...>& __t)
463     { return __get_helper<__i>(__t); }
464
465   // This class helps construct the various comparison operations on tuples
466   template<std::size_t __check_equal_size, std::size_t __i, std::size_t __j,
467            typename _Tp, typename _Up>
468     struct __tuple_compare;
469
470   template<std::size_t __i, std::size_t __j, typename _Tp, typename _Up>
471     struct __tuple_compare<0, __i, __j, _Tp, _Up>
472     {
473       static bool __eq(const _Tp& __t, const _Up& __u)
474       {
475         return (get<__i>(__t) == get<__i>(__u) &&
476                 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__eq(__t, __u));
477       }
478      
479       static bool __less(const _Tp& __t, const _Up& __u)
480       {
481         return ((get<__i>(__t) < get<__i>(__u))
482                 || !(get<__i>(__u) < get<__i>(__t)) &&
483                 __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__less(__t, __u));
484       }
485     };
486
487   template<std::size_t __i, typename _Tp, typename _Up>
488     struct __tuple_compare<0, __i, __i, _Tp, _Up>
489     {
490       static bool __eq(const _Tp&, const _Up&)
491       { return true; }
492      
493       static bool __less(const _Tp&, const _Up&)
494       { return false; }
495     };
496
497   template<typename... _TElements, typename... _UElements>
498     bool
499     operator==(const tuple<_TElements...>& __t,
500                const tuple<_UElements...>& __u)
501     {
502       typedef tuple<_TElements...> _Tp;
503       typedef tuple<_UElements...> _Up;
504       return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
505               0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
506     }
507
508   template<typename... _TElements, typename... _UElements>
509     bool
510     operator<(const tuple<_TElements...>& __t,
511               const tuple<_UElements...>& __u)
512     {
513       typedef tuple<_TElements...> _Tp;
514       typedef tuple<_UElements...> _Up;
515       return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
516               0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
517     }
518
519   template<typename... _TElements, typename... _UElements>
520     inline bool
521     operator!=(const tuple<_TElements...>& __t,
522                const tuple<_UElements...>& __u)
523     { return !(__t == __u); }
524
525   template<typename... _TElements, typename... _UElements>
526     inline bool
527     operator>(const tuple<_TElements...>& __t,
528               const tuple<_UElements...>& __u)
529     { return __u < __t; }
530
531   template<typename... _TElements, typename... _UElements>
532     inline bool
533     operator<=(const tuple<_TElements...>& __t,
534                const tuple<_UElements...>& __u)
535     { return !(__u < __t); }
536
537   template<typename... _TElements, typename... _UElements>
538     inline bool
539     operator>=(const tuple<_TElements...>& __t,
540                const tuple<_UElements...>& __u)
541     { return !(__t < __u); }
542
543   // NB: DR 705.
544   template<typename... _Elements>
545     inline tuple<typename __decay_and_strip<_Elements>::__type...>
546     make_tuple(_Elements&&... __args)
547     {
548       typedef tuple<typename __decay_and_strip<_Elements>::__type...>
549         __result_type;
550       return __result_type(std::forward<_Elements>(__args)...);
551     }
552
553   template<std::size_t...> struct __index_holder { };    
554
555   template<std::size_t __i, typename _IdxHolder, typename... _Elements>
556     struct __index_holder_impl;
557
558   template<std::size_t __i, std::size_t... _Indexes, typename _IdxHolder,
559            typename... _Elements>
560     struct __index_holder_impl<__i, __index_holder<_Indexes...>,
561                                _IdxHolder, _Elements...> 
562     {
563       typedef typename __index_holder_impl<__i + 1,
564                                            __index_holder<_Indexes..., __i>,
565                                            _Elements...>::type type;
566     };
567  
568   template<std::size_t __i, std::size_t... _Indexes>
569     struct __index_holder_impl<__i, __index_holder<_Indexes...> >
570     { typedef __index_holder<_Indexes...> type; };
571
572   template<typename... _Elements>
573     struct __make_index_holder 
574     : __index_holder_impl<0, __index_holder<>, _Elements...> { };
575     
576   template<typename... _TElements, std::size_t... _TIdx,
577            typename... _UElements, std::size_t... _UIdx> 
578     inline tuple<_TElements..., _UElements...> 
579     __tuple_cat_helper(const tuple<_TElements...>& __t,
580                        const __index_holder<_TIdx...>&,
581                        const tuple<_UElements...>& __u,
582                        const __index_holder<_UIdx...>&)
583     { return tuple<_TElements..., _UElements...>(get<_TIdx>(__t)...,
584                                                  get<_UIdx>(__u)...); }
585
586   template<typename... _TElements, std::size_t... _TIdx,
587            typename... _UElements, std::size_t... _UIdx> 
588     inline tuple<_TElements..., _UElements...> 
589     __tuple_cat_helper(tuple<_TElements...>&& __t,
590                        const __index_holder<_TIdx...>&, 
591                        const tuple<_UElements...>& __u,
592                        const __index_holder<_UIdx...>&)
593     { return tuple<_TElements..., _UElements...>
594         (std::move(get<_TIdx>(__t))..., get<_UIdx>(__u)...); }
595
596   template<typename... _TElements, std::size_t... _TIdx,
597            typename... _UElements, std::size_t... _UIdx>
598     inline tuple<_TElements..., _UElements...> 
599     __tuple_cat_helper(const tuple<_TElements...>& __t,
600                        const __index_holder<_TIdx...>&, 
601                        tuple<_UElements...>&& __u,
602                        const __index_holder<_UIdx...>&)
603     { return tuple<_TElements..., _UElements...>
604         (get<_TIdx>(__t)..., std::move(get<_UIdx>(__u))...); }
605
606   template<typename... _TElements, std::size_t... _TIdx,
607            typename... _UElements, std::size_t... _UIdx> 
608     inline tuple<_TElements..., _UElements...> 
609     __tuple_cat_helper(tuple<_TElements...>&& __t,
610                        const __index_holder<_TIdx...>&, 
611                        tuple<_UElements...>&& __u,
612                        const __index_holder<_UIdx...>&)
613     { return tuple<_TElements..., _UElements...>
614         (std::move(get<_TIdx>(__t))..., std::move(get<_UIdx>(__u))...); }
615
616   template<typename... _TElements, typename... _UElements>
617     inline tuple<_TElements..., _UElements...> 
618     tuple_cat(const tuple<_TElements...>& __t, const tuple<_UElements...>& __u)
619     {
620       return __tuple_cat_helper(__t, typename
621                                 __make_index_holder<_TElements...>::type(),
622                                 __u, typename
623                                 __make_index_holder<_UElements...>::type());
624     }
625
626   template<typename... _TElements, typename... _UElements>
627     inline tuple<_TElements..., _UElements...> 
628     tuple_cat(tuple<_TElements...>&& __t, const tuple<_UElements...>& __u)
629     {
630       return __tuple_cat_helper(std::move(__t), typename
631                                  __make_index_holder<_TElements...>::type(),
632                                  __u, typename
633                                  __make_index_holder<_UElements...>::type());
634     }
635
636   template<typename... _TElements, typename... _UElements>
637     inline tuple<_TElements..., _UElements...> 
638     tuple_cat(const tuple<_TElements...>& __t, tuple<_UElements...>&& __u)
639     {
640       return __tuple_cat_helper(__t, typename
641                                 __make_index_holder<_TElements...>::type(),
642                                 std::move(__u), typename
643                                 __make_index_holder<_UElements...>::type());
644     }
645
646   template<typename... _TElements, typename... _UElements>
647     inline tuple<_TElements..., _UElements...>
648     tuple_cat(tuple<_TElements...>&& __t, tuple<_UElements...>&& __u)
649     {
650       return __tuple_cat_helper(std::move(__t), typename
651                                 __make_index_holder<_TElements...>::type(),
652                                 std::move(__u), typename
653                                 __make_index_holder<_UElements...>::type());
654     }
655
656   template<typename... _Elements>
657     inline tuple<_Elements&...>
658     tie(_Elements&... __args)
659     { return tuple<_Elements&...>(__args...); }
660
661   template<typename... _Elements>
662     inline void 
663     swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
664     { __x.swap(__y); }
665
666   // A class (and instance) which can be used in 'tie' when an element
667   // of a tuple is not required
668   struct _Swallow_assign
669   {
670     template<class _Tp>
671       _Swallow_assign&
672       operator=(const _Tp&)
673       { return *this; }
674   };
675
676   // TODO: Put this in some kind of shared file.
677   namespace
678   {
679     _Swallow_assign ignore;
680   }; // anonymous namespace
681 }
682
683 #endif // __GXX_EXPERIMENTAL_CXX0X__
684
685 #endif // _GLIBCXX_TUPLE