| 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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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; |
| 134 } | 134 } |
| 135 | 135 |
| 136 // Checks if the identity of |other| and this object is the same. | |
| 137 bool IsSameObject(IUnknown* other) { | |
| 138 if (!other && !ptr_) | |
| 139 return true; | |
| 140 | |
| 141 if (!other || !ptr_) | |
| 142 return false; | |
| 143 | |
| 144 ScopedComPtr<IUnknown> my_identity; | |
| 145 CopyTo(IID_PPV_ARGS(my_identity.Receive())); | |
| 146 | |
| 147 ScopedComPtr<IUnknown> other_identity; | |
| 148 other->QueryInterface(IID_PPV_ARGS(other_identity.Receive())); | |
| 149 | |
| 150 return my_identity == other_identity; | |
| 151 } | |
| 152 | |
| 153 // Provides direct access to the interface. | 136 // Provides direct access to the interface. |
| 154 // Here we use a well known trick to make sure we block access to | 137 // Here we use a well known trick to make sure we block access to |
| 155 // IUnknown methods so that something bad like this doesn't happen: | 138 // IUnknown methods so that something bad like this doesn't happen: |
| 156 // ScopedComPtr<IUnknown> p(Foo()); | 139 // ScopedComPtr<IUnknown> p(Foo()); |
| 157 // p->Release(); | 140 // p->Release(); |
| 158 // ... later the destructor runs, which will Release() again. | 141 // ... later the destructor runs, which will Release() again. |
| 159 // and to get the benefit of the DCHECKs we add to QueryInterface. | 142 // and to get the benefit of the DCHECKs we add to QueryInterface. |
| 160 // There's still a way to call these methods if you absolutely must | 143 // There's still a way to call these methods if you absolutely must |
| 161 // by statically casting the ScopedComPtr instance to the wrapped interface | 144 // by statically casting the ScopedComPtr instance to the wrapped interface |
| 162 // and then making the call... but generally that shouldn't be necessary. | 145 // and then making the call... but generally that shouldn't be necessary. |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 // Helper to make IID_PPV_ARGS work with ScopedComPtr. | 277 // Helper to make IID_PPV_ARGS work with ScopedComPtr. |
| 295 template <typename T> | 278 template <typename T> |
| 296 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() { |
| 297 return pp; | 280 return pp; |
| 298 } | 281 } |
| 299 | 282 |
| 300 } // namespace win | 283 } // namespace win |
| 301 } // namespace base | 284 } // namespace base |
| 302 | 285 |
| 303 #endif // BASE_WIN_SCOPED_COMPTR_H_ | 286 #endif // BASE_WIN_SCOPED_COMPTR_H_ |
| OLD | NEW |