| 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" |
| 14 #include "mojo/public/c/system/functions.h" | 15 #include "mojo/public/c/system/functions.h" |
| 15 #include "mojo/public/c/system/types.h" | 16 #include "mojo/public/c/system/types.h" |
| 16 | 17 |
| 17 namespace mojo { | 18 namespace mojo { |
| 18 | 19 |
| 19 // OVERVIEW | 20 // OVERVIEW |
| 20 // | 21 // |
| 21 // |Handle| and |...Handle|: | 22 // |Handle| and |...Handle|: |
| 22 // | 23 // |
| 23 // |Handle| is a simple, copyable wrapper for the C type |MojoHandle| (which is | 24 // |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... |
| 64 // | 65 // |
| 65 // An exception are some of the |...Raw()| functions. E.g., |CloseRaw()| takes a | 66 // An exception are some of the |...Raw()| functions. E.g., |CloseRaw()| takes a |
| 66 // |Handle|, leaving the user to discard the wrapper. | 67 // |Handle|, leaving the user to discard the wrapper. |
| 67 // | 68 // |
| 68 // ScopedHandleBase ------------------------------------------------------------ | 69 // ScopedHandleBase ------------------------------------------------------------ |
| 69 | 70 |
| 70 // Scoper for the actual handle types defined further below. It's move-only, | 71 // Scoper for the actual handle types defined further below. It's move-only, |
| 71 // like the C++11 |unique_ptr|. | 72 // like the C++11 |unique_ptr|. |
| 72 template <class HandleType> | 73 template <class HandleType> |
| 73 class ScopedHandleBase { | 74 class ScopedHandleBase { |
| 75 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedHandleBase); |
| 76 |
| 74 public: | 77 public: |
| 75 using RawHandleType = HandleType; | 78 using RawHandleType = HandleType; |
| 76 | 79 |
| 77 ScopedHandleBase() {} | 80 ScopedHandleBase() {} |
| 78 explicit ScopedHandleBase(HandleType handle) : handle_(handle) {} | 81 explicit ScopedHandleBase(HandleType handle) : handle_(handle) {} |
| 79 ~ScopedHandleBase() { CloseIfNecessary(); } | 82 ~ScopedHandleBase() { CloseIfNecessary(); } |
| 80 | 83 |
| 81 template <class CompatibleHandleType> | 84 template <class CompatibleHandleType> |
| 82 explicit ScopedHandleBase(ScopedHandleBase<CompatibleHandleType> other) | 85 explicit ScopedHandleBase(ScopedHandleBase<CompatibleHandleType> other) |
| 83 : handle_(other.release()) {} | 86 : handle_(other.release()) {} |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 return handle_.value() == other.get().value(); | 126 return handle_.value() == other.get().value(); |
| 124 } | 127 } |
| 125 | 128 |
| 126 private: | 129 private: |
| 127 void CloseIfNecessary() { | 130 void CloseIfNecessary() { |
| 128 if (handle_.is_valid()) | 131 if (handle_.is_valid()) |
| 129 handle_.Close(); | 132 handle_.Close(); |
| 130 } | 133 } |
| 131 | 134 |
| 132 HandleType handle_; | 135 HandleType handle_; |
| 133 | |
| 134 DISALLOW_COPY_AND_ASSIGN(ScopedHandleBase); | |
| 135 }; | 136 }; |
| 136 | 137 |
| 137 template <typename HandleType> | 138 template <typename HandleType> |
| 138 inline ScopedHandleBase<HandleType> MakeScopedHandle(HandleType handle) { | 139 inline ScopedHandleBase<HandleType> MakeScopedHandle(HandleType handle) { |
| 139 return ScopedHandleBase<HandleType>(handle); | 140 return ScopedHandleBase<HandleType>(handle); |
| 140 } | 141 } |
| 141 | 142 |
| 142 // Handle ---------------------------------------------------------------------- | 143 // Handle ---------------------------------------------------------------------- |
| 143 | 144 |
| 144 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... |
| 298 } | 299 } |
| 299 | 300 |
| 300 // 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. |
| 301 inline bool operator==(const Handle a, const Handle b) { | 302 inline bool operator==(const Handle a, const Handle b) { |
| 302 return a.value() == b.value(); | 303 return a.value() == b.value(); |
| 303 } | 304 } |
| 304 | 305 |
| 305 } // namespace mojo | 306 } // namespace mojo |
| 306 | 307 |
| 307 #endif // MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ | 308 #endif // MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ |
| OLD | NEW |