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

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

cpp: temporarily removed noexcept on swap due to a missing requirement metafunction

  • Property mode set to 100644
File size: 5.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 <type_traits>
33
34namespace std
35{
36 /**
37 * 20.2.1, operators:
38 */
39
40 template<typename T>
41 bool operator!=(const T& lhs, const T& rhs)
42 {
43 return !(lhs == rhs);
44 }
45
46 template<typename T>
47 bool operator>(const T& lhs, const T& rhs)
48 {
49 return (rhs < lhs);
50 }
51
52 template<typename T>
53 bool operator<=(const T& lhs, const T& rhs)
54 {
55 return !(rhs < lhs);
56 }
57
58 template<typename T>
59 bool operator>=(const T& lhs, const T& rhs)
60 {
61 return !(lhs < rhs);
62 }
63
64 /**
65 * 20.2.4, forward/move helpers:
66 */
67
68 template<class T>
69 inline constexpr T&& forward(remove_reference_t<T>& t) noexcept
70 {
71 return static_cast<T&&>(t);
72 }
73
74 template<class T>
75 inline constexpr T&& forward(remove_reference_t<T>&& t) noexcept
76 {
77 // TODO: check if t is lvalue reference, if it is, the program
78 // is ill-formed according to the standard
79 return static_cast<T&&>(t);
80 }
81
82 template<class T>
83 inline constexpr remove_reference_t<T>&& move(T&& t) noexcept
84 {
85 return static_cast<remove_reference_t<T>&&>(t);
86 }
87
88 /**
89 * 20.2.2, swap:
90 */
91
92 template<class T>
93 void swap(T& x, T& y)
94 /* noexcept(is_nothrow_move_constructible<T>::value && */
95 /* is_nothrow_move_assignable<T>::value) */
96 {
97 T tmp{move(x)};
98 x = move(y);
99 y = move(tmp);
100 }
101
102 template<class T, size_t N>
103 void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)))
104 {
105 // TODO: Use swap_ranges(a, a + N, b); when implemented.
106 }
107
108 /**
109 * 20.2.3, exchange:
110 */
111
112 template<class T, class U = T>
113 T exchange(T& obj, U&& new_val)
114 {
115 T old_val = move(obj);
116 obj = forward<U>(new_val);
117
118 return old_val;
119 }
120
121 /**
122 * 20.2.5, function template declval:
123 * Note: This function only needs declaration, not
124 * implementation.
125 */
126
127 template<class T>
128 add_rvalue_reference_t<T> declval() noexcept;
129
130 /**
131 * 20.3, pairs:
132 */
133
134 struct piecewise_construct_t
135 {
136 explicit piecewise_construct_t() = default;
137 };
138
139 template<typename T1, typename T2>
140 struct pair
141 {
142 using first_type = T1;
143 using second_type = T2;
144
145 T1 first;
146 T2 second;
147
148 pair(const pair&) = default;
149 pair(pair&&) = default;
150
151 constexpr pair()
152 : first{}, second{}
153 { /* DUMMY BODY */ }
154
155 constexpr pair(const T1& x, const T2& y)
156 : first{x}, second{y}
157 { /* DUMMY BODY */ }
158
159 template<typename U, typename V>
160 constexpr pair(U&& x, V&& y)
161 : first(x), second(y)
162 { /* DUMMY BODY */ }
163
164 template<typename U, typename V>
165 constexpr pair(const pair<U, V>& other)
166 : first(other.first), second(other.second)
167 { /* DUMMY BODY */ }
168
169 template<typename U, typename V>
170 constexpr pair(pair<U, V>&& other)
171 : first(forward<first_type>(other.first)),
172 second(forward<second_type>(other.second))
173 { /* DUMMY BODY */ }
174
175 /* TODO: need tuple, piecewise_construct_t
176 template<class... Args1, class... Args2>
177 pair(piecewise_construct_t, tuple<Args1...> first_args, tuple<Args2...> second_args)
178 {
179 // TODO:
180 }
181 */
182
183 pair& operator=(const pair& other)
184 {
185 first = other.first;
186 second = other.second;
187
188 return *this;
189 }
190
191 template<typename U, typename V>
192 pair& operator=(const pair<U, V>& other)
193 {
194 first = other.first;
195 second = other.second;
196
197 return *this;
198 }
199
200 pair& operator=(pair&& other) noexcept
201 {
202 first = forward<first_type>(other.first);
203 second = forward<second_type>(other.second);
204
205 return *this;
206 }
207 };
208}
209
210#endif
Note: See TracBrowser for help on using the repository browser.