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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 public: | 42 public: |
43 typedef typename Traits::Handle Handle; | 43 typedef typename Traits::Handle Handle; |
44 | 44 |
45 GenericScopedHandle() : handle_(Traits::NullHandle()) {} | 45 GenericScopedHandle() : handle_(Traits::NullHandle()) {} |
46 | 46 |
47 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) { | 47 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) { |
48 Set(handle); | 48 Set(handle); |
49 } | 49 } |
50 | 50 |
51 // Move constructor for C++03 move emulation of this type. | 51 // Move constructor for C++03 move emulation of this type. |
52 GenericScopedHandle(RValue& other) : handle_(other.Take()) { | 52 GenericScopedHandle(RValue& other) : handle_(Traits::NullHandle()) { |
| 53 Set(other.Take()); |
53 } | 54 } |
54 | 55 |
55 ~GenericScopedHandle() { | 56 ~GenericScopedHandle() { |
56 Close(); | 57 Close(); |
57 } | 58 } |
58 | 59 |
59 bool IsValid() const { | 60 bool IsValid() const { |
60 return Traits::IsHandleValid(handle_); | 61 return Traits::IsHandleValid(handle_); |
61 } | 62 } |
62 | 63 |
63 // Move operator= for C++03 move emulation of this type. | 64 // Move operator= for C++03 move emulation of this type. |
64 GenericScopedHandle& operator=(RValue& other) { | 65 GenericScopedHandle& operator=(RValue& other) { |
65 // Swapping the handles helps to avoid problems while assigning a handle | 66 if (this != &other) { |
66 // to itself. It is also cheap and matches base::scoped_ptr behavior. | 67 Set(other.Take()); |
67 Swap(other); | 68 } |
68 return *this; | 69 return *this; |
69 } | 70 } |
70 | 71 |
71 void Set(Handle handle) { | 72 void Set(Handle handle) { |
72 if (handle_ != handle) { | 73 if (handle_ != handle) { |
73 Close(); | 74 Close(); |
74 | 75 |
75 if (Traits::IsHandleValid(handle)) { | 76 if (Traits::IsHandleValid(handle)) { |
76 handle_ = handle; | 77 handle_ = handle; |
77 Verifier::StartTracking(handle, this, BASE_WIN_GET_CALLER, | 78 Verifier::StartTracking(handle, this, BASE_WIN_GET_CALLER, |
(...skipping 12 matching lines...) Expand all Loading... |
90 | 91 |
91 Handle* Receive() { | 92 Handle* Receive() { |
92 DCHECK(!Traits::IsHandleValid(handle_)) << "Handle must be NULL"; | 93 DCHECK(!Traits::IsHandleValid(handle_)) << "Handle must be NULL"; |
93 | 94 |
94 // We cannot track this case :(. Just tell the verifier about it. | 95 // We cannot track this case :(. Just tell the verifier about it. |
95 Verifier::StartTracking(INVALID_HANDLE_VALUE, this, BASE_WIN_GET_CALLER, | 96 Verifier::StartTracking(INVALID_HANDLE_VALUE, this, BASE_WIN_GET_CALLER, |
96 tracked_objects::GetProgramCounter()); | 97 tracked_objects::GetProgramCounter()); |
97 return &handle_; | 98 return &handle_; |
98 } | 99 } |
99 | 100 |
100 void Swap(GenericScopedHandle& other) { | |
101 Handle tmp = handle_; | |
102 handle_ = other.handle_; | |
103 other.handle_ = tmp; | |
104 } | |
105 | |
106 // Transfers ownership away from this object. | 101 // Transfers ownership away from this object. |
107 Handle Take() { | 102 Handle Take() { |
108 Handle temp = handle_; | 103 Handle temp = handle_; |
109 handle_ = Traits::NullHandle(); | 104 handle_ = Traits::NullHandle(); |
110 Verifier::StopTracking(temp, this, BASE_WIN_GET_CALLER, | 105 Verifier::StopTracking(temp, this, BASE_WIN_GET_CALLER, |
111 tracked_objects::GetProgramCounter()); | 106 tracked_objects::GetProgramCounter()); |
112 return temp; | 107 return temp; |
113 } | 108 } |
114 | 109 |
115 // Explicitly closes the owned handle. | 110 // Explicitly closes the owned handle. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 private: | 177 private: |
183 DISALLOW_IMPLICIT_CONSTRUCTORS(VerifierTraits); | 178 DISALLOW_IMPLICIT_CONSTRUCTORS(VerifierTraits); |
184 }; | 179 }; |
185 | 180 |
186 typedef GenericScopedHandle<HandleTraits, VerifierTraits> ScopedHandle; | 181 typedef GenericScopedHandle<HandleTraits, VerifierTraits> ScopedHandle; |
187 | 182 |
188 } // namespace win | 183 } // namespace win |
189 } // namespace base | 184 } // namespace base |
190 | 185 |
191 #endif // BASE_SCOPED_HANDLE_WIN_H_ | 186 #endif // BASE_SCOPED_HANDLE_WIN_H_ |
OLD | NEW |