Add android::base::expected::ok()

This is an alias for has_value() which is meant to be more concise, yet
more explicit than the conversions to bool.

Leave operator bool() in place for now: due to the large number of
users, removal of operator bool() needs to be broken up into incremental
stages.

Test: cd system/core && atest
Test: m
Change-Id: Ib1eb00f47d3c0f2229bb176b62687463b834f280
This commit is contained in:
Bernie Innocenti 2020-02-06 02:41:13 +09:00
parent 07e74d65a2
commit 4adfde41ea

View file

@ -331,6 +331,7 @@ class _NODISCARD_ expected {
constexpr explicit operator bool() const noexcept { return has_value(); }
constexpr bool has_value() const noexcept { return var_.index() == 0; }
constexpr bool ok() const noexcept { return has_value(); }
constexpr const T& value() const& { return std::get<T>(var_); }
constexpr T& value() & { return std::get<T>(var_); }
@ -557,6 +558,7 @@ class _NODISCARD_ expected<void, E> {
// observers
constexpr explicit operator bool() const noexcept { return has_value(); }
constexpr bool has_value() const noexcept { return var_.index() == 0; }
constexpr bool ok() const noexcept { return has_value(); }
constexpr void value() const& { if (!has_value()) std::get<0>(var_); }