| 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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.Receive()); |
| 97 Interface** Receive() { | 97 Interface** Receive() { |
| 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 QueryInterface(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 QueryInterface(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(Receive())); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 135 | 135 |
| 136 // Checks if the identity of |other| and this object is the same. | 136 // Checks if the identity of |other| and this object is the same. |
| 137 bool IsSameObject(IUnknown* other) { | 137 bool IsSameObject(IUnknown* other) { |
| 138 if (!other && !ptr_) | 138 if (!other && !ptr_) |
| 139 return true; | 139 return true; |
| 140 | 140 |
| 141 if (!other || !ptr_) | 141 if (!other || !ptr_) |
| 142 return false; | 142 return false; |
| 143 | 143 |
| 144 ScopedComPtr<IUnknown> my_identity; | 144 ScopedComPtr<IUnknown> my_identity; |
| 145 QueryInterface(IID_PPV_ARGS(my_identity.Receive())); | 145 CopyTo(IID_PPV_ARGS(my_identity.Receive())); |
| 146 | 146 |
| 147 ScopedComPtr<IUnknown> other_identity; | 147 ScopedComPtr<IUnknown> other_identity; |
| 148 other->QueryInterface(IID_PPV_ARGS(other_identity.Receive())); | 148 other->QueryInterface(IID_PPV_ARGS(other_identity.Receive())); |
| 149 | 149 |
| 150 return my_identity == other_identity; | 150 return my_identity == other_identity; |
| 151 } | 151 } |
| 152 | 152 |
| 153 // Provides direct access to the interface. | 153 // Provides direct access to the interface. |
| 154 // Here we use a well known trick to make sure we block access to | 154 // 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: | 155 // IUnknown methods so that something bad like this doesn't happen: |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 // Helper to make IID_PPV_ARGS work with ScopedComPtr. | 294 // Helper to make IID_PPV_ARGS work with ScopedComPtr. |
| 295 template <typename T> | 295 template <typename T> |
| 296 void** IID_PPV_ARGS_Helper(base::win::details::ScopedComPtrRef<T> pp) throw() { | 296 void** IID_PPV_ARGS_Helper(base::win::details::ScopedComPtrRef<T> pp) throw() { |
| 297 return pp; | 297 return pp; |
| 298 } | 298 } |
| 299 | 299 |
| 300 } // namespace win | 300 } // namespace win |
| 301 } // namespace base | 301 } // namespace base |
| 302 | 302 |
| 303 #endif // BASE_WIN_SCOPED_COMPTR_H_ | 303 #endif // BASE_WIN_SCOPED_COMPTR_H_ |
| OLD | NEW |