Skip to content

Commit

Permalink
4-way overloads for Expected::value and Expected::error
Browse files Browse the repository at this point in the history
Summary: Overload on the receiver reference category and `const`-qualification, deriving the return type reference category and `const`-qualification. Like `Optional`, `Try`, etc.

Reviewed By: Gownta

Differential Revision: D6062982

fbshipit-source-id: b781020210d6899dd7bca6fcdd95e01b5251cf5c
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Apr 17, 2024
1 parent a9c8474 commit 2a9804b
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions folly/Expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Unexpected final {
Error& error() & { return error_; }
const Error& error() const& { return error_; }
Error&& error() && { return std::move(error_); }
const Error&& error() const&& { return std::move(error_); }

private:
Error error_;
Expand Down Expand Up @@ -351,9 +352,11 @@ struct ExpectedStorage {
Value& value() & { return value_; }
const Value& value() const& { return value_; }
Value&& value() && { return std::move(value_); }
const Value&& value() const&& { return std::move(value_); }
Error& error() & { return error_; }
const Error& error() const& { return error_; }
Error&& error() && { return std::move(error_); }
const Error&& error() const&& { return std::move(error_); }
};

template <class Value, class Error>
Expand Down Expand Up @@ -382,9 +385,11 @@ struct ExpectedUnion {
Value& value() & { return value_; }
const Value& value() const& { return value_; }
Value&& value() && { return std::move(value_); }
const Value&& value() const&& { return std::move(value_); }
Error& error() & { return error_; }
const Error& error() const& { return error_; }
Error&& error() && { return std::move(error_); }
const Error&& error() const&& { return std::move(error_); }
};

template <class Derived, bool, bool Noexcept>
Expand Down Expand Up @@ -613,9 +618,11 @@ struct ExpectedStorage<Value, Error, StorageType::ePODStruct> {
Value& value() & { return value_; }
const Value& value() const& { return value_; }
Value&& value() && { return std::move(value_); }
const Value&& value() const&& { return std::move(value_); }
Error& error() & { return error_; }
const Error& error() const& { return error_; }
Error&& error() && { return std::move(error_); }
const Error&& error() const&& { return std::move(error_); }
};

namespace expected_detail_ExpectedHelper {
Expand Down Expand Up @@ -1186,6 +1193,11 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> {
return this->Base::value();
}

const Value&& value() const&& {
requireValueMove();
return std::move(this->Base::value());
}

Value&& value() && {
requireValueMove();
return std::move(this->Base::value());
Expand All @@ -1201,6 +1213,11 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> {
return this->Base::error();
}

const Error&& error() const&& {
requireError();
return std::move(this->Base::error());
}

Error&& error() && {
requireError();
return std::move(this->Base::error());
Expand Down Expand Up @@ -1375,15 +1392,19 @@ class Expected final : expected_detail::ExpectedStorage<Value, Error> {
}
}

void requireValueMove() {
if (FOLLY_UNLIKELY(!hasValue())) {
if (FOLLY_LIKELY(hasError())) {
throw_exception<BadExpectedAccess<Error>>(std::move(this->error_));
template <typename Self>
static void requireValueMove(Self& self) {
if (FOLLY_UNLIKELY(!self.hasValue())) {
if (FOLLY_LIKELY(self.hasError())) {
throw_exception<BadExpectedAccess<Error>>(std::move(self.error_));
}
throw_exception<BadExpectedAccess<void>>();
}
}

void requireValueMove() { return requireValueMove(*this); }
void requireValueMove() const { return requireValueMove(*this); }

void requireError() const {
if (FOLLY_UNLIKELY(!hasError())) {
throw_exception<BadExpectedAccess<void>>();
Expand Down

0 comments on commit 2a9804b

Please sign in to comment.