source: mainline/uspace/lib/cpp/include/impl/utility.hpp@ b024c0c9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b024c0c9 was c866a83, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: split too big files into smaller (loosely related) sub files

  • Property mode set to 100644
File size: 11.4 KB
RevLine 
[b1cd380c]1/*
[9594c0c6]2 * Copyright (c) 2018 Jaroslav Jindrak
[b1cd380c]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_UTILITY
30#define LIBCPP_UTILITY
31
[de53138]32#include <cstdint>
[7a666789]33#include <internal/type_transformation.hpp>
[c866a83]34#include <internal/utility/forward_move.hpp>
[2084bfcd]35#include <type_traits>
36
[b1cd380c]37namespace std
38{
39 /**
40 * 20.2.1, operators:
41 */
[aab972f]42
[f041811]43 namespace rel_ops
[b1cd380c]44 {
[f041811]45 template<typename T>
46 bool operator!=(const T& lhs, const T& rhs)
47 {
48 return !(lhs == rhs);
49 }
[b1cd380c]50
[f041811]51 template<typename T>
52 bool operator>(const T& lhs, const T& rhs)
53 {
54 return (rhs < lhs);
55 }
[b1cd380c]56
[f041811]57 template<typename T>
58 bool operator<=(const T& lhs, const T& rhs)
59 {
60 return !(rhs < lhs);
61 }
[b1cd380c]62
[f041811]63 template<typename T>
64 bool operator>=(const T& lhs, const T& rhs)
65 {
66 return !(lhs < rhs);
67 }
[b1cd380c]68 }
69
[5abc7fd]70 /**
71 * 20.2.2, swap:
72 */
73
74 template<class T>
75 void swap(T& x, T& y)
[18944e0]76 /* noexcept(is_nothrow_move_constructible<T>::value && */
77 /* is_nothrow_move_assignable<T>::value) */
[5abc7fd]78 {
[add816c7]79 T tmp{move(x)};
80 x = move(y);
81 y = move(tmp);
[5abc7fd]82 }
83
[9594c0c6]84 template<class F1, class F2>
85 F2 swap_ranges(F1, F1, F2);
86
[5abc7fd]87 template<class T, size_t N>
88 void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)))
89 {
[9594c0c6]90 swap_ranges(a, a + N, b);
[5abc7fd]91 }
92
93 /**
94 * 20.2.3, exchange:
95 */
96
97 template<class T, class U = T>
98 T exchange(T& obj, U&& new_val)
99 {
[add816c7]100 T old_val = move(obj);
101 obj = forward<U>(new_val);
[5abc7fd]102
103 return old_val;
104 }
[b1cd380c]105
[aa0fa86a]106 /**
107 * 20.5.2, class template integer_sequence:
108 */
109
110 template<class T, T... Is>
111 struct integer_sequence
112 {
113 using value_type = T;
114
115 static constexpr size_t size() noexcept
116 {
117 return sizeof...(Is);
118 }
119
120 using next = integer_sequence<T, Is..., sizeof...(Is)>;
121 };
122
123 template<std::size_t... Is>
124 using index_sequence = integer_sequence<std::size_t, Is...>;
125
126 /**
127 * 20.5.3, alias template make_integer_sequence:
128 */
129
130 namespace aux
131 {
132 template<class T, uintmax_t N>
133 struct make_integer_sequence
134 {
135 /**
136 * Recursive to the bottom case below, appends sizeof...(Is) in
137 * every next "call", building the sequence.
138 */
139 using type = typename make_integer_sequence<T, N - 1>::type::next;
140 };
141
142 template<class T>
143 struct make_integer_sequence<T, std::uintmax_t(0)>
144 {
145 using type = integer_sequence<T>;
146 };
147 }
148
149
150 /**
151 * Problem: We can't specialize the N parameter because it is a value parameter
152 * depending on a type parameter.
153 * Solution: According to the standard: if N is negative, the program is ill-formed,
154 * so we just recast it to uintmax_t :)
155 */
156 template<class T, T N>
157 using make_integer_sequence = typename aux::make_integer_sequence<T, std::uintmax_t(N)>::type;
158
159 template<size_t N>
160 using make_index_sequence = make_integer_sequence<std::size_t, N>;
161
[b1cd380c]162 /**
163 * 20.3, pairs:
164 */
165
[aa0fa86a]166 template<size_t, class>
167 class tuple_element;
168
169 template<size_t I, class T>
170 using tuple_element_t = typename tuple_element<I, T>::type;
171
172 template<class...>
173 class tuple;
174
175 template<size_t I, class... Ts>
176 constexpr tuple_element_t<I, tuple<Ts...>>&& get(tuple<Ts...>&&) noexcept;
177
178 namespace aux
179 {
180 template<class T, class... Args, size_t... Is>
181 T from_tuple(tuple<Args...>&& tpl, index_sequence<Is...>)
182 {
183 return T{get<Is>(move(tpl))...};
184 }
185
186 template<class T, class... Args>
187 T from_tuple(tuple<Args...>&& tpl)
188 {
189 return from_tuple<T>(move(tpl), make_index_sequence<sizeof...(Args)>{});
190 }
191 }
192
[b1cd380c]193 struct piecewise_construct_t
194 {
195 explicit piecewise_construct_t() = default;
196 };
197
[aa0fa86a]198 inline constexpr piecewise_construct_t piecewise_construct{};
199
[b1cd380c]200 template<typename T1, typename T2>
201 struct pair
202 {
203 using first_type = T1;
204 using second_type = T2;
205
206 T1 first;
207 T2 second;
208
209 pair(const pair&) = default;
210 pair(pair&&) = default;
211
212 constexpr pair()
213 : first{}, second{}
214 { /* DUMMY BODY */ }
215
216 constexpr pair(const T1& x, const T2& y)
217 : first{x}, second{y}
218 { /* DUMMY BODY */ }
219
220 template<typename U, typename V>
221 constexpr pair(U&& x, V&& y)
222 : first(x), second(y)
223 { /* DUMMY BODY */ }
224
225 template<typename U, typename V>
226 constexpr pair(const pair<U, V>& other)
227 : first(other.first), second(other.second)
228 { /* DUMMY BODY */ }
229
230 template<typename U, typename V>
231 constexpr pair(pair<U, V>&& other)
232 : first(forward<first_type>(other.first)),
233 second(forward<second_type>(other.second))
234 { /* DUMMY BODY */ }
235
236 template<class... Args1, class... Args2>
237 pair(piecewise_construct_t, tuple<Args1...> first_args, tuple<Args2...> second_args)
[aa0fa86a]238 : first{aux::from_tuple<first_type>(move(first_args))},
239 second{aux::from_tuple<second_type>(move(second_args))}
240 { /* DUMMY BODY */ }
[b1cd380c]241
242 pair& operator=(const pair& other)
243 {
244 first = other.first;
245 second = other.second;
246
247 return *this;
248 }
249
250 template<typename U, typename V>
251 pair& operator=(const pair<U, V>& other)
252 {
253 first = other.first;
254 second = other.second;
255
256 return *this;
257 }
258
259 pair& operator=(pair&& other) noexcept
260 {
261 first = forward<first_type>(other.first);
262 second = forward<second_type>(other.second);
263
264 return *this;
265 }
266 };
[82ef902]267
[7a666789]268 /**
269 * 20.3.3, specialized algorithms:
270 */
271
[82d256e]272 template<class T1, class T2>
273 constexpr bool operator==(const pair<T1, T2>& lhs,
274 const pair<T1, T2>& rhs)
275 {
276 return lhs.first == rhs.first && lhs.second == rhs.second;
277 }
278
279 template<class T1, class T2>
280 constexpr bool operator<(const pair<T1, T2>& lhs,
281 const pair<T1, T2>& rhs)
282 {
283 return lhs.first < rhs.first ||
284 (!(rhs.first < lhs.first) && lhs.second < rhs.second);
285 }
286
287 template<class T1, class T2>
288 constexpr bool operator!=(const pair<T1, T2>& lhs,
289 const pair<T1, T2>& rhs)
290 {
291 return !(lhs == rhs);
292 }
293
294 template<class T1, class T2>
295 constexpr bool operator>(const pair<T1, T2>& lhs,
296 const pair<T1, T2>& rhs)
297 {
298 return rhs < lhs;
299 }
300
301 template<class T1, class T2>
302 constexpr bool operator>=(const pair<T1, T2>& lhs,
303 const pair<T1, T2>& rhs)
304 {
305 return !(lhs < rhs);
306 }
307
308 template<class T1, class T2>
309 constexpr bool operator<=(const pair<T1, T2>& lhs,
310 const pair<T1, T2>& rhs)
311 {
312 return !(rhs < lhs);
313 }
314
315 template<class T1, class T2>
316 constexpr void swap(pair<T1, T2>& lhs, pair<T1, T2>& rhs)
317 noexcept(noexcept(lhs.swap(rhs)))
318 {
319 lhs.swap(rhs);
320 }
[7a666789]321
322 template<class T1, class T2>
323 constexpr auto make_pair(T1&& t1, T2&& t2)
324 {
325 return pair<
326 aux::transform_tuple_types_t<T1>,
327 aux::transform_tuple_types_t<T2>
328 >{
329 forward<T1>(t1), forward<T2>(t2)
330 };
331 }
332
333 /**
334 * 20.3.4, tuple-like access to pair:
335 */
336
[016d86e]337 template<class>
338 struct tuple_size;
339
340 template<class T1, class T2>
341 struct tuple_size<pair<T1, T2>>
342 : integral_constant<size_t, 2>
343 { /* DUMMY BODY */ };
344
345 template<size_t, class>
346 struct tuple_element;
347
348 template<class T1, class T2>
349 struct tuple_element<0, pair<T1, T2>>
350 : aux::type_is<T1>
351 { /* DUMMY BODY */ };
352
353 template<class T1, class T2>
354 struct tuple_element<1, pair<T1, T2>>
355 : aux::type_is<T2>
356 { /* DUMMY BODY */ };
357
358 template<size_t I, class T>
359 using tuple_element_t = typename tuple_element<I, T>::type;
360
361 template<size_t I, class T1, class T2>
362 constexpr tuple_element_t<I, pair<T1, T2>>&
363 get(pair<T1, T2>& p) noexcept
364 {
365 if constexpr (I == 0)
366 return p.first;
367 else
368 return p.second;
369 }
370
371 template<size_t I, class T1, class T2>
372 constexpr const tuple_element_t<I, pair<T1, T2>>&
373 get(const pair<T1, T2>& p) noexcept
374 {
375 if constexpr (I == 0)
376 return p.first;
377 else
378 return p.second;
379 }
380
381 template<size_t I, class T1, class T2>
382 constexpr tuple_element_t<I, pair<T1, T2>>&&
383 get(pair<T1, T2>&& p) noexcept
384 {
385 if constexpr (I == 0)
386 return forward<T1>(p.first);
387 else
388 return forward<T2>(p.second);
389 }
390
391 template<class T, class U>
392 constexpr T& get(pair<T, U>& p) noexcept
393 {
394 static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
395
396 return get<0>(p);
397 }
398
399 template<class T, class U>
400 constexpr const T& get(const pair<T, U>& p) noexcept
401 {
402 static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
403
404 return get<0>(p);
405 }
406
407 template<class T, class U>
408 constexpr T&& get(pair<T, U>&& p) noexcept
409 {
410 static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
411
412 return get<0>(move(p));
413 }
414
415 template<class T, class U>
416 constexpr T& get(pair<U, T>& p) noexcept
417 {
418 static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
419
420 return get<1>(p);
421 }
422
423 template<class T, class U>
424 constexpr const T& get(const pair<U, T>& p) noexcept
425 {
426 static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
427
428 return get<1>(p);
429 }
430
431 template<class T, class U>
432 constexpr T&& get(pair<U, T>&& p) noexcept
433 {
434 static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
435
436 return get<1>(move(p));
437 }
[b1cd380c]438}
439
440#endif
Note: See TracBrowser for help on using the repository browser.