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

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

cpp: added exchange and partial swap definitions

  • 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 template<typename T>
40 bool operator!=(const T& lhs, const T& rhs)
41 {
42 return !(lhs == rhs);
43 }
44
45 template<typename T>
46 bool operator>(const T& lhs, const T& rhs)
47 {
48 return (rhs < lhs);
49 }
50
51 template<typename T>
52 bool operator<=(const T& lhs, const T& rhs)
53 {
54 return !(rhs < lhs);
55 }
56
57 template<typename T>
58 bool operator>=(const T& lhs, const T& rhs)
59 {
60 return !(lhs < rhs);
61 }
62
63 /**
64 * 20.2.2, swap:
65 */
66
67 template<class T>
68 void swap(T& x, T& y)
69 noexcept(is_nothrow_move_constructible<T>::value &&
70 is_nothrow_move_assignable<T>::value)
71 {
72 T tmp{std::move(x)};
73 x = std::move(y);
74 y = std::move(tmp);
75 }
76
77 template<class T, size_t N>
78 void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)))
79 {
80 // TODO: Use swap_ranges(a, a + N, b); when implemented.
81 }
82
83 /**
84 * 20.2.3, exchange:
85 */
86
87 template<class T, class U = T>
88 T exchange(T& obj, U&& new_val)
89 {
90 T old_val = std::move(obj);
91 obj = std::forward<U>(new_val);
92
93 return old_val;
94 }
95
96 /**
97 * 20.2.4, forward/move helpers:
98 */
99
100 template<class T>
101 inline constexpr T&& forward(remove_reference_t<T>& t) noexcept
102 {
103 return static_cast<T&&>(t);
104 }
105
106 template<class T>
107 inline constexpr T&& forward(remove_reference_t<T>&& t) noexcept
108 {
109 // TODO: check if t is lvalue reference, if it is, the program
110 // is ill-formed according to the standard
111 return static_cast<T&&>(t);
112 }
113
114 template<class T>
115 inline constexpr remove_reference_t<T>&& move(T&& t) noexcept
116 {
117 return static_cast<remove_reference_t<T>&&>(t);
118 }
119
120 /**
121 * 20.2.5, function template declval:
122 * Note: This function only needs declaration, not
123 * implementation.
124 */
125
126 template<class T>
127 add_rvalue_reference_t<T> declval() noexcept;
128
129 /**
130 * 20.3, pairs:
131 */
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.