OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_MEMORY_REF_COUNTED_H_ | 5 #ifndef BASE_MEMORY_REF_COUNTED_H_ |
6 #define BASE_MEMORY_REF_COUNTED_H_ | 6 #define BASE_MEMORY_REF_COUNTED_H_ |
7 | 7 |
8 #include <cassert> | 8 #include <cassert> |
9 #include <iosfwd> | 9 #include <iosfwd> |
10 #include <type_traits> | 10 #include <type_traits> |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 void swap(T** pp) { | 355 void swap(T** pp) { |
356 T* p = ptr_; | 356 T* p = ptr_; |
357 ptr_ = *pp; | 357 ptr_ = *pp; |
358 *pp = p; | 358 *pp = p; |
359 } | 359 } |
360 | 360 |
361 void swap(scoped_refptr<T>& r) { | 361 void swap(scoped_refptr<T>& r) { |
362 swap(&r.ptr_); | 362 swap(&r.ptr_); |
363 } | 363 } |
364 | 364 |
365 private: | 365 explicit operator bool() const { return ptr_ != nullptr; } |
366 template <typename U> friend class scoped_refptr; | |
367 | |
368 // Implement "Safe Bool Idiom" | |
369 // https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool | |
370 // | |
371 // Allow scoped_refptr<T> to be used in boolean expressions such as | |
372 // if (ref_ptr_instance) | |
373 // But do not become convertible to a real bool (which is dangerous). | |
374 // Implementation requires: | |
375 // typedef Testable | |
376 // operator Testable() const | |
377 // operator== | |
378 // operator!= | |
379 // | |
380 // == and != operators must be declared explicitly or dissallowed, as | |
381 // otherwise "ptr1 == ptr2" will compile but do the wrong thing (i.e., convert | |
382 // to Testable and then do the comparison). | |
383 // | |
384 // C++11 provides for "explicit operator bool()", however it is currently | |
385 // banned due to MSVS2013. https://chromium-cpp.appspot.com/#core-blacklist | |
386 typedef T* scoped_refptr::*Testable; | |
387 public: | |
388 operator Testable() const { return ptr_ ? &scoped_refptr::ptr_ : nullptr; } | |
389 | 366 |
390 template <typename U> | 367 template <typename U> |
391 bool operator==(const scoped_refptr<U>& rhs) const { | 368 bool operator==(const scoped_refptr<U>& rhs) const { |
392 return ptr_ == rhs.get(); | 369 return ptr_ == rhs.get(); |
393 } | 370 } |
394 | 371 |
395 template <typename U> | 372 template <typename U> |
396 bool operator!=(const scoped_refptr<U>& rhs) const { | 373 bool operator!=(const scoped_refptr<U>& rhs) const { |
397 return !operator==(rhs); | 374 return !operator==(rhs); |
398 } | 375 } |
399 | 376 |
400 template <typename U> | 377 template <typename U> |
401 bool operator<(const scoped_refptr<U>& rhs) const { | 378 bool operator<(const scoped_refptr<U>& rhs) const { |
402 return ptr_ < rhs.get(); | 379 return ptr_ < rhs.get(); |
403 } | 380 } |
404 | 381 |
405 protected: | 382 protected: |
406 T* ptr_; | 383 T* ptr_; |
407 | 384 |
408 private: | 385 private: |
| 386 // Friend required for move constructors that set r.ptr_ to null. |
| 387 template <typename U> |
| 388 friend class scoped_refptr; |
| 389 |
409 // Non-inline helpers to allow: | 390 // Non-inline helpers to allow: |
410 // class Opaque; | 391 // class Opaque; |
411 // extern template class scoped_refptr<Opaque>; | 392 // extern template class scoped_refptr<Opaque>; |
412 // Otherwise the compiler will complain that Opaque is an incomplete type. | 393 // Otherwise the compiler will complain that Opaque is an incomplete type. |
413 static void AddRef(T* ptr); | 394 static void AddRef(T* ptr); |
414 static void Release(T* ptr); | 395 static void Release(T* ptr); |
415 }; | 396 }; |
416 | 397 |
417 template <typename T> | 398 template <typename T> |
418 void scoped_refptr<T>::AddRef(T* ptr) { | 399 void scoped_refptr<T>::AddRef(T* ptr) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { | 431 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { |
451 return !operator==(lhs, rhs); | 432 return !operator==(lhs, rhs); |
452 } | 433 } |
453 | 434 |
454 template <typename T> | 435 template <typename T> |
455 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 436 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
456 return out << p.get(); | 437 return out << p.get(); |
457 } | 438 } |
458 | 439 |
459 #endif // BASE_MEMORY_REF_COUNTED_H_ | 440 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |