92 l.value_.emplace(std::move(prior));
99 l.value_.emplace(std::move(v));
108 l.error_ = std::move(err);
114 l.error_ = std::move(err);
115 l.value_.emplace(std::move(prior));
137 return value_.has_value();
149 [[nodiscard]]
const T*
value() const noexcept {
150 return value_.has_value() ? &*value_ :
nullptr;
156 return value_.has_value() ? *value_ : T{std::forward<U>(fallback)};
170 requires std::equality_comparable<T> {
171 return a.state_ == b.state_ && a.value_ == b.value_ && a.error_ == b.error_;
180 [[nodiscard]]
auto map(F&& f)
const
183 using U = std::remove_cvref_t<std::invoke_result_t<F, const T&>>;
185 out.set_state_(state_);
186 if (value_.has_value()) {
187 out.set_value_(std::invoke(std::forward<F>(f), *value_));
189 if (error_.has_value()) {
190 out.set_error_(*error_);
198 void set_state_(
LoadState s)
noexcept { state_ = s; }
200 void set_value_(U&& v) { value_.emplace(std::forward<U>(v)); }
201 void set_error_(Error e) { error_.emplace(std::move(e)); }
204 std::optional<T> value_{};
205 std::optional<Error> error_{};
T value_type
Definition loadable.hpp:76
friend bool operator==(const Loadable &a, const Loadable &b)
Definition loadable.hpp:169
bool is_refreshing() const noexcept
Definition loadable.hpp:124
bool is_loading() const noexcept
Definition loadable.hpp:123
bool has_error() const noexcept
True iff an error is currently surfaced.
Definition loadable.hpp:143
const T * value() const noexcept
Returns a pointer to the value if has_value(), else nullptr.
Definition loadable.hpp:149
bool has_value() const noexcept
True iff the loadable currently has a value to show (Success, Refreshing, or Error+prior).
Definition loadable.hpp:136
LoadState state() const noexcept
Definition loadable.hpp:120
static Loadable error(Error err)
Build an Error state.
Definition loadable.hpp:105
bool is_idle() const noexcept
Definition loadable.hpp:122
bool is_error() const noexcept
Definition loadable.hpp:126
bool in_flight() const noexcept
True iff a fetch is currently in flight (Loading or Refreshing).
Definition loadable.hpp:130
static Loadable loading()
Definition loadable.hpp:81
bool is_success() const noexcept
Definition loadable.hpp:125
static Loadable refreshing(T prior)
Build a Refreshing state from an existing Success payload.
Definition loadable.hpp:89
T value_or(U &&fallback) const
Returns a copy of the value, or constructs T from fallback if absent.
Definition loadable.hpp:155
const Error * error() const noexcept
Returns a pointer to the error if has_error(), else nullptr.
Definition loadable.hpp:160
auto map(F &&f) const -> Loadable< std::remove_cvref_t< std::invoke_result_t< F, const T & > > >
Definition loadable.hpp:180
static Loadable success(T v)
Definition loadable.hpp:96
friend class Loadable
Definition loadable.hpp:196
static Loadable error(Error err, T prior)
Definition loadable.hpp:111
static Loadable idle() noexcept
Definition loadable.hpp:79
LoadState
Discriminator for Loadable<T>.
Definition loadable.hpp:57
@ Loading
Definition loadable.hpp:59
@ Success
Definition loadable.hpp:61
@ Error
Definition loadable.hpp:62
@ Refreshing
Definition loadable.hpp:60
@ Idle
Definition loadable.hpp:58
One uniform error record.
Definition error.hpp:129