OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_QUIC_PLATFORM_IMPL_QUIC_REFERENCE_COUNTED_IMPL_H_ |
| 6 #define NET_QUIC_PLATFORM_IMPL_QUIC_REFERENCE_COUNTED_IMPL_H_ |
| 7 |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "net/quic/platform/api/quic_export.h" |
| 10 |
| 11 namespace net { |
| 12 |
| 13 class QUIC_EXPORT_PRIVATE QuicReferenceCountedImpl |
| 14 : public base::RefCountedThreadSafe<QuicReferenceCountedImpl> { |
| 15 public: |
| 16 QuicReferenceCountedImpl() {} |
| 17 |
| 18 protected: |
| 19 virtual ~QuicReferenceCountedImpl() {} |
| 20 |
| 21 private: |
| 22 friend class base::RefCountedThreadSafe<QuicReferenceCountedImpl>; |
| 23 }; |
| 24 |
| 25 template <class T> |
| 26 class QuicReferenceCountedPointerImpl { |
| 27 public: |
| 28 QuicReferenceCountedPointerImpl() = default; |
| 29 |
| 30 // Constructor from raw pointer |p|. This guarantees the reference count of *p |
| 31 // is 1. This should be only called when a new object is created, calling this |
| 32 // on an already existent object does not increase its reference count. |
| 33 explicit QuicReferenceCountedPointerImpl(T* p) : refptr_(p) {} |
| 34 |
| 35 // Allows implicit conversion from nullptr. |
| 36 QuicReferenceCountedPointerImpl(std::nullptr_t) // NOLINT |
| 37 : refptr_(nullptr) {} |
| 38 |
| 39 // Copy and copy conversion constructors. It does not take the reference away |
| 40 // from |other| and they each end up with their own reference. |
| 41 template <typename U> |
| 42 QuicReferenceCountedPointerImpl( // NOLINT |
| 43 const QuicReferenceCountedPointerImpl<U>& other) |
| 44 : refptr_(other.refptr()) {} |
| 45 QuicReferenceCountedPointerImpl(const QuicReferenceCountedPointerImpl& other) |
| 46 : refptr_(other.refptr()) {} |
| 47 |
| 48 // Move constructors. After move, It adopts the reference from |other|. |
| 49 template <typename U> |
| 50 QuicReferenceCountedPointerImpl( // NOLINT |
| 51 QuicReferenceCountedPointerImpl<U>&& other) |
| 52 : refptr_(std::move(other.refptr())) {} |
| 53 QuicReferenceCountedPointerImpl(QuicReferenceCountedPointerImpl&& other) |
| 54 : refptr_(std::move(other.refptr())) {} |
| 55 |
| 56 ~QuicReferenceCountedPointerImpl() = default; |
| 57 |
| 58 // Copy assignments. |
| 59 QuicReferenceCountedPointerImpl& operator=( |
| 60 const QuicReferenceCountedPointerImpl& other) { |
| 61 refptr_ = other.refptr(); |
| 62 return *this; |
| 63 } |
| 64 template <typename U> |
| 65 QuicReferenceCountedPointerImpl<T>& operator=( |
| 66 const QuicReferenceCountedPointerImpl<U>& other) { |
| 67 refptr_ = other.refptr(); |
| 68 return *this; |
| 69 } |
| 70 |
| 71 // Move assignments. |
| 72 QuicReferenceCountedPointerImpl& operator=( |
| 73 QuicReferenceCountedPointerImpl&& other) { |
| 74 refptr_ = std::move(other.refptr()); |
| 75 return *this; |
| 76 } |
| 77 template <typename U> |
| 78 QuicReferenceCountedPointerImpl<T>& operator=( |
| 79 QuicReferenceCountedPointerImpl<U>&& other) { |
| 80 refptr_ = std::move(other.refptr()); |
| 81 return *this; |
| 82 } |
| 83 |
| 84 explicit operator bool() const { return static_cast<bool>(refptr_); } |
| 85 |
| 86 // Assignment operator on raw pointer. Drops a reference to current pointee, |
| 87 // if any and replaces it with |p|. This garantee the reference count of *p is |
| 88 // 1. This should only be used when a new object is created, calling this |
| 89 // on a already existent object does not increase its reference count. |
| 90 QuicReferenceCountedPointerImpl<T>& operator=(T* p) { |
| 91 refptr_ = p; |
| 92 return *this; |
| 93 } |
| 94 // Returns the raw pointer with no change in reference. |
| 95 T* get() const { return refptr_.get(); } |
| 96 |
| 97 // Accessors for the referenced object. |
| 98 // operator* and operator-> will assert() if there is no current object. |
| 99 T& operator*() const { return *refptr_; } |
| 100 T* operator->() const { |
| 101 assert(refptr_ != nullptr); |
| 102 return refptr_.get(); |
| 103 } |
| 104 |
| 105 scoped_refptr<T>& refptr() { return refptr_; } |
| 106 const scoped_refptr<T>& refptr() const { return refptr_; } |
| 107 |
| 108 private: |
| 109 scoped_refptr<T> refptr_; |
| 110 }; |
| 111 |
| 112 } // namespace net |
| 113 |
| 114 #endif // NET_QUIC_PLATFORM_IMPL_QUIC_REFERENCE_COUNTED_IMPL_H_ |
OLD | NEW |