| 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 17 matching lines...) Expand all Loading... |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 ScopedComPtr() { | 30 ScopedComPtr() { |
| 31 } | 31 } |
| 32 | 32 |
| 33 explicit ScopedComPtr(Interface* p) : ptr_(p) { | 33 explicit ScopedComPtr(Interface* p) : ptr_(p) { |
| 34 if (ptr_) | 34 if (ptr_) |
| 35 ptr_->AddRef(); | 35 ptr_->AddRef(); |
| 36 } | 36 } |
| 37 | 37 |
| 38 ScopedComPtr(const ScopedComPtr<Interface, interface_id>& p) : ptr_(p.get()) { | 38 ScopedComPtr(const ScopedComPtr<Interface, interface_id>& p) : ptr_(p.Get()) { |
| 39 if (ptr_) | 39 if (ptr_) |
| 40 ptr_->AddRef(); | 40 ptr_->AddRef(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 ~ScopedComPtr() { | 43 ~ScopedComPtr() { |
| 44 // We don't want the smart pointer class to be bigger than the pointer | 44 // We don't want the smart pointer class to be bigger than the pointer |
| 45 // it wraps. | 45 // it wraps. |
| 46 static_assert( | 46 static_assert( |
| 47 sizeof(ScopedComPtr<Interface, interface_id>) == sizeof(Interface*), | 47 sizeof(ScopedComPtr<Interface, interface_id>) == sizeof(Interface*), |
| 48 "ScopedComPtrSize"); | 48 "ScopedComPtrSize"); |
| 49 Reset(); | 49 Reset(); |
| 50 } | 50 } |
| 51 | 51 |
| 52 Interface* get() const { return ptr_; } | 52 Interface* Get() const { return ptr_; } |
| 53 | 53 |
| 54 explicit operator bool() const { return ptr_ != nullptr; } | 54 explicit operator bool() const { return ptr_ != nullptr; } |
| 55 | 55 |
| 56 // Explicit Release() of the held object. Useful for reuse of the | 56 // Explicit Release() of the held object. Useful for reuse of the |
| 57 // ScopedComPtr instance. | 57 // ScopedComPtr instance. |
| 58 // Note that this function equates to IUnknown::Release and should not | 58 // Note that this function equates to IUnknown::Release and should not |
| 59 // be confused with e.g. unique_ptr::release(). | 59 // be confused with e.g. unique_ptr::release(). |
| 60 unsigned long Reset() { | 60 unsigned long Reset() { |
| 61 unsigned long ref = 0; | 61 unsigned long ref = 0; |
| 62 Interface* temp = ptr_; | 62 Interface* temp = ptr_; |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 const ScopedComPtr<Interface, interface_id>& rhs) { | 176 const ScopedComPtr<Interface, interface_id>& rhs) { |
| 177 return *this = rhs.ptr_; | 177 return *this = rhs.ptr_; |
| 178 } | 178 } |
| 179 | 179 |
| 180 Interface& operator*() const { | 180 Interface& operator*() const { |
| 181 DCHECK(ptr_); | 181 DCHECK(ptr_); |
| 182 return *ptr_; | 182 return *ptr_; |
| 183 } | 183 } |
| 184 | 184 |
| 185 bool operator==(const ScopedComPtr<Interface, interface_id>& rhs) const { | 185 bool operator==(const ScopedComPtr<Interface, interface_id>& rhs) const { |
| 186 return ptr_ == rhs.get(); | 186 return ptr_ == rhs.Get(); |
| 187 } | 187 } |
| 188 | 188 |
| 189 template <typename U> | 189 template <typename U> |
| 190 bool operator==(const ScopedComPtr<U>& rhs) const { | 190 bool operator==(const ScopedComPtr<U>& rhs) const { |
| 191 return ptr_ == rhs.get(); | 191 return ptr_ == rhs.Get(); |
| 192 } | 192 } |
| 193 | 193 |
| 194 template <typename U> | 194 template <typename U> |
| 195 bool operator==(const U* rhs) const { | 195 bool operator==(const U* rhs) const { |
| 196 return ptr_ == rhs; | 196 return ptr_ == rhs; |
| 197 } | 197 } |
| 198 | 198 |
| 199 bool operator!=(const ScopedComPtr<Interface, interface_id>& rhs) const { | 199 bool operator!=(const ScopedComPtr<Interface, interface_id>& rhs) const { |
| 200 return ptr_ != rhs.get(); | 200 return ptr_ != rhs.Get(); |
| 201 } | 201 } |
| 202 | 202 |
| 203 template <typename U> | 203 template <typename U> |
| 204 bool operator!=(const ScopedComPtr<U>& rhs) const { | 204 bool operator!=(const ScopedComPtr<U>& rhs) const { |
| 205 return ptr_ != rhs.get(); | 205 return ptr_ != rhs.Get(); |
| 206 } | 206 } |
| 207 | 207 |
| 208 template <typename U> | 208 template <typename U> |
| 209 bool operator!=(const U* rhs) const { | 209 bool operator!=(const U* rhs) const { |
| 210 return ptr_ != rhs; | 210 return ptr_ != rhs; |
| 211 } | 211 } |
| 212 | 212 |
| 213 void swap(ScopedComPtr<Interface, interface_id>& r) { | 213 void swap(ScopedComPtr<Interface, interface_id>& r) { |
| 214 Interface* tmp = ptr_; | 214 Interface* tmp = ptr_; |
| 215 ptr_ = r.ptr_; | 215 ptr_ = r.ptr_; |
| 216 r.ptr_ = tmp; | 216 r.ptr_ = tmp; |
| 217 } | 217 } |
| 218 | 218 |
| 219 private: | 219 private: |
| 220 Interface* ptr_ = nullptr; | 220 Interface* ptr_ = nullptr; |
| 221 }; | 221 }; |
| 222 | 222 |
| 223 template <typename T, typename U> | 223 template <typename T, typename U> |
| 224 bool operator==(const T* lhs, const ScopedComPtr<U>& rhs) { | 224 bool operator==(const T* lhs, const ScopedComPtr<U>& rhs) { |
| 225 return lhs == rhs.get(); | 225 return lhs == rhs.Get(); |
| 226 } | 226 } |
| 227 | 227 |
| 228 template <typename T> | 228 template <typename T> |
| 229 bool operator==(const ScopedComPtr<T>& lhs, std::nullptr_t null) { | 229 bool operator==(const ScopedComPtr<T>& lhs, std::nullptr_t null) { |
| 230 return !static_cast<bool>(lhs); | 230 return !static_cast<bool>(lhs); |
| 231 } | 231 } |
| 232 | 232 |
| 233 template <typename T> | 233 template <typename T> |
| 234 bool operator==(std::nullptr_t null, const ScopedComPtr<T>& rhs) { | 234 bool operator==(std::nullptr_t null, const ScopedComPtr<T>& rhs) { |
| 235 return !static_cast<bool>(rhs); | 235 return !static_cast<bool>(rhs); |
| 236 } | 236 } |
| 237 | 237 |
| 238 template <typename T, typename U> | 238 template <typename T, typename U> |
| 239 bool operator!=(const T* lhs, const ScopedComPtr<U>& rhs) { | 239 bool operator!=(const T* lhs, const ScopedComPtr<U>& rhs) { |
| 240 return !operator==(lhs, rhs); | 240 return !operator==(lhs, rhs); |
| 241 } | 241 } |
| 242 | 242 |
| 243 template <typename T> | 243 template <typename T> |
| 244 bool operator!=(const ScopedComPtr<T>& lhs, std::nullptr_t null) { | 244 bool operator!=(const ScopedComPtr<T>& lhs, std::nullptr_t null) { |
| 245 return !operator==(lhs, null); | 245 return !operator==(lhs, null); |
| 246 } | 246 } |
| 247 | 247 |
| 248 template <typename T> | 248 template <typename T> |
| 249 bool operator!=(std::nullptr_t null, const ScopedComPtr<T>& rhs) { | 249 bool operator!=(std::nullptr_t null, const ScopedComPtr<T>& rhs) { |
| 250 return !operator==(null, rhs); | 250 return !operator==(null, rhs); |
| 251 } | 251 } |
| 252 | 252 |
| 253 template <typename T> | 253 template <typename T> |
| 254 std::ostream& operator<<(std::ostream& out, const ScopedComPtr<T>& p) { | 254 std::ostream& operator<<(std::ostream& out, const ScopedComPtr<T>& p) { |
| 255 return out << p.get(); | 255 return out << p.Get(); |
| 256 } | 256 } |
| 257 | 257 |
| 258 // Helper to make IID_PPV_ARGS work with ScopedComPtr. | 258 // Helper to make IID_PPV_ARGS work with ScopedComPtr. |
| 259 template <typename T> | 259 template <typename T> |
| 260 void** IID_PPV_ARGS_Helper(base::win::ScopedComPtr<T>* pp) throw() { | 260 void** IID_PPV_ARGS_Helper(base::win::ScopedComPtr<T>* pp) throw() { |
| 261 return pp->ReceiveVoid(); | 261 return pp->ReceiveVoid(); |
| 262 } | 262 } |
| 263 | 263 |
| 264 } // namespace win | 264 } // namespace win |
| 265 } // namespace base | 265 } // namespace base |
| 266 | 266 |
| 267 #endif // BASE_WIN_SCOPED_COMPTR_H_ | 267 #endif // BASE_WIN_SCOPED_COMPTR_H_ |
| OLD | NEW |