summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorCasey Dahlin <sadmac@google.com>2015-11-12 14:52:13 -0800
committerCasey Dahlin <sadmac@google.com>2015-11-18 12:01:24 -0800
commit918b9e8dc6dfd7fcbb17a9ac6c4c96fd1bbf78f1 (patch)
treea6c29f32b9f864ee82c83caf94192ab970094ce0 /base
parent1337349ea11a6bb5c44ba5c5bb064c750bd626c5 (diff)
downloadcore-918b9e8dc6dfd7fcbb17a9ac6c4c96fd1bbf78f1.tar.gz
core-918b9e8dc6dfd7fcbb17a9ac6c4c96fd1bbf78f1.tar.bz2
core-918b9e8dc6dfd7fcbb17a9ac6c4c96fd1bbf78f1.zip
Introduce unique_fd
unique_fds are wrappers for integer file descriptors that automatically close when they go out of scope. They are movable but not copyable and generally mimic the semantics of std::unique_ptr Change-Id: I0657b848b6536d5ad9fc06686e240ea5c387b0ac Test: Considerable exercise in new AIDL integration tests Bug: None Signed-off-by: Casey Dahlin <sadmac@google.com>
Diffstat (limited to 'base')
-rw-r--r--base/include/base/unique_fd.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/base/include/base/unique_fd.h b/base/include/base/unique_fd.h
new file mode 100644
index 000000000..411777507
--- /dev/null
+++ b/base/include/base/unique_fd.h
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_BASE_UNIQUE_FD_H
+#define ANDROID_BASE_UNIQUE_FD_H
+
+#include <unistd.h>
+
+#include <base/macros.h>
+
+/* Container for a file descriptor that automatically closes the descriptor as
+ * it goes out of scope.
+ *
+ * unique_fd ufd(open("/some/path", "r"));
+ *
+ * if (ufd.get() < 0) // invalid descriptor
+ * return error;
+ *
+ * // Do something useful
+ *
+ * return 0; // descriptor is closed here
+ */
+namespace android {
+namespace base {
+
+class unique_fd final {
+ public:
+ unique_fd() : value_(-1) {}
+
+ explicit unique_fd(int value) : value_(value) {}
+ ~unique_fd() { clear(); }
+
+ unique_fd(unique_fd&& other) : value_(other.release()) {}
+ unique_fd& operator = (unique_fd&& s) {
+ reset(s.release());
+ return *this;
+ }
+
+ void reset(int new_value) {
+ if (value_ >= 0)
+ close(value_);
+ value_ = new_value;
+ }
+
+ void clear() {
+ reset(-1);
+ }
+
+ int get() const { return value_; }
+
+ int release() {
+ int ret = value_;
+ value_ = -1;
+ return ret;
+ }
+
+ private:
+ int value_;
+
+ DISALLOW_COPY_AND_ASSIGN(unique_fd);
+};
+
+} // namespace base
+} // namespace android
+
+#endif // ANDROID_BASE_UNIQUE_FD_H