OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 7 |
8 #include <objbase.h> | 8 #include <objbase.h> |
9 #include <unknwn.h> | 9 #include <unknwn.h> |
10 | 10 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 | 86 |
87 // Accepts an interface pointer that has already been addref-ed. | 87 // Accepts an interface pointer that has already been addref-ed. |
88 void Attach(Interface* p) { | 88 void Attach(Interface* p) { |
89 DCHECK(!ptr_); | 89 DCHECK(!ptr_); |
90 ptr_ = p; | 90 ptr_ = p; |
91 } | 91 } |
92 | 92 |
93 // Retrieves the pointer address. | 93 // Retrieves the pointer address. |
94 // Used to receive object pointers as out arguments (and take ownership). | 94 // Used to receive object pointers as out arguments (and take ownership). |
95 // The function DCHECKs on the current value being NULL. | 95 // The function DCHECKs on the current value being NULL. |
96 // Usage: Foo(p.Receive()); | 96 // Usage: Foo(p.GetAddressOf()); |
97 Interface** Receive() { | 97 Interface** GetAddressOf() { |
98 DCHECK(!ptr_) << "Object leak. Pointer must be NULL"; | 98 DCHECK(!ptr_) << "Object leak. Pointer must be NULL"; |
99 return &ptr_; | 99 return &ptr_; |
100 } | 100 } |
101 | 101 |
102 template <class Query> | 102 template <class Query> |
103 HRESULT CopyTo(Query** p) { | 103 HRESULT CopyTo(Query** p) { |
104 DCHECK(p); | 104 DCHECK(p); |
105 DCHECK(ptr_); | 105 DCHECK(ptr_); |
106 // IUnknown already has a template version of QueryInterface | 106 // IUnknown already has a template version of QueryInterface |
107 // so the iid parameter is implicit here. The only thing this | 107 // so the iid parameter is implicit here. The only thing this |
108 // function adds are the DCHECKs. | 108 // function adds are the DCHECKs. |
109 return ptr_->QueryInterface(IID_PPV_ARGS(p)); | 109 return ptr_->QueryInterface(IID_PPV_ARGS(p)); |
110 } | 110 } |
111 | 111 |
112 // QI for times when the IID is not associated with the type. | 112 // QI for times when the IID is not associated with the type. |
113 HRESULT CopyTo(const IID& iid, void** obj) { | 113 HRESULT CopyTo(const IID& iid, void** obj) { |
114 DCHECK(obj); | 114 DCHECK(obj); |
115 DCHECK(ptr_); | 115 DCHECK(ptr_); |
116 return ptr_->QueryInterface(iid, obj); | 116 return ptr_->QueryInterface(iid, obj); |
117 } | 117 } |
118 | 118 |
119 // Queries |other| for the interface this object wraps and returns the | 119 // Queries |other| for the interface this object wraps and returns the |
120 // error code from the other->QueryInterface operation. | 120 // error code from the other->QueryInterface operation. |
121 HRESULT QueryFrom(IUnknown* object) { | 121 HRESULT QueryFrom(IUnknown* object) { |
122 DCHECK(object); | 122 DCHECK(object); |
123 return object->QueryInterface(IID_PPV_ARGS(Receive())); | 123 return object->QueryInterface(IID_PPV_ARGS(GetAddressOf())); |
124 } | 124 } |
125 | 125 |
126 // Convenience wrapper around CoCreateInstance | 126 // Convenience wrapper around CoCreateInstance |
127 HRESULT CreateInstance(const CLSID& clsid, | 127 HRESULT CreateInstance(const CLSID& clsid, |
128 IUnknown* outer = nullptr, | 128 IUnknown* outer = nullptr, |
129 DWORD context = CLSCTX_ALL) { | 129 DWORD context = CLSCTX_ALL) { |
130 DCHECK(!ptr_); | 130 DCHECK(!ptr_); |
131 HRESULT hr = ::CoCreateInstance(clsid, outer, context, *interface_id, | 131 HRESULT hr = ::CoCreateInstance(clsid, outer, context, *interface_id, |
132 reinterpret_cast<void**>(&ptr_)); | 132 reinterpret_cast<void**>(&ptr_)); |
133 return hr; | 133 return hr; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 // ComPtrRef equivalent transitional reference type to handle ComPtr equivalent | 217 // ComPtrRef equivalent transitional reference type to handle ComPtr equivalent |
218 // void** implicit casting. T should be a ScopedComPtr. | 218 // void** implicit casting. T should be a ScopedComPtr. |
219 template <typename T> | 219 template <typename T> |
220 class ScopedComPtrRef { | 220 class ScopedComPtrRef { |
221 public: | 221 public: |
222 explicit ScopedComPtrRef(T* scoped_com_ptr) | 222 explicit ScopedComPtrRef(T* scoped_com_ptr) |
223 : scoped_com_ptr_(scoped_com_ptr) {} | 223 : scoped_com_ptr_(scoped_com_ptr) {} |
224 | 224 |
225 // ComPtr equivalent conversion operators. | 225 // ComPtr equivalent conversion operators. |
226 operator void**() const { | 226 operator void**() const { |
227 return reinterpret_cast<void**>(scoped_com_ptr_->Receive()); | 227 return reinterpret_cast<void**>(scoped_com_ptr_->GetAddressOf()); |
228 } | 228 } |
229 | 229 |
230 // Allows ScopedComPtr to be passed to functions as a pointer. | 230 // Allows ScopedComPtr to be passed to functions as a pointer. |
231 operator T*() { return scoped_com_ptr_; } | 231 operator T*() { return scoped_com_ptr_; } |
232 | 232 |
233 // Allows IID_PPV_ARGS to perform __uuidof(**(ppType)). | 233 // Allows IID_PPV_ARGS to perform __uuidof(**(ppType)). |
234 typename T::InterfaceType* operator*() { return scoped_com_ptr_->Get(); } | 234 typename T::InterfaceType* operator*() { return scoped_com_ptr_->Get(); } |
235 | 235 |
236 private: | 236 private: |
237 T* const scoped_com_ptr_; | 237 T* const scoped_com_ptr_; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 // Helper to make IID_PPV_ARGS work with ScopedComPtr. | 277 // Helper to make IID_PPV_ARGS work with ScopedComPtr. |
278 template <typename T> | 278 template <typename T> |
279 void** IID_PPV_ARGS_Helper(base::win::details::ScopedComPtrRef<T> pp) throw() { | 279 void** IID_PPV_ARGS_Helper(base::win::details::ScopedComPtrRef<T> pp) throw() { |
280 return pp; | 280 return pp; |
281 } | 281 } |
282 | 282 |
283 } // namespace win | 283 } // namespace win |
284 } // namespace base | 284 } // namespace base |
285 | 285 |
286 #endif // BASE_WIN_SCOPED_COMPTR_H_ | 286 #endif // BASE_WIN_SCOPED_COMPTR_H_ |
OLD | NEW |