16namespace switchboard {
39 using ValueType = std::conditional_t<std::is_void_v<T>, std::monostate, T>;
40 std::variant<ValueType, Error> result;
43 explicit Result(ValueType
value) : result(std::move(
value)) {}
52 return std::holds_alternative<ValueType>(result);
61 return std::holds_alternative<Error>(result);
69 [[nodiscard]] ValueType
value()
const {
71 throw std::runtime_error(
"Result does not contain a value, operation failed.");
73 return std::get<ValueType>(result);
83 throw std::runtime_error(
"Result does not contain an error, operation was successful.");
85 return std::get<Error>(result);
96 [[nodiscard]] Result<std::any>
toAny()
const {
98 return Result<std::any>(std::any(
value()));
100 return Result<std::any>(
Error(
error().message));
113Result<T> makeSuccess(T value) {
114 return Result<T>(std::move(value));
134Result<T> makeError(std::string errorMessage) {
Represents the result of an operation that can either succeed or fail.
Definition Result.hpp:37
bool isSuccess() const
Returns true if the operation was successful.
Definition Result.hpp:51
ValueType value() const
Returns the value of the operation if it was successful.
Definition Result.hpp:69
Result< std::any > toAny() const
Converts the result to a std::any type.
Definition Result.hpp:96
Error error() const
Returns the error of the operation if it failed.
Definition Result.hpp:81
bool isError() const
Returns true if the operation failed.
Definition Result.hpp:60
Represents an error that can occur during an operation.
Definition Result.hpp:21
std::string message
Error message.
Definition Result.hpp:23
Error(std::string msg)
Error constructor.
Definition Result.hpp:30