Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / memory / weak_ptr.cc
blobd9ce86ad18901a0719255c031fae1a1357ad1f2f
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
3 // found in the LICENSE file.
5 #include "base/memory/weak_ptr.h"
7 namespace base {
8 namespace internal {
10 WeakReference::Flag::Flag() : is_valid_(true) {
11 // Flags only become bound when checked for validity, or invalidated,
12 // so that we can check that later validity/invalidation operations on
13 // the same Flag take place on the same sequenced thread.
14 sequence_checker_.DetachFromSequence();
17 void WeakReference::Flag::Invalidate() {
18 // The flag being invalidated with a single ref implies that there are no
19 // weak pointers in existence. Allow deletion on other thread in this case.
20 DCHECK(sequence_checker_.CalledOnValidSequencedThread() || HasOneRef())
21 << "WeakPtrs must be invalidated on the same sequenced thread.";
22 is_valid_ = false;
25 bool WeakReference::Flag::IsValid() const {
26 DCHECK(sequence_checker_.CalledOnValidSequencedThread())
27 << "WeakPtrs must be checked on the same sequenced thread.";
28 return is_valid_;
31 WeakReference::Flag::~Flag() {
34 WeakReference::WeakReference() {
37 WeakReference::WeakReference(const Flag* flag) : flag_(flag) {
40 WeakReference::~WeakReference() {
43 bool WeakReference::is_valid() const { return flag_.get() && flag_->IsValid(); }
45 WeakReferenceOwner::WeakReferenceOwner() {
48 WeakReferenceOwner::~WeakReferenceOwner() {
49 Invalidate();
52 WeakReference WeakReferenceOwner::GetRef() const {
53 // If we hold the last reference to the Flag then create a new one.
54 if (!HasRefs())
55 flag_ = new WeakReference::Flag();
57 return WeakReference(flag_.get());
60 void WeakReferenceOwner::Invalidate() {
61 if (flag_.get()) {
62 flag_->Invalidate();
63 flag_ = NULL;
67 WeakPtrBase::WeakPtrBase() {
70 WeakPtrBase::~WeakPtrBase() {
73 WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) {
76 } // namespace internal
77 } // namespace base