9#include "fastmm/core/config_macros.hpp"
27template <
class T,
class E>
28class [[nodiscard]] Result {
29 static_assert(!std::is_void_v<E>,
"error type must not be void");
30 static_assert(!std::is_reference_v<T> && !std::is_reference_v<E>);
37 constexpr Result(
const T& v)
noexcept(std::is_nothrow_copy_constructible_v<T>)
39 ::new (
static_cast<void*
>(&value_)) T(v);
42 constexpr Result(T&& v)
noexcept(std::is_nothrow_move_constructible_v<T>) : has_value_(true) {
43 ::new (
static_cast<void*
>(&value_)) T(std::move(v));
48 ::new (
static_cast<void*
>(&error_)) E(std::move(u.error));
51 constexpr Result(
const Result& o)
noexcept(std::is_nothrow_copy_constructible_v<T> &&
52 std::is_nothrow_copy_constructible_v<E>)
53 : has_value_(o.has_value_) {
55 ::new (
static_cast<void*
>(&value_)) T(o.value_);
57 ::new (
static_cast<void*
>(&error_)) E(o.error_);
60 constexpr Result(
Result&& o)
noexcept(std::is_nothrow_move_constructible_v<T> &&
61 std::is_nothrow_move_constructible_v<E>)
62 : has_value_(o.has_value_) {
64 ::new (
static_cast<void*
>(&value_)) T(std::move(o.value_));
66 ::new (
static_cast<void*
>(&error_)) E(std::move(o.error_));
72 has_value_ = o.has_value_;
74 ::new (
static_cast<void*
>(&value_)) T(o.
value_);
76 ::new (
static_cast<void*
>(&error_)) E(o.
error_);
81 constexpr Result& operator=(
Result&& o)
noexcept(std::is_nothrow_move_constructible_v<T> &&
82 std::is_nothrow_move_constructible_v<E>) {
85 has_value_ = o.has_value_;
87 ::new (
static_cast<void*
>(&value_)) T(std::move(o.value_));
89 ::new (
static_cast<void*
>(&error_)) E(std::move(o.error_));
96 [[nodiscard]]
constexpr bool has_value() const noexcept {
return has_value_; }
97 constexpr explicit operator bool() const noexcept {
return has_value_; }
99 [[nodiscard]]
constexpr T& value() &
noexcept {
100 FASTMM_ASSERT(has_value_);
103 [[nodiscard]]
constexpr const T& value() const& noexcept {
104 FASTMM_ASSERT(has_value_);
107 [[nodiscard]]
constexpr T&& value() &&
noexcept {
108 FASTMM_ASSERT(has_value_);
109 return std::move(value_);
111 [[nodiscard]]
constexpr const E& error() const noexcept {
112 FASTMM_ASSERT(!has_value_);
115 [[nodiscard]]
constexpr E& error() noexcept {
116 FASTMM_ASSERT(!has_value_);
120 constexpr const T&
operator*() const noexcept {
return value(); }
122 constexpr const T* operator->() const noexcept {
return &value(); }
125 [[nodiscard]]
constexpr T value_or(U&& fallback)
const {
126 return has_value_ ? value_ :
static_cast<T
>(std::forward<U>(fallback));
131 [[nodiscard]]
constexpr auto and_then(F&& f)
const -> std::invoke_result_t<F, const T&> {
132 using R = std::invoke_result_t<F, const T&>;
133 static_assert(std::is_same_v<typename R::error_type, E>);
134 if (has_value_)
return std::forward<F>(f)(value_);
135 return R(
fail(error_));
140 using U = std::invoke_result_t<F, const T&>;
141 if (has_value_)
return Result<U, E>(std::forward<F>(f)(value_));
146 constexpr void destroy() noexcept {
147 if constexpr (!std::is_trivially_destructible_v<T> || !std::is_trivially_destructible_v<E>) {
165class [[nodiscard]]
Result<void, E> {
170 constexpr Result() noexcept : error_(), has_value_(true) {}
174 [[nodiscard]]
constexpr bool has_value() const noexcept {
return has_value_; }
175 constexpr explicit operator bool() const noexcept {
return has_value_; }
176 [[nodiscard]]
constexpr const E& error() const noexcept {
177 FASTMM_ASSERT(!has_value_);
181 [[nodiscard]]
constexpr auto and_then(F&& f)
const -> std::invoke_result_t<F> {
182 using R = std::invoke_result_t<F>;
183 if (has_value_)
return std::forward<F>(f)();
184 return R(
fail(error_));
constexpr Result(Unexpected< E > u) noexcept
Definition result.hpp:170
E error_type
Definition result.hpp:167
void value_type
Definition result.hpp:166
E error_type
Definition result.hpp:36
E error_
Definition result.hpp:157
T value_
Definition result.hpp:156
constexpr Result(T &&v) noexcept(std::is_nothrow_move_constructible_v< T >)
Definition result.hpp:42
constexpr const T * operator->() const noexcept
Definition result.hpp:121
T value_type
Definition result.hpp:35
constexpr const T & operator*() const noexcept
Definition result.hpp:119
Definition strategy.hpp:16
constexpr Fixed< T > operator*(Fixed< T > v, Ratio r) noexcept
value * ratio in the value's domain, through Int128 with one truncation toward zero: mid * 5_bps,...
Definition fixed_point.hpp:343
constexpr Unexpected< std::decay_t< E > > fail(E &&e) noexcept
Definition result.hpp:25
E error
Definition result.hpp:21