libstdc++
|
00001 // <utility> -*- C++ -*- 00002 00003 // Copyright (C) 2001-2014 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /* 00026 * 00027 * Copyright (c) 1994 00028 * Hewlett-Packard Company 00029 * 00030 * Permission to use, copy, modify, distribute and sell this software 00031 * and its documentation for any purpose is hereby granted without fee, 00032 * provided that the above copyright notice appear in all copies and 00033 * that both that copyright notice and this permission notice appear 00034 * in supporting documentation. Hewlett-Packard Company makes no 00035 * representations about the suitability of this software for any 00036 * purpose. It is provided "as is" without express or implied warranty. 00037 * 00038 * 00039 * Copyright (c) 1996,1997 00040 * Silicon Graphics Computer Systems, Inc. 00041 * 00042 * Permission to use, copy, modify, distribute and sell this software 00043 * and its documentation for any purpose is hereby granted without fee, 00044 * provided that the above copyright notice appear in all copies and 00045 * that both that copyright notice and this permission notice appear 00046 * in supporting documentation. Silicon Graphics makes no 00047 * representations about the suitability of this software for any 00048 * purpose. It is provided "as is" without express or implied warranty. 00049 */ 00050 00051 /** @file include/utility 00052 * This is a Standard C++ Library header. 00053 */ 00054 00055 #ifndef _GLIBCXX_UTILITY 00056 #define _GLIBCXX_UTILITY 1 00057 00058 #pragma GCC system_header 00059 00060 /** 00061 * @defgroup utilities Utilities 00062 * 00063 * Components deemed generally useful. Includes pair, tuple, 00064 * forward/move helpers, ratio, function object, metaprogramming and 00065 * type traits, time, date, and memory functions. 00066 */ 00067 00068 #include <bits/c++config.h> 00069 #include <bits/stl_relops.h> 00070 #include <bits/stl_pair.h> 00071 00072 #if __cplusplus >= 201103L 00073 #include <bits/move.h> 00074 #include <initializer_list> 00075 00076 namespace std _GLIBCXX_VISIBILITY(default) 00077 { 00078 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00079 00080 template<class _Tp> 00081 class tuple_size; 00082 00083 template<std::size_t _Int, class _Tp> 00084 class tuple_element; 00085 00086 // Various functions which give std::pair a tuple-like interface. 00087 template<class _Tp1, class _Tp2> 00088 struct tuple_size<std::pair<_Tp1, _Tp2>> 00089 : public integral_constant<std::size_t, 2> { }; 00090 00091 template<class _Tp1, class _Tp2> 00092 struct tuple_element<0, std::pair<_Tp1, _Tp2>> 00093 { typedef _Tp1 type; }; 00094 00095 template<class _Tp1, class _Tp2> 00096 struct tuple_element<1, std::pair<_Tp1, _Tp2>> 00097 { typedef _Tp2 type; }; 00098 00099 template<std::size_t _Int> 00100 struct __pair_get; 00101 00102 template<> 00103 struct __pair_get<0> 00104 { 00105 template<typename _Tp1, typename _Tp2> 00106 static constexpr _Tp1& 00107 __get(std::pair<_Tp1, _Tp2>& __pair) noexcept 00108 { return __pair.first; } 00109 00110 template<typename _Tp1, typename _Tp2> 00111 static constexpr _Tp1&& 00112 __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept 00113 { return std::forward<_Tp1>(__pair.first); } 00114 00115 template<typename _Tp1, typename _Tp2> 00116 static constexpr const _Tp1& 00117 __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept 00118 { return __pair.first; } 00119 }; 00120 00121 template<> 00122 struct __pair_get<1> 00123 { 00124 template<typename _Tp1, typename _Tp2> 00125 static constexpr _Tp2& 00126 __get(std::pair<_Tp1, _Tp2>& __pair) noexcept 00127 { return __pair.second; } 00128 00129 template<typename _Tp1, typename _Tp2> 00130 static constexpr _Tp2&& 00131 __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept 00132 { return std::forward<_Tp2>(__pair.second); } 00133 00134 template<typename _Tp1, typename _Tp2> 00135 static constexpr const _Tp2& 00136 __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept 00137 { return __pair.second; } 00138 }; 00139 00140 template<std::size_t _Int, class _Tp1, class _Tp2> 00141 constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type& 00142 get(std::pair<_Tp1, _Tp2>& __in) noexcept 00143 { return __pair_get<_Int>::__get(__in); } 00144 00145 template<std::size_t _Int, class _Tp1, class _Tp2> 00146 constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&& 00147 get(std::pair<_Tp1, _Tp2>&& __in) noexcept 00148 { return __pair_get<_Int>::__move_get(std::move(__in)); } 00149 00150 template<std::size_t _Int, class _Tp1, class _Tp2> 00151 constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type& 00152 get(const std::pair<_Tp1, _Tp2>& __in) noexcept 00153 { return __pair_get<_Int>::__const_get(__in); } 00154 00155 #if __cplusplus > 201103L 00156 template <typename _Tp, typename _Up> 00157 constexpr _Tp& 00158 get(pair<_Tp, _Up>& __p) noexcept 00159 { return __p.first; } 00160 00161 template <typename _Tp, typename _Up> 00162 constexpr const _Tp& 00163 get(const pair<_Tp, _Up>& __p) noexcept 00164 { return __p.first; } 00165 00166 template <typename _Tp, typename _Up> 00167 constexpr _Tp&& 00168 get(pair<_Tp, _Up>&& __p) noexcept 00169 { return std::move(__p.first); } 00170 00171 template <typename _Tp, typename _Up> 00172 constexpr _Tp& 00173 get(pair<_Up, _Tp>& __p) noexcept 00174 { return __p.second; } 00175 00176 template <typename _Tp, typename _Up> 00177 constexpr const _Tp& 00178 get(const pair<_Up, _Tp>& __p) noexcept 00179 { return __p.second; } 00180 00181 template <typename _Tp, typename _Up> 00182 constexpr _Tp&& 00183 get(pair<_Up, _Tp>&& __p) noexcept 00184 { return std::move(__p.second); } 00185 00186 /// Assign @p __new_val to @p __obj and return its previous value. 00187 template <typename _Tp, typename _Up = _Tp> 00188 inline _Tp 00189 exchange(_Tp& __obj, _Up&& __new_val) 00190 { 00191 _Tp __old_val = std::move(__obj); 00192 __obj = std::forward<_Up>(__new_val); 00193 return __old_val; 00194 } 00195 #endif 00196 00197 // Stores a tuple of indices. Used by tuple and pair, and by bind() to 00198 // extract the elements in a tuple. 00199 template<size_t... _Indexes> 00200 struct _Index_tuple 00201 { 00202 typedef _Index_tuple<_Indexes..., sizeof...(_Indexes)> __next; 00203 }; 00204 00205 // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>. 00206 template<size_t _Num> 00207 struct _Build_index_tuple 00208 { 00209 typedef typename _Build_index_tuple<_Num - 1>::__type::__next __type; 00210 }; 00211 00212 template<> 00213 struct _Build_index_tuple<0> 00214 { 00215 typedef _Index_tuple<> __type; 00216 }; 00217 00218 #if __cplusplus > 201103L 00219 /// Class template integer_sequence 00220 template<typename _Tp, _Tp... _Idx> 00221 struct integer_sequence 00222 { 00223 typedef _Tp value_type; 00224 static constexpr size_t size() { return sizeof...(_Idx); } 00225 }; 00226 00227 template<typename _Tp, _Tp _Num, 00228 typename _ISeq = typename _Build_index_tuple<_Num>::__type> 00229 struct _Make_integer_sequence; 00230 00231 template<typename _Tp, _Tp _Num, size_t... _Idx> 00232 struct _Make_integer_sequence<_Tp, _Num, _Index_tuple<_Idx...>> 00233 { 00234 static_assert( _Num >= 0, 00235 "Cannot make integer sequence of negative length" ); 00236 00237 typedef integer_sequence<_Tp, static_cast<_Tp>(_Idx)...> __type; 00238 }; 00239 00240 /// Alias template make_integer_sequence 00241 template<typename _Tp, _Tp _Num> 00242 using make_integer_sequence 00243 = typename _Make_integer_sequence<_Tp, _Num>::__type; 00244 00245 /// Alias template index_sequence 00246 template<size_t... _Idx> 00247 using index_sequence = integer_sequence<size_t, _Idx...>; 00248 00249 /// Alias template make_index_sequence 00250 template<size_t _Num> 00251 using make_index_sequence = make_integer_sequence<size_t, _Num>; 00252 00253 /// Alias template index_sequence_for 00254 template<typename... _Types> 00255 using index_sequence_for = make_index_sequence<sizeof...(_Types)>; 00256 #endif 00257 00258 _GLIBCXX_END_NAMESPACE_VERSION 00259 } // namespace 00260 00261 #endif 00262 00263 #endif /* _GLIBCXX_UTILITY */