| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #include "rtc_base/weak_ptr.h" |
| 12 |
| 13 | // The implementation is borrowed from chromium except that it does not |
| 14 | // implement SupportsWeakPtr. |
| 15 |
| 16 | namespace rtc { |
| 17 | namespace internal { |
| 18 |
| 19 | WeakReference::Flag::Flag() : is_valid_(true) {} |
| 20 |
| 21 | void WeakReference::Flag::Invalidate() { |
| 22 | RTC_DCHECK(checker_.IsCurrent()) |
| 23 | << "WeakPtrs must be invalidated on the same sequence."; |
| 24 | is_valid_ = false; |
| 25 | } |
| 26 |
| 27 | bool WeakReference::Flag::IsValid() const { |
| 28 | RTC_DCHECK(checker_.IsCurrent()) |
| 29 | << "WeakPtrs must be checked on the same sequence."; |
| 30 | return is_valid_; |
| 31 | } |
| 32 |
| 33 | WeakReference::Flag::~Flag() {} |
| 34 |
| 35 | WeakReference::WeakReference() {} |
| 36 |
| 37 | WeakReference::WeakReference(const Flag* flag) : flag_(flag) {} |
| 38 |
| 39 | WeakReference::~WeakReference() {} |
| 40 |
| 41 | WeakReference::WeakReference(WeakReference&& other) = default; |
| 42 |
| 43 | WeakReference::WeakReference(const WeakReference& other) = default; |
| 44 |
| 45 | bool WeakReference::is_valid() const { |
| 46 | return flag_.get() && flag_->IsValid(); |
| 47 | } |
| 48 |
| 49 | WeakReferenceOwner::WeakReferenceOwner() {} |
| 50 |
| 51 | WeakReferenceOwner::~WeakReferenceOwner() { |
| 52 | Invalidate(); |
| 53 | } |
| 54 |
| 55 | WeakReference WeakReferenceOwner::GetRef() const { |
| 56 | // If we hold the last reference to the Flag then create a new one. |
| 57 | if (!HasRefs()) |
| 58 | flag_ = new RefCountedObject<WeakReference::Flag>(); |
| 59 |
| 60 | return WeakReference(flag_.get()); |
| 61 | } |
| 62 |
| 63 | void WeakReferenceOwner::Invalidate() { |
| 64 | if (flag_.get()) { |
| 65 | flag_->Invalidate(); |
| 66 | flag_ = nullptr; |
| 67 | } |
| 68 | } |
| 69 |
| 70 | WeakPtrBase::WeakPtrBase() {} |
| 71 |
| 72 | WeakPtrBase::~WeakPtrBase() {} |
| 73 |
| 74 | WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) {} |
| 75 |
| 76 | } // namespace internal |
| 77 | } // namespace rtc |
| 78 |