1 | // tuple_basic.hpp ----------------------------------------------------- |
2 | |
3 | // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi) |
4 | // |
5 | // Distributed under the Boost Software License, Version 1.0. (See |
6 | // accompanying file LICENSE_1_0.txt or copy at |
7 | // http://www.boost.org/LICENSE_1_0.txt) |
8 | |
9 | // For more information, see http://www.boost.org |
10 | |
11 | // Outside help: |
12 | // This and that, Gary Powell. |
13 | // Fixed return types for get_head/get_tail |
14 | // ( and other bugs ) per suggestion of Jens Maurer |
15 | // simplified element type accessors + bug fix (Jeremy Siek) |
16 | // Several changes/additions according to suggestions by Douglas Gregor, |
17 | // William Kempf, Vesa Karvonen, John Max Skaller, Ed Brey, Beman Dawes, |
18 | // David Abrahams. |
19 | |
20 | // Revision history: |
21 | // 2002 05 01 Hugo Duncan: Fix for Borland after Jaakko's previous changes |
22 | // 2002 04 18 Jaakko: tuple element types can be void or plain function |
23 | // types, as long as no object is created. |
24 | // Tuple objects can no hold even noncopyable types |
25 | // such as arrays. |
26 | // 2001 10 22 John Maddock |
27 | // Fixes for Borland C++ |
28 | // 2001 08 30 David Abrahams |
29 | // Added default constructor for cons<>. |
30 | // ----------------------------------------------------------------- |
31 | |
32 | #ifndef BOOST_TUPLE_BASIC_HPP |
33 | #define BOOST_TUPLE_BASIC_HPP |
34 | |
35 | |
36 | #include <utility> // needed for the assignment from pair to tuple |
37 | #include <cstddef> // for std::size_t |
38 | |
39 | #include <boost/core/invoke_swap.hpp> |
40 | #include <boost/type_traits/cv_traits.hpp> |
41 | #include <boost/type_traits/function_traits.hpp> |
42 | #include <boost/type_traits/integral_constant.hpp> |
43 | |
44 | #include <boost/detail/workaround.hpp> // needed for BOOST_WORKAROUND |
45 | |
46 | #if defined(BOOST_GCC) && (BOOST_GCC >= 40700) |
47 | #pragma GCC diagnostic push |
48 | #pragma GCC diagnostic ignored "-Wunused-local-typedefs" |
49 | #endif |
50 | |
51 | namespace boost { |
52 | namespace tuples { |
53 | |
54 | // -- null_type -------------------------------------------------------- |
55 | struct null_type {}; |
56 | |
57 | // a helper function to provide a const null_type type temporary |
58 | namespace detail { |
59 | inline const null_type cnull() { return null_type(); } |
60 | |
61 | |
62 | // -- if construct ------------------------------------------------ |
63 | // Proposed by Krzysztof Czarnecki and Ulrich Eisenecker |
64 | |
65 | template <bool If, class Then, class Else> struct IF { typedef Then RET; }; |
66 | |
67 | template <class Then, class Else> struct IF<false, Then, Else> { |
68 | typedef Else RET; |
69 | }; |
70 | |
71 | } // end detail |
72 | |
73 | // - cons forward declaration ----------------------------------------------- |
74 | template <class HT, class TT> struct cons; |
75 | |
76 | |
77 | // - tuple forward declaration ----------------------------------------------- |
78 | template < |
79 | class T0 = null_type, class T1 = null_type, class T2 = null_type, |
80 | class T3 = null_type, class T4 = null_type, class T5 = null_type, |
81 | class T6 = null_type, class T7 = null_type, class T8 = null_type, |
82 | class T9 = null_type> |
83 | class tuple; |
84 | |
85 | // tuple_length forward declaration |
86 | template<class T> struct length; |
87 | |
88 | |
89 | |
90 | namespace detail { |
91 | |
92 | // -- generate error template, referencing to non-existing members of this |
93 | // template is used to produce compilation errors intentionally |
94 | template<class T> |
95 | class generate_error; |
96 | |
97 | template<std::size_t N> |
98 | struct drop_front { |
99 | template<class Tuple> |
100 | struct apply { |
101 | typedef BOOST_DEDUCED_TYPENAME drop_front<N-1>::BOOST_NESTED_TEMPLATE |
102 | apply<Tuple> next; |
103 | typedef BOOST_DEDUCED_TYPENAME next::type::tail_type type; |
104 | static const type& call(const Tuple& tup) { |
105 | return next::call(tup).tail; |
106 | } |
107 | }; |
108 | }; |
109 | |
110 | template<> |
111 | struct drop_front<0> { |
112 | template<class Tuple> |
113 | struct apply { |
114 | typedef Tuple type; |
115 | static const type& call(const Tuple& tup) { |
116 | return tup; |
117 | } |
118 | }; |
119 | }; |
120 | |
121 | } // end of namespace detail |
122 | |
123 | |
124 | // -cons type accessors ---------------------------------------- |
125 | // typename tuples::element<N,T>::type gets the type of the |
126 | // Nth element ot T, first element is at index 0 |
127 | // ------------------------------------------------------- |
128 | |
129 | #ifndef BOOST_NO_CV_SPECIALIZATIONS |
130 | |
131 | template<std::size_t N, class T> |
132 | struct element |
133 | { |
134 | typedef BOOST_DEDUCED_TYPENAME detail::drop_front<N>::BOOST_NESTED_TEMPLATE |
135 | apply<T>::type::head_type type; |
136 | }; |
137 | |
138 | template<std::size_t N, class T> |
139 | struct element<N, const T> |
140 | { |
141 | private: |
142 | typedef BOOST_DEDUCED_TYPENAME detail::drop_front<N>::BOOST_NESTED_TEMPLATE |
143 | apply<T>::type::head_type unqualified_type; |
144 | public: |
145 | #if BOOST_WORKAROUND(BOOST_BORLANDC,<0x600) |
146 | typedef const unqualified_type type; |
147 | #else |
148 | typedef BOOST_DEDUCED_TYPENAME boost::add_const<unqualified_type>::type type; |
149 | #endif |
150 | }; |
151 | #else // def BOOST_NO_CV_SPECIALIZATIONS |
152 | |
153 | namespace detail { |
154 | |
155 | template<std::size_t N, class T, bool IsConst> |
156 | struct element_impl |
157 | { |
158 | typedef BOOST_DEDUCED_TYPENAME detail::drop_front<N>::BOOST_NESTED_TEMPLATE |
159 | apply<T>::type::head_type type; |
160 | }; |
161 | |
162 | template<std::size_t N, class T> |
163 | struct element_impl<N, T, true /* IsConst */> |
164 | { |
165 | typedef BOOST_DEDUCED_TYPENAME detail::drop_front<N>::BOOST_NESTED_TEMPLATE |
166 | apply<T>::type::head_type unqualified_type; |
167 | typedef const unqualified_type type; |
168 | }; |
169 | |
170 | } // end of namespace detail |
171 | |
172 | |
173 | template<std::size_t N, class T> |
174 | struct element: |
175 | public detail::element_impl<N, T, ::boost::is_const<T>::value> |
176 | { |
177 | }; |
178 | |
179 | #endif |
180 | |
181 | |
182 | // -get function templates ----------------------------------------------- |
183 | // Usage: get<N>(aTuple) |
184 | |
185 | // -- some traits classes for get functions |
186 | |
187 | // access traits lifted from detail namespace to be part of the interface, |
188 | // (Joel de Guzman's suggestion). Rationale: get functions are part of the |
189 | // interface, so should the way to express their return types be. |
190 | |
191 | template <class T> struct access_traits { |
192 | typedef const T& const_type; |
193 | typedef T& non_const_type; |
194 | |
195 | typedef const typename boost::remove_cv<T>::type& parameter_type; |
196 | |
197 | // used as the tuple constructors parameter types |
198 | // Rationale: non-reference tuple element types can be cv-qualified. |
199 | // It should be possible to initialize such types with temporaries, |
200 | // and when binding temporaries to references, the reference must |
201 | // be non-volatile and const. 8.5.3. (5) |
202 | }; |
203 | |
204 | template <class T> struct access_traits<T&> { |
205 | |
206 | typedef T& const_type; |
207 | typedef T& non_const_type; |
208 | |
209 | typedef T& parameter_type; |
210 | }; |
211 | |
212 | // get function for non-const cons-lists, returns a reference to the element |
213 | |
214 | template<std::size_t N, class HT, class TT> |
215 | inline typename access_traits< |
216 | typename element<N, cons<HT, TT> >::type |
217 | >::non_const_type |
218 | get(cons<HT, TT>& c) { |
219 | typedef BOOST_DEDUCED_TYPENAME detail::drop_front<N>::BOOST_NESTED_TEMPLATE |
220 | apply<cons<HT, TT> > impl; |
221 | typedef BOOST_DEDUCED_TYPENAME impl::type cons_element; |
222 | return const_cast<cons_element&>(impl::call(c)).head; |
223 | } |
224 | |
225 | // get function for const cons-lists, returns a const reference to |
226 | // the element. If the element is a reference, returns the reference |
227 | // as such (that is, can return a non-const reference) |
228 | template<std::size_t N, class HT, class TT> |
229 | inline typename access_traits< |
230 | typename element<N, cons<HT, TT> >::type |
231 | >::const_type |
232 | get(const cons<HT, TT>& c) { |
233 | typedef BOOST_DEDUCED_TYPENAME detail::drop_front<N>::BOOST_NESTED_TEMPLATE |
234 | apply<cons<HT, TT> > impl; |
235 | return impl::call(c).head; |
236 | } |
237 | |
238 | // -- the cons template -------------------------------------------------- |
239 | namespace detail { |
240 | |
241 | // These helper templates wrap void types and plain function types. |
242 | // The reationale is to allow one to write tuple types with those types |
243 | // as elements, even though it is not possible to instantiate such object. |
244 | // E.g: typedef tuple<void> some_type; // ok |
245 | // but: some_type x; // fails |
246 | |
247 | template <class T> class non_storeable_type { |
248 | non_storeable_type(); |
249 | }; |
250 | |
251 | template <class T> struct wrap_non_storeable_type { |
252 | typedef typename IF< |
253 | ::boost::is_function<T>::value, non_storeable_type<T>, T |
254 | >::RET type; |
255 | }; |
256 | template <> struct wrap_non_storeable_type<void> { |
257 | typedef non_storeable_type<void> type; |
258 | }; |
259 | |
260 | } // detail |
261 | |
262 | template <class HT, class TT> |
263 | struct cons { |
264 | |
265 | typedef HT head_type; |
266 | typedef TT tail_type; |
267 | |
268 | typedef typename |
269 | detail::wrap_non_storeable_type<head_type>::type stored_head_type; |
270 | |
271 | stored_head_type head; |
272 | tail_type tail; |
273 | |
274 | typename access_traits<stored_head_type>::non_const_type |
275 | get_head() { return head; } |
276 | |
277 | typename access_traits<tail_type>::non_const_type |
278 | get_tail() { return tail; } |
279 | |
280 | typename access_traits<stored_head_type>::const_type |
281 | get_head() const { return head; } |
282 | |
283 | typename access_traits<tail_type>::const_type |
284 | get_tail() const { return tail; } |
285 | |
286 | cons() : head(), tail() {} |
287 | // cons() : head(detail::default_arg<HT>::f()), tail() {} |
288 | |
289 | // the argument for head is not strictly needed, but it prevents |
290 | // array type elements. This is good, since array type elements |
291 | // cannot be supported properly in any case (no assignment, |
292 | // copy works only if the tails are exactly the same type, ...) |
293 | |
294 | cons(typename access_traits<stored_head_type>::parameter_type h, |
295 | const tail_type& t) |
296 | : head (h), tail(t) {} |
297 | |
298 | template <class T1, class T2, class T3, class T4, class T5, |
299 | class T6, class T7, class T8, class T9, class T10> |
300 | cons( T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, |
301 | T6& t6, T7& t7, T8& t8, T9& t9, T10& t10 ) |
302 | : head (t1), |
303 | tail (t2, t3, t4, t5, t6, t7, t8, t9, t10, detail::cnull()) |
304 | {} |
305 | |
306 | template <class T2, class T3, class T4, class T5, |
307 | class T6, class T7, class T8, class T9, class T10> |
308 | cons( const null_type& /*t1*/, T2& t2, T3& t3, T4& t4, T5& t5, |
309 | T6& t6, T7& t7, T8& t8, T9& t9, T10& t10 ) |
310 | : head (), |
311 | tail (t2, t3, t4, t5, t6, t7, t8, t9, t10, detail::cnull()) |
312 | {} |
313 | |
314 | cons( const cons& u ) : head(u.head), tail(u.tail) {} |
315 | |
316 | template <class HT2, class TT2> |
317 | cons( const cons<HT2, TT2>& u ) : head(u.head), tail(u.tail) {} |
318 | |
319 | template <class HT2, class TT2> |
320 | cons& operator=( const cons<HT2, TT2>& u ) { |
321 | head=u.head; tail=u.tail; return *this; |
322 | } |
323 | |
324 | // must define assignment operator explicitly, implicit version is |
325 | // illformed if HT is a reference (12.8. (12)) |
326 | cons& operator=(const cons& u) { |
327 | head = u.head; tail = u.tail; return *this; |
328 | } |
329 | |
330 | template <class T1, class T2> |
331 | cons& operator=( const std::pair<T1, T2>& u ) { |
332 | BOOST_STATIC_ASSERT(length<cons>::value == 2); // check length = 2 |
333 | head = u.first; tail.head = u.second; return *this; |
334 | } |
335 | |
336 | // get member functions (non-const and const) |
337 | template <std::size_t N> |
338 | typename access_traits< |
339 | typename element<N, cons<HT, TT> >::type |
340 | >::non_const_type |
341 | get() { |
342 | return boost::tuples::get<N>(*this); // delegate to non-member get |
343 | } |
344 | |
345 | template <std::size_t N> |
346 | typename access_traits< |
347 | typename element<N, cons<HT, TT> >::type |
348 | >::const_type |
349 | get() const { |
350 | return boost::tuples::get<N>(*this); // delegate to non-member get |
351 | } |
352 | }; |
353 | |
354 | template <class HT> |
355 | struct cons<HT, null_type> { |
356 | |
357 | typedef HT head_type; |
358 | typedef null_type tail_type; |
359 | typedef cons<HT, null_type> self_type; |
360 | |
361 | typedef typename |
362 | detail::wrap_non_storeable_type<head_type>::type stored_head_type; |
363 | stored_head_type head; |
364 | |
365 | typename access_traits<stored_head_type>::non_const_type |
366 | get_head() { return head; } |
367 | |
368 | null_type get_tail() { return null_type(); } |
369 | |
370 | typename access_traits<stored_head_type>::const_type |
371 | get_head() const { return head; } |
372 | |
373 | const null_type get_tail() const { return null_type(); } |
374 | |
375 | // cons() : head(detail::default_arg<HT>::f()) {} |
376 | cons() : head() {} |
377 | |
378 | cons(typename access_traits<stored_head_type>::parameter_type h, |
379 | const null_type& = null_type()) |
380 | : head (h) {} |
381 | |
382 | template<class T1> |
383 | cons(T1& t1, const null_type&, const null_type&, const null_type&, |
384 | const null_type&, const null_type&, const null_type&, |
385 | const null_type&, const null_type&, const null_type&) |
386 | : head (t1) {} |
387 | |
388 | cons(const null_type&, |
389 | const null_type&, const null_type&, const null_type&, |
390 | const null_type&, const null_type&, const null_type&, |
391 | const null_type&, const null_type&, const null_type&) |
392 | : head () {} |
393 | |
394 | cons( const cons& u ) : head(u.head) {} |
395 | |
396 | template <class HT2> |
397 | cons( const cons<HT2, null_type>& u ) : head(u.head) {} |
398 | |
399 | template <class HT2> |
400 | cons& operator=(const cons<HT2, null_type>& u ) |
401 | { head = u.head; return *this; } |
402 | |
403 | // must define assignment operator explicitely, implicit version |
404 | // is illformed if HT is a reference |
405 | cons& operator=(const cons& u) { head = u.head; return *this; } |
406 | |
407 | template <std::size_t N> |
408 | typename access_traits< |
409 | typename element<N, self_type>::type |
410 | >::non_const_type |
411 | get() { |
412 | return boost::tuples::get<N>(*this); |
413 | } |
414 | |
415 | template <std::size_t N> |
416 | typename access_traits< |
417 | typename element<N, self_type>::type |
418 | >::const_type |
419 | get() const { |
420 | return boost::tuples::get<N>(*this); |
421 | } |
422 | |
423 | }; |
424 | |
425 | // templates for finding out the length of the tuple ------------------- |
426 | |
427 | template<class T> |
428 | struct length: boost::integral_constant<std::size_t, 1 + length<typename T::tail_type>::value> |
429 | { |
430 | }; |
431 | |
432 | template<> |
433 | struct length<tuple<> >: boost::integral_constant<std::size_t, 0> |
434 | { |
435 | }; |
436 | |
437 | template<> |
438 | struct length<tuple<> const>: boost::integral_constant<std::size_t, 0> |
439 | { |
440 | }; |
441 | |
442 | template<> |
443 | struct length<null_type>: boost::integral_constant<std::size_t, 0> |
444 | { |
445 | }; |
446 | |
447 | template<> |
448 | struct length<null_type const>: boost::integral_constant<std::size_t, 0> |
449 | { |
450 | }; |
451 | |
452 | namespace detail { |
453 | |
454 | // Tuple to cons mapper -------------------------------------------------- |
455 | template <class T0, class T1, class T2, class T3, class T4, |
456 | class T5, class T6, class T7, class T8, class T9> |
457 | struct map_tuple_to_cons |
458 | { |
459 | typedef cons<T0, |
460 | typename map_tuple_to_cons<T1, T2, T3, T4, T5, |
461 | T6, T7, T8, T9, null_type>::type |
462 | > type; |
463 | }; |
464 | |
465 | // The empty tuple is a null_type |
466 | template <> |
467 | struct map_tuple_to_cons<null_type, null_type, null_type, null_type, null_type, null_type, null_type, null_type, null_type, null_type> |
468 | { |
469 | typedef null_type type; |
470 | }; |
471 | |
472 | } // end detail |
473 | |
474 | // ------------------------------------------------------------------- |
475 | // -- tuple ------------------------------------------------------ |
476 | template <class T0, class T1, class T2, class T3, class T4, |
477 | class T5, class T6, class T7, class T8, class T9> |
478 | |
479 | class tuple : |
480 | public detail::map_tuple_to_cons<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::type |
481 | { |
482 | public: |
483 | typedef typename |
484 | detail::map_tuple_to_cons<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::type inherited; |
485 | typedef typename inherited::head_type head_type; |
486 | typedef typename inherited::tail_type tail_type; |
487 | |
488 | |
489 | // access_traits<T>::parameter_type takes non-reference types as const T& |
490 | tuple() {} |
491 | |
492 | explicit tuple(typename access_traits<T0>::parameter_type t0) |
493 | : inherited(t0, detail::cnull(), detail::cnull(), detail::cnull(), |
494 | detail::cnull(), detail::cnull(), detail::cnull(), |
495 | detail::cnull(), detail::cnull(), detail::cnull()) {} |
496 | |
497 | tuple(typename access_traits<T0>::parameter_type t0, |
498 | typename access_traits<T1>::parameter_type t1) |
499 | : inherited(t0, t1, detail::cnull(), detail::cnull(), |
500 | detail::cnull(), detail::cnull(), detail::cnull(), |
501 | detail::cnull(), detail::cnull(), detail::cnull()) {} |
502 | |
503 | tuple(typename access_traits<T0>::parameter_type t0, |
504 | typename access_traits<T1>::parameter_type t1, |
505 | typename access_traits<T2>::parameter_type t2) |
506 | : inherited(t0, t1, t2, detail::cnull(), detail::cnull(), |
507 | detail::cnull(), detail::cnull(), detail::cnull(), |
508 | detail::cnull(), detail::cnull()) {} |
509 | |
510 | tuple(typename access_traits<T0>::parameter_type t0, |
511 | typename access_traits<T1>::parameter_type t1, |
512 | typename access_traits<T2>::parameter_type t2, |
513 | typename access_traits<T3>::parameter_type t3) |
514 | : inherited(t0, t1, t2, t3, detail::cnull(), detail::cnull(), |
515 | detail::cnull(), detail::cnull(), detail::cnull(), |
516 | detail::cnull()) {} |
517 | |
518 | tuple(typename access_traits<T0>::parameter_type t0, |
519 | typename access_traits<T1>::parameter_type t1, |
520 | typename access_traits<T2>::parameter_type t2, |
521 | typename access_traits<T3>::parameter_type t3, |
522 | typename access_traits<T4>::parameter_type t4) |
523 | : inherited(t0, t1, t2, t3, t4, detail::cnull(), detail::cnull(), |
524 | detail::cnull(), detail::cnull(), detail::cnull()) {} |
525 | |
526 | tuple(typename access_traits<T0>::parameter_type t0, |
527 | typename access_traits<T1>::parameter_type t1, |
528 | typename access_traits<T2>::parameter_type t2, |
529 | typename access_traits<T3>::parameter_type t3, |
530 | typename access_traits<T4>::parameter_type t4, |
531 | typename access_traits<T5>::parameter_type t5) |
532 | : inherited(t0, t1, t2, t3, t4, t5, detail::cnull(), detail::cnull(), |
533 | detail::cnull(), detail::cnull()) {} |
534 | |
535 | tuple(typename access_traits<T0>::parameter_type t0, |
536 | typename access_traits<T1>::parameter_type t1, |
537 | typename access_traits<T2>::parameter_type t2, |
538 | typename access_traits<T3>::parameter_type t3, |
539 | typename access_traits<T4>::parameter_type t4, |
540 | typename access_traits<T5>::parameter_type t5, |
541 | typename access_traits<T6>::parameter_type t6) |
542 | : inherited(t0, t1, t2, t3, t4, t5, t6, detail::cnull(), |
543 | detail::cnull(), detail::cnull()) {} |
544 | |
545 | tuple(typename access_traits<T0>::parameter_type t0, |
546 | typename access_traits<T1>::parameter_type t1, |
547 | typename access_traits<T2>::parameter_type t2, |
548 | typename access_traits<T3>::parameter_type t3, |
549 | typename access_traits<T4>::parameter_type t4, |
550 | typename access_traits<T5>::parameter_type t5, |
551 | typename access_traits<T6>::parameter_type t6, |
552 | typename access_traits<T7>::parameter_type t7) |
553 | : inherited(t0, t1, t2, t3, t4, t5, t6, t7, detail::cnull(), |
554 | detail::cnull()) {} |
555 | |
556 | tuple(typename access_traits<T0>::parameter_type t0, |
557 | typename access_traits<T1>::parameter_type t1, |
558 | typename access_traits<T2>::parameter_type t2, |
559 | typename access_traits<T3>::parameter_type t3, |
560 | typename access_traits<T4>::parameter_type t4, |
561 | typename access_traits<T5>::parameter_type t5, |
562 | typename access_traits<T6>::parameter_type t6, |
563 | typename access_traits<T7>::parameter_type t7, |
564 | typename access_traits<T8>::parameter_type t8) |
565 | : inherited(t0, t1, t2, t3, t4, t5, t6, t7, t8, detail::cnull()) {} |
566 | |
567 | tuple(typename access_traits<T0>::parameter_type t0, |
568 | typename access_traits<T1>::parameter_type t1, |
569 | typename access_traits<T2>::parameter_type t2, |
570 | typename access_traits<T3>::parameter_type t3, |
571 | typename access_traits<T4>::parameter_type t4, |
572 | typename access_traits<T5>::parameter_type t5, |
573 | typename access_traits<T6>::parameter_type t6, |
574 | typename access_traits<T7>::parameter_type t7, |
575 | typename access_traits<T8>::parameter_type t8, |
576 | typename access_traits<T9>::parameter_type t9) |
577 | : inherited(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9) {} |
578 | |
579 | |
580 | template<class U1, class U2> |
581 | tuple(const cons<U1, U2>& p) : inherited(p) {} |
582 | |
583 | template <class U1, class U2> |
584 | tuple& operator=(const cons<U1, U2>& k) { |
585 | inherited::operator=(k); |
586 | return *this; |
587 | } |
588 | |
589 | template <class U1, class U2> |
590 | tuple& operator=(const std::pair<U1, U2>& k) { |
591 | BOOST_STATIC_ASSERT(length<tuple>::value == 2);// check_length = 2 |
592 | this->head = k.first; |
593 | this->tail.head = k.second; |
594 | return *this; |
595 | } |
596 | |
597 | }; |
598 | |
599 | // The empty tuple |
600 | template <> |
601 | class tuple<null_type, null_type, null_type, null_type, null_type, null_type, null_type, null_type, null_type, null_type> : |
602 | public null_type |
603 | { |
604 | public: |
605 | typedef null_type inherited; |
606 | }; |
607 | |
608 | |
609 | // Swallows any assignment (by Doug Gregor) |
610 | namespace detail { |
611 | |
612 | struct swallow_assign; |
613 | typedef void (detail::swallow_assign::*ignore_t)(); |
614 | struct swallow_assign { |
615 | swallow_assign(ignore_t(*)(ignore_t)) {} |
616 | template<typename T> |
617 | swallow_assign const& operator=(const T&) const { |
618 | return *this; |
619 | } |
620 | }; |
621 | |
622 | |
623 | } // namespace detail |
624 | |
625 | // "ignore" allows tuple positions to be ignored when using "tie". |
626 | inline detail::ignore_t ignore(detail::ignore_t) { return 0; } |
627 | |
628 | // --------------------------------------------------------------------------- |
629 | // The call_traits for make_tuple |
630 | // Honours the reference_wrapper class. |
631 | |
632 | // Must be instantiated with plain or const plain types (not with references) |
633 | |
634 | // from template<class T> foo(const T& t) : make_tuple_traits<const T>::type |
635 | // from template<class T> foo(T& t) : make_tuple_traits<T>::type |
636 | |
637 | // Conversions: |
638 | // T -> T, |
639 | // references -> compile_time_error |
640 | // reference_wrapper<T> -> T& |
641 | // const reference_wrapper<T> -> T& |
642 | // array -> const ref array |
643 | |
644 | |
645 | template<class T> |
646 | struct make_tuple_traits { |
647 | typedef T type; |
648 | |
649 | // commented away, see below (JJ) |
650 | // typedef typename IF< |
651 | // boost::is_function<T>::value, |
652 | // T&, |
653 | // T>::RET type; |
654 | |
655 | }; |
656 | |
657 | // The is_function test was there originally for plain function types, |
658 | // which can't be stored as such (we must either store them as references or |
659 | // pointers). Such a type could be formed if make_tuple was called with a |
660 | // reference to a function. |
661 | // But this would mean that a const qualified function type was formed in |
662 | // the make_tuple function and hence make_tuple can't take a function |
663 | // reference as a parameter, and thus T can't be a function type. |
664 | // So is_function test was removed. |
665 | // (14.8.3. says that type deduction fails if a cv-qualified function type |
666 | // is created. (It only applies for the case of explicitly specifying template |
667 | // args, though?)) (JJ) |
668 | |
669 | template<class T> |
670 | struct make_tuple_traits<T&> { |
671 | typedef typename |
672 | detail::generate_error<T&>:: |
673 | do_not_use_with_reference_type error; |
674 | }; |
675 | |
676 | // Arrays can't be stored as plain types; convert them to references. |
677 | // All arrays are converted to const. This is because make_tuple takes its |
678 | // parameters as const T& and thus the knowledge of the potential |
679 | // non-constness of actual argument is lost. |
680 | template<class T, std::size_t n> struct make_tuple_traits <T[n]> { |
681 | typedef const T (&type)[n]; |
682 | }; |
683 | |
684 | template<class T, std::size_t n> |
685 | struct make_tuple_traits<const T[n]> { |
686 | typedef const T (&type)[n]; |
687 | }; |
688 | |
689 | template<class T, std::size_t n> struct make_tuple_traits<volatile T[n]> { |
690 | typedef const volatile T (&type)[n]; |
691 | }; |
692 | |
693 | template<class T, std::size_t n> |
694 | struct make_tuple_traits<const volatile T[n]> { |
695 | typedef const volatile T (&type)[n]; |
696 | }; |
697 | |
698 | template<class T> |
699 | struct make_tuple_traits<reference_wrapper<T> >{ |
700 | typedef T& type; |
701 | }; |
702 | |
703 | template<class T> |
704 | struct make_tuple_traits<const reference_wrapper<T> >{ |
705 | typedef T& type; |
706 | }; |
707 | |
708 | template<> |
709 | struct make_tuple_traits<detail::ignore_t(detail::ignore_t)> { |
710 | typedef detail::swallow_assign type; |
711 | }; |
712 | |
713 | |
714 | |
715 | namespace detail { |
716 | |
717 | // a helper traits to make the make_tuple functions shorter (Vesa Karvonen's |
718 | // suggestion) |
719 | template < |
720 | class T0 = null_type, class T1 = null_type, class T2 = null_type, |
721 | class T3 = null_type, class T4 = null_type, class T5 = null_type, |
722 | class T6 = null_type, class T7 = null_type, class T8 = null_type, |
723 | class T9 = null_type |
724 | > |
725 | struct make_tuple_mapper { |
726 | typedef |
727 | tuple<typename make_tuple_traits<T0>::type, |
728 | typename make_tuple_traits<T1>::type, |
729 | typename make_tuple_traits<T2>::type, |
730 | typename make_tuple_traits<T3>::type, |
731 | typename make_tuple_traits<T4>::type, |
732 | typename make_tuple_traits<T5>::type, |
733 | typename make_tuple_traits<T6>::type, |
734 | typename make_tuple_traits<T7>::type, |
735 | typename make_tuple_traits<T8>::type, |
736 | typename make_tuple_traits<T9>::type> type; |
737 | }; |
738 | |
739 | } // end detail |
740 | |
741 | // -make_tuple function templates ----------------------------------- |
742 | inline tuple<> make_tuple() { |
743 | return tuple<>(); |
744 | } |
745 | |
746 | template<class T0> |
747 | inline typename detail::make_tuple_mapper<T0>::type |
748 | make_tuple(const T0& t0) { |
749 | typedef typename detail::make_tuple_mapper<T0>::type t; |
750 | return t(t0); |
751 | } |
752 | |
753 | template<class T0, class T1> |
754 | inline typename detail::make_tuple_mapper<T0, T1>::type |
755 | make_tuple(const T0& t0, const T1& t1) { |
756 | typedef typename detail::make_tuple_mapper<T0, T1>::type t; |
757 | return t(t0, t1); |
758 | } |
759 | |
760 | template<class T0, class T1, class T2> |
761 | inline typename detail::make_tuple_mapper<T0, T1, T2>::type |
762 | make_tuple(const T0& t0, const T1& t1, const T2& t2) { |
763 | typedef typename detail::make_tuple_mapper<T0, T1, T2>::type t; |
764 | return t(t0, t1, t2); |
765 | } |
766 | |
767 | template<class T0, class T1, class T2, class T3> |
768 | inline typename detail::make_tuple_mapper<T0, T1, T2, T3>::type |
769 | make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3) { |
770 | typedef typename detail::make_tuple_mapper<T0, T1, T2, T3>::type t; |
771 | return t(t0, t1, t2, t3); |
772 | } |
773 | |
774 | template<class T0, class T1, class T2, class T3, class T4> |
775 | inline typename detail::make_tuple_mapper<T0, T1, T2, T3, T4>::type |
776 | make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, |
777 | const T4& t4) { |
778 | typedef typename detail::make_tuple_mapper<T0, T1, T2, T3, T4>::type t; |
779 | return t(t0, t1, t2, t3, t4); |
780 | } |
781 | |
782 | template<class T0, class T1, class T2, class T3, class T4, class T5> |
783 | inline typename detail::make_tuple_mapper<T0, T1, T2, T3, T4, T5>::type |
784 | make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, |
785 | const T4& t4, const T5& t5) { |
786 | typedef typename detail::make_tuple_mapper<T0, T1, T2, T3, T4, T5>::type t; |
787 | return t(t0, t1, t2, t3, t4, t5); |
788 | } |
789 | |
790 | template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> |
791 | inline typename detail::make_tuple_mapper<T0, T1, T2, T3, T4, T5, T6>::type |
792 | make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, |
793 | const T4& t4, const T5& t5, const T6& t6) { |
794 | typedef typename detail::make_tuple_mapper |
795 | <T0, T1, T2, T3, T4, T5, T6>::type t; |
796 | return t(t0, t1, t2, t3, t4, t5, t6); |
797 | } |
798 | |
799 | template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, |
800 | class T7> |
801 | inline typename detail::make_tuple_mapper<T0, T1, T2, T3, T4, T5, T6, T7>::type |
802 | make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, |
803 | const T4& t4, const T5& t5, const T6& t6, const T7& t7) { |
804 | typedef typename detail::make_tuple_mapper |
805 | <T0, T1, T2, T3, T4, T5, T6, T7>::type t; |
806 | return t(t0, t1, t2, t3, t4, t5, t6, t7); |
807 | } |
808 | |
809 | template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, |
810 | class T7, class T8> |
811 | inline typename detail::make_tuple_mapper |
812 | <T0, T1, T2, T3, T4, T5, T6, T7, T8>::type |
813 | make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, |
814 | const T4& t4, const T5& t5, const T6& t6, const T7& t7, |
815 | const T8& t8) { |
816 | typedef typename detail::make_tuple_mapper |
817 | <T0, T1, T2, T3, T4, T5, T6, T7, T8>::type t; |
818 | return t(t0, t1, t2, t3, t4, t5, t6, t7, t8); |
819 | } |
820 | |
821 | template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, |
822 | class T7, class T8, class T9> |
823 | inline typename detail::make_tuple_mapper |
824 | <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::type |
825 | make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, |
826 | const T4& t4, const T5& t5, const T6& t6, const T7& t7, |
827 | const T8& t8, const T9& t9) { |
828 | typedef typename detail::make_tuple_mapper |
829 | <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::type t; |
830 | return t(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9); |
831 | } |
832 | |
833 | namespace detail { |
834 | |
835 | template<class T> |
836 | struct tie_traits { |
837 | typedef T& type; |
838 | }; |
839 | |
840 | template<> |
841 | struct tie_traits<ignore_t(ignore_t)> { |
842 | typedef swallow_assign type; |
843 | }; |
844 | |
845 | template<> |
846 | struct tie_traits<void> { |
847 | typedef null_type type; |
848 | }; |
849 | |
850 | template < |
851 | class T0 = void, class T1 = void, class T2 = void, |
852 | class T3 = void, class T4 = void, class T5 = void, |
853 | class T6 = void, class T7 = void, class T8 = void, |
854 | class T9 = void |
855 | > |
856 | struct tie_mapper { |
857 | typedef |
858 | tuple<typename tie_traits<T0>::type, |
859 | typename tie_traits<T1>::type, |
860 | typename tie_traits<T2>::type, |
861 | typename tie_traits<T3>::type, |
862 | typename tie_traits<T4>::type, |
863 | typename tie_traits<T5>::type, |
864 | typename tie_traits<T6>::type, |
865 | typename tie_traits<T7>::type, |
866 | typename tie_traits<T8>::type, |
867 | typename tie_traits<T9>::type> type; |
868 | }; |
869 | |
870 | } |
871 | |
872 | // Tie function templates ------------------------------------------------- |
873 | template<class T0> |
874 | inline typename detail::tie_mapper<T0>::type |
875 | tie(T0& t0) { |
876 | typedef typename detail::tie_mapper<T0>::type t; |
877 | return t(t0); |
878 | } |
879 | |
880 | template<class T0, class T1> |
881 | inline typename detail::tie_mapper<T0, T1>::type |
882 | tie(T0& t0, T1& t1) { |
883 | typedef typename detail::tie_mapper<T0, T1>::type t; |
884 | return t(t0, t1); |
885 | } |
886 | |
887 | template<class T0, class T1, class T2> |
888 | inline typename detail::tie_mapper<T0, T1, T2>::type |
889 | tie(T0& t0, T1& t1, T2& t2) { |
890 | typedef typename detail::tie_mapper<T0, T1, T2>::type t; |
891 | return t(t0, t1, t2); |
892 | } |
893 | |
894 | template<class T0, class T1, class T2, class T3> |
895 | inline typename detail::tie_mapper<T0, T1, T2, T3>::type |
896 | tie(T0& t0, T1& t1, T2& t2, T3& t3) { |
897 | typedef typename detail::tie_mapper<T0, T1, T2, T3>::type t; |
898 | return t(t0, t1, t2, t3); |
899 | } |
900 | |
901 | template<class T0, class T1, class T2, class T3, class T4> |
902 | inline typename detail::tie_mapper<T0, T1, T2, T3, T4>::type |
903 | tie(T0& t0, T1& t1, T2& t2, T3& t3, |
904 | T4& t4) { |
905 | typedef typename detail::tie_mapper<T0, T1, T2, T3, T4>::type t; |
906 | return t(t0, t1, t2, t3, t4); |
907 | } |
908 | |
909 | template<class T0, class T1, class T2, class T3, class T4, class T5> |
910 | inline typename detail::tie_mapper<T0, T1, T2, T3, T4, T5>::type |
911 | tie(T0& t0, T1& t1, T2& t2, T3& t3, |
912 | T4& t4, T5& t5) { |
913 | typedef typename detail::tie_mapper<T0, T1, T2, T3, T4, T5>::type t; |
914 | return t(t0, t1, t2, t3, t4, t5); |
915 | } |
916 | |
917 | template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> |
918 | inline typename detail::tie_mapper<T0, T1, T2, T3, T4, T5, T6>::type |
919 | tie(T0& t0, T1& t1, T2& t2, T3& t3, |
920 | T4& t4, T5& t5, T6& t6) { |
921 | typedef typename detail::tie_mapper |
922 | <T0, T1, T2, T3, T4, T5, T6>::type t; |
923 | return t(t0, t1, t2, t3, t4, t5, t6); |
924 | } |
925 | |
926 | template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, |
927 | class T7> |
928 | inline typename detail::tie_mapper<T0, T1, T2, T3, T4, T5, T6, T7>::type |
929 | tie(T0& t0, T1& t1, T2& t2, T3& t3, |
930 | T4& t4, T5& t5, T6& t6, T7& t7) { |
931 | typedef typename detail::tie_mapper |
932 | <T0, T1, T2, T3, T4, T5, T6, T7>::type t; |
933 | return t(t0, t1, t2, t3, t4, t5, t6, t7); |
934 | } |
935 | |
936 | template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, |
937 | class T7, class T8> |
938 | inline typename detail::tie_mapper |
939 | <T0, T1, T2, T3, T4, T5, T6, T7, T8>::type |
940 | tie(T0& t0, T1& t1, T2& t2, T3& t3, |
941 | T4& t4, T5& t5, T6& t6, T7& t7, |
942 | T8& t8) { |
943 | typedef typename detail::tie_mapper |
944 | <T0, T1, T2, T3, T4, T5, T6, T7, T8>::type t; |
945 | return t(t0, t1, t2, t3, t4, t5, t6, t7, t8); |
946 | } |
947 | |
948 | template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, |
949 | class T7, class T8, class T9> |
950 | inline typename detail::tie_mapper |
951 | <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::type |
952 | tie(T0& t0, T1& t1, T2& t2, T3& t3, |
953 | T4& t4, T5& t5, T6& t6, T7& t7, |
954 | T8& t8, T9& t9) { |
955 | typedef typename detail::tie_mapper |
956 | <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::type t; |
957 | return t(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9); |
958 | } |
959 | |
960 | template <class T0, class T1, class T2, class T3, class T4, |
961 | class T5, class T6, class T7, class T8, class T9> |
962 | void swap(tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>& lhs, |
963 | tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>& rhs); |
964 | inline void swap(null_type&, null_type&) {} |
965 | template<class HH> |
966 | inline void swap(cons<HH, null_type>& lhs, cons<HH, null_type>& rhs) { |
967 | ::boost::core::invoke_swap(lhs.head, rhs.head); |
968 | } |
969 | template<class HH, class TT> |
970 | inline void swap(cons<HH, TT>& lhs, cons<HH, TT>& rhs) { |
971 | ::boost::core::invoke_swap(lhs.head, rhs.head); |
972 | ::boost::tuples::swap(lhs.tail, rhs.tail); |
973 | } |
974 | template <class T0, class T1, class T2, class T3, class T4, |
975 | class T5, class T6, class T7, class T8, class T9> |
976 | inline void swap(tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>& lhs, |
977 | tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>& rhs) { |
978 | typedef tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> tuple_type; |
979 | typedef typename tuple_type::inherited base; |
980 | ::boost::tuples::swap(static_cast<base&>(lhs), static_cast<base&>(rhs)); |
981 | } |
982 | |
983 | } // end of namespace tuples |
984 | } // end of namespace boost |
985 | |
986 | |
987 | #if defined(BOOST_GCC) && (BOOST_GCC >= 40700) |
988 | #pragma GCC diagnostic pop |
989 | #endif |
990 | |
991 | |
992 | #endif // BOOST_TUPLE_BASIC_HPP |
993 | |