OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_FILES_SCOPED_PLATFORM_HANDLE_H_ | |
6 #define BASE_FILES_SCOPED_PLATFORM_HANDLE_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include "base/base_export.h" | |
12 #include "build/build_config.h" | |
13 | |
14 #if defined(OS_WIN) | |
15 #include <windows.h> | |
16 | |
17 #include "base/win/scoped_handle.h" | |
18 #elif defined(OS_POSIX) | |
19 #include "base/files/scoped_file.h" | |
20 #endif | |
21 | |
22 namespace base { | |
23 | |
24 // A ScopedPlatformHandle encapsulates ownership of either a Windows handle or | |
25 // a POSIX file descriptor, while presenting a common interface for the sake | |
26 // of simple, consistent, and safe ownership semantics. Platform-specific usage | |
27 // details are thus relegated to code which either acquires or uses the | |
28 // underlying platform resource. | |
29 class BASE_EXPORT ScopedPlatformHandle { | |
30 public: | |
31 #if defined(OS_WIN) | |
32 using HandleType = HANDLE; | |
33 using ScopedHandleType = win::ScopedHandle; | |
34 #elif defined(OS_POSIX) | |
35 using HandleType = int; | |
36 using ScopedHandleType = ScopedFD; | |
erikchen
2017/02/23 21:52:40
Mach Ports?
| |
37 #endif | |
38 | |
39 // Constructors for an invalid ScopedPlatformHandle. | |
40 ScopedPlatformHandle(); | |
41 ScopedPlatformHandle(std::nullptr_t); | |
42 | |
43 ScopedPlatformHandle(ScopedPlatformHandle&& other); | |
44 | |
45 // These constructors always take ownership of the given handle. | |
46 explicit ScopedPlatformHandle(HandleType handle); | |
47 explicit ScopedPlatformHandle(ScopedHandleType handle); | |
48 | |
49 ~ScopedPlatformHandle(); | |
50 | |
51 ScopedPlatformHandle& operator=(ScopedPlatformHandle&& other); | |
52 | |
53 // Indicates whether this ScopedPlatformHandle is holding a valid handle. | |
54 bool is_valid() const; | |
55 | |
56 // Closes the handle. | |
57 void reset(); | |
58 | |
59 // Returns the platform-specific handle value. | |
60 HandleType get() const; | |
61 | |
62 // Returns the platform-specific handle value, releasing ownership of the | |
63 // handle. | |
64 HandleType release(); | |
65 | |
66 // Transfers ownership of the handle to a platform-specific scoper. | |
67 ScopedHandleType Take(); | |
68 | |
69 private: | |
70 ScopedHandleType handle_; | |
71 }; | |
72 | |
73 } // namespace base | |
74 | |
75 #endif // BASE_FILES_SCOPED_PLATFORM_HANDLE_H_ | |
OLD | NEW |