OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_COMPTR_H_ | 5 #ifndef BASE_WIN_SCOPED_COMPTR_H_ |
6 #define BASE_WIN_SCOPED_COMPTR_H_ | 6 #define BASE_WIN_SCOPED_COMPTR_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <unknwn.h> | 9 #include <unknwn.h> |
10 | 10 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 // Sets the internal pointer to NULL and returns the held object without | 62 // Sets the internal pointer to NULL and returns the held object without |
63 // releasing the reference. | 63 // releasing the reference. |
64 Interface* Detach() { | 64 Interface* Detach() { |
65 Interface* p = ptr_; | 65 Interface* p = ptr_; |
66 ptr_ = NULL; | 66 ptr_ = NULL; |
67 return p; | 67 return p; |
68 } | 68 } |
69 | 69 |
70 // Accepts an interface pointer that has already been addref-ed. | 70 // Accepts an interface pointer that has already been addref-ed. |
71 void Attach(Interface* p) { | 71 void Attach(Interface* p) { |
72 DCHECK(ptr_ == NULL); | 72 DCHECK(!ptr_); |
73 ptr_ = p; | 73 ptr_ = p; |
74 } | 74 } |
75 | 75 |
76 // Retrieves the pointer address. | 76 // Retrieves the pointer address. |
77 // Used to receive object pointers as out arguments (and take ownership). | 77 // Used to receive object pointers as out arguments (and take ownership). |
78 // The function DCHECKs on the current value being NULL. | 78 // The function DCHECKs on the current value being NULL. |
79 // Usage: Foo(p.Receive()); | 79 // Usage: Foo(p.Receive()); |
80 Interface** Receive() { | 80 Interface** Receive() { |
81 DCHECK(ptr_ == NULL) << "Object leak. Pointer must be NULL"; | 81 DCHECK(!ptr_) << "Object leak. Pointer must be NULL"; |
82 return &ptr_; | 82 return &ptr_; |
83 } | 83 } |
84 | 84 |
85 // A convenience for whenever a void pointer is needed as an out argument. | 85 // A convenience for whenever a void pointer is needed as an out argument. |
86 void** ReceiveVoid() { | 86 void** ReceiveVoid() { |
87 return reinterpret_cast<void**>(Receive()); | 87 return reinterpret_cast<void**>(Receive()); |
88 } | 88 } |
89 | 89 |
90 template <class Query> | 90 template <class Query> |
91 HRESULT QueryInterface(Query** p) { | 91 HRESULT QueryInterface(Query** p) { |
(...skipping 15 matching lines...) Expand all Loading... |
107 // Queries |other| for the interface this object wraps and returns the | 107 // Queries |other| for the interface this object wraps and returns the |
108 // error code from the other->QueryInterface operation. | 108 // error code from the other->QueryInterface operation. |
109 HRESULT QueryFrom(IUnknown* object) { | 109 HRESULT QueryFrom(IUnknown* object) { |
110 DCHECK(object != NULL); | 110 DCHECK(object != NULL); |
111 return object->QueryInterface(Receive()); | 111 return object->QueryInterface(Receive()); |
112 } | 112 } |
113 | 113 |
114 // Convenience wrapper around CoCreateInstance | 114 // Convenience wrapper around CoCreateInstance |
115 HRESULT CreateInstance(const CLSID& clsid, IUnknown* outer = NULL, | 115 HRESULT CreateInstance(const CLSID& clsid, IUnknown* outer = NULL, |
116 DWORD context = CLSCTX_ALL) { | 116 DWORD context = CLSCTX_ALL) { |
117 DCHECK(ptr_ == NULL); | 117 DCHECK(!ptr_); |
118 HRESULT hr = ::CoCreateInstance(clsid, outer, context, *interface_id, | 118 HRESULT hr = ::CoCreateInstance(clsid, outer, context, *interface_id, |
119 reinterpret_cast<void**>(&ptr_)); | 119 reinterpret_cast<void**>(&ptr_)); |
120 return hr; | 120 return hr; |
121 } | 121 } |
122 | 122 |
123 // Checks if the identity of |other| and this object is the same. | 123 // Checks if the identity of |other| and this object is the same. |
124 bool IsSameObject(IUnknown* other) { | 124 bool IsSameObject(IUnknown* other) { |
125 if (!other && !ptr_) | 125 if (!other && !ptr_) |
126 return true; | 126 return true; |
127 | 127 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 | 160 |
161 static const IID& iid() { | 161 static const IID& iid() { |
162 return *interface_id; | 162 return *interface_id; |
163 } | 163 } |
164 }; | 164 }; |
165 | 165 |
166 } // namespace win | 166 } // namespace win |
167 } // namespace base | 167 } // namespace base |
168 | 168 |
169 #endif // BASE_WIN_SCOPED_COMPTR_H_ | 169 #endif // BASE_WIN_SCOPED_COMPTR_H_ |
OLD | NEW |