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 | |
58 GenericScopedHandle() : handle_(Traits::NullHandle()) {} | 42 GenericScopedHandle() : handle_(Traits::NullHandle()) {} |
59 | 43 |
60 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) { | 44 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) { |
61 Set(handle); | 45 Set(handle); |
62 } | 46 } |
63 | 47 |
64 // Move constructor for C++03 move emulation of this type. | 48 // Move constructor for C++03 move emulation of this type. |
65 GenericScopedHandle(RValue other) : handle_(Traits::NullHandle()) { | 49 GenericScopedHandle(RValue other) : handle_(Traits::NullHandle()) { |
66 Set(other.object->Take()); | 50 Set(other.object->Take()); |
67 } | 51 } |
(...skipping 27 matching lines...) Expand all Loading... |
95 } | 79 } |
96 | 80 |
97 Handle Get() const { | 81 Handle Get() const { |
98 return handle_; | 82 return handle_; |
99 } | 83 } |
100 | 84 |
101 operator Handle() const { | 85 operator Handle() const { |
102 return handle_; | 86 return handle_; |
103 } | 87 } |
104 | 88 |
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 | |
115 // Transfers ownership away from this object. | 89 // Transfers ownership away from this object. |
116 Handle Take() { | 90 Handle Take() { |
117 Handle temp = handle_; | 91 Handle temp = handle_; |
118 handle_ = Traits::NullHandle(); | 92 handle_ = Traits::NullHandle(); |
119 if (Traits::IsHandleValid(temp)) { | 93 if (Traits::IsHandleValid(temp)) { |
120 Verifier::StopTracking(temp, this, BASE_WIN_GET_CALLER, | 94 Verifier::StopTracking(temp, this, BASE_WIN_GET_CALLER, |
121 tracked_objects::GetProgramCounter()); | 95 tracked_objects::GetProgramCounter()); |
122 } | 96 } |
123 return temp; | 97 return temp; |
124 } | 98 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 private: | 167 private: |
194 DISALLOW_IMPLICIT_CONSTRUCTORS(VerifierTraits); | 168 DISALLOW_IMPLICIT_CONSTRUCTORS(VerifierTraits); |
195 }; | 169 }; |
196 | 170 |
197 typedef GenericScopedHandle<HandleTraits, VerifierTraits> ScopedHandle; | 171 typedef GenericScopedHandle<HandleTraits, VerifierTraits> ScopedHandle; |
198 | 172 |
199 } // namespace win | 173 } // namespace win |
200 } // namespace base | 174 } // namespace base |
201 | 175 |
202 #endif // BASE_SCOPED_HANDLE_WIN_H_ | 176 #endif // BASE_SCOPED_HANDLE_WIN_H_ |
OLD | NEW |