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

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

cpp: added missing make_pair (and added todos for missed sections), moved type transformation to an auxiliary header

  • Property mode set to 100644
File size: 7.4 KB
Line 
1/*
2 * Copyright (c) 2017 Jaroslav Jindrak
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
32#include <cstdint>
33#include <internal/type_transformation.hpp>
34#include <type_traits>
35
36namespace std
37{
38 /**
39 * 20.2.1, operators:
40 */
41
42 namespace rel_ops
43 {
44 template<typename T>
45 bool operator!=(const T& lhs, const T& rhs)
46 {
47 return !(lhs == rhs);
48 }
49
50 template<typename T>
51 bool operator>(const T& lhs, const T& rhs)
52 {
53 return (rhs < lhs);
54 }
55
56 template<typename T>
57 bool operator<=(const T& lhs, const T& rhs)
58 {
59 return !(rhs < lhs);
60 }
61
62 template<typename T>
63 bool operator>=(const T& lhs, const T& rhs)
64 {
65 return !(lhs < rhs);
66 }
67 }
68
69 /**
70 * 20.2.4, forward/move helpers:
71 */
72
73 template<class T>
74 constexpr T&& forward(remove_reference_t<T>& t) noexcept
75 {
76 return static_cast<T&&>(t);
77 }
78
79 template<class T>
80 constexpr T&& forward(remove_reference_t<T>&& t) noexcept
81 {
82 return static_cast<T&&>(t);
83 }
84
85 template<class T>
86 constexpr remove_reference_t<T>&& move(T&& t) noexcept
87 {
88 return static_cast<remove_reference_t<T>&&>(t);
89 }
90
91 /**
92 * 20.2.2, swap:
93 */
94
95 template<class T>
96 void swap(T& x, T& y)
97 /* noexcept(is_nothrow_move_constructible<T>::value && */
98 /* is_nothrow_move_assignable<T>::value) */
99 {
100 T tmp{move(x)};
101 x = move(y);
102 y = move(tmp);
103 }
104
105 template<class T, size_t N>
106 void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)))
107 {
108 // TODO: Use swap_ranges(a, a + N, b); when implemented.
109 }
110
111 /**
112 * 20.2.3, exchange:
113 */
114
115 template<class T, class U = T>
116 T exchange(T& obj, U&& new_val)
117 {
118 T old_val = move(obj);
119 obj = forward<U>(new_val);
120
121 return old_val;
122 }
123
124 /**
125 * 20.2.5, function template declval:
126 * Note: This function only needs declaration, not
127 * implementation.
128 */
129
130 template<class T>
131 add_rvalue_reference_t<T> declval() noexcept;
132
133 /**
134 * 20.3, pairs:
135 */
136
137 struct piecewise_construct_t
138 {
139 explicit piecewise_construct_t() = default;
140 };
141
142 template<typename T1, typename T2>
143 struct pair
144 {
145 using first_type = T1;
146 using second_type = T2;
147
148 T1 first;
149 T2 second;
150
151 pair(const pair&) = default;
152 pair(pair&&) = default;
153
154 constexpr pair()
155 : first{}, second{}
156 { /* DUMMY BODY */ }
157
158 constexpr pair(const T1& x, const T2& y)
159 : first{x}, second{y}
160 { /* DUMMY BODY */ }
161
162 template<typename U, typename V>
163 constexpr pair(U&& x, V&& y)
164 : first(x), second(y)
165 { /* DUMMY BODY */ }
166
167 template<typename U, typename V>
168 constexpr pair(const pair<U, V>& other)
169 : first(other.first), second(other.second)
170 { /* DUMMY BODY */ }
171
172 template<typename U, typename V>
173 constexpr pair(pair<U, V>&& other)
174 : first(forward<first_type>(other.first)),
175 second(forward<second_type>(other.second))
176 { /* DUMMY BODY */ }
177
178 /* TODO: need tuple, piecewise_construct_t
179 template<class... Args1, class... Args2>
180 pair(piecewise_construct_t, tuple<Args1...> first_args, tuple<Args2...> second_args)
181 {
182 // TODO:
183 }
184 */
185
186 pair& operator=(const pair& other)
187 {
188 first = other.first;
189 second = other.second;
190
191 return *this;
192 }
193
194 template<typename U, typename V>
195 pair& operator=(const pair<U, V>& other)
196 {
197 first = other.first;
198 second = other.second;
199
200 return *this;
201 }
202
203 pair& operator=(pair&& other) noexcept
204 {
205 first = forward<first_type>(other.first);
206 second = forward<second_type>(other.second);
207
208 return *this;
209 }
210 };
211
212 /**
213 * 20.3.3, specialized algorithms:
214 */
215
216 // TODO: implement
217
218 template<class T1, class T2>
219 constexpr auto make_pair(T1&& t1, T2&& t2)
220 {
221 return pair<
222 aux::transform_tuple_types_t<T1>,
223 aux::transform_tuple_types_t<T2>
224 >{
225 forward<T1>(t1), forward<T2>(t2)
226 };
227 }
228
229 /**
230 * 20.3.4, tuple-like access to pair:
231 */
232
233 // TODO: implement
234
235 /**
236 * 20.5.2, class template integer_sequence:
237 */
238
239 template<class T, T... Is>
240 struct integer_sequence
241 {
242 using value_type = T;
243
244 static constexpr size_t size() noexcept
245 {
246 return sizeof...(Is);
247 }
248
249 using next = integer_sequence<T, Is..., sizeof...(Is)>;
250 };
251
252 template<std::size_t... Is>
253 using index_sequence = integer_sequence<std::size_t, Is...>;
254
255 /**
256 * 20.5.3, alias template make_integer_sequence:
257 */
258
259 namespace aux
260 {
261 template<class T, uintmax_t N>
262 struct make_integer_sequence
263 {
264 /**
265 * Recursive to the bottom case below, appends sizeof...(Is) in
266 * every next "call", building the sequence.
267 */
268 using type = typename make_integer_sequence<T, N - 1>::type::next;
269 };
270
271 template<class T>
272 struct make_integer_sequence<T, std::uintmax_t(0)>
273 {
274 using type = integer_sequence<T>;
275 };
276 }
277
278
279 /**
280 * Problem: We can't specialize the N parameter because it is a value parameter
281 * depending on a type parameter.
282 * Solution: According to the standard: if N is negative, the program is ill-formed,
283 * so we just recast it to uintmax_t :)
284 */
285 template<class T, T N>
286 using make_integer_sequence = typename aux::make_integer_sequence<T, std::uintmax_t(N)>::type;
287
288 template<size_t N>
289 using make_index_sequence = make_integer_sequence<std::size_t, N>;
290}
291
292#endif
Note: See TracBrowser for help on using the repository browser.