| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 BASE_WIN_SCOPED_HANDLE_H_ | 5 #ifndef BASE_WIN_SCOPED_HANDLE_H_ |
| 6 #define BASE_WIN_SCOPED_HANDLE_H_ | 6 #define BASE_WIN_SCOPED_HANDLE_H_ |
| 7 | 7 |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 // and INVALID_HANDLE_VALUE (-1) for Win32 handles. | 32 // and INVALID_HANDLE_VALUE (-1) for Win32 handles. |
| 33 // - Receive() method allows to receive a handle value from a function that | 33 // - Receive() method allows to receive a handle value from a function that |
| 34 // takes a raw handle pointer only. | 34 // takes a raw handle pointer only. |
| 35 template <class Traits, class Verifier> | 35 template <class Traits, class Verifier> |
| 36 class GenericScopedHandle { | 36 class GenericScopedHandle { |
| 37 MOVE_ONLY_TYPE_FOR_CPP_03(GenericScopedHandle, RValue) | 37 MOVE_ONLY_TYPE_FOR_CPP_03(GenericScopedHandle, RValue) |
| 38 | 38 |
| 39 public: | 39 public: |
| 40 typedef typename Traits::Handle Handle; | 40 typedef typename Traits::Handle Handle; |
| 41 | 41 |
| 42 // Helper object to contain the effect of Receive() to the function that needs |
| 43 // a pointer, and allow proper tracking of the handle. |
| 44 class Receiver { |
| 45 public: |
| 46 explicit Receiver(GenericScopedHandle* owner) |
| 47 : handle_(Traits::NullHandle()), |
| 48 owner_(owner) {} |
| 49 ~Receiver() { owner_->Set(handle_); } |
| 50 |
| 51 operator Handle*() { return &handle_; } |
| 52 |
| 53 private: |
| 54 Handle handle_; |
| 55 GenericScopedHandle* owner_; |
| 56 }; |
| 57 |
| 42 GenericScopedHandle() : handle_(Traits::NullHandle()) {} | 58 GenericScopedHandle() : handle_(Traits::NullHandle()) {} |
| 43 | 59 |
| 44 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) { | 60 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) { |
| 45 Set(handle); | 61 Set(handle); |
| 46 } | 62 } |
| 47 | 63 |
| 48 // Move constructor for C++03 move emulation of this type. | 64 // Move constructor for C++03 move emulation of this type. |
| 49 GenericScopedHandle(RValue other) : handle_(Traits::NullHandle()) { | 65 GenericScopedHandle(RValue other) : handle_(Traits::NullHandle()) { |
| 50 Set(other.object->Take()); | 66 Set(other.object->Take()); |
| 51 } | 67 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 79 } | 95 } |
| 80 | 96 |
| 81 Handle Get() const { | 97 Handle Get() const { |
| 82 return handle_; | 98 return handle_; |
| 83 } | 99 } |
| 84 | 100 |
| 85 operator Handle() const { | 101 operator Handle() const { |
| 86 return handle_; | 102 return handle_; |
| 87 } | 103 } |
| 88 | 104 |
| 105 // This method is intended to be used with functions that require a pointer to |
| 106 // a destination handle, like so: |
| 107 // void CreateRequiredHandle(Handle* out_handle); |
| 108 // ScopedHandle a; |
| 109 // CreateRequiredHandle(a.Receive()); |
| 110 Receiver Receive() { |
| 111 DCHECK(!Traits::IsHandleValid(handle_)) << "Handle must be NULL"; |
| 112 return Receiver(this); |
| 113 } |
| 114 |
| 89 // Transfers ownership away from this object. | 115 // Transfers ownership away from this object. |
| 90 Handle Take() { | 116 Handle Take() { |
| 91 Handle temp = handle_; | 117 Handle temp = handle_; |
| 92 handle_ = Traits::NullHandle(); | 118 handle_ = Traits::NullHandle(); |
| 93 if (Traits::IsHandleValid(temp)) { | 119 if (Traits::IsHandleValid(temp)) { |
| 94 Verifier::StopTracking(temp, this, BASE_WIN_GET_CALLER, | 120 Verifier::StopTracking(temp, this, BASE_WIN_GET_CALLER, |
| 95 tracked_objects::GetProgramCounter()); | 121 tracked_objects::GetProgramCounter()); |
| 96 } | 122 } |
| 97 return temp; | 123 return temp; |
| 98 } | 124 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 private: | 193 private: |
| 168 DISALLOW_IMPLICIT_CONSTRUCTORS(VerifierTraits); | 194 DISALLOW_IMPLICIT_CONSTRUCTORS(VerifierTraits); |
| 169 }; | 195 }; |
| 170 | 196 |
| 171 typedef GenericScopedHandle<HandleTraits, VerifierTraits> ScopedHandle; | 197 typedef GenericScopedHandle<HandleTraits, VerifierTraits> ScopedHandle; |
| 172 | 198 |
| 173 } // namespace win | 199 } // namespace win |
| 174 } // namespace base | 200 } // namespace base |
| 175 | 201 |
| 176 #endif // BASE_SCOPED_HANDLE_WIN_H_ | 202 #endif // BASE_SCOPED_HANDLE_WIN_H_ |
| OLD | NEW |