Debian Codegraph @

src/common/expect.h

raw symbols SCIP
1 // Copyright (c) 2018-2022, The Monero Project
2
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #pragma once
31
32 #include <cassert>
33 #include <system_error>
34 #include <type_traits>
35 #include <utility>
36
37 #include "common/error.h"
38
39 //! If precondition fails, return `::error::kInvalidArgument` in current scope.
40 #define MONERO_PRECOND(...) \
41 do \
42 { \
43 if (!( __VA_ARGS__ )) \
44 return {::common_error::kInvalidArgument}; \
45 } while (0)
46
47 //! Check `expect<void>` and return errors in current scope.
48 #define MONERO_CHECK(...) \
49 do \
50 { \
51 const ::expect<void> result = __VA_ARGS__ ; \
52 if (!result) \
53 return result.error(); \
54 } while (0)
55
56 /*! Get `T` from `expect<T>` by `std::move` as-if by function call.
57 `expect<void>` returns nothing.
58
59 \throw std::system_error with `expect<T>::error()`, filename and line
60 number when `expect<T>::has_error() == true`.*/
61 #define MONERO_UNWRAP(...) \
62 ::detail::expect::unwrap( __VA_ARGS__ , nullptr, __FILE__ , __LINE__ )
63
64 /* \throw std::system_error with `code` and `msg` as part of the details. The
65 filename and line number will automatically be injected into the explanation
66 string. `code` can be any enum convertible to `std::error_code`. */
67 #define MONERO_THROW(code, msg) \
68 ::detail::expect::throw_( code , msg , __FILE__ , __LINE__ )
69
70
71 template<typename> class expect;
72
73 namespace detail
74 {
75 // Shortens the characters in the places that `enable_if` is used below.
76 template<bool C>
77 using enable_if = typename std::enable_if<C>::type;
78
79 struct expect
80 {
81 //! \throw std::system_error with `ec`, optional `msg` and/or optional `file` + `line`.
82 static void throw_(std::error_code ec, const char* msg, const char* file, unsigned line);
83
84 //! If `result.has_error()` call `throw_`. Otherwise, \return `*result` by move.
85 template<typename T>
86 static T unwrap(::expect<T>&& result, const char* error_msg, const char* file, unsigned line)
87 {
88 if (!result)
89 throw_(result.error(), error_msg, file, line);
90 return std::move(*result);
91 }
92
93 //! If `result.has_error()` call `throw_`.
94 static void unwrap(::expect<void>&& result, const char* error_msg, const char* file, unsigned line);
95 };
96 }
97
98 /*!
99 `expect<T>` is a value or error implementation, similar to Rust std::result
100 or various C++ proposals (boost::expected, boost::outcome). This
101 implementation currently has a strict error type, `std::error_code`, and a
102 templated value type `T`. `expect<T>` is implicitly convertible from `T`
103 or `std::error_code`, and one `expect<T>` object type is implicitly
104 convertible to another `expect<U>` object iff the destination value type
105 can be implicitly constructed from the source value type (i.e.
106 `struct U { ... U(T src) { ...} ... };`).
107
108 `operator==` and `operator!=` are the only comparison operators provided;
109 comparison between different value types is allowed provided the two values
110 types have a `operator==` defined between them (i.e.
111 `assert(expect<int>{100} == expect<short>{100});`). Comparisons can also be
112 done against `std::error_code` objects or error code enums directly (i.e.
113 `assert(expect<int>{make_error_code(common_error::kInvalidArgument)} == error::kInvalidArgument)`).
114 Comparison of default constructed `std::error_code` will always fail.
115 "Generic" comparisons can be done with `std::error_condition` via the `matches`
116 method only (i.e.
117 `assert(expect<int>{make_error_code{common_error::kInvalidErrorCode}.matches(std::errc::invalid_argument))`),
118 `operator==` and `operator!=` will not work with `std::errc` or
119 `std::error_condition`. A comparison with `matches` is more expensive
120 because an equivalency between error categories is computed, but is
121 recommended when an error can be one of several categories (this is going
122 to be the case in nearly every situation when calling a function from
123 another C++ struct/class).
124
125 `expect<void>` is a special case with no stored value. It is used by
126 functions that can fail, but otherwise would return `void`. It is useful
127 for consistency; all macros, standalone functions, and comparison operators
128 work with `expect<void>`.
129
130 \note See `src/common/error.h` for creating a custom error enum.
131 */
132 template<typename T>
133 class expect
134 {
135 static_assert(std::is_nothrow_destructible<T>(), "T must have a nothrow destructor");
136
137 template<typename U>
138 static constexpr bool is_convertible() noexcept
139 {
140 return std::is_constructible<T, U>() &&
141 std::is_convertible<U, T>();
142 }
143
144 // MEMBERS
145 std::error_code code_;
146 typename std::aligned_storage<sizeof(T), alignof(T)>::type storage_;
147 // MEMBERS
148
149 T& get() noexcept
150 {
151 assert(has_value());
152 return *reinterpret_cast<T*>(std::addressof(storage_));
153 }
154
155 T const& get() const noexcept
156 {
157 assert(has_value());
158 return *reinterpret_cast<T const*>(std::addressof(storage_));
159 }
160
161 template<typename U>
162 void store(U&& value) noexcept(std::is_nothrow_constructible<T, U>())
163 {
164 new (std::addressof(storage_)) T{std::forward<U>(value)};
165 code_ = std::error_code{};
166 }
167
168 void maybe_throw() const
169 {
170 if (has_error())
171 ::detail::expect::throw_(error(), nullptr, nullptr, 0);
172 }
173
174 public:
175 using value_type = T;
176 using error_type = std::error_code;
177
178 expect() = delete;
179
180 /*! Store an error, `code`, in the `expect` object. If `code` creates a
181 `std::error_code` object whose `.value() == 0`, then `error()` will be set
182 to `::common_error::kInvalidErrorCode`. */
183 expect(std::error_code const& code) noexcept
184 : code_(code), storage_()
185 {
186 if (!has_error())
187 code_ = ::common_error::kInvalidErrorCode;
188 }
189
190 //! Store a value, `val`, in the `expect` object.
191 expect(T val) noexcept(std::is_nothrow_move_constructible<T>())
192 : code_(), storage_()
193 {
194 store(std::move(val));
195 }
196
197 expect(expect const& src) noexcept(std::is_nothrow_copy_constructible<T>())
198 : code_(src.error()), storage_()
199 {
200 if (src.has_value())
201 store(src.get());
202 }
203
204 //! Copy conversion from `U` to `T`.
205 template<typename U, typename = detail::enable_if<is_convertible<U const&>()>>
206 expect(expect<U> const& src) noexcept(std::is_nothrow_constructible<T, U const&>())
207 : code_(src.error()), storage_()
208 {
209 if (src.has_value())
210 store(*src);
211 }
212
213 expect(expect&& src) noexcept(std::is_nothrow_move_constructible<T>())
214 : code_(src.error()), storage_()
215 {
216 if (src.has_value())
217 store(std::move(src.get()));
218 }
219
220 //! Move conversion from `U` to `T`.
221 template<typename U, typename = detail::enable_if<is_convertible<U>()>>
222 expect(expect<U>&& src) noexcept(std::is_nothrow_constructible<T, U>())
223 : code_(src.error()), storage_()
224 {
225 if (src.has_value())
226 store(std::move(*src));
227 }
228
229 ~expect() noexcept
230 {
231 if (has_value())
232 get().~T();
233 }
234
235 expect& operator=(expect const& src) noexcept(std::is_nothrow_copy_constructible<T>() && std::is_nothrow_copy_assignable<T>())
236 {
237 if (this != std::addressof(src))
238 {
239 if (has_value() && src.has_value())
240 get() = src.get();
241 else if (has_value())
242 get().~T();
243 else if (src.has_value())
244 store(src.get());
245 code_ = src.error();
246 }
247 return *this;
248 }
249
250 /*! Move `src` into `this`. If `src.has_value() && addressof(src) != this`
251 then `src.value() will be in a "moved from state". */
252 expect& operator=(expect&& src) noexcept(std::is_nothrow_move_constructible<T>() && std::is_nothrow_move_assignable<T>())
253 {
254 if (this != std::addressof(src))
255 {
256 if (has_value() && src.has_value())
257 get() = std::move(src.get());
258 else if (has_value())
259 get().~T();
260 else if (src.has_value())
261 store(std::move(src.get()));
262 code_ = src.error();
263 }
264 return *this;
265 }
266
267 //! \return True if `this` is storing a value instead of an error.
268 explicit operator bool() const noexcept { return has_value(); }
269
270 //! \return True if `this` is storing an error instead of a value.
271 bool has_error() const noexcept { return bool(code_); }
272
273 //! \return True if `this` is storing a value instead of an error.
274 bool has_value() const noexcept { return !has_error(); }
275
276 //! \return Error - always safe to call. Empty when `!has_error()`.
277 std::error_code error() const noexcept { return code_; }
278
279 //! \return Value if `has_value()` otherwise \throw `std::system_error{error()}`.
280 T& value() &
281 {
282 maybe_throw();
283 return get();
284 }
285
286 //! \return Value if `has_value()` otherwise \throw `std::system_error{error()}`.
287 T const& value() const &
288 {
289 maybe_throw();
290 return get();
291 }
292
293 /*! Same as other overloads, but expressions such as `foo(bar().value())`
294 will automatically perform moves with no copies. */
295 T&& value() &&
296 {
297 maybe_throw();
298 return std::move(get());
299 }
300
301 //! \return Value, \pre `has_value()`.
302 T* operator->() noexcept { return std::addressof(get()); }
303 //! \return Value, \pre `has_value()`.
304 T const* operator->() const noexcept { return std::addressof(get()); }
305 //! \return Value, \pre `has_value()`.
306 T& operator*() noexcept { return get(); }
307 //! \return Value, \pre `has_value()`.
308 T const& operator*() const noexcept { return get(); }
309
310 /*!
311 \note This function is `noexcept` when `U == T` is `noexcept`.
312 \return True if `has_value() == rhs.has_value()` and if values or errors are equal.
313 */
314 template<typename U>
315 bool equal(expect<U> const& rhs) const noexcept(noexcept(*std::declval<expect<T>>() == *rhs))
316 {
317 return has_value() && rhs.has_value() ?
318 get() == *rhs : error() == rhs.error();
319 }
320
321 //! \return False if `has_value()`, otherwise `error() == rhs`.
322 bool equal(std::error_code const& rhs) const noexcept
323 {
324 return has_error() && error() == rhs;
325 }
326
327 /*!
328 \note This function is `noexcept` when `U == T` is `noexcept`.
329 \return False if `has_error()`, otherwise `value() == rhs`.
330 */
331 template<typename U, typename = detail::enable_if<!std::is_constructible<std::error_code, U>::value>>
332 bool equal(U const& rhs) const noexcept(noexcept(*std::declval<expect<T>>() == rhs))
333 {
334 return has_value() && get() == rhs;
335 }
336
337 //! \return False if `has_value()`, otherwise `error() == rhs`.
338 bool matches(std::error_condition const& rhs) const noexcept
339 {
340 return has_error() && error() == rhs;
341 }
342 };
343
344 template<>
345 class expect<void>
346 {
347 std::error_code code_;
348
349 public:
350 using value_type = void;
351 using error_type = std::error_code;
352
353 //! Create a successful object.
354 expect() noexcept
355 : code_()
356 {}
357
358 expect(std::error_code const& code) noexcept
359 : code_(code)
360 {
361 if (!has_error())
362 code_ = ::common_error::kInvalidErrorCode;
363 }
364
365 expect(expect const&) = default;
366 ~expect() = default;
367 expect& operator=(expect const&) = default;
368
369 //! \return True if `this` is storing a value instead of an error.
370 explicit operator bool() const noexcept { return !has_error(); }
371
372 //! \return True if `this` is storing an error instead of a value.
373 bool has_error() const noexcept { return bool(code_); }
374
375 //! \return Error - alway
376 std::error_code error() const noexcept { return code_; }
377
378 //! \return `error() == rhs.error()`.
379 bool equal(expect const& rhs) const noexcept
380 {
381 return error() == rhs.error();
382 }
383
384 //! \return `has_error() && error() == rhs`.
385 bool equal(std::error_code const& rhs) const noexcept
386 {
387 return has_error() && error() == rhs;
388 }
389
390 //! \return False if `has_value()`, otherwise `error() == rhs`.
391 bool matches(std::error_condition const& rhs) const noexcept
392 {
393 return has_error() && error() == rhs;
394 }
395 };
396
397 //! \return An `expect<void>` object with `!has_error()`.
398 inline expect<void> success() noexcept { return expect<void>{}; }
399
400 template<typename T, typename U>
401 inline
402 bool operator==(expect<T> const& lhs, expect<U> const& rhs) noexcept(noexcept(lhs.equal(rhs)))
403 {
404 return lhs.equal(rhs);
405 }
406
407 template<typename T, typename U>
408 inline
409 bool operator==(expect<T> const& lhs, U const& rhs) noexcept(noexcept(lhs.equal(rhs)))
410 {
411 return lhs.equal(rhs);
412 }
413
414 template<typename T, typename U>
415 inline
416 bool operator==(T const& lhs, expect<U> const& rhs) noexcept(noexcept(rhs.equal(lhs)))
417 {
418 return rhs.equal(lhs);
419 }
420
421 template<typename T, typename U>
422 inline
423 bool operator!=(expect<T> const& lhs, expect<U> const& rhs) noexcept(noexcept(lhs.equal(rhs)))
424 {
425 return !lhs.equal(rhs);
426 }
427
428 template<typename T, typename U>
429 inline
430 bool operator!=(expect<T> const& lhs, U const& rhs) noexcept(noexcept(lhs.equal(rhs)))
431 {
432 return !lhs.equal(rhs);
433 }
434
435 template<typename T, typename U>
436 inline
437 bool operator!=(T const& lhs, expect<U> const& rhs) noexcept(noexcept(rhs.equal(lhs)))
438 {
439 return !rhs.equal(lhs);
440 }
441
442 namespace detail
443 {
444 inline void expect::unwrap(::expect<void>&& result, const char* error_msg, const char* file, unsigned line)
445 {
446 if (!result)
447 throw_(result.error(), error_msg, file, line);
448 }
449 }
450