From c3fc9607c2b264f4ccfea42511656b350300479d Mon Sep 17 00:00:00 2001 From: Tomasz Wasilczyk Date: Wed, 17 Jul 2019 17:25:23 -0700 Subject: [PATCH] Implement unique_fd.ok() While I'm here, I'll fix how unique_fd disallows copy and assignment constructors (detele instead of marking them private). Bug: 135918744 Test: WiP change in master Change-Id: Idefcc685943326c511f59d18790c1c4fa2e04989 --- base/include/android-base/unique_fd.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/base/include/android-base/unique_fd.h b/base/include/android-base/unique_fd.h index 3a02cffba..2b059565c 100644 --- a/base/include/android-base/unique_fd.h +++ b/base/include/android-base/unique_fd.h @@ -92,6 +92,8 @@ class unique_fd_impl final { explicit unique_fd_impl(int fd) { reset(fd); } ~unique_fd_impl() { reset(); } + unique_fd_impl(const unique_fd_impl&) = delete; + void operator=(const unique_fd_impl&) = delete; unique_fd_impl(unique_fd_impl&& other) noexcept { reset(other.release()); } unique_fd_impl& operator=(unique_fd_impl&& s) noexcept { int fd = s.fd_; @@ -118,6 +120,8 @@ class unique_fd_impl final { // Catch bogus error checks (i.e.: "!fd" instead of "fd != -1"). bool operator!() const = delete; + bool ok() const { return get() != -1; } + int release() __attribute__((warn_unused_result)) { tag(fd_, this, nullptr); int ret = fd_; @@ -167,9 +171,6 @@ class unique_fd_impl final { static auto close(int fd, void*) -> decltype(T::Close(fd), void()) { T::Close(fd); } - - unique_fd_impl(const unique_fd_impl&); - void operator=(const unique_fd_impl&); }; using unique_fd = unique_fd_impl;