Line data Source code
1 : /*
2 : * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 : *
4 : * Use of this source code is governed by a BSD-style license
5 : * that can be found in the LICENSE file in the root of the source
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 "webrtc/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 0 : WeakReference::Flag::Flag() : is_valid_(true) {
20 : // Flags only become bound when checked for validity, or invalidated,
21 : // so that we can check that later validity/invalidation operations on
22 : // the same Flag take place on the same sequence.
23 0 : checker_.Detach();
24 0 : }
25 :
26 0 : void WeakReference::Flag::Invalidate() {
27 0 : RTC_DCHECK(checker_.CalledSequentially())
28 0 : << "WeakPtrs must be invalidated on the same sequence.";
29 0 : is_valid_ = false;
30 0 : }
31 :
32 0 : bool WeakReference::Flag::IsValid() const {
33 0 : RTC_DCHECK(checker_.CalledSequentially())
34 0 : << "WeakPtrs must be checked on the same sequence.";
35 0 : return is_valid_;
36 : }
37 :
38 0 : WeakReference::Flag::~Flag() {}
39 :
40 0 : WeakReference::WeakReference() {}
41 :
42 0 : WeakReference::WeakReference(const Flag* flag) : flag_(flag) {}
43 :
44 0 : WeakReference::~WeakReference() {}
45 :
46 : WeakReference::WeakReference(WeakReference&& other) = default;
47 :
48 : WeakReference::WeakReference(const WeakReference& other) = default;
49 :
50 0 : bool WeakReference::is_valid() const {
51 0 : return flag_.get() && flag_->IsValid();
52 : }
53 :
54 0 : WeakReferenceOwner::WeakReferenceOwner() {
55 0 : checker_.Detach();
56 0 : }
57 :
58 0 : WeakReferenceOwner::~WeakReferenceOwner() {
59 0 : RTC_DCHECK(checker_.CalledSequentially());
60 0 : Invalidate();
61 0 : }
62 :
63 0 : WeakReference WeakReferenceOwner::GetRef() const {
64 0 : RTC_DCHECK(checker_.CalledSequentially());
65 : // If we hold the last reference to the Flag then create a new one.
66 0 : if (!HasRefs())
67 0 : flag_ = new RefCountedObject<WeakReference::Flag>();
68 :
69 0 : return WeakReference(flag_.get());
70 : }
71 :
72 0 : void WeakReferenceOwner::Invalidate() {
73 0 : RTC_DCHECK(checker_.CalledSequentially());
74 0 : if (flag_.get()) {
75 0 : flag_->Invalidate();
76 0 : flag_ = NULL;
77 : }
78 0 : }
79 :
80 0 : WeakPtrBase::WeakPtrBase() {}
81 :
82 0 : WeakPtrBase::~WeakPtrBase() {}
83 :
84 0 : WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) {}
85 :
86 : } // namespace internal
87 : } // namespace rtc
|