Home | History | Annotate | Download | only in include
      1 // <utility> -*- C++ -*-
      2 
      3 // Copyright (C) 2001-2014 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 /*
     26  *
     27  * Copyright (c) 1994
     28  * Hewlett-Packard Company
     29  *
     30  * Permission to use, copy, modify, distribute and sell this software
     31  * and its documentation for any purpose is hereby granted without fee,
     32  * provided that the above copyright notice appear in all copies and
     33  * that both that copyright notice and this permission notice appear
     34  * in supporting documentation.  Hewlett-Packard Company makes no
     35  * representations about the suitability of this software for any
     36  * purpose.  It is provided "as is" without express or implied warranty.
     37  *
     38  *
     39  * Copyright (c) 1996,1997
     40  * Silicon Graphics Computer Systems, Inc.
     41  *
     42  * Permission to use, copy, modify, distribute and sell this software
     43  * and its documentation for any purpose is hereby granted without fee,
     44  * provided that the above copyright notice appear in all copies and
     45  * that both that copyright notice and this permission notice appear
     46  * in supporting documentation.  Silicon Graphics makes no
     47  * representations about the suitability of this software for any
     48  * purpose.  It is provided "as is" without express or implied warranty.
     49  */
     50 
     51 /** @file include/utility
     52  *  This is a Standard C++ Library header. 
     53  */
     54 
     55 #ifndef _GLIBCXX_UTILITY
     56 #define _GLIBCXX_UTILITY 1
     57 
     58 #pragma GCC system_header
     59 
     60 /**
     61  * @defgroup utilities Utilities
     62  *
     63  * Components deemed generally useful. Includes pair, tuple,
     64  * forward/move helpers, ratio, function object, metaprogramming and
     65  * type traits, time, date, and memory functions.
     66  */
     67 
     68 #include <bits/c++config.h>
     69 #include <bits/stl_relops.h>
     70 #include <bits/stl_pair.h>
     71 
     72 #if __cplusplus >= 201103L
     73 #include <bits/move.h>
     74 #include <initializer_list>
     75 
     76 namespace std _GLIBCXX_VISIBILITY(default)
     77 {
     78 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     79 
     80   template<class _Tp>
     81     class tuple_size;
     82 
     83   template<std::size_t _Int, class _Tp>
     84     class tuple_element;
     85 
     86    // Various functions which give std::pair a tuple-like interface.
     87   template<class _Tp1, class _Tp2>
     88     struct tuple_size<std::pair<_Tp1, _Tp2>>
     89     : public integral_constant<std::size_t, 2> { };
     90 
     91   template<class _Tp1, class _Tp2>
     92     struct tuple_element<0, std::pair<_Tp1, _Tp2>>
     93     { typedef _Tp1 type; };
     94  
     95   template<class _Tp1, class _Tp2>
     96     struct tuple_element<1, std::pair<_Tp1, _Tp2>>
     97     { typedef _Tp2 type; };
     98 
     99   template<std::size_t _Int>
    100     struct __pair_get;
    101 
    102   template<>
    103     struct __pair_get<0>
    104     {
    105       template<typename _Tp1, typename _Tp2>
    106         static constexpr _Tp1&
    107         __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
    108         { return __pair.first; }
    109 
    110       template<typename _Tp1, typename _Tp2>
    111         static constexpr _Tp1&&
    112         __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
    113         { return std::forward<_Tp1>(__pair.first); }
    114 
    115       template<typename _Tp1, typename _Tp2>
    116         static constexpr const _Tp1&
    117         __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
    118         { return __pair.first; }
    119     };
    120 
    121   template<>
    122     struct __pair_get<1>
    123     {
    124       template<typename _Tp1, typename _Tp2>
    125         static constexpr _Tp2&
    126         __get(std::pair<_Tp1, _Tp2>& __pair) noexcept
    127         { return __pair.second; }
    128 
    129       template<typename _Tp1, typename _Tp2>
    130         static constexpr _Tp2&&
    131         __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
    132         { return std::forward<_Tp2>(__pair.second); }
    133 
    134       template<typename _Tp1, typename _Tp2>
    135         static constexpr const _Tp2&
    136         __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
    137         { return __pair.second; }
    138     };
    139 
    140   template<std::size_t _Int, class _Tp1, class _Tp2>
    141     constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
    142     get(std::pair<_Tp1, _Tp2>& __in) noexcept
    143     { return __pair_get<_Int>::__get(__in); }
    144 
    145   template<std::size_t _Int, class _Tp1, class _Tp2>
    146     constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
    147     get(std::pair<_Tp1, _Tp2>&& __in) noexcept
    148     { return __pair_get<_Int>::__move_get(std::move(__in)); }
    149 
    150   template<std::size_t _Int, class _Tp1, class _Tp2>
    151     constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
    152     get(const std::pair<_Tp1, _Tp2>& __in) noexcept
    153     { return __pair_get<_Int>::__const_get(__in); }
    154 
    155 #if __cplusplus > 201103L
    156   template <typename _Tp, typename _Up>
    157     constexpr _Tp&
    158     get(pair<_Tp, _Up>& __p) noexcept
    159     { return __p.first; }
    160 
    161   template <typename _Tp, typename _Up>
    162     constexpr const _Tp&
    163     get(const pair<_Tp, _Up>& __p) noexcept
    164     { return __p.first; }
    165 
    166   template <typename _Tp, typename _Up>
    167     constexpr _Tp&&
    168     get(pair<_Tp, _Up>&& __p) noexcept
    169     { return std::move(__p.first); }
    170 
    171   template <typename _Tp, typename _Up>
    172     constexpr _Tp&
    173     get(pair<_Up, _Tp>& __p) noexcept
    174     { return __p.second; }
    175 
    176   template <typename _Tp, typename _Up>
    177     constexpr const _Tp&
    178     get(const pair<_Up, _Tp>& __p) noexcept
    179     { return __p.second; }
    180 
    181   template <typename _Tp, typename _Up>
    182     constexpr _Tp&&
    183     get(pair<_Up, _Tp>&& __p) noexcept
    184     { return std::move(__p.second); }
    185 
    186   /// Assign @p __new_val to @p __obj and return its previous value.
    187   template <typename _Tp, typename _Up = _Tp>
    188     inline _Tp
    189     exchange(_Tp& __obj, _Up&& __new_val)
    190     {
    191       _Tp __old_val = std::move(__obj);
    192       __obj = std::forward<_Up>(__new_val);
    193       return __old_val;
    194     }
    195 #endif
    196 
    197   // Stores a tuple of indices.  Used by tuple and pair, and by bind() to
    198   // extract the elements in a tuple.
    199   template<size_t... _Indexes>
    200     struct _Index_tuple
    201     {
    202       typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next;
    203     };
    204 
    205   // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
    206   template<size_t _Num>
    207     struct _Build_index_tuple
    208     {
    209       typedef typename _Build_index_tuple<_Num - 1>::__type::__next __type;
    210     };
    211 
    212   template<>
    213     struct _Build_index_tuple<0>
    214     {
    215       typedef _Index_tuple<> __type;
    216     };
    217 
    218 #if __cplusplus > 201103L
    219   /// Class template integer_sequence
    220   template<typename _Tp, _Tp... _Idx>
    221     struct integer_sequence
    222     {
    223       typedef _Tp value_type;
    224       static constexpr size_t size() { return sizeof...(_Idx); }
    225     };
    226 
    227   template<typename _Tp, _Tp _Num,
    228 	   typename _ISeq = typename _Build_index_tuple<_Num>::__type>
    229     struct _Make_integer_sequence;
    230 
    231   template<typename _Tp, _Tp _Num,  size_t... _Idx>
    232     struct _Make_integer_sequence<_Tp, _Num, _Index_tuple<_Idx...>>
    233     {
    234       static_assert( _Num >= 0,
    235 		     "Cannot make integer sequence of negative length" );
    236 
    237       typedef integer_sequence<_Tp, static_cast<_Tp>(_Idx)...> __type;
    238     };
    239 
    240   /// Alias template make_integer_sequence
    241   template<typename _Tp, _Tp _Num>
    242     using make_integer_sequence
    243       = typename _Make_integer_sequence<_Tp, _Num>::__type;
    244 
    245   /// Alias template index_sequence
    246   template<size_t... _Idx>
    247     using index_sequence = integer_sequence<size_t, _Idx...>;
    248 
    249   /// Alias template make_index_sequence
    250   template<size_t _Num>
    251     using make_index_sequence = make_integer_sequence<size_t, _Num>;
    252 
    253   /// Alias template index_sequence_for
    254   template<typename... _Types>
    255     using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
    256 #endif
    257 
    258 _GLIBCXX_END_NAMESPACE_VERSION
    259 } // namespace
    260 
    261 #endif
    262 
    263 #endif /* _GLIBCXX_UTILITY */
    264