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 <unknwn.h> | 8 #include <unknwn.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 ScopedComPtr() { | 33 ScopedComPtr() { |
34 } | 34 } |
35 | 35 |
36 explicit ScopedComPtr(Interface* p) : ParentClass(p) { | 36 explicit ScopedComPtr(Interface* p) : ParentClass(p) { |
37 } | 37 } |
38 | 38 |
39 ScopedComPtr(const ScopedComPtr<Interface, interface_id>& p) | 39 ScopedComPtr(const ScopedComPtr<Interface, interface_id>& p) |
40 : ParentClass(p) { | 40 : ParentClass(p) { |
41 } | 41 } |
42 | 42 |
43 ~ScopedComPtr() { | |
44 // We don't want the smart pointer class to be bigger than the pointer | |
45 // it wraps. | |
46 static_assert( | |
47 sizeof(ScopedComPtr<Interface, interface_id>) == sizeof(Interface*), | |
48 "ScopedComPtrSize"); | |
49 } | |
50 | |
51 // Explicit Release() of the held object. Useful for reuse of the | 43 // Explicit Release() of the held object. Useful for reuse of the |
52 // ScopedComPtr instance. | 44 // ScopedComPtr instance. |
53 // Note that this function equates to IUnknown::Release and should not | 45 // Note that this function equates to IUnknown::Release and should not |
54 // be confused with e.g. scoped_ptr::release(). | 46 // be confused with e.g. scoped_ptr::release(). |
55 void Release() { | 47 void Release() { |
56 if (this->ptr_ != NULL) { | 48 if (this->ptr_ != NULL) { |
57 this->ptr_->Release(); | 49 this->ptr_->Release(); |
58 this->ptr_ = NULL; | 50 this->ptr_ = NULL; |
59 } | 51 } |
60 } | 52 } |
61 | 53 |
62 // Sets the internal pointer to NULL and returns the held object without | 54 // Sets the internal pointer to NULL and returns the held object without |
63 // releasing the reference. | 55 // releasing the reference. |
64 Interface* Detach() { | 56 Interface* Detach() { |
65 Interface* p = this->ptr_; | 57 Interface* p = this->ptr_; |
66 this->ptr_ = NULL; | 58 this->ptr_ = NULL; |
67 return p; | 59 return p; |
68 } | 60 } |
69 | 61 |
70 // Accepts an interface pointer that has already been addref-ed. | 62 // Accepts an interface pointer that has already been addref-ed. |
71 void Attach(Interface* p) { | 63 void Attach(Interface* p) { |
72 DCHECK(!this->ptr_); | 64 DCHECK(!this->ptr_); |
| 65 this->ptr_caller_ = &ParentClass::PtrCallerImpl; |
73 this->ptr_ = p; | 66 this->ptr_ = p; |
74 } | 67 } |
75 | 68 |
76 // Retrieves the pointer address. | 69 // Retrieves the pointer address. |
77 // Used to receive object pointers as out arguments (and take ownership). | 70 // Used to receive object pointers as out arguments (and take ownership). |
78 // The function DCHECKs on the current value being NULL. | 71 // The function DCHECKs on the current value being NULL. |
79 // Usage: Foo(p.Receive()); | 72 // Usage: Foo(p.Receive()); |
80 Interface** Receive() { | 73 Interface** Receive() { |
81 DCHECK(!this->ptr_) << "Object leak. Pointer must be NULL"; | 74 DCHECK(!this->ptr_) << "Object leak. Pointer must be NULL"; |
| 75 this->ptr_caller_ = &ParentClass::PtrCallerImpl; |
82 return &this->ptr_; | 76 return &this->ptr_; |
83 } | 77 } |
84 | 78 |
85 // A convenience for whenever a void pointer is needed as an out argument. | 79 // A convenience for whenever a void pointer is needed as an out argument. |
86 void** ReceiveVoid() { | 80 void** ReceiveVoid() { |
87 return reinterpret_cast<void**>(Receive()); | 81 return reinterpret_cast<void**>(Receive()); |
88 } | 82 } |
89 | 83 |
90 template <class Query> | 84 template <class Query> |
91 HRESULT QueryInterface(Query** p) { | 85 HRESULT QueryInterface(Query** p) { |
(...skipping 15 matching lines...) Expand all Loading... |
107 // Queries |other| for the interface this object wraps and returns the | 101 // Queries |other| for the interface this object wraps and returns the |
108 // error code from the other->QueryInterface operation. | 102 // error code from the other->QueryInterface operation. |
109 HRESULT QueryFrom(IUnknown* object) { | 103 HRESULT QueryFrom(IUnknown* object) { |
110 DCHECK(object != NULL); | 104 DCHECK(object != NULL); |
111 return object->QueryInterface(Receive()); | 105 return object->QueryInterface(Receive()); |
112 } | 106 } |
113 | 107 |
114 // Convenience wrapper around CoCreateInstance | 108 // Convenience wrapper around CoCreateInstance |
115 HRESULT CreateInstance(const CLSID& clsid, IUnknown* outer = NULL, | 109 HRESULT CreateInstance(const CLSID& clsid, IUnknown* outer = NULL, |
116 DWORD context = CLSCTX_ALL) { | 110 DWORD context = CLSCTX_ALL) { |
117 DCHECK(!this->ptr_); | |
118 HRESULT hr = ::CoCreateInstance(clsid, outer, context, *interface_id, | 111 HRESULT hr = ::CoCreateInstance(clsid, outer, context, *interface_id, |
119 reinterpret_cast<void**>(&this->ptr_)); | 112 ReceiveVoid()); |
120 return hr; | 113 return hr; |
121 } | 114 } |
122 | 115 |
123 // Checks if the identity of |other| and this object is the same. | 116 // Checks if the identity of |other| and this object is the same. |
124 bool IsSameObject(IUnknown* other) { | 117 bool IsSameObject(IUnknown* other) { |
125 if (!other && !this->ptr_) | 118 if (!other && !this->ptr_) |
126 return true; | 119 return true; |
127 | 120 |
128 if (!other || !this->ptr_) | 121 if (!other || !this->ptr_) |
129 return false; | 122 return false; |
(...skipping 29 matching lines...) Expand all Loading... |
159 | 152 |
160 static const IID& iid() { | 153 static const IID& iid() { |
161 return *interface_id; | 154 return *interface_id; |
162 } | 155 } |
163 }; | 156 }; |
164 | 157 |
165 } // namespace win | 158 } // namespace win |
166 } // namespace base | 159 } // namespace base |
167 | 160 |
168 #endif // BASE_WIN_SCOPED_COMPTR_H_ | 161 #endif // BASE_WIN_SCOPED_COMPTR_H_ |
OLD | NEW |