39template <
typename T,
typename E>
43 template <typename U = T, typename = std::enable_if_t<std::is_constructible<T, U&&>::value>>
45 return Result(std::variant<T, E>(std::in_place_index<0>, std::forward<U>(value)));
49 template <typename F = E, typename = std::enable_if_t<std::is_constructible<E, F&&>::value>>
51 return Result(std::variant<T, E>(std::in_place_index<1>, std::forward<F>(error)));
55 bool ok() const noexcept {
return storage_.index() == 0; }
57 bool hasError() const noexcept {
return !ok(); }
59 explicit operator bool() const noexcept {
return ok(); }
66 throw std::logic_error(
"Result::value() called on an error result");
68 return std::get<0>(storage_);
76 throw std::logic_error(
"Result::value() called on an error result");
78 return std::get<0>(storage_);
86 throw std::logic_error(
"Result::value() called on an error result");
88 return std::get<0>(std::move(storage_));
96 throw std::logic_error(
"Result::value() called on an error result");
98 return std::get<0>(std::move(storage_));
106 throw std::logic_error(
"Result::error() called on a success result");
108 return std::get<1>(storage_);
116 throw std::logic_error(
"Result::error() called on a success result");
118 return std::get<1>(storage_);
126 throw std::logic_error(
"Result::error() called on a success result");
128 return std::get<1>(std::move(storage_));
136 throw std::logic_error(
"Result::error() called on a success result");
138 return std::get<1>(std::move(storage_));
142 explicit Result(std::variant<T, E> storage) : storage_(std::move(storage)) {}
144 std::variant<T, E> storage_;
158 template <typename F = E, typename = std::enable_if_t<std::is_constructible<E, F&&>::value>>
160 return Result(std::optional<E>(std::forward<F>(error)));
164 bool ok() const noexcept {
return !error_.has_value(); }
166 bool hasError() const noexcept {
return error_.has_value(); }
168 explicit operator bool() const noexcept {
return ok(); }
176 throw std::logic_error(
"Result::value() called on an error result");
184 if (!error_.has_value()) {
185 throw std::logic_error(
"Result::error() called on a success result");
194 if (!error_.has_value()) {
195 throw std::logic_error(
"Result::error() called on a success result");
204 if (!error_.has_value()) {
205 throw std::logic_error(
"Result::error() called on a success result");
207 return std::move(*error_);
214 if (!error_.has_value()) {
215 throw std::logic_error(
"Result::error() called on a success result");
217 return std::move(*error_);
221 explicit Result(std::optional<E> error) : error_(std::move(error)) {}
223 std::optional<E> error_;
static Result failure(F &&error)
Construct a failed result containing an error.
Definition result.h:159
const E && error() const &&
Move the error value out.
Definition result.h:213
const E & error() const &
Access the error value.
Definition result.h:193
void value() const
Validates success.
Definition result.h:174
E && error() &&
Move the error value out.
Definition result.h:203
bool ok() const noexcept
True when the operation succeeded.
Definition result.h:164
static Result success()
Construct a successful result with no payload.
Definition result.h:155
bool hasError() const noexcept
True when the operation failed.
Definition result.h:166
E & error() &
Access the error value.
Definition result.h:183
Lightweight success-or-error return type for non-exceptional API failures.
Definition result.h:40
const T & value() const &
Access the success value.
Definition result.h:74
static Result failure(F &&error)
Construct a failed result containing an error.
Definition result.h:50
static Result success(U &&value)
Construct a successful result containing a value.
Definition result.h:44
const T && value() const &&
Move the success value out.
Definition result.h:94
bool ok() const noexcept
True when the result contains a success value.
Definition result.h:55
const E & error() const &
Access the error value.
Definition result.h:114
bool hasError() const noexcept
True when the result contains an error.
Definition result.h:57
T && value() &&
Move the success value out.
Definition result.h:84
T & value() &
Access the success value.
Definition result.h:64
const E && error() const &&
Move the error value out.
Definition result.h:134
E && error() &&
Move the error value out.
Definition result.h:124
E & error() &
Access the error value.
Definition result.h:104
Public API for the LiveKit C++ Client SDK.
Definition audio_frame.h:25