From a2c868a098fcb51dcdf629045c5f5c0f68c2766f Mon Sep 17 00:00:00 2001 From: Jamie Liu Date: Tue, 11 Dec 2018 17:04:42 -0800 Subject: Add rvalue ref-qualified PosixErrorOr::ValueOrDie() overloads. This allows ValueOrDie() to be called on PosixErrorOr rvalues (e.g. temporaries) holding move-only types without extraneous std::move()s. PiperOrigin-RevId: 225098036 Change-Id: I662862e4f3562141f941845fc6e197edb27ce29b --- test/util/posix_error.h | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'test/util/posix_error.h') diff --git a/test/util/posix_error.h b/test/util/posix_error.h index 8450be9b9..2dc5504fd 100644 --- a/test/util/posix_error.h +++ b/test/util/posix_error.h @@ -103,8 +103,10 @@ class ABSL_MUST_USE_RESULT PosixErrorOr { bool ok() const; // Returns a reference to our current value, or CHECK-fails if !this->ok(). - const T& ValueOrDie() const; - T& ValueOrDie(); + const T& ValueOrDie() const&; + T& ValueOrDie() &; + const T&& ValueOrDie() const&&; + T&& ValueOrDie() &&; // Ignores any errors. This method does nothing except potentially suppress // complaints from any tools that are checking that errors are not dropped on @@ -179,17 +181,29 @@ bool PosixErrorOr::ok() const { } template -const T& PosixErrorOr::ValueOrDie() const { +const T& PosixErrorOr::ValueOrDie() const& { TEST_CHECK(absl::holds_alternative(value_)); return absl::get(value_); } template -T& PosixErrorOr::ValueOrDie() { +T& PosixErrorOr::ValueOrDie() & { TEST_CHECK(absl::holds_alternative(value_)); return absl::get(value_); } +template +const T&& PosixErrorOr::ValueOrDie() const&& { + TEST_CHECK(absl::holds_alternative(value_)); + return std::move(absl::get(value_)); +} + +template +T&& PosixErrorOr::ValueOrDie() && { + TEST_CHECK(absl::holds_alternative(value_)); + return std::move(absl::get(value_)); +} + extern ::std::ostream& operator<<(::std::ostream& os, const PosixError& e); template @@ -399,7 +413,7 @@ IsPosixErrorOkAndHolds(InnerMatcher&& inner_matcher) { if (!posixerroror.ok()) { \ return (posixerroror.error()); \ } \ - lhs = std::move(posixerroror.ValueOrDie()) + lhs = std::move(posixerroror).ValueOrDie() #define EXPECT_NO_ERRNO(expression) \ EXPECT_THAT(expression, IsPosixErrorOkMatcher()) @@ -419,7 +433,7 @@ IsPosixErrorOkAndHolds(InnerMatcher&& inner_matcher) { ({ \ auto _expr_result = (expr); \ ASSERT_NO_ERRNO(_expr_result); \ - std::move(_expr_result.ValueOrDie()); \ + std::move(_expr_result).ValueOrDie(); \ }) } // namespace testing -- cgit v1.2.3