expected: add const member accessors

This commit is contained in:
Danny Robson 2019-03-05 23:15:21 +11:00
parent b86a9bbdfb
commit bf734c1f73

View File

@ -66,6 +66,7 @@ namespace cruft {
expected (unexpected<ErrorT> &&_error) expected (unexpected<ErrorT> &&_error)
{ {
::new (&m_store.error) unexpected<ErrorT> (std::move (_error)); ::new (&m_store.error) unexpected<ErrorT> (std::move (_error));
m_valid = false;
} }
~expected () ~expected ()
@ -128,20 +129,16 @@ namespace cruft {
ValueT* operator-> (void)& { return &value (); } ValueT* operator-> (void)& { return &value (); }
ValueT& operator* (void)& { return value (); } ValueT& operator* (void)& { return value (); }
template <typename FunctionT, typename ...ArgsT> ValueT const* operator-> (void) const& { return &value (); }
expected then (FunctionT &&func, ArgsT &&...args) ValueT const& operator* (void) const& { return value (); }
{
if (!m_valid)
return *this;
else
return expected (std::invoke (func, args..., value ()));
}
bool has_value (void) const noexcept { return m_valid; } bool has_value (void) const noexcept { return m_valid; }
explicit operator bool() const noexcept { return has_value (); } explicit operator bool() const noexcept { return has_value (); }
private: private:
bool m_valid = false;
union storage_t { union storage_t {
storage_t () {}; storage_t () {};
~storage_t () {}; ~storage_t () {};
@ -150,7 +147,5 @@ namespace cruft {
ValueT value; ValueT value;
unexpected<ErrorT> error; unexpected<ErrorT> error;
} m_store; } m_store;
bool m_valid = false;
}; };
} }