diff options
author | Jamie Liu <jamieliu@google.com> | 2018-12-11 17:04:42 -0800 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2018-12-11 17:05:53 -0800 |
commit | a2c868a098fcb51dcdf629045c5f5c0f68c2766f (patch) | |
tree | 2c1f2c35e3a8033a50b08fe31a32a5b03c901db5 /test | |
parent | 24c1158b9c21f7f8b7126e810d623a518422052e (diff) |
Add rvalue ref-qualified PosixErrorOr<T>::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
Diffstat (limited to 'test')
-rw-r--r-- | test/syscalls/linux/itimer.cc | 4 | ||||
-rw-r--r-- | test/util/posix_error.h | 26 | ||||
-rw-r--r-- | test/util/proc_util.h | 2 |
3 files changed, 23 insertions, 9 deletions
diff --git a/test/syscalls/linux/itimer.cc b/test/syscalls/linux/itimer.cc index ee5871cbe..5d42529e2 100644 --- a/test/syscalls/linux/itimer.cc +++ b/test/syscalls/linux/itimer.cc @@ -126,7 +126,7 @@ SignalTestResult ItimerSignalTest(int id, clock_t main_clock, sa.sa_handler = &SignalTestSignalHandler; sa.sa_flags = SA_RESTART; sigemptyset(&sa.sa_mask); - auto sigaction_cleanup = std::move(ScopedSigaction(signal, sa).ValueOrDie()); + auto sigaction_cleanup = ScopedSigaction(signal, sa).ValueOrDie(); int socketfds[2]; TEST_PCHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, socketfds) == 0); @@ -167,7 +167,7 @@ SignalTestResult ItimerSignalTest(int id, clock_t main_clock, struct itimerval timer = {}; timer.it_value = absl::ToTimeval(kPeriod); timer.it_interval = absl::ToTimeval(kPeriod); - auto cleanup_itimer = std::move(ScopedItimer(id, timer).ValueOrDie()); + auto cleanup_itimer = ScopedItimer(id, timer).ValueOrDie(); // Unblock th1. // 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<T>::ok() const { } template <typename T> -const T& PosixErrorOr<T>::ValueOrDie() const { +const T& PosixErrorOr<T>::ValueOrDie() const& { TEST_CHECK(absl::holds_alternative<T>(value_)); return absl::get<T>(value_); } template <typename T> -T& PosixErrorOr<T>::ValueOrDie() { +T& PosixErrorOr<T>::ValueOrDie() & { TEST_CHECK(absl::holds_alternative<T>(value_)); return absl::get<T>(value_); } +template <typename T> +const T&& PosixErrorOr<T>::ValueOrDie() const&& { + TEST_CHECK(absl::holds_alternative<T>(value_)); + return std::move(absl::get<T>(value_)); +} + +template <typename T> +T&& PosixErrorOr<T>::ValueOrDie() && { + TEST_CHECK(absl::holds_alternative<T>(value_)); + return std::move(absl::get<T>(value_)); +} + extern ::std::ostream& operator<<(::std::ostream& os, const PosixError& e); template <typename T> @@ -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 diff --git a/test/util/proc_util.h b/test/util/proc_util.h index f8021d92e..e1ee2db9c 100644 --- a/test/util/proc_util.h +++ b/test/util/proc_util.h @@ -115,7 +115,7 @@ MATCHER_P(ContainsMappings, mappings, return false; } - auto maps = std::move(maps_or.ValueOrDie()); + auto maps = std::move(maps_or).ValueOrDie(); // Does maps contain all elements in mappings? The comparator ignores // the major, minor, and inode fields. |