1 // class template tuple -*- C++ -*-
3 // Copyright (C) 2004, 2005 Free Software Foundation, Inc.
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)
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.
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,
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.
31 * This is a TR1 C++ Library header.
34 // Chris Jefferson <chris@bubblescope.net>
39 #include <tr1/utility>
40 #include <tr1/ref_fwd.h>
46 // An implementation specific class which is used in the tuple class
47 // when the tuple is not maximum possible size.
48 struct _NullClass { };
50 template<typename _Tp0 = _NullClass, typename _Tp1 = _NullClass,
51 typename _Tp2 = _NullClass, typename _Tp3 = _NullClass,
52 typename _Tp4 = _NullClass, typename _Tp5 = _NullClass,
53 typename _Tp6 = _NullClass, typename _Tp7 = _NullClass,
54 typename _Tp8 = _NullClass, typename _Tp9 = _NullClass>
57 /// Gives the type of the ith element of a given tuple type.
58 template<int __i, typename _Tp>
61 /// Finds the size of a given tuple type.
62 template<typename _Tp>
65 // Adds a const reference to a non-reference type.
66 template<typename _Tp>
68 { typedef const _Tp& type; };
70 template<typename _Tp>
71 struct __add_c_ref<_Tp&>
72 { typedef _Tp& type; };
74 // Adds a reference to a non-reference type.
75 template<typename _Tp>
77 { typedef _Tp& type; };
79 template<typename _Tp>
80 struct __add_ref<_Tp&>
81 { typedef _Tp& type; };
83 // Class used in the implementation of get
84 template<int __i, typename _Tp>
87 // Returns a const reference to the ith element of a tuple.
88 // Any const or non-const ref elements are returned with their original type.
89 template<int __i, typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
90 typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
91 typename _Tp8, typename _Tp9>
92 typename __add_ref<typename tuple_element<__i, tuple<_Tp0, _Tp1, _Tp2,
96 get(tuple<_Tp0, _Tp1, _Tp2, _Tp3, _Tp4, _Tp5, _Tp6, _Tp7, _Tp8,
99 return __get_helper<__i, tuple<_Tp0, _Tp1, _Tp2, _Tp3, _Tp4, _Tp5, _Tp6,
100 _Tp7, _Tp8, _Tp9> >::get_value(__t);
103 template<int __i, typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
104 typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
105 typename _Tp8, typename _Tp9>
106 typename __add_c_ref<typename tuple_element<__i, tuple<_Tp0, _Tp1, _Tp2,
110 get(const tuple<_Tp0, _Tp1, _Tp2, _Tp3, _Tp4, _Tp5, _Tp6, _Tp7, _Tp8,
113 return __get_helper<__i, tuple<_Tp0, _Tp1, _Tp2, _Tp3, _Tp4, _Tp5, _Tp6,
114 _Tp7, _Tp8, _Tp9> >::get_value(__t);
117 // This class helps construct the various comparison operations on tuples
118 template<int __check_equal_size, int __i, int __j, typename _Tp, typename _Up>
119 struct __tuple_compare;
121 template<int __i, int __j, typename _Tp, typename _Up>
122 struct __tuple_compare<0, __i, __j, _Tp, _Up>
124 static bool __eq(const _Tp& __t, const _Up& __u)
126 return get<__i>(__t) == get<__i>(__u) &&
127 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__eq(__t, __u);
129 static bool __less(const _Tp& __t, const _Up& __u)
131 return (get<__i>(__t) < get<__i>(__u)) || !(get<__i>(__u) < get<__i>(__t)) &&
132 __tuple_compare<0, __i+1, __j, _Tp, _Up>::__less(__t, __u);
136 template<int __i, typename _Tp, typename _Up>
137 struct __tuple_compare<0, __i, __i, _Tp, _Up>
139 static bool __eq(const _Tp&, const _Up&)
141 static bool __less(const _Tp&, const _Up&)
145 template<typename _T1, typename _T2, typename _T3, typename _T4, typename _T5,
146 typename _T6, typename _T7, typename _T8, typename _T9, typename _T10,
147 typename _U1, typename _U2, typename _U3, typename _U4, typename _U5,
148 typename _U6, typename _U7, typename _U8, typename _U9, typename _U10>
150 operator==(const tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10>& __t,
151 const tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8, _U9, _U10>& __u)
153 typedef tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10> _Tp;
154 typedef tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8,_U9, _U10> _Up;
155 return __tuple_compare<tuple_size<_Tp>::value - tuple_size<_Tp>::value, 0,
156 tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u);
159 template<typename _T1, typename _T2, typename _T3, typename _T4, typename _T5,
160 typename _T6, typename _T7, typename _T8, typename _T9, typename _T10,
161 typename _U1, typename _U2, typename _U3, typename _U4, typename _U5,
162 typename _U6, typename _U7, typename _U8, typename _U9, typename _U10>
164 operator<(const tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10>& __t,
165 const tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8, _U9, _U10>& __u)
167 typedef tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10> _Tp;
168 typedef tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8,_U9, _U10> _Up;
169 return __tuple_compare<tuple_size<_Tp>::value - tuple_size<_Tp>::value, 0,
170 tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u);
173 template<typename _T1, typename _T2, typename _T3, typename _T4, typename _T5,
174 typename _T6, typename _T7, typename _T8, typename _T9, typename _T10,
175 typename _U1, typename _U2, typename _U3, typename _U4, typename _U5,
176 typename _U6, typename _U7, typename _U8, typename _U9, typename _U10>
178 operator!=(const tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10>& __t,
179 const tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8, _U9, _U10>& __u)
180 { return !(__t == __u); }
182 template<typename _T1, typename _T2, typename _T3, typename _T4, typename _T5,
183 typename _T6, typename _T7, typename _T8, typename _T9, typename _T10,
184 typename _U1, typename _U2, typename _U3, typename _U4, typename _U5,
185 typename _U6, typename _U7, typename _U8, typename _U9, typename _U10>
187 operator>(const tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10>& __t,
188 const tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8, _U9, _U10>& __u)
189 { return __u < __t; }
191 template<typename _T1, typename _T2, typename _T3, typename _T4, typename _T5,
192 typename _T6, typename _T7, typename _T8, typename _T9, typename _T10,
193 typename _U1, typename _U2, typename _U3, typename _U4, typename _U5,
194 typename _U6, typename _U7, typename _U8, typename _U9, typename _U10>
196 operator<=(const tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10>& __t,
197 const tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8, _U9, _U10>& __u)
198 { return !(__u < __t); }
200 template<typename _T1, typename _T2, typename _T3, typename _T4, typename _T5,
201 typename _T6, typename _T7, typename _T8, typename _T9, typename _T10,
202 typename _U1, typename _U2, typename _U3, typename _U4, typename _U5,
203 typename _U6, typename _U7, typename _U8, typename _U9, typename _U10>
205 operator>=(const tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10>& __t,
206 const tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8, _U9, _U10>& __u)
207 { return !(__t < __u); }
209 // Helper which adds a reference to a type when given a reference_wrapper
210 template<typename _Tp>
211 struct __strip_reference_wrapper
216 template<typename _Tp>
217 struct __strip_reference_wrapper<reference_wrapper<_Tp> >
222 template<typename _Tp>
223 struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
228 template<typename _Tp0 = _NullClass, typename _Tp1 = _NullClass,
229 typename _Tp2 = _NullClass, typename _Tp3 = _NullClass,
230 typename _Tp4 = _NullClass, typename _Tp5 = _NullClass,
231 typename _Tp6 = _NullClass, typename _Tp7 = _NullClass,
232 typename _Tp8 = _NullClass, typename _Tp9 = _NullClass>
233 struct __stripped_tuple_type
235 typedef tuple<typename __strip_reference_wrapper<_Tp0>::__type,
236 typename __strip_reference_wrapper<_Tp1>::__type,
237 typename __strip_reference_wrapper<_Tp2>::__type,
238 typename __strip_reference_wrapper<_Tp3>::__type,
239 typename __strip_reference_wrapper<_Tp4>::__type,
240 typename __strip_reference_wrapper<_Tp5>::__type,
241 typename __strip_reference_wrapper<_Tp6>::__type,
242 typename __strip_reference_wrapper<_Tp7>::__type,
243 typename __strip_reference_wrapper<_Tp8>::__type,
244 typename __strip_reference_wrapper<_Tp9>::__type> __type;
247 // A class (and instance) which can be used in 'tie' when an element
248 // of a tuple is not required
249 struct swallow_assign
257 // TODO: Put this in some kind of shared file.
260 swallow_assign ignore;
263 #define _GLIBCXX_CAT(x,y) _GLIBCXX_CAT2(x,y)
264 #define _GLIBCXX_CAT2(x,y) x##y
265 #define _SHORT_REPEAT
266 #define _GLIBCXX_REPEAT_HEADER <tr1/tuple_iterate.h>
267 #include <tr1/repeat.h>
268 #undef _GLIBCXX_REPEAT_HEADER
273 #include <tr1/functional>