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

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

Issue 2889263002: Remove Interface ID Template Parameter from ScopedComPtr (Closed)
Patch Set: Created 3 years, 7 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 | « no previous file | 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"
11 11
12 namespace base { 12 namespace base {
13 namespace win { 13 namespace win {
14 14
15 namespace details { 15 namespace details {
16 16
17 template <typename T> 17 template <typename T>
18 class ScopedComPtrRef; 18 class ScopedComPtrRef;
19 19
20 } // details 20 } // details
21 21
22 // DEPRECATED: Use Microsoft::WRL::ComPtr instead. 22 // DEPRECATED: Use Microsoft::WRL::ComPtr instead.
23 // A fairly minimalistic smart class for COM interface pointers. 23 // A fairly minimalistic smart class for COM interface pointers.
24 template <class Interface, const IID* interface_id = &__uuidof(Interface)> 24 template <class Interface>
25 class ScopedComPtr { 25 class ScopedComPtr {
26 public: 26 public:
27 using InterfaceType = Interface; 27 using InterfaceType = Interface;
28 28
29 // Utility template to prevent users of ScopedComPtr from calling AddRef 29 // Utility template to prevent users of ScopedComPtr from calling AddRef
30 // and/or Release() without going through the ScopedComPtr class. 30 // and/or Release() without going through the ScopedComPtr class.
31 class BlockIUnknownMethods : public Interface { 31 class BlockIUnknownMethods : public Interface {
32 private: 32 private:
33 STDMETHOD(QueryInterface)(REFIID iid, void** object) = 0; 33 STDMETHOD(QueryInterface)(REFIID iid, void** object) = 0;
34 STDMETHOD_(ULONG, AddRef)() = 0; 34 STDMETHOD_(ULONG, AddRef)() = 0;
35 STDMETHOD_(ULONG, Release)() = 0; 35 STDMETHOD_(ULONG, Release)() = 0;
36 }; 36 };
37 37
38 ScopedComPtr() { 38 ScopedComPtr() {
39 } 39 }
40 40
41 explicit ScopedComPtr(Interface* p) : ptr_(p) { 41 explicit ScopedComPtr(Interface* p) : ptr_(p) {
42 if (ptr_) 42 if (ptr_)
43 ptr_->AddRef(); 43 ptr_->AddRef();
44 } 44 }
45 45
46 ScopedComPtr(const ScopedComPtr<Interface, interface_id>& p) : ptr_(p.Get()) { 46 ScopedComPtr(const ScopedComPtr<Interface>& p) : ptr_(p.Get()) {
47 if (ptr_) 47 if (ptr_)
48 ptr_->AddRef(); 48 ptr_->AddRef();
49 } 49 }
50 50
51 ~ScopedComPtr() { 51 ~ScopedComPtr() {
52 // We don't want the smart pointer class to be bigger than the pointer 52 // We don't want the smart pointer class to be bigger than the pointer
53 // it wraps. 53 // it wraps.
54 static_assert( 54 static_assert(sizeof(ScopedComPtr<Interface>) == sizeof(Interface*),
55 sizeof(ScopedComPtr<Interface, interface_id>) == sizeof(Interface*), 55 "ScopedComPtrSize");
56 "ScopedComPtrSize");
57 Reset(); 56 Reset();
58 } 57 }
59 58
60 Interface* Get() const { return ptr_; } 59 Interface* Get() const { return ptr_; }
61 60
62 explicit operator bool() const { return ptr_ != nullptr; } 61 explicit operator bool() const { return ptr_ != nullptr; }
63 62
64 // Explicit Release() of the held object. Useful for reuse of the 63 // Explicit Release() of the held object. Useful for reuse of the
65 // ScopedComPtr instance. 64 // ScopedComPtr instance.
66 // Note that this function equates to IUnknown::Release and should not 65 // Note that this function equates to IUnknown::Release and should not
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 // ... later the destructor runs, which will Release() again. 122 // ... later the destructor runs, which will Release() again.
124 // and to get the benefit of the DCHECKs we add to QueryInterface. 123 // and to get the benefit of the DCHECKs we add to QueryInterface.
125 // There's still a way to call these methods if you absolutely must 124 // There's still a way to call these methods if you absolutely must
126 // by statically casting the ScopedComPtr instance to the wrapped interface 125 // by statically casting the ScopedComPtr instance to the wrapped interface
127 // and then making the call... but generally that shouldn't be necessary. 126 // and then making the call... but generally that shouldn't be necessary.
128 BlockIUnknownMethods* operator->() const { 127 BlockIUnknownMethods* operator->() const {
129 DCHECK(ptr_); 128 DCHECK(ptr_);
130 return reinterpret_cast<BlockIUnknownMethods*>(ptr_); 129 return reinterpret_cast<BlockIUnknownMethods*>(ptr_);
131 } 130 }
132 131
133 ScopedComPtr<Interface, interface_id>& operator=(Interface* rhs) { 132 ScopedComPtr<Interface>& operator=(Interface* rhs) {
134 // AddRef first so that self assignment should work 133 // AddRef first so that self assignment should work
135 if (rhs) 134 if (rhs)
136 rhs->AddRef(); 135 rhs->AddRef();
137 Interface* old_ptr = ptr_; 136 Interface* old_ptr = ptr_;
138 ptr_ = rhs; 137 ptr_ = rhs;
139 if (old_ptr) 138 if (old_ptr)
140 old_ptr->Release(); 139 old_ptr->Release();
141 return *this; 140 return *this;
142 } 141 }
143 142
144 ScopedComPtr<Interface, interface_id>& operator=( 143 ScopedComPtr<Interface>& operator=(const ScopedComPtr<Interface>& rhs) {
145 const ScopedComPtr<Interface, interface_id>& rhs) {
146 return *this = rhs.ptr_; 144 return *this = rhs.ptr_;
147 } 145 }
148 146
149 Interface& operator*() const { 147 Interface& operator*() const {
150 DCHECK(ptr_); 148 DCHECK(ptr_);
151 return *ptr_; 149 return *ptr_;
152 } 150 }
153 151
154 bool operator==(const ScopedComPtr<Interface, interface_id>& rhs) const { 152 bool operator==(const ScopedComPtr<Interface>& rhs) const {
155 return ptr_ == rhs.Get(); 153 return ptr_ == rhs.Get();
156 } 154 }
157 155
158 template <typename U> 156 template <typename U>
159 bool operator==(const ScopedComPtr<U>& rhs) const { 157 bool operator==(const ScopedComPtr<U>& rhs) const {
160 return ptr_ == rhs.Get(); 158 return ptr_ == rhs.Get();
161 } 159 }
162 160
163 template <typename U> 161 template <typename U>
164 bool operator==(const U* rhs) const { 162 bool operator==(const U* rhs) const {
165 return ptr_ == rhs; 163 return ptr_ == rhs;
166 } 164 }
167 165
168 bool operator!=(const ScopedComPtr<Interface, interface_id>& rhs) const { 166 bool operator!=(const ScopedComPtr<Interface>& rhs) const {
169 return ptr_ != rhs.Get(); 167 return ptr_ != rhs.Get();
170 } 168 }
171 169
172 template <typename U> 170 template <typename U>
173 bool operator!=(const ScopedComPtr<U>& rhs) const { 171 bool operator!=(const ScopedComPtr<U>& rhs) const {
174 return ptr_ != rhs.Get(); 172 return ptr_ != rhs.Get();
175 } 173 }
176 174
177 template <typename U> 175 template <typename U>
178 bool operator!=(const U* rhs) const { 176 bool operator!=(const U* rhs) const {
179 return ptr_ != rhs; 177 return ptr_ != rhs;
180 } 178 }
181 179
182 details::ScopedComPtrRef<ScopedComPtr<Interface, interface_id>> operator&() { 180 details::ScopedComPtrRef<ScopedComPtr<Interface>> operator&() {
183 return details::ScopedComPtrRef<ScopedComPtr<Interface, interface_id>>( 181 return details::ScopedComPtrRef<ScopedComPtr<Interface>>(this);
184 this);
185 } 182 }
186 183
187 void swap(ScopedComPtr<Interface, interface_id>& r) { 184 void swap(ScopedComPtr<Interface>& r) {
188 Interface* tmp = ptr_; 185 Interface* tmp = ptr_;
189 ptr_ = r.ptr_; 186 ptr_ = r.ptr_;
190 r.ptr_ = tmp; 187 r.ptr_ = tmp;
191 } 188 }
192 189
193 private: 190 private:
194 Interface* ptr_ = nullptr; 191 Interface* ptr_ = nullptr;
195 }; 192 };
196 193
197 namespace details { 194 namespace details {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 // Helper to make IID_PPV_ARGS work with ScopedComPtr. 256 // Helper to make IID_PPV_ARGS work with ScopedComPtr.
260 template <typename T> 257 template <typename T>
261 void** IID_PPV_ARGS_Helper(base::win::details::ScopedComPtrRef<T> pp) throw() { 258 void** IID_PPV_ARGS_Helper(base::win::details::ScopedComPtrRef<T> pp) throw() {
262 return pp; 259 return pp;
263 } 260 }
264 261
265 } // namespace win 262 } // namespace win
266 } // namespace base 263 } // namespace base
267 264
268 #endif // BASE_WIN_SCOPED_COMPTR_H_ 265 #endif // BASE_WIN_SCOPED_COMPTR_H_
OLDNEW
« no previous file with comments | « no previous file | base/win/scoped_comptr_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698