OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ | 5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ |
6 #define MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ | 6 #define MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/move.h" | |
15 #include "mojo/public/c/system/functions.h" | 14 #include "mojo/public/c/system/functions.h" |
16 #include "mojo/public/c/system/types.h" | 15 #include "mojo/public/c/system/types.h" |
17 | 16 |
18 namespace mojo { | 17 namespace mojo { |
19 | 18 |
20 // OVERVIEW | 19 // OVERVIEW |
21 // | 20 // |
22 // |Handle| and |...Handle|: | 21 // |Handle| and |...Handle|: |
23 // | 22 // |
24 // |Handle| is a simple, copyable wrapper for the C type |MojoHandle| (which is | 23 // |Handle| is a simple, copyable wrapper for the C type |MojoHandle| (which is |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 // | 64 // |
66 // An exception are some of the |...Raw()| functions. E.g., |CloseRaw()| takes a | 65 // An exception are some of the |...Raw()| functions. E.g., |CloseRaw()| takes a |
67 // |Handle|, leaving the user to discard the wrapper. | 66 // |Handle|, leaving the user to discard the wrapper. |
68 // | 67 // |
69 // ScopedHandleBase ------------------------------------------------------------ | 68 // ScopedHandleBase ------------------------------------------------------------ |
70 | 69 |
71 // Scoper for the actual handle types defined further below. It's move-only, | 70 // Scoper for the actual handle types defined further below. It's move-only, |
72 // like the C++11 |unique_ptr|. | 71 // like the C++11 |unique_ptr|. |
73 template <class HandleType> | 72 template <class HandleType> |
74 class ScopedHandleBase { | 73 class ScopedHandleBase { |
75 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedHandleBase); | |
76 | |
77 public: | 74 public: |
78 using RawHandleType = HandleType; | 75 using RawHandleType = HandleType; |
79 | 76 |
80 ScopedHandleBase() {} | 77 ScopedHandleBase() {} |
81 explicit ScopedHandleBase(HandleType handle) : handle_(handle) {} | 78 explicit ScopedHandleBase(HandleType handle) : handle_(handle) {} |
82 ~ScopedHandleBase() { CloseIfNecessary(); } | 79 ~ScopedHandleBase() { CloseIfNecessary(); } |
83 | 80 |
84 template <class CompatibleHandleType> | 81 template <class CompatibleHandleType> |
85 explicit ScopedHandleBase(ScopedHandleBase<CompatibleHandleType> other) | 82 explicit ScopedHandleBase(ScopedHandleBase<CompatibleHandleType> other) |
86 : handle_(other.release()) {} | 83 : handle_(other.release()) {} |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 return handle_.value() == other.get().value(); | 124 return handle_.value() == other.get().value(); |
128 } | 125 } |
129 | 126 |
130 private: | 127 private: |
131 void CloseIfNecessary() { | 128 void CloseIfNecessary() { |
132 if (handle_.is_valid()) | 129 if (handle_.is_valid()) |
133 handle_.Close(); | 130 handle_.Close(); |
134 } | 131 } |
135 | 132 |
136 HandleType handle_; | 133 HandleType handle_; |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(ScopedHandleBase); |
137 }; | 136 }; |
138 | 137 |
139 template <typename HandleType> | 138 template <typename HandleType> |
140 inline ScopedHandleBase<HandleType> MakeScopedHandle(HandleType handle) { | 139 inline ScopedHandleBase<HandleType> MakeScopedHandle(HandleType handle) { |
141 return ScopedHandleBase<HandleType>(handle); | 140 return ScopedHandleBase<HandleType>(handle); |
142 } | 141 } |
143 | 142 |
144 // Handle ---------------------------------------------------------------------- | 143 // Handle ---------------------------------------------------------------------- |
145 | 144 |
146 const MojoHandle kInvalidHandleValue = MOJO_HANDLE_INVALID; | 145 const MojoHandle kInvalidHandleValue = MOJO_HANDLE_INVALID; |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 } | 299 } |
301 | 300 |
302 // Comparison, so that |Handle|s can be used as keys in hash maps. | 301 // Comparison, so that |Handle|s can be used as keys in hash maps. |
303 inline bool operator==(const Handle a, const Handle b) { | 302 inline bool operator==(const Handle a, const Handle b) { |
304 return a.value() == b.value(); | 303 return a.value() == b.value(); |
305 } | 304 } |
306 | 305 |
307 } // namespace mojo | 306 } // namespace mojo |
308 | 307 |
309 #endif // MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ | 308 #endif // MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ |
OLD | NEW |