| 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> | |
| 9 #include <unknwn.h> | 8 #include <unknwn.h> |
| 10 | 9 |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 | 11 |
| 13 namespace base { | 12 namespace base { |
| 14 namespace win { | 13 namespace win { |
| 15 | 14 |
| 16 namespace details { | 15 namespace details { |
| 17 | 16 |
| 18 template <typename T> | 17 template <typename T> |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 return ptr_->QueryInterface(IID_PPV_ARGS(p)); | 108 return ptr_->QueryInterface(IID_PPV_ARGS(p)); |
| 110 } | 109 } |
| 111 | 110 |
| 112 // QI for times when the IID is not associated with the type. | 111 // QI for times when the IID is not associated with the type. |
| 113 HRESULT CopyTo(const IID& iid, void** obj) { | 112 HRESULT CopyTo(const IID& iid, void** obj) { |
| 114 DCHECK(obj); | 113 DCHECK(obj); |
| 115 DCHECK(ptr_); | 114 DCHECK(ptr_); |
| 116 return ptr_->QueryInterface(iid, obj); | 115 return ptr_->QueryInterface(iid, obj); |
| 117 } | 116 } |
| 118 | 117 |
| 119 // Convenience wrapper around CoCreateInstance | |
| 120 HRESULT CreateInstance(const CLSID& clsid, | |
| 121 IUnknown* outer = nullptr, | |
| 122 DWORD context = CLSCTX_ALL) { | |
| 123 DCHECK(!ptr_); | |
| 124 HRESULT hr = ::CoCreateInstance(clsid, outer, context, *interface_id, | |
| 125 reinterpret_cast<void**>(&ptr_)); | |
| 126 return hr; | |
| 127 } | |
| 128 | |
| 129 // Provides direct access to the interface. | 118 // Provides direct access to the interface. |
| 130 // Here we use a well known trick to make sure we block access to | 119 // Here we use a well known trick to make sure we block access to |
| 131 // IUnknown methods so that something bad like this doesn't happen: | 120 // IUnknown methods so that something bad like this doesn't happen: |
| 132 // ScopedComPtr<IUnknown> p(Foo()); | 121 // ScopedComPtr<IUnknown> p(Foo()); |
| 133 // p->Release(); | 122 // p->Release(); |
| 134 // ... later the destructor runs, which will Release() again. | 123 // ... later the destructor runs, which will Release() again. |
| 135 // and to get the benefit of the DCHECKs we add to QueryInterface. | 124 // and to get the benefit of the DCHECKs we add to QueryInterface. |
| 136 // There's still a way to call these methods if you absolutely must | 125 // There's still a way to call these methods if you absolutely must |
| 137 // by statically casting the ScopedComPtr instance to the wrapped interface | 126 // by statically casting the ScopedComPtr instance to the wrapped interface |
| 138 // and then making the call... but generally that shouldn't be necessary. | 127 // 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... |
| 270 // Helper to make IID_PPV_ARGS work with ScopedComPtr. | 259 // Helper to make IID_PPV_ARGS work with ScopedComPtr. |
| 271 template <typename T> | 260 template <typename T> |
| 272 void** IID_PPV_ARGS_Helper(base::win::details::ScopedComPtrRef<T> pp) throw() { | 261 void** IID_PPV_ARGS_Helper(base::win::details::ScopedComPtrRef<T> pp) throw() { |
| 273 return pp; | 262 return pp; |
| 274 } | 263 } |
| 275 | 264 |
| 276 } // namespace win | 265 } // namespace win |
| 277 } // namespace base | 266 } // namespace base |
| 278 | 267 |
| 279 #endif // BASE_WIN_SCOPED_COMPTR_H_ | 268 #endif // BASE_WIN_SCOPED_COMPTR_H_ |
| OLD | NEW |