Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(521)

Unified Diff: base/files/scoped_platform_handle.h

Issue 2705743002: Introduce base::ScopedPlatformHandle (Closed)
Patch Set: . Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/BUILD.gn ('k') | base/files/scoped_platform_handle.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/files/scoped_platform_handle.h
diff --git a/base/files/scoped_platform_handle.h b/base/files/scoped_platform_handle.h
new file mode 100644
index 0000000000000000000000000000000000000000..cfff5a7146bfba770efaa380abdabf3313f730d3
--- /dev/null
+++ b/base/files/scoped_platform_handle.h
@@ -0,0 +1,75 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_FILES_SCOPED_PLATFORM_HANDLE_H_
+#define BASE_FILES_SCOPED_PLATFORM_HANDLE_H_
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "base/base_export.h"
+#include "build/build_config.h"
+
+#if defined(OS_WIN)
+#include <windows.h>
+
+#include "base/win/scoped_handle.h"
+#elif defined(OS_POSIX)
+#include "base/files/scoped_file.h"
+#endif
+
+namespace base {
+
+// A ScopedPlatformHandle encapsulates ownership of either a Windows handle or
+// a POSIX file descriptor, while presenting a common interface for the sake
+// of simple, consistent, and safe ownership semantics. Platform-specific usage
+// details are thus relegated to code which either acquires or uses the
+// underlying platform resource.
+class BASE_EXPORT ScopedPlatformHandle {
+ public:
+#if defined(OS_WIN)
+ using HandleType = HANDLE;
+ using ScopedHandleType = win::ScopedHandle;
+#elif defined(OS_POSIX)
+ using HandleType = int;
+ using ScopedHandleType = ScopedFD;
+#endif
+
+ // Constructors for an invalid ScopedPlatformHandle.
+ ScopedPlatformHandle();
+ ScopedPlatformHandle(std::nullptr_t);
+
+ ScopedPlatformHandle(ScopedPlatformHandle&& other);
+
+ // These constructors always take ownership of the given handle.
+ explicit ScopedPlatformHandle(HandleType handle);
+ explicit ScopedPlatformHandle(ScopedHandleType handle);
+
+ ~ScopedPlatformHandle();
+
+ ScopedPlatformHandle& operator=(ScopedPlatformHandle&& other);
+
+ // Indicates whether this ScopedPlatformHandle is holding a valid handle.
+ bool is_valid() const;
+
+ // Closes the handle.
+ void reset();
+
+ // Returns the platform-specific handle value.
+ HandleType get() const;
+
+ // Returns the platform-specific handle value, releasing ownership of the
+ // handle.
+ HandleType release();
+
+ // Transfers ownership of the handle to a platform-specific scoper.
+ ScopedHandleType Take();
+
+ private:
+ ScopedHandleType handle_;
+};
+
+} // namespace base
+
+#endif // BASE_FILES_SCOPED_PLATFORM_HANDLE_H_
« no previous file with comments | « base/BUILD.gn ('k') | base/files/scoped_platform_handle.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698