14template<
typename T,
typename E>
19 return Result{std::expected<T,E>{std::in_place, std::move(value)}};
24 return Result{std::expected<T,E>{std::unexpect, std::move(
error)}};
27 [[nodiscard]]
bool isOk() const noexcept {
return m_data.has_value(); }
28 [[nodiscard]]
bool isErr() const noexcept {
return !
m_data.has_value(); }
29 explicit operator bool() const noexcept {
return m_data.has_value(); }
34 throw std::logic_error(
"Called unwrap() on an Err Result");
40 return m_data.value_or(std::move(defaultValue));
47 : std::invoke(std::forward<F>(fn));
50 [[nodiscard]]
const E&
error()
const
53 throw std::logic_error(
"Called error() on an Ok Result");
58 [[nodiscard]]
auto map(F&& fn)
60 using U = std::invoke_result_t<F, T>;
69 using E2 = std::invoke_result_t<F, E>;
76 [[nodiscard]]
auto andThen(F&& fn) -> std::invoke_result_t<F, T>
78 using Ret = std::invoke_result_t<F, T>;
80 return std::invoke(std::forward<F>(fn), std::move(*
m_data));
81 return Ret::Err(
m_data.error());
88 return std::invoke(std::forward<F>(fn),
m_data.error());
92 [[nodiscard]] std::optional<T>
ok()
const
98 [[nodiscard]] std::optional<E>
err()
const
116 [[nodiscard]]
static Result Ok(std::monostate = {})
118 return Result{std::expected<void,E>{}};
123 return Result{std::expected<void,E>{std::unexpect, std::move(
error)}};
126 [[nodiscard]]
bool isOk() const noexcept {
return m_data.has_value(); }
127 [[nodiscard]]
bool isErr() const noexcept {
return !
m_data.has_value(); }
128 explicit operator bool() const noexcept {
return m_data.has_value(); }
130 [[nodiscard]]
const E&
error()
const
133 throw std::logic_error(
"Called error() on an Ok Result<void,E>");
138 explicit Result(std::expected<void,E> data) :
m_data(std::move(data)) {}
bool isErr() const noexcept
Definition Result.hpp:127
bool isOk() const noexcept
Definition Result.hpp:126
static Result Err(E error)
Definition Result.hpp:121
static Result Ok(std::monostate={})
Definition Result.hpp:116
std::expected< void, E > m_data
Definition Result.hpp:139
const E & error() const
Definition Result.hpp:130
Result(std::expected< void, E > data)
Definition Result.hpp:138
static Result Ok(T value)
Definition Result.hpp:17
std::optional< T > ok() const
Definition Result.hpp:92
Result orElse(F &&fn)
Definition Result.hpp:85
Result(std::expected< T, E > data)
Definition Result.hpp:105
auto andThen(F &&fn) -> std::invoke_result_t< F, T >
Definition Result.hpp:76
auto mapErr(F &&fn)
Definition Result.hpp:67
bool isErr() const noexcept
Definition Result.hpp:28
static Result Err(E error)
Definition Result.hpp:22
bool isOk() const noexcept
Definition Result.hpp:27
auto map(F &&fn)
Definition Result.hpp:58
T unwrap()
Definition Result.hpp:31
std::expected< T, E > m_data
Definition Result.hpp:106
T unwrapOr(T defaultValue) const
Definition Result.hpp:38
const E & error() const
Definition Result.hpp:50
std::optional< E > err() const
Definition Result.hpp:98
T unwrapOrElse(F &&fn)
Definition Result.hpp:44
Utility components and helper types for the LogViewer application.