-
Notifications
You must be signed in to change notification settings - Fork 3
/
error_or.hpp
257 lines (214 loc) · 9.87 KB
/
error_or.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2012 Andrew C. Morrow
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef included_108fbb04_f426_414d_9edd_b4d2af941e96
#define included_108fbb04_f426_414d_9edd_b4d2af941e96
#include <cassert>
#include <initializer_list>
#include <memory>
#include <system_error>
#include <type_traits>
#include "detail/is_nothrow_swappable.hpp"
namespace acm {
template<typename E, typename T>
class error_or final {
public:
using error_type = E;
using value_type = T;
private:
static bool constexpr is_nothrow_swappable =
detail::is_nothrow_swappable<error_type>::value and
detail::is_nothrow_swappable<value_type>::value and
std::is_nothrow_move_constructible<error_type>::value and
std::is_nothrow_move_constructible<value_type>::value and
std::is_nothrow_destructible<error_type>::value and
std::is_nothrow_destructible<value_type>::value;
public:
inline error_or() noexcept(std::is_nothrow_default_constructible<value_type>::value) {
new(&val_.value) value_type;
}
inline error_or(error_type error) noexcept(std::is_nothrow_move_constructible<error_type>::value)
: ok_(false) {
assert(error);
new(&val_.error) error_type(std::move(error));
}
inline error_or(value_type value) noexcept(std::is_nothrow_move_constructible<value_type>::value) {
new(&val_.value) value_type(std::move(value));
}
template<typename U = value_type>
inline error_or(std::initializer_list<typename U::value_type> values) noexcept(std::is_nothrow_constructible<U, std::initializer_list<typename U::value_type>>::value) {
new(&val_.value) value_type(values);
}
inline error_or(error_or const& other) noexcept(std::is_nothrow_copy_constructible<error_type>::value and
std::is_nothrow_copy_constructible<value_type>::value) {
if (other.ok_)
new(&val_.value) value_type(other.val_.value);
else
new(&val_.error) error_type(other.val_.error);
ok_ = other.ok_;
}
inline error_or(error_or&& other) noexcept((std::is_nothrow_move_constructible<error_type>::value or
std::is_nothrow_copy_constructible<error_type>::value) and
(std::is_nothrow_move_constructible<value_type>::value or
std::is_nothrow_copy_constructible<value_type>::value)) {
if (other.ok_)
new(&val_.value) value_type(std::move_if_noexcept(other.val_.value));
else
new(&val_.error) error_type(std::move_if_noexcept(other.val_.error));
ok_ = other.ok_;
}
template<typename U>
inline error_or(error_or<error_type, U> const& other) noexcept(std::is_nothrow_copy_constructible<error_type>::value and
std::is_nothrow_copy_constructible<value_type>::value) {
if (other.ok_)
new(&val_.value) value_type(other.val_.value);
else
new(&val_.error) error_type(other.val_.error);
ok_ = other.ok_;
}
template<typename U>
inline error_or(error_or<error_type, U>&& other) noexcept((std::is_nothrow_move_constructible<error_type>::value or
std::is_nothrow_copy_constructible<error_type>::value) and
(std::is_nothrow_constructible<value_type, typename std::add_rvalue_reference<U>::type>::value or
std::is_nothrow_constructible<value_type, typename std::add_lvalue_reference<U>::type>::value)) {
if (other.ok_)
new(&val_.value) value_type(move_if_noexcept_from(other.val_.value));
else
new(&val_.error) error_type(std::move_if_noexcept(other.val_.error));
ok_ = other.ok_;
}
void swap(error_or& other) noexcept(is_nothrow_swappable) {
other.sfinae_swap(*this);
}
friend inline void swap(error_or& a, error_or& b) noexcept(is_nothrow_swappable) {
a.swap(b);
}
inline error_or& operator=(error_type error) noexcept(std::is_nothrow_constructible<error_or, typename std::add_rvalue_reference<error_type>::type>::value) {
error_or(std::move(error)).swap(*this);
return *this;
}
inline error_or& operator=(value_type value) noexcept(std::is_nothrow_constructible<error_or, typename std::add_rvalue_reference<value_type>::type>::value) {
error_or(std::move(value)).swap(*this);
return *this;
}
inline error_or& operator=(error_or other) noexcept(std::is_nothrow_move_constructible<error_or>::value) {
error_or(std::move(other)).swap(*this);
return *this;
}
template<typename U>
inline error_or& operator=(error_or<error_type, U> other) noexcept(std::is_nothrow_constructible<error_or, error_or<E, U>&&>::value) {
error_or(std::move(other)).swap(*this);
return *this;
}
inline ~error_or() noexcept(std::is_nothrow_destructible<error_type>::value and
std::is_nothrow_destructible<value_type>::value) {
if (ok_)
val_.value.~value_type();
else
val_.error.~error_type();
}
inline bool ok() const noexcept {
return ok_;
}
inline explicit operator bool() const noexcept {
return ok_;
}
inline error_type const& error() const noexcept {
assert(!ok_);
return val_.error;
}
inline value_type& value() noexcept {
assert(ok_);
return val_.value;
}
inline value_type const& value() const noexcept {
assert(ok_);
return val_.value;
}
inline error_type&& release_error() noexcept {
assert(!ok_);
return std::move(val_.error);
}
inline value_type&& release_value() noexcept {
assert(ok_);
return std::move(val_.value);
}
private:
union val {
// The lifecycle of members is handled by the enclosing class.
inline val() noexcept {}
inline ~val() noexcept {}
value_type value;
error_type error;
} val_;
bool ok_ = true;
template<typename U = value_type>
typename std::enable_if<error_or<error_type, U>::is_nothrow_swappable>::type sfinae_swap(error_or& other) noexcept {
using std::swap;
if (ok_ == other.ok_) {
if (ok_)
swap(val_.value, other.val_.value);
else
swap(val_.error, other.val_.error);
} else {
if (ok_) {
new(&val_.error) error_type(std::move(other.val_.error));
new(&other.val_.value) value_type(std::move(val_.value));
val_.value.~value_type();
other.val_.error.~error_type();
} else {
new(&other.val_.error) error_type(std::move(val_.error));
new(&val_.value) value_type(std::move(other.val_.value));
other.val_.value.~value_type();
val_.error.~error_type();
}
swap(ok_, other.ok_);
}
}
template<typename U>
typename std::conditional
<
!std::is_nothrow_constructible<value_type, typename std::add_rvalue_reference<U>::type>::value and std::is_constructible<U, typename std::add_lvalue_reference<U>::type>::value,
U const&,
U&&
>::type
move_if_noexcept_from(U& u) {
return std::move(u);
}
};
template<typename E1, typename T1, typename E2, typename T2>
bool operator==(error_or<E1, T1> const& lhs, error_or<E2, T2> const& rhs) noexcept(noexcept(lhs.value() == rhs.value()) and
noexcept(lhs.error() == rhs.error())) {
if (lhs.ok() == rhs.ok()) {
if (lhs.ok()) {
return lhs.value() == rhs.value();
} else {
return lhs.error() == rhs.error();
}
}
return false;
}
template<typename E1, typename T1, typename E2, typename T2>
bool operator!=(error_or<E1, T1> const& lhs, error_or<E2, T2> const& rhs) noexcept(noexcept(lhs == rhs)) {
return not lhs == rhs;
}
template<typename T>
using error_code_or = error_or<std::error_code, T>;
template<typename T>
using error_condition_or = error_or<std::error_condition, T>;
template<typename T>
using error_code_or_unique = error_code_or<std::unique_ptr<T>>;
template<typename T>
using error_condition_or_unique = error_condition_or<std::unique_ptr<T>>;
} // namespace acm
#endif // included_108fbb04_f426_414d_9edd_b4d2af941e96