82 "error_code values must be in range [-199, -100]");
85 "error_code values must be in range [-199, -100]");
93 using map_entry = std::pair<error_code, std::string_view>;
94 static constexpr map_entry kMap[] = {
120 for (
const auto& [k, v] : kMap) {
121 if (k == code)
return std::string(v);
123 return std::string(
"Unknown error code");
168 operator std::string()
const {
237 explicit operator bool() const noexcept {
return !
has_error_; }
273 [[nodiscard]]
explicit operator bool() const noexcept {
290 [[nodiscard]]
bool is_ok() const noexcept {
310 throw std::runtime_error(
"Cannot access value of an error result");
320 [[nodiscard]]
const T&
value() const & {
322 throw std::runtime_error(
"Cannot access value of an error result");
334 throw std::runtime_error(
"Cannot access value of an error result");
346 throw std::runtime_error(
"Cannot get error from successful result");
358 throw std::runtime_error(
"Cannot get error from successful result");
370 throw std::runtime_error(
"Cannot get error from successful result");
380 template <
typename U>
381 [[nodiscard]] T
value_or(U&& default_value)
const & {
385 return static_cast<T
>(std::forward<U>(default_value));
393 template <
typename U>
398 return static_cast<T
>(std::forward<U>(default_value));
430 template <
typename Fn>
435 return ResultType(std::invoke(std::forward<Fn>(fn),
value_));
437 return ResultType(
error_);
445 template <
typename Fn>
446 [[nodiscard]]
auto and_then(Fn&& fn)
const -> std::invoke_result_t<Fn, const T&> {
447 using ResultType = std::invoke_result_t<Fn, const T&>;
448 static_assert(std::is_same_v<typename ResultType::error_type, error>,
"Function must return a result with the same error type");
451 return std::invoke(std::forward<Fn>(fn),
value_);
453 return ResultType(
error_);
491 [[nodiscard]]
explicit operator bool() const noexcept {
508 [[nodiscard]]
bool is_ok() const noexcept {
528 throw std::runtime_error(
"Cannot get error from successful result");
540 throw std::runtime_error(
"Cannot get error from successful result");
552 throw std::runtime_error(
"Cannot get error from successful result");
572 template <
typename Fn>
577 return ResultType(std::invoke(std::forward<Fn>(fn)));
579 return ResultType(
error_);
587 template <
typename Fn>
588 [[nodiscard]]
auto and_then(Fn&& fn)
const -> std::invoke_result_t<Fn> {
589 using ResultType = std::invoke_result_t<Fn>;
590 static_assert(std::is_same_v<typename ResultType::error_type, error>,
"Function must return a result with the same error type");
593 return std::invoke(std::forward<Fn>(fn));
595 return ResultType(
error_);
599 bool success_ =
false;
613 return result<T>(std::forward<T>(value));
635 return {res.
value(), std::nullopt};
Represents an error in the thread system.
error_code code() const noexcept
Gets the error code.
error(error_code code, std::string message={})
Constructs an error with a code and optional message.
const std::string & message() const noexcept
Gets the error message.
std::string to_string() const
Converts the error to a string representation.
auto map(Fn &&fn) const -> result< std::invoke_result_t< Fn > >
Maps the result to another type using a function.
error & get_error() &
Gets the error.
const error & get_error() const &
Gets the error.
error && get_error() &&
Gets the error.
result(error err)
Constructs a result with an error.
bool has_value() const noexcept
Checks if the result is successful.
auto and_then(Fn &&fn) const -> std::invoke_result_t< Fn >
Maps the result to another type using a function that returns a result.
result()
Constructs a successful void result.
void value_or_throw() const
Throws an exception if the result contains an error.
result(const result_void &r)
Constructs a void result from a result_void.
bool is_ok() const noexcept
Checks if the result is successful.
bool is_error() const noexcept
Checks if the result contains an error.
bool has_value() const noexcept
Checks if the result is successful (has a value)
result_void()=default
Constructs a successful result.
bool is_error() const noexcept
Checks if the result contains an error.
bool has_error() const noexcept
Checks if the result contains an error.
result_void(const error &err)
Constructs a result with an error.
bool is_ok() const noexcept
Checks if the result is successful.
const error & get_error() const
Gets the error.
A template class representing either a value or an error.
auto map(Fn &&fn) const -> result< std::invoke_result_t< Fn, const T & > >
Maps the result to another type using a function.
error & get_error() &
Gets the error.
bool is_error() const noexcept
Checks if the result contains an error.
result(error err)
Constructs a result with an error.
T && value() &&
Gets the value.
auto and_then(Fn &&fn) const -> std::invoke_result_t< Fn, const T & >
Maps the result to another type using a function that returns a result.
T & value() &
Gets the value.
T value_or(U &&default_value) &&
Gets the value or a default.
bool has_value() const noexcept
Checks if the result contains a value.
T value_or_throw() &&
Gets the value or throws an exception.
error && get_error() &&
Gets the error.
bool is_ok() const noexcept
Checks if the result is successful.
const T & value() const &
Gets the value.
T value_or(U &&default_value) const &
Gets the value or a default.
T value_or_throw() const &
Gets the value or throws an exception.
const error & get_error() const &
Gets the error.
result(T value)
Constructs a result with a value.
Core threading foundation of the thread system library.
error_code
Strongly typed error codes for thread system operations.
@ condition_variable_error
@ resource_allocation_failed
result< T > optional_error_to_result(const std::optional< std::string > &error, T &&value)
std::optional< std::string > result_to_optional_error(const result< void > &res)
@ error
Error events that might still allow continuation.
std::string error_code_to_string(error_code code)
Converts an error_code to a string representation.
std::pair< std::optional< T >, std::optional< std::string > > result_to_pair(const result< T > &res)