OSDN Git Service

2008-03-25 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / tr1 / tuple
1 // class template tuple -*- C++ -*-
2
3 // Copyright (C) 2004, 2005, 2006, 2007, 2008 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 along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // 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 tr1/tuple
31 *  This is a TR1 C++ Library header.
32 */
33
34 // Chris Jefferson <chris@bubblescope.net>
35 // Variadic Templates support by Douglas Gregor <doug.gregor@gmail.com>
36
37 #ifndef _GLIBCXX_TR1_TUPLE
38 #define _GLIBCXX_TR1_TUPLE 1
39
40 #pragma GCC system_header
41
42 #include <utility>
43
44 namespace std
45 {
46 namespace tr1
47 {
48   // Adds a const reference to a non-reference type.
49   template<typename _Tp>
50     struct __add_c_ref
51     { typedef const _Tp& type; };
52
53   template<typename _Tp>
54     struct __add_c_ref<_Tp&>
55     { typedef _Tp& type; };
56
57   // Adds a reference to a non-reference type.
58   template<typename _Tp>
59     struct __add_ref
60     { typedef _Tp& type; };
61
62   template<typename _Tp>
63     struct __add_ref<_Tp&>
64     { typedef _Tp& type; };
65
66   /**
67    * Contains the actual implementation of the @c tuple template, stored
68    * as a recursive inheritance hierarchy from the first element (most
69    * derived class) to the last (least derived class). The @c Idx
70    * parameter gives the 0-based index of the element stored at this
71    * point in the hierarchy; we use it to implement a constant-time
72    * get() operation.
73    */
74   template<int _Idx, typename... _Elements>
75     struct _Tuple_impl; 
76
77   /**
78    * Zero-element tuple implementation. This is the basis case for the 
79    * inheritance recursion.
80    */
81   template<int _Idx>
82     struct _Tuple_impl<_Idx> { };
83
84   /**
85    * Recursive tuple implementation. Here we store the @c Head element
86    * and derive from a @c Tuple_impl containing the remaining elements
87    * (which contains the @c Tail).
88    */
89   template<int _Idx, typename _Head, typename... _Tail>
90     struct _Tuple_impl<_Idx, _Head, _Tail...>
91     : public _Tuple_impl<_Idx + 1, _Tail...>
92     {
93       typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
94       
95       _Head _M_head;
96       
97       _Inherited&       _M_tail()       { return *this; }
98       const _Inherited& _M_tail() const { return *this; }
99       
100       _Tuple_impl() : _Inherited(), _M_head() { }
101       
102       explicit 
103       _Tuple_impl(typename __add_c_ref<_Head>::type __head,
104                   typename __add_c_ref<_Tail>::type... __tail)
105       : _Inherited(__tail...), _M_head(__head) { }
106
107       template<typename... _UElements>
108       _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
109       : _Inherited(__in._M_tail()), _M_head(__in._M_head) { }
110
111       _Tuple_impl(const _Tuple_impl& __in)
112       : _Inherited(__in._M_tail()), _M_head(__in._M_head) { }
113      
114       template<typename... _UElements>
115         _Tuple_impl&
116         operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
117         {
118           _M_head = __in._M_head;
119           _M_tail() = __in._M_tail();
120           return *this;
121         }
122
123       _Tuple_impl&
124       operator=(const _Tuple_impl& __in)
125       {
126         _M_head = __in._M_head;
127         _M_tail() = __in._M_tail();
128         return *this;
129       }
130     };
131
132   template<typename... _Elements> 
133     class tuple : public _Tuple_impl<0, _Elements...>
134     {
135       typedef _Tuple_impl<0, _Elements...> _Inherited;
136
137     public:
138       tuple() : _Inherited() { }
139
140       explicit
141       tuple(typename __add_c_ref<_Elements>::type... __elements)
142       : _Inherited(__elements...) { }
143
144       template<typename... _UElements>
145         tuple(const tuple<_UElements...>& __in)
146         : _Inherited(__in) { }
147
148       tuple(const tuple& __in)
149       : _Inherited(__in) { }
150
151       template<typename... _UElements>
152         tuple&
153         operator=(const tuple<_UElements...>& __in)
154         {
155           static_cast<_Inherited&>(*this) = __in;
156           return *this;
157         }
158
159       tuple&
160       operator=(const tuple& __in)
161       {
162         static_cast<_Inherited&>(*this) = __in;
163         return *this;
164       }
165     };
166
167   template<> class tuple<> { };
168
169   // 2-element tuple, with construction and assignment from a pair.
170   template<typename _T1, typename _T2>
171     class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
172     {
173       typedef _Tuple_impl<0, _T1, _T2> _Inherited;
174
175     public:
176       tuple() : _Inherited() { }
177
178       explicit
179       tuple(typename __add_c_ref<_T1>::type __a1,
180             typename __add_c_ref<_T2>::type __a2)
181       : _Inherited(__a1, __a2) { }
182
183       template<typename _U1, typename _U2>
184         tuple(const tuple<_U1, _U2>& __in)
185         : _Inherited(__in) { }
186
187       tuple(const tuple& __in)
188       : _Inherited(__in) { }
189
190       template<typename _U1, typename _U2>
191         tuple(const pair<_U1, _U2>& __in)
192         : _Inherited(_Tuple_impl<0, 
193                      typename __add_c_ref<_U1>::type,
194                      typename __add_c_ref<_U2>::type>(__in.first, 
195                                                       __in.second))
196         { }
197   
198       template<typename _U1, typename _U2>
199         tuple&
200         operator=(const tuple<_U1, _U2>& __in)
201         {
202           static_cast<_Inherited&>(*this) = __in;
203           return *this;
204         }
205
206       tuple&
207       operator=(const tuple& __in)
208       {
209         static_cast<_Inherited&>(*this) = __in;
210         return *this;
211       }
212
213       template<typename _U1, typename _U2>
214         tuple&
215         operator=(const pair<_U1, _U2>& __in)
216         {
217           this->_M_head = __in.first;
218           this->_M_tail()._M_head = __in.second;
219           return *this;
220         }
221     };
222
223   
224   /// Gives the type of the ith element of a given tuple type.
225   template<int __i, typename _Tp>
226     struct tuple_element;
227
228   /**
229    * Recursive case for tuple_element: strip off the first element in
230    * the tuple and retrieve the (i-1)th element of the remaining tuple.
231    */
232   template<int __i, typename _Head, typename... _Tail>
233     struct tuple_element<__i, tuple<_Head, _Tail...> >
234     : tuple_element<__i - 1, tuple<_Tail...> > { };
235
236   /**
237    * Basis case for tuple_element: The first element is the one we're seeking.
238    */
239   template<typename _Head, typename... _Tail>
240     struct tuple_element<0, tuple<_Head, _Tail...> >
241     {
242       typedef _Head type;
243     };
244
245   /// Finds the size of a given tuple type.
246   template<typename _Tp>
247     struct tuple_size;
248
249   /// class tuple_size
250   template<typename... _Elements>
251     struct tuple_size<tuple<_Elements...> >
252     {
253       static const int value = sizeof...(_Elements);
254     };
255
256   template<typename... _Elements>
257     const int tuple_size<tuple<_Elements...> >::value;
258
259   template<int __i, typename _Head, typename... _Tail>
260     inline typename __add_ref<_Head>::type
261     __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t)
262     {
263       return __t._M_head;
264     }
265
266   template<int __i, typename _Head, typename... _Tail>
267     inline typename __add_c_ref<_Head>::type
268     __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t)
269     {
270       return __t._M_head;
271     }
272
273   // Return a reference (const reference) to the ith element of a tuple.
274   // Any const or non-const ref elements are returned with their original type.
275   template<int __i, typename... _Elements>
276     inline typename __add_ref<
277                       typename tuple_element<__i, tuple<_Elements...> >::type
278                     >::type
279     get(tuple<_Elements...>& __t)
280     { 
281       return __get_helper<__i>(__t); 
282     }
283
284   template<int __i, typename... _Elements>
285     inline typename __add_c_ref<
286                       typename tuple_element<__i, tuple<_Elements...> >::type
287                     >::type
288     get(const tuple<_Elements...>& __t)
289     {
290       return __get_helper<__i>(__t);
291     }
292
293   // This class helps construct the various comparison operations on tuples
294   template<int __check_equal_size, int __i, int __j,
295            typename _Tp, typename _Up>
296     struct __tuple_compare;
297
298   template<int __i, int __j, typename _Tp, typename _Up>
299     struct __tuple_compare<0, __i, __j, _Tp, _Up>
300     {
301       static bool __eq(const _Tp& __t, const _Up& __u)
302       {
303         return (get<__i>(__t) == get<__i>(__u) &&
304                 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__eq(__t, __u));
305       }
306      
307       static bool __less(const _Tp& __t, const _Up& __u)
308       {
309         return ((get<__i>(__t) < get<__i>(__u))
310                 || !(get<__i>(__u) < get<__i>(__t)) &&
311                 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__less(__t, __u));
312       }
313     };
314
315   template<int __i, typename _Tp, typename _Up>
316     struct __tuple_compare<0, __i, __i, _Tp, _Up>
317     {
318       static bool __eq(const _Tp&, const _Up&)
319       { return true; }
320      
321       static bool __less(const _Tp&, const _Up&)
322       { return false; }
323     };
324
325   template<typename... _TElements, typename... _UElements>
326     bool
327     operator==(const tuple<_TElements...>& __t,
328                const tuple<_UElements...>& __u)
329     {
330       typedef tuple<_TElements...> _Tp;
331       typedef tuple<_UElements...> _Up;
332       return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
333               0, tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u));
334     }
335
336   template<typename... _TElements, typename... _UElements>
337     bool
338     operator<(const tuple<_TElements...>& __t,
339               const tuple<_UElements...>& __u)
340     {
341       typedef tuple<_TElements...> _Tp;
342       typedef tuple<_UElements...> _Up;
343       return (__tuple_compare<tuple_size<_Tp>::value - tuple_size<_Up>::value,
344               0, tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u));
345     }
346
347   template<typename... _TElements, typename... _UElements>
348     inline bool
349     operator!=(const tuple<_TElements...>& __t,
350                const tuple<_UElements...>& __u)
351     { return !(__t == __u); }
352
353   template<typename... _TElements, typename... _UElements>
354     inline bool
355     operator>(const tuple<_TElements...>& __t,
356               const tuple<_UElements...>& __u)
357     { return __u < __t; }
358
359   template<typename... _TElements, typename... _UElements>
360     inline bool
361     operator<=(const tuple<_TElements...>& __t,
362                const tuple<_UElements...>& __u)
363     { return !(__u < __t); }
364
365   template<typename... _TElements, typename... _UElements>
366     inline bool
367     operator>=(const tuple<_TElements...>& __t,
368                const tuple<_UElements...>& __u)
369     { return !(__t < __u); }
370
371   template<typename _Tp>
372     class reference_wrapper;
373
374   // Helper which adds a reference to a type when given a reference_wrapper
375   template<typename _Tp>
376     struct __strip_reference_wrapper
377     {
378       typedef _Tp __type;
379     };
380
381   template<typename _Tp>
382     struct __strip_reference_wrapper<reference_wrapper<_Tp> >
383     {
384       typedef _Tp& __type;
385     };
386
387   template<typename _Tp>
388     struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
389     {
390       typedef _Tp& __type;
391     };
392
393   template<typename... _Elements>
394     inline tuple<typename __strip_reference_wrapper<_Elements>::__type...>
395     make_tuple(_Elements... __args)
396     {
397       typedef tuple<typename __strip_reference_wrapper<_Elements>::__type...>
398         __result_type;
399       return __result_type(__args...);
400     }
401
402   template<typename... _Elements>
403     inline tuple<_Elements&...>
404     tie(_Elements&... __args)
405     {
406       return tuple<_Elements&...>(__args...);
407     }
408
409   // A class (and instance) which can be used in 'tie' when an element
410   // of a tuple is not required
411   struct _Swallow_assign
412   {
413     template<class _Tp>
414       _Swallow_assign&
415       operator=(const _Tp&)
416       { return *this; }
417   };
418
419   // TODO: Put this in some kind of shared file.
420   namespace
421   {
422     _Swallow_assign ignore;
423   }; // anonymous namespace
424 }
425 }
426
427 #endif // _GLIBCXX_TR1_TUPLE