# tuple

Go to the documentation of this file.

    00001 // class template tuple -*- C++ -*-
    00002
    00003 // Copyright (C) 2004, 2005 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 /** @file
    00031 *  This is a TR1 C++ Library header.
    00032 */
    00033
    00034 // Chris Jefferson <chris@bubblescope.net>
    00035
    00036 #ifndef _TUPLE
    00037 #define _TUPLE 1
    00038
    00039 #include <tr1/utility>
    00040 #include <tr1/ref_fwd.h>
    00041
    00042 namespace std
    00043 {
    00044 namespace tr1
    00045 {
    00046  // An implementation specific class which is used in the tuple class
    00047  // when the tuple is not maximum possible size.
    00048  struct _NullClass { };
    00049
    00050  template<typename _Tp0 = _NullClass, typename _Tp1 = _NullClass,
    00051           typename _Tp2 = _NullClass, typename _Tp3 = _NullClass,
    00052           typename _Tp4 = _NullClass, typename _Tp5 = _NullClass,
    00053           typename _Tp6 = _NullClass, typename _Tp7 = _NullClass,
    00054           typename _Tp8 = _NullClass, typename _Tp9 = _NullClass>
    00055    class tuple;
    00056
    00057  /// Gives the type of the ith element of a given tuple type.
    00058  template<int __i, typename _Tp>
    00059    struct tuple_element;
    00060
    00061  /// Finds the size of a given tuple type.
    00062  template<typename _Tp>
    00063    struct tuple_size;
    00064
    00065  // Adds a const reference to a non-reference type.
    00066  template<typename _Tp>
    00067    struct __add_c_ref
    00068    { typedef const _Tp& type; };
    00069
    00070  template<typename _Tp>
    00071    struct __add_c_ref<_Tp&>
    00072    { typedef _Tp& type; };
    00073
    00074  // Adds a reference to a non-reference type.
    00075  template<typename _Tp>
    00076    struct __add_ref
    00077    { typedef _Tp& type; };
    00078
    00079  template<typename _Tp>
    00080    struct __add_ref<_Tp&>
    00081    { typedef _Tp& type; };
    00082
    00083  // Class used in the implementation of get
    00084  template<int __i, typename _Tp>
    00085    struct __get_helper;
    00086
    00087  // Returns a const reference to the ith element of a tuple.
    00088  // Any const or non-const ref elements are returned with their original type.
    00089    template<int __i, typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
    00090                    typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
    00091                    typename _Tp8, typename _Tp9>
    00092    typename __add_ref<typename tuple_element<__i, tuple<_Tp0, _Tp1, _Tp2,
    00093                                                         _Tp3, _Tp4, _Tp5,
    00094                                                         _Tp6, _Tp7, _Tp8,
    00095                                                         _Tp9> >::type>::type
    00096    get(tuple<_Tp0, _Tp1, _Tp2, _Tp3, _Tp4, _Tp5, _Tp6, _Tp7, _Tp8,
    00097              _Tp9>& __t)
    00098    {
    00099      return __get_helper<__i, tuple<_Tp0, _Tp1, _Tp2, _Tp3, _Tp4, _Tp5, _Tp6,
    00100                                     _Tp7, _Tp8, _Tp9> >::get_value(__t);
    00101    }
    00102
    00103  template<int __i, typename _Tp0, typename _Tp1, typename _Tp2, typename _Tp3,
    00104                    typename _Tp4, typename _Tp5, typename _Tp6, typename _Tp7,
    00105                    typename _Tp8, typename _Tp9>
    00106    typename __add_c_ref<typename tuple_element<__i, tuple<_Tp0, _Tp1, _Tp2,
    00107                                                           _Tp3, _Tp4, _Tp5,
    00108                                                           _Tp6, _Tp7, _Tp8,
    00109                                                           _Tp9> >::type>::type
    00110    get(const tuple<_Tp0, _Tp1, _Tp2, _Tp3, _Tp4, _Tp5, _Tp6, _Tp7, _Tp8,
    00111                    _Tp9>& __t)
    00112    {
    00113      return __get_helper<__i, tuple<_Tp0, _Tp1, _Tp2, _Tp3, _Tp4, _Tp5, _Tp6,
    00114                                     _Tp7, _Tp8, _Tp9> >::get_value(__t);
    00115    }
    00116
    00117  // This class helps construct the various comparison operations on tuples
    00118  template<int __check_equal_size, int __i, int __j, typename _Tp, typename _Up>
    00119    struct __tuple_compare;
    00120
    00121  template<int __i, int __j, typename _Tp, typename _Up>
    00122    struct __tuple_compare<0, __i, __j, _Tp, _Up>
    00123    {
    00124      static bool __eq(const _Tp& __t, const _Up& __u)
    00125      {
    00126        return get<__i>(__t) == get<__i>(__u) &&
    00127           __tuple_compare<0, __i+1, __j, _Tp, _Up>::__eq(__t, __u);
    00128      }
    00129      static bool __less(const _Tp& __t, const _Up& __u)
    00130      {
    00131        return (get<__i>(__t) < get<__i>(__u)) || !(get<__i>(__u) < get<__i>(__t)) &&
    00132           __tuple_compare<0, __i+1, __j, _Tp, _Up>::__less(__t, __u);
    00133      }
    00134    };
    00135
    00136  template<int __i, typename _Tp, typename _Up>
    00137    struct __tuple_compare<0, __i, __i, _Tp, _Up>
    00138    {
    00139      static bool __eq(const _Tp&, const _Up&)
    00140      { return true; }
    00141      static bool __less(const _Tp&, const _Up&)
    00142      { return false; }
    00143    };
    00144
    00145  template<typename _T1, typename _T2, typename _T3, typename _T4, typename _T5,
    00146           typename _T6, typename _T7, typename _T8, typename _T9, typename _T10,
    00147           typename _U1, typename _U2, typename _U3, typename _U4, typename _U5,
    00148           typename _U6, typename _U7, typename _U8, typename _U9, typename _U10>
    00149  bool
    00150  operator==(const tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10>& __t,
    00151             const tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8, _U9, _U10>& __u)
    00152  {
    00153    typedef tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10> _Tp;
    00154    typedef tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8,_U9, _U10> _Up;
    00155    return __tuple_compare<tuple_size<_Tp>::value - tuple_size<_Tp>::value, 0,
    00156                           tuple_size<_Tp>::value, _Tp, _Up>::__eq(__t, __u);
    00157  }
    00158
    00159  template<typename _T1, typename _T2, typename _T3, typename _T4, typename _T5,
    00160           typename _T6, typename _T7, typename _T8, typename _T9, typename _T10,
    00161           typename _U1, typename _U2, typename _U3, typename _U4, typename _U5,
    00162           typename _U6, typename _U7, typename _U8, typename _U9, typename _U10>
    00163  bool
    00164  operator<(const tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10>& __t,
    00165            const tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8, _U9, _U10>& __u)
    00166  {
    00167    typedef tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10> _Tp;
    00168    typedef tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8,_U9, _U10> _Up;
    00169    return __tuple_compare<tuple_size<_Tp>::value - tuple_size<_Tp>::value, 0,
    00170                           tuple_size<_Tp>::value, _Tp, _Up>::__less(__t, __u);
    00171  }
    00172
    00173  template<typename _T1, typename _T2, typename _T3, typename _T4, typename _T5,
    00174           typename _T6, typename _T7, typename _T8, typename _T9, typename _T10,
    00175           typename _U1, typename _U2, typename _U3, typename _U4, typename _U5,
    00176           typename _U6, typename _U7, typename _U8, typename _U9, typename _U10>
    00177  bool
    00178  operator!=(const tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10>& __t,
    00179             const tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8, _U9, _U10>& __u)
    00180  { return !(__t == __u); }
    00181
    00182  template<typename _T1, typename _T2, typename _T3, typename _T4, typename _T5,
    00183           typename _T6, typename _T7, typename _T8, typename _T9, typename _T10,
    00184           typename _U1, typename _U2, typename _U3, typename _U4, typename _U5,
    00185           typename _U6, typename _U7, typename _U8, typename _U9, typename _U10>
    00186  bool
    00187  operator>(const tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10>& __t,
    00188            const tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8, _U9, _U10>& __u)
    00189  { return __u < __t; }
    00190
    00191  template<typename _T1, typename _T2, typename _T3, typename _T4, typename _T5,
    00192           typename _T6, typename _T7, typename _T8, typename _T9, typename _T10,
    00193           typename _U1, typename _U2, typename _U3, typename _U4, typename _U5,
    00194           typename _U6, typename _U7, typename _U8, typename _U9, typename _U10>
    00195  bool
    00196  operator<=(const tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10>& __t,
    00197             const tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8, _U9, _U10>& __u)
    00198  { return !(__u < __t); }
    00199
    00200  template<typename _T1, typename _T2, typename _T3, typename _T4, typename _T5,
    00201           typename _T6, typename _T7, typename _T8, typename _T9, typename _T10,
    00202           typename _U1, typename _U2, typename _U3, typename _U4, typename _U5,
    00203           typename _U6, typename _U7, typename _U8, typename _U9, typename _U10>
    00204  bool
    00205  operator>=(const tuple<_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9, _T10>& __t,
    00206             const tuple<_U1, _U2, _U3, _U4, _U5, _U6, _U7, _U8, _U9, _U10>& __u)
    00207  { return !(__t < __u); }
    00208
    00209  // Helper which adds a reference to a type when given a reference_wrapper
    00210  template<typename _Tp>
    00211    struct __strip_reference_wrapper
    00212    {
    00213        typedef _Tp __type;
    00214    };
    00215
    00216  template<typename _Tp>
    00217    struct __strip_reference_wrapper<reference_wrapper<_Tp> >
    00218    {
    00219      typedef _Tp& __type;
    00220    };
    00221
    00222  template<typename _Tp>
    00223    struct __strip_reference_wrapper<const reference_wrapper<_Tp> >
    00224    {
    00225        typedef _Tp& __type;
    00226    };
    00227
    00228  template<typename _Tp0 = _NullClass, typename _Tp1 = _NullClass,
    00229           typename _Tp2 = _NullClass, typename _Tp3 = _NullClass,
    00230           typename _Tp4 = _NullClass, typename _Tp5 = _NullClass,
    00231           typename _Tp6 = _NullClass, typename _Tp7 = _NullClass,
    00232           typename _Tp8 = _NullClass, typename _Tp9 = _NullClass>
    00233    struct __stripped_tuple_type
    00234    {
    00235      typedef tuple<typename __strip_reference_wrapper<_Tp0>::__type,
    00236                    typename __strip_reference_wrapper<_Tp1>::__type,
    00237                    typename __strip_reference_wrapper<_Tp2>::__type,
    00238                    typename __strip_reference_wrapper<_Tp3>::__type,
    00239                    typename __strip_reference_wrapper<_Tp4>::__type,
    00240                    typename __strip_reference_wrapper<_Tp5>::__type,
    00241                    typename __strip_reference_wrapper<_Tp6>::__type,
    00242                    typename __strip_reference_wrapper<_Tp7>::__type,
    00243                    typename __strip_reference_wrapper<_Tp8>::__type,
    00244                    typename __strip_reference_wrapper<_Tp9>::__type>      __type;
    00245    };
    00246
    00247  // A class (and instance) which can be used in 'tie' when an element
    00248  // of a tuple is not required
    00249  struct swallow_assign
    00250  {
    00251    template<class T>
    00252    swallow_assign&
    00253      operator=(const T&)
    00254      { return *this; }
    00255  };
    00256
    00257  // TODO: Put this in some kind of shared file.
    00258  namespace
    00259  {
    00260    swallow_assign ignore;
    00261  };
    00262
    00263 #define _GLIBCXX_CAT(x,y) _GLIBCXX_CAT2(x,y)
    00264 #define _GLIBCXX_CAT2(x,y) x##y
    00265 #define _SHORT_REPEAT
    00266 #define _GLIBCXX_REPEAT_HEADER <tr1/tuple_iterate.h>
    00267 #include <tr1/repeat.h>
    00268 #undef _GLIBCXX_REPEAT_HEADER
    00269 #undef _SHORT_REPEAT
    00270 }
    00271 }
    00272
    00273 #include <tr1/functional>
    00274
    00275 #endif