1 | // <utility> -*- C++ -*- |
2 | |
3 | // Copyright (C) 2001-2019 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 | |
74 | #include <type_traits> |
75 | #include <bits/move.h> |
76 | #include <initializer_list> |
77 | |
78 | namespace std _GLIBCXX_VISIBILITY(default) |
79 | { |
80 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
81 | |
82 | /// Finds the size of a given tuple type. |
83 | template<typename _Tp> |
84 | struct tuple_size; |
85 | |
86 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
87 | // 2313. tuple_size should always derive from integral_constant<size_t, N> |
88 | // 2770. tuple_size<const T> specialization is not SFINAE compatible |
89 | |
90 | template<typename _Tp, |
91 | typename _Up = typename remove_cv<_Tp>::type, |
92 | typename = typename enable_if<is_same<_Tp, _Up>::value>::type, |
93 | size_t = tuple_size<_Tp>::value> |
94 | using __enable_if_has_tuple_size = _Tp; |
95 | |
96 | template<typename _Tp> |
97 | struct tuple_size<const __enable_if_has_tuple_size<_Tp>> |
98 | : public tuple_size<_Tp> { }; |
99 | |
100 | template<typename _Tp> |
101 | struct tuple_size<volatile __enable_if_has_tuple_size<_Tp>> |
102 | : public tuple_size<_Tp> { }; |
103 | |
104 | template<typename _Tp> |
105 | struct tuple_size<const volatile __enable_if_has_tuple_size<_Tp>> |
106 | : public tuple_size<_Tp> { }; |
107 | |
108 | /// Gives the type of the ith element of a given tuple type. |
109 | template<std::size_t __i, typename _Tp> |
110 | struct tuple_element; |
111 | |
112 | // Duplicate of C++14's tuple_element_t for internal use in C++11 mode |
113 | template<std::size_t __i, typename _Tp> |
114 | using __tuple_element_t = typename tuple_element<__i, _Tp>::type; |
115 | |
116 | template<std::size_t __i, typename _Tp> |
117 | struct tuple_element<__i, const _Tp> |
118 | { |
119 | typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type; |
120 | }; |
121 | |
122 | template<std::size_t __i, typename _Tp> |
123 | struct tuple_element<__i, volatile _Tp> |
124 | { |
125 | typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type; |
126 | }; |
127 | |
128 | template<std::size_t __i, typename _Tp> |
129 | struct tuple_element<__i, const volatile _Tp> |
130 | { |
131 | typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type; |
132 | }; |
133 | |
134 | #if __cplusplus >= 201402L |
135 | // The standard says this macro and alias template should be in <tuple> |
136 | // but we define them here, to be available when the partial specializations |
137 | // of tuple_element<pair<T,U>> and tuple_element<array<T,N>> are defined. |
138 | #define __cpp_lib_tuple_element_t 201402L |
139 | |
140 | template<std::size_t __i, typename _Tp> |
141 | using tuple_element_t = typename tuple_element<__i, _Tp>::type; |
142 | #endif |
143 | |
144 | // Various functions which give std::pair a tuple-like interface. |
145 | |
146 | /// Partial specialization for std::pair |
147 | template<typename _T1, typename _T2> |
148 | struct __is_tuple_like_impl<std::pair<_T1, _T2>> : true_type |
149 | { }; |
150 | |
151 | /// Partial specialization for std::pair |
152 | template<class _Tp1, class _Tp2> |
153 | struct tuple_size<std::pair<_Tp1, _Tp2>> |
154 | : public integral_constant<std::size_t, 2> { }; |
155 | |
156 | /// Partial specialization for std::pair |
157 | template<class _Tp1, class _Tp2> |
158 | struct tuple_element<0, std::pair<_Tp1, _Tp2>> |
159 | { typedef _Tp1 type; }; |
160 | |
161 | /// Partial specialization for std::pair |
162 | template<class _Tp1, class _Tp2> |
163 | struct tuple_element<1, std::pair<_Tp1, _Tp2>> |
164 | { typedef _Tp2 type; }; |
165 | |
166 | template<std::size_t _Int> |
167 | struct __pair_get; |
168 | |
169 | template<> |
170 | struct __pair_get<0> |
171 | { |
172 | template<typename _Tp1, typename _Tp2> |
173 | static constexpr _Tp1& |
174 | __get(std::pair<_Tp1, _Tp2>& __pair) noexcept |
175 | { return __pair.first; } |
176 | |
177 | template<typename _Tp1, typename _Tp2> |
178 | static constexpr _Tp1&& |
179 | __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept |
180 | { return std::forward<_Tp1>(__pair.first); } |
181 | |
182 | template<typename _Tp1, typename _Tp2> |
183 | static constexpr const _Tp1& |
184 | __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept |
185 | { return __pair.first; } |
186 | |
187 | template<typename _Tp1, typename _Tp2> |
188 | static constexpr const _Tp1&& |
189 | __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept |
190 | { return std::forward<const _Tp1>(__pair.first); } |
191 | }; |
192 | |
193 | template<> |
194 | struct __pair_get<1> |
195 | { |
196 | template<typename _Tp1, typename _Tp2> |
197 | static constexpr _Tp2& |
198 | __get(std::pair<_Tp1, _Tp2>& __pair) noexcept |
199 | { return __pair.second; } |
200 | |
201 | template<typename _Tp1, typename _Tp2> |
202 | static constexpr _Tp2&& |
203 | __move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept |
204 | { return std::forward<_Tp2>(__pair.second); } |
205 | |
206 | template<typename _Tp1, typename _Tp2> |
207 | static constexpr const _Tp2& |
208 | __const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept |
209 | { return __pair.second; } |
210 | |
211 | template<typename _Tp1, typename _Tp2> |
212 | static constexpr const _Tp2&& |
213 | __const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept |
214 | { return std::forward<const _Tp2>(__pair.second); } |
215 | }; |
216 | |
217 | template<std::size_t _Int, class _Tp1, class _Tp2> |
218 | constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type& |
219 | get(std::pair<_Tp1, _Tp2>& __in) noexcept |
220 | { return __pair_get<_Int>::__get(__in); } |
221 | |
222 | template<std::size_t _Int, class _Tp1, class _Tp2> |
223 | constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&& |
224 | get(std::pair<_Tp1, _Tp2>&& __in) noexcept |
225 | { return __pair_get<_Int>::__move_get(std::move(__in)); } |
226 | |
227 | template<std::size_t _Int, class _Tp1, class _Tp2> |
228 | constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type& |
229 | get(const std::pair<_Tp1, _Tp2>& __in) noexcept |
230 | { return __pair_get<_Int>::__const_get(__in); } |
231 | |
232 | template<std::size_t _Int, class _Tp1, class _Tp2> |
233 | constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&& |
234 | get(const std::pair<_Tp1, _Tp2>&& __in) noexcept |
235 | { return __pair_get<_Int>::__const_move_get(std::move(__in)); } |
236 | |
237 | #if __cplusplus > 201103L |
238 | |
239 | #define __cpp_lib_tuples_by_type 201304 |
240 | |
241 | template <typename _Tp, typename _Up> |
242 | constexpr _Tp& |
243 | get(pair<_Tp, _Up>& __p) noexcept |
244 | { return __p.first; } |
245 | |
246 | template <typename _Tp, typename _Up> |
247 | constexpr const _Tp& |
248 | get(const pair<_Tp, _Up>& __p) noexcept |
249 | { return __p.first; } |
250 | |
251 | template <typename _Tp, typename _Up> |
252 | constexpr _Tp&& |
253 | get(pair<_Tp, _Up>&& __p) noexcept |
254 | { return std::move(__p.first); } |
255 | |
256 | template <typename _Tp, typename _Up> |
257 | constexpr const _Tp&& |
258 | get(const pair<_Tp, _Up>&& __p) noexcept |
259 | { return std::move(__p.first); } |
260 | |
261 | template <typename _Tp, typename _Up> |
262 | constexpr _Tp& |
263 | get(pair<_Up, _Tp>& __p) noexcept |
264 | { return __p.second; } |
265 | |
266 | template <typename _Tp, typename _Up> |
267 | constexpr const _Tp& |
268 | get(const pair<_Up, _Tp>& __p) noexcept |
269 | { return __p.second; } |
270 | |
271 | template <typename _Tp, typename _Up> |
272 | constexpr _Tp&& |
273 | get(pair<_Up, _Tp>&& __p) noexcept |
274 | { return std::move(__p.second); } |
275 | |
276 | template <typename _Tp, typename _Up> |
277 | constexpr const _Tp&& |
278 | get(const pair<_Up, _Tp>&& __p) noexcept |
279 | { return std::move(__p.second); } |
280 | |
281 | #define __cpp_lib_exchange_function 201304 |
282 | |
283 | /// Assign @p __new_val to @p __obj and return its previous value. |
284 | template <typename _Tp, typename _Up = _Tp> |
285 | inline _Tp |
286 | exchange(_Tp& __obj, _Up&& __new_val) |
287 | { return std::__exchange(__obj, std::forward<_Up>(__new_val)); } |
288 | #endif |
289 | |
290 | // Stores a tuple of indices. Used by tuple and pair, and by bind() to |
291 | // extract the elements in a tuple. |
292 | template<size_t... _Indexes> struct _Index_tuple { }; |
293 | |
294 | #ifdef __has_builtin |
295 | # if __has_builtin(__make_integer_seq) |
296 | # define _GLIBCXX_USE_MAKE_INTEGER_SEQ 1 |
297 | # endif |
298 | #endif |
299 | |
300 | // Builds an _Index_tuple<0, 1, 2, ..., _Num-1>. |
301 | template<size_t _Num> |
302 | struct _Build_index_tuple |
303 | { |
304 | #if _GLIBCXX_USE_MAKE_INTEGER_SEQ |
305 | template<typename, size_t... _Indices> |
306 | using _IdxTuple = _Index_tuple<_Indices...>; |
307 | |
308 | using __type = __make_integer_seq<_IdxTuple, size_t, _Num>; |
309 | #else |
310 | using __type = _Index_tuple<__integer_pack(_Num)...>; |
311 | #endif |
312 | }; |
313 | |
314 | #if __cplusplus > 201103L |
315 | |
316 | #define __cpp_lib_integer_sequence 201304 |
317 | |
318 | /// Class template integer_sequence |
319 | template<typename _Tp, _Tp... _Idx> |
320 | struct integer_sequence |
321 | { |
322 | typedef _Tp value_type; |
323 | static constexpr size_t size() noexcept { return sizeof...(_Idx); } |
324 | }; |
325 | |
326 | /// Alias template make_integer_sequence |
327 | template<typename _Tp, _Tp _Num> |
328 | using make_integer_sequence |
329 | #if _GLIBCXX_USE_MAKE_INTEGER_SEQ |
330 | = __make_integer_seq<integer_sequence, _Tp, _Num>; |
331 | #else |
332 | = integer_sequence<_Tp, __integer_pack(_Num)...>; |
333 | #endif |
334 | |
335 | #undef _GLIBCXX_USE_MAKE_INTEGER_SEQ |
336 | |
337 | /// Alias template index_sequence |
338 | template<size_t... _Idx> |
339 | using index_sequence = integer_sequence<size_t, _Idx...>; |
340 | |
341 | /// Alias template make_index_sequence |
342 | template<size_t _Num> |
343 | using make_index_sequence = make_integer_sequence<size_t, _Num>; |
344 | |
345 | /// Alias template index_sequence_for |
346 | template<typename... _Types> |
347 | using index_sequence_for = make_index_sequence<sizeof...(_Types)>; |
348 | #endif |
349 | |
350 | #if __cplusplus > 201402L |
351 | |
352 | struct in_place_t { |
353 | explicit in_place_t() = default; |
354 | }; |
355 | |
356 | inline constexpr in_place_t in_place{}; |
357 | |
358 | template<typename _Tp> struct in_place_type_t |
359 | { |
360 | explicit in_place_type_t() = default; |
361 | }; |
362 | |
363 | template<typename _Tp> |
364 | inline constexpr in_place_type_t<_Tp> in_place_type{}; |
365 | |
366 | template<size_t _Idx> struct in_place_index_t |
367 | { |
368 | explicit in_place_index_t() = default; |
369 | }; |
370 | |
371 | template<size_t _Idx> |
372 | inline constexpr in_place_index_t<_Idx> in_place_index{}; |
373 | |
374 | template<typename> |
375 | struct __is_in_place_type_impl : false_type |
376 | { }; |
377 | |
378 | template<typename _Tp> |
379 | struct __is_in_place_type_impl<in_place_type_t<_Tp>> : true_type |
380 | { }; |
381 | |
382 | template<typename _Tp> |
383 | struct __is_in_place_type |
384 | : public __is_in_place_type_impl<_Tp> |
385 | { }; |
386 | |
387 | #define __cpp_lib_as_const 201510 |
388 | template<typename _Tp> |
389 | constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; } |
390 | |
391 | template<typename _Tp> |
392 | void as_const(const _Tp&&) = delete; |
393 | |
394 | #endif // C++17 |
395 | |
396 | _GLIBCXX_END_NAMESPACE_VERSION |
397 | } // namespace |
398 | |
399 | #endif |
400 | |
401 | #endif /* _GLIBCXX_UTILITY */ |
402 | |