Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: base/win/scoped_comptr.h

Issue 1641513004: Update //base to chromium 9659b08ea5a34f889dc4166217f438095ddc10d2 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/win/iat_patch_function.cc ('k') | base/win/scoped_comptr_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 // it wraps. 45 // it wraps.
46 COMPILE_ASSERT(sizeof(ScopedComPtr<Interface, interface_id>) == 46 COMPILE_ASSERT(sizeof(ScopedComPtr<Interface, interface_id>) ==
47 sizeof(Interface*), ScopedComPtrSize); 47 sizeof(Interface*), ScopedComPtrSize);
48 } 48 }
49 49
50 // Explicit Release() of the held object. Useful for reuse of the 50 // Explicit Release() of the held object. Useful for reuse of the
51 // ScopedComPtr instance. 51 // ScopedComPtr instance.
52 // Note that this function equates to IUnknown::Release and should not 52 // Note that this function equates to IUnknown::Release and should not
53 // be confused with e.g. scoped_ptr::release(). 53 // be confused with e.g. scoped_ptr::release().
54 void Release() { 54 void Release() {
55 if (ptr_ != NULL) { 55 if (this->ptr_ != NULL) {
56 ptr_->Release(); 56 this->ptr_->Release();
57 ptr_ = NULL; 57 this->ptr_ = NULL;
58 } 58 }
59 } 59 }
60 60
61 // Sets the internal pointer to NULL and returns the held object without 61 // Sets the internal pointer to NULL and returns the held object without
62 // releasing the reference. 62 // releasing the reference.
63 Interface* Detach() { 63 Interface* Detach() {
64 Interface* p = ptr_; 64 Interface* p = this->ptr_;
65 ptr_ = NULL; 65 this->ptr_ = NULL;
66 return p; 66 return p;
67 } 67 }
68 68
69 // Accepts an interface pointer that has already been addref-ed. 69 // Accepts an interface pointer that has already been addref-ed.
70 void Attach(Interface* p) { 70 void Attach(Interface* p) {
71 DCHECK(!ptr_); 71 DCHECK(!this->ptr_);
72 ptr_ = p; 72 this->ptr_ = p;
73 } 73 }
74 74
75 // Retrieves the pointer address. 75 // Retrieves the pointer address.
76 // Used to receive object pointers as out arguments (and take ownership). 76 // Used to receive object pointers as out arguments (and take ownership).
77 // The function DCHECKs on the current value being NULL. 77 // The function DCHECKs on the current value being NULL.
78 // Usage: Foo(p.Receive()); 78 // Usage: Foo(p.Receive());
79 Interface** Receive() { 79 Interface** Receive() {
80 DCHECK(!ptr_) << "Object leak. Pointer must be NULL"; 80 DCHECK(!this->ptr_) << "Object leak. Pointer must be NULL";
81 return &ptr_; 81 return &this->ptr_;
82 } 82 }
83 83
84 // A convenience for whenever a void pointer is needed as an out argument. 84 // A convenience for whenever a void pointer is needed as an out argument.
85 void** ReceiveVoid() { 85 void** ReceiveVoid() {
86 return reinterpret_cast<void**>(Receive()); 86 return reinterpret_cast<void**>(Receive());
87 } 87 }
88 88
89 template <class Query> 89 template <class Query>
90 HRESULT QueryInterface(Query** p) { 90 HRESULT QueryInterface(Query** p) {
91 DCHECK(p != NULL); 91 DCHECK(p != NULL);
92 DCHECK(ptr_ != NULL); 92 DCHECK(this->ptr_ != NULL);
93 // IUnknown already has a template version of QueryInterface 93 // IUnknown already has a template version of QueryInterface
94 // so the iid parameter is implicit here. The only thing this 94 // so the iid parameter is implicit here. The only thing this
95 // function adds are the DCHECKs. 95 // function adds are the DCHECKs.
96 return ptr_->QueryInterface(p); 96 return this->ptr_->QueryInterface(p);
97 } 97 }
98 98
99 // QI for times when the IID is not associated with the type. 99 // QI for times when the IID is not associated with the type.
100 HRESULT QueryInterface(const IID& iid, void** obj) { 100 HRESULT QueryInterface(const IID& iid, void** obj) {
101 DCHECK(obj != NULL); 101 DCHECK(obj != NULL);
102 DCHECK(ptr_ != NULL); 102 DCHECK(this->ptr_ != NULL);
103 return ptr_->QueryInterface(iid, obj); 103 return this->ptr_->QueryInterface(iid, obj);
104 } 104 }
105 105
106 // Queries |other| for the interface this object wraps and returns the 106 // Queries |other| for the interface this object wraps and returns the
107 // error code from the other->QueryInterface operation. 107 // error code from the other->QueryInterface operation.
108 HRESULT QueryFrom(IUnknown* object) { 108 HRESULT QueryFrom(IUnknown* object) {
109 DCHECK(object != NULL); 109 DCHECK(object != NULL);
110 return object->QueryInterface(Receive()); 110 return object->QueryInterface(Receive());
111 } 111 }
112 112
113 // Convenience wrapper around CoCreateInstance 113 // Convenience wrapper around CoCreateInstance
114 HRESULT CreateInstance(const CLSID& clsid, IUnknown* outer = NULL, 114 HRESULT CreateInstance(const CLSID& clsid, IUnknown* outer = NULL,
115 DWORD context = CLSCTX_ALL) { 115 DWORD context = CLSCTX_ALL) {
116 DCHECK(!ptr_); 116 DCHECK(!this->ptr_);
117 HRESULT hr = ::CoCreateInstance(clsid, outer, context, *interface_id, 117 HRESULT hr = ::CoCreateInstance(clsid, outer, context, *interface_id,
118 reinterpret_cast<void**>(&ptr_)); 118 reinterpret_cast<void**>(&this->ptr_));
119 return hr; 119 return hr;
120 } 120 }
121 121
122 // Checks if the identity of |other| and this object is the same. 122 // Checks if the identity of |other| and this object is the same.
123 bool IsSameObject(IUnknown* other) { 123 bool IsSameObject(IUnknown* other) {
124 if (!other && !ptr_) 124 if (!other && !this->ptr_)
125 return true; 125 return true;
126 126
127 if (!other || !ptr_) 127 if (!other || !this->ptr_)
128 return false; 128 return false;
129 129
130 ScopedComPtr<IUnknown> my_identity; 130 ScopedComPtr<IUnknown> my_identity;
131 QueryInterface(my_identity.Receive()); 131 QueryInterface(my_identity.Receive());
132 132
133 ScopedComPtr<IUnknown> other_identity; 133 ScopedComPtr<IUnknown> other_identity;
134 other->QueryInterface(other_identity.Receive()); 134 other->QueryInterface(other_identity.Receive());
135 135
136 return my_identity == other_identity; 136 return my_identity == other_identity;
137 } 137 }
138 138
139 // Provides direct access to the interface. 139 // Provides direct access to the interface.
140 // Here we use a well known trick to make sure we block access to 140 // Here we use a well known trick to make sure we block access to
141 // IUnknown methods so that something bad like this doesn't happen: 141 // IUnknown methods so that something bad like this doesn't happen:
142 // ScopedComPtr<IUnknown> p(Foo()); 142 // ScopedComPtr<IUnknown> p(Foo());
143 // p->Release(); 143 // p->Release();
144 // ... later the destructor runs, which will Release() again. 144 // ... later the destructor runs, which will Release() again.
145 // and to get the benefit of the DCHECKs we add to QueryInterface. 145 // and to get the benefit of the DCHECKs we add to QueryInterface.
146 // There's still a way to call these methods if you absolutely must 146 // There's still a way to call these methods if you absolutely must
147 // by statically casting the ScopedComPtr instance to the wrapped interface 147 // by statically casting the ScopedComPtr instance to the wrapped interface
148 // and then making the call... but generally that shouldn't be necessary. 148 // and then making the call... but generally that shouldn't be necessary.
149 BlockIUnknownMethods* operator->() const { 149 BlockIUnknownMethods* operator->() const {
150 DCHECK(ptr_ != NULL); 150 DCHECK(this->ptr_ != NULL);
151 return reinterpret_cast<BlockIUnknownMethods*>(ptr_); 151 return reinterpret_cast<BlockIUnknownMethods*>(this->ptr_);
152 } 152 }
153 153
154 // Pull in operator=() from the parent class. 154 // Pull in operator=() from the parent class.
155 using scoped_refptr<Interface>::operator=; 155 using scoped_refptr<Interface>::operator=;
156 156
157 // static methods 157 // static methods
158 158
159 static const IID& iid() { 159 static const IID& iid() {
160 return *interface_id; 160 return *interface_id;
161 } 161 }
162 }; 162 };
163 163
164 } // namespace win 164 } // namespace win
165 } // namespace base 165 } // namespace base
166 166
167 #endif // BASE_WIN_SCOPED_COMPTR_H_ 167 #endif // BASE_WIN_SCOPED_COMPTR_H_
OLDNEW
« no previous file with comments | « base/win/iat_patch_function.cc ('k') | base/win/scoped_comptr_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698