libstdc++
|
00001 // <utility> -*- C++ -*- 00002 00003 // Copyright (C) 2001-2016 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 00074 #include <type_traits> 00075 #include <bits/move.h> 00076 #include <initializer_list> 00077 00078 namespace std _GLIBCXX_VISIBILITY(default) 00079 { 00080 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00081 00082 /// Finds the size of a given tuple type. 00083 template<typename _Tp> 00084 struct tuple_size; 00085 00086 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00087 // 2313. tuple_size should always derive from integral_constant<size_t, N> 00088 template<typename _Tp> 00089 struct tuple_size<const _Tp> 00090 : integral_constant<size_t, tuple_size<_Tp>::value> { }; 00091 00092 template<typename _Tp> 00093 struct tuple_size<volatile _Tp> 00094 : integral_constant<size_t, tuple_size<_Tp>::value> { }; 00095 00096 template<typename _Tp> 00097 struct tuple_size<const volatile _Tp> 00098 : integral_constant<size_t, tuple_size<_Tp>::value> { }; 00099 00100 /// Gives the type of the ith element of a given tuple type. 00101 template<std::size_t __i, typename _Tp> 00102 struct tuple_element; 00103 00104 // Duplicate of C++14's tuple_element_t for internal use in C++11 mode 00105 template<std::size_t __i, typename _Tp> 00106 using __tuple_element_t = typename tuple_element<__i, _Tp>::type; 00107 00108 template<std::size_t __i, typename _Tp> 00109 struct tuple_element<__i, const _Tp> 00110 { 00111 typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type; 00112 }; 00113 00114 template<std::size_t __i, typename _Tp> 00115 struct tuple_element<__i, volatile _Tp> 00116 { 00117 typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type; 00118 }; 00119 00120 template<std::size_t __i, typename _Tp> 00121 struct tuple_element<__i, const volatile _Tp> 00122 { 00123 typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type; 00124 }; 00125 00126 #if __cplusplus > 201103L 00127 #define __cpp_lib_tuple_element_t 201402 00128 00129 template<std::size_t __i, typename _Tp> 00130 using tuple_element_t = typename tuple_element<__i, _Tp>::type; 00131 #endif 00132 00133 template<typename> 00134 struct __is_tuple_like_impl : false_type 00135 { }; 00136 00137 // Various functions which give std::pair a tuple-like interface. 00138 00139 /// Partial specialization for std::pair 00140 template<typename _T1, typename _T2> 00141 struct __is_tuple_like_impl<std::pair<_T1, _T2>> : true_type 00142 { }; 00143 00144 /// Partial specialization for std::pair 00145 template<class _Tp1, class _Tp2> 00146 struct tuple_size<std::pair<_Tp1, _Tp2>> 00147 : public integral_constant<std::size_t, 2> { }; 00148 00149 /// Partial specialization for std::pair 00150 template<class _Tp1, class _Tp2> 00151 struct tuple_element<0, std::pair<_Tp1, _Tp2>> 00152 { typedef _Tp1 type; }; 00153 00154 /// Partial specialization for std::pair 00155 template<class _Tp1, class _Tp2> 00156 struct tuple_element<1, std::pair<_Tp1, _Tp2>> 00157 { typedef _Tp2 type; }; 00158 00159 template<std::size_t _Int> 00160 struct __pair_get; 00161 00162 template<> 00163 struct __pair_get<0> 00164 { 00165 template<typename _Tp1, typename _Tp2> 00166 static constexpr _Tp1& 00167 __get(std::pair<_Tp1, _Tp2>& __pair) noexcept 00168 { return __pair.first; } 00169 00170 template<typename _Tp1, typename _Tp2> 00171 static constexpr _Tp1&& 00172 __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept 00173 { return std::forward<_Tp1>(__pair.first); } 00174 00175 template<typename _Tp1, typename _Tp2> 00176 static constexpr const _Tp1& 00177 __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept 00178 { return __pair.first; } 00179 }; 00180 00181 template<> 00182 struct __pair_get<1> 00183 { 00184 template<typename _Tp1, typename _Tp2> 00185 static constexpr _Tp2& 00186 __get(std::pair<_Tp1, _Tp2>& __pair) noexcept 00187 { return __pair.second; } 00188 00189 template<typename _Tp1, typename _Tp2> 00190 static constexpr _Tp2&& 00191 __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept 00192 { return std::forward<_Tp2>(__pair.second); } 00193 00194 template<typename _Tp1, typename _Tp2> 00195 static constexpr const _Tp2& 00196 __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept 00197 { return __pair.second; } 00198 }; 00199 00200 template<std::size_t _Int, class _Tp1, class _Tp2> 00201 constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type& 00202 get(std::pair<_Tp1, _Tp2>& __in) noexcept 00203 { return __pair_get<_Int>::__get(__in); } 00204 00205 template<std::size_t _Int, class _Tp1, class _Tp2> 00206 constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&& 00207 get(std::pair<_Tp1, _Tp2>&& __in) noexcept 00208 { return __pair_get<_Int>::__move_get(std::move(__in)); } 00209 00210 template<std::size_t _Int, class _Tp1, class _Tp2> 00211 constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type& 00212 get(const std::pair<_Tp1, _Tp2>& __in) noexcept 00213 { return __pair_get<_Int>::__const_get(__in); } 00214 00215 #if __cplusplus > 201103L 00216 00217 #define __cpp_lib_tuples_by_type 201304 00218 00219 template <typename _Tp, typename _Up> 00220 constexpr _Tp& 00221 get(pair<_Tp, _Up>& __p) noexcept 00222 { return __p.first; } 00223 00224 template <typename _Tp, typename _Up> 00225 constexpr const _Tp& 00226 get(const pair<_Tp, _Up>& __p) noexcept 00227 { return __p.first; } 00228 00229 template <typename _Tp, typename _Up> 00230 constexpr _Tp&& 00231 get(pair<_Tp, _Up>&& __p) noexcept 00232 { return std::move(__p.first); } 00233 00234 template <typename _Tp, typename _Up> 00235 constexpr _Tp& 00236 get(pair<_Up, _Tp>& __p) noexcept 00237 { return __p.second; } 00238 00239 template <typename _Tp, typename _Up> 00240 constexpr const _Tp& 00241 get(const pair<_Up, _Tp>& __p) noexcept 00242 { return __p.second; } 00243 00244 template <typename _Tp, typename _Up> 00245 constexpr _Tp&& 00246 get(pair<_Up, _Tp>&& __p) noexcept 00247 { return std::move(__p.second); } 00248 00249 #define __cpp_lib_exchange_function 201304 00250 00251 /// Assign @p __new_val to @p __obj and return its previous value. 00252 template <typename _Tp, typename _Up = _Tp> 00253 inline _Tp 00254 exchange(_Tp& __obj, _Up&& __new_val) 00255 { return std::__exchange(__obj, std::forward<_Up>(__new_val)); } 00256 #endif 00257 00258 // Stores a tuple of indices. Used by tuple and pair, and by bind() to 00259 // extract the elements in a tuple. 00260 template<size_t... _Indexes> struct _Index_tuple { }; 00261 00262 // Concatenates two _Index_tuples. 00263 template<typename _Itup1, typename _Itup2> struct _Itup_cat; 00264 00265 template<size_t... _Ind1, size_t... _Ind2> 00266 struct _Itup_cat<_Index_tuple<_Ind1...>, _Index_tuple<_Ind2...>> 00267 { 00268 using __type = _Index_tuple<_Ind1..., (_Ind2 + sizeof...(_Ind1))...>; 00269 }; 00270 00271 // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>. 00272 template<size_t _Num> 00273 struct _Build_index_tuple 00274 : _Itup_cat<typename _Build_index_tuple<_Num / 2>::__type, 00275 typename _Build_index_tuple<_Num - _Num / 2>::__type> 00276 { }; 00277 00278 template<> 00279 struct _Build_index_tuple<1> 00280 { 00281 typedef _Index_tuple<0> __type; 00282 }; 00283 00284 template<> 00285 struct _Build_index_tuple<0> 00286 { 00287 typedef _Index_tuple<> __type; 00288 }; 00289 00290 #if __cplusplus > 201103L 00291 00292 #define __cpp_lib_integer_sequence 201304 00293 00294 /// Class template integer_sequence 00295 template<typename _Tp, _Tp... _Idx> 00296 struct integer_sequence 00297 { 00298 typedef _Tp value_type; 00299 static constexpr size_t size() { return sizeof...(_Idx); } 00300 }; 00301 00302 template<typename _Tp, _Tp _Num, 00303 typename _ISeq = typename _Build_index_tuple<_Num>::__type> 00304 struct _Make_integer_sequence; 00305 00306 template<typename _Tp, _Tp _Num, size_t... _Idx> 00307 struct _Make_integer_sequence<_Tp, _Num, _Index_tuple<_Idx...>> 00308 { 00309 static_assert( _Num >= 0, 00310 "Cannot make integer sequence of negative length" ); 00311 00312 typedef integer_sequence<_Tp, static_cast<_Tp>(_Idx)...> __type; 00313 }; 00314 00315 /// Alias template make_integer_sequence 00316 template<typename _Tp, _Tp _Num> 00317 using make_integer_sequence 00318 = typename _Make_integer_sequence<_Tp, _Num>::__type; 00319 00320 /// Alias template index_sequence 00321 template<size_t... _Idx> 00322 using index_sequence = integer_sequence<size_t, _Idx...>; 00323 00324 /// Alias template make_index_sequence 00325 template<size_t _Num> 00326 using make_index_sequence = make_integer_sequence<size_t, _Num>; 00327 00328 /// Alias template index_sequence_for 00329 template<typename... _Types> 00330 using index_sequence_for = make_index_sequence<sizeof...(_Types)>; 00331 #endif 00332 00333 _GLIBCXX_END_NAMESPACE_VERSION 00334 } // namespace 00335 00336 #endif 00337 00338 #endif /* _GLIBCXX_UTILITY */