3#include <yaclib/config.hpp>
18#if defined(_MSC_VER) && !defined(__clang__)
19# define YACLIB_RESULT_EMPTY_VALUE
21# define YACLIB_RESULT_EMPTY_VALUE YACLIB_NO_UNIQUE_ADDRESS
29 return "yaclib::StopException";
55 using V = std::conditional_t<std::is_void_v<ValueT>,
Unit,
ValueT>;
61 State() noexcept : stub{} {
77 if (_error ==
nullptr) {
82 template <
typename...
Args>
83 explicit Result(std::in_place_t,
Args&&...
args)
noexcept(std::is_nothrow_constructible_v<V,
Args&&...>) {
84 ::new (&_value.value) V(std::forward<Args>(
args)...);
87 template <
typename...
Args,
88 typename = std::enable_if_t<(
sizeof...(Args) > 1) ||
90 std::is_same_v<std::decay_t<
head_t<
Args&&...>>, std::exception_ptr> ||
92 std::is_same_v<std::decay_t<
head_t<
Args&&...>>, std::in_place_t>)>>
106 ::new (&_value.value) V(std::move(
other._value.value));
111 std::is_nothrow_copy_assignable_v<V>) {
112 if (
this != &
other) {
115 _value.value =
other._value.value;
124 _error =
other._error;
131 std::is_nothrow_move_assignable_v<V>) {
132 if (
this != &
other) {
135 _value.value = std::move(
other._value.value);
137 ::new (&_value.value) V(std::move(
other._value.value));
144 _error =
other._error;
150 template <
typename Arg,
typename = std::enable_if_t<!std::is_same_v<std::decay_t<Arg>, Result>>>
166 void Ok() & =
delete;
175 std::rethrow_exception(_error);
177 return std::move(_value.value);
181 std::rethrow_exception(_error);
188 return std::move(_value.value);
205 std::exception_ptr _error;
219 template <
typename V>
224 template <
typename R>
227 template <
typename V,
typename...
Args>
229 static_assert(
sizeof...(Args) > 0,
"Use MakeResult<V>(Unit{}) to construct default value");
231 if constexpr (
sizeof...(Args) == 1 && std::is_same_v<Head, Unit>) {
233 }
else if constexpr (std::is_same_v<Head, std::in_place_t> ||
234 (
sizeof...(Args) == 1 &&
235 (std::is_same_v<Head, StopTag> || std::is_same_v<Head, std::exception_ptr> ||
243 template <
typename V>
245 return static_cast<bool>(
r);
248 template <
typename R>
250 return std::forward<R>(
r).Value();
253 template <
typename R>
255 return std::forward<R>(
r).Error();
258 template <
typename R>
260 return std::forward<R>(
r).Ok();
Encapsulated return value from caller.
void Ok() const &&=delete
const std::exception_ptr & Error() const &noexcept
Result(Result &&other) noexcept(std::is_nothrow_move_constructible_v< V >)
Result(std::exception_ptr error) noexcept
Result(const Result &other) noexcept(std::is_nothrow_copy_constructible_v< V >)
Result & operator=(Result &&other) noexcept(std::is_nothrow_move_constructible_v< V > &&std::is_nothrow_move_assignable_v< V >)
Result(std::in_place_t, Args &&... args) noexcept(std::is_nothrow_constructible_v< V, Args &&... >)
const V & Value() const &noexcept
Result & operator=(Arg &&arg)
Result & operator=(const Result &other) noexcept(std::is_nothrow_copy_constructible_v< V > &&std::is_nothrow_copy_assignable_v< V >)
const std::exception_ptr & Error() &&noexcept
Result(Args &&... args) noexcept(std::is_nothrow_constructible_v< V, Args &&... >)
#define YACLIB_ASSERT(cond)
Contract< V, T > MakeContract()
Creates related future and promise.
typename detail::Head< Args... >::Type head_t
bool IsStop(const std::exception_ptr &error) noexcept
Check if error represents cancellation,.
const std::exception_ptr & StopPtr() noexcept
Cached std::exception_ptr with StopException, copy is cheap and never allocates.
#define YACLIB_RESULT_EMPTY_VALUE
Trait used by default for all async abstractions, a separate struct so it can be forward declared.
Default result trait, describes how async abstractions create and inspect results.
static YACLIB_INLINE yaclib::Result< V > MakeResult(Args &&... args)
static YACLIB_INLINE bool IsStop(const std::exception_ptr &error) noexcept
typename detail::InstantiationType< yaclib::Result, R >::Value Value
static YACLIB_INLINE decltype(auto) Get(R &&r)
static YACLIB_INLINE decltype(auto) GetError(R &&r) noexcept
static YACLIB_INLINE decltype(auto) GetValue(R &&r) noexcept
static YACLIB_INLINE bool Ok(const yaclib::Result< V > &r) noexcept
Exception that represents cancellation of an async operation,.
const char * what() const noexcept override