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 #include "base/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
6 | 6 |
7 namespace base { | 7 namespace base { |
8 namespace internal { | 8 namespace internal { |
9 | 9 |
10 WeakReference::Flag::Flag() : is_valid_(true) { | 10 WeakReference::Flag::Flag() : is_valid_(true) { |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 WeakReference::WeakReference() { | 34 WeakReference::WeakReference() { |
35 } | 35 } |
36 | 36 |
37 WeakReference::WeakReference(const Flag* flag) : flag_(flag) { | 37 WeakReference::WeakReference(const Flag* flag) : flag_(flag) { |
38 } | 38 } |
39 | 39 |
40 WeakReference::~WeakReference() { | 40 WeakReference::~WeakReference() { |
41 } | 41 } |
42 | 42 |
43 bool WeakReference::is_valid() const { | 43 bool WeakReference::is_valid() const { return flag_.get() && flag_->IsValid(); } |
44 return flag_ && flag_->IsValid(); | |
45 } | |
46 | 44 |
47 WeakReferenceOwner::WeakReferenceOwner() { | 45 WeakReferenceOwner::WeakReferenceOwner() { |
48 } | 46 } |
49 | 47 |
50 WeakReferenceOwner::~WeakReferenceOwner() { | 48 WeakReferenceOwner::~WeakReferenceOwner() { |
51 Invalidate(); | 49 Invalidate(); |
52 } | 50 } |
53 | 51 |
54 WeakReference WeakReferenceOwner::GetRef() const { | 52 WeakReference WeakReferenceOwner::GetRef() const { |
55 // If we hold the last reference to the Flag then create a new one. | 53 // If we hold the last reference to the Flag then create a new one. |
56 if (!HasRefs()) | 54 if (!HasRefs()) |
57 flag_ = new WeakReference::Flag(); | 55 flag_ = new WeakReference::Flag(); |
58 | 56 |
59 return WeakReference(flag_); | 57 return WeakReference(flag_.get()); |
60 } | 58 } |
61 | 59 |
62 void WeakReferenceOwner::Invalidate() { | 60 void WeakReferenceOwner::Invalidate() { |
63 if (flag_) { | 61 if (flag_.get()) { |
64 flag_->Invalidate(); | 62 flag_->Invalidate(); |
65 flag_ = NULL; | 63 flag_ = NULL; |
66 } | 64 } |
67 } | 65 } |
68 | 66 |
69 WeakPtrBase::WeakPtrBase() { | 67 WeakPtrBase::WeakPtrBase() { |
70 } | 68 } |
71 | 69 |
72 WeakPtrBase::~WeakPtrBase() { | 70 WeakPtrBase::~WeakPtrBase() { |
73 } | 71 } |
74 | 72 |
75 WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) { | 73 WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) { |
76 } | 74 } |
77 | 75 |
78 } // namespace internal | 76 } // namespace internal |
79 } // namespace base | 77 } // namespace base |
OLD | NEW |