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. 4 5 #include "base/memory/weak_ptr.h" 6 7 namespace base { 8 namespace internal { 9 Flag(Flag ** handle)10WeakReference::Flag::Flag(Flag** handle) : handle_(handle) { 11 } 12 Invalidate()13void WeakReference::Flag::Invalidate() { 14 DCHECK(thread_checker_.CalledOnValidThread()); 15 handle_ = NULL; 16 } 17 IsValid() const18bool WeakReference::Flag::IsValid() const { 19 DCHECK(thread_checker_.CalledOnValidThread()); 20 return handle_ != NULL; 21 } 22 ~Flag()23WeakReference::Flag::~Flag() { 24 if (handle_) 25 *handle_ = NULL; 26 } 27 WeakReference()28WeakReference::WeakReference() { 29 } 30 WeakReference(Flag * flag)31WeakReference::WeakReference(Flag* flag) : flag_(flag) { 32 } 33 ~WeakReference()34WeakReference::~WeakReference() { 35 } 36 is_valid() const37bool WeakReference::is_valid() const { 38 return flag_ && flag_->IsValid(); 39 } 40 WeakReferenceOwner()41WeakReferenceOwner::WeakReferenceOwner() : flag_(NULL) { 42 } 43 ~WeakReferenceOwner()44WeakReferenceOwner::~WeakReferenceOwner() { 45 Invalidate(); 46 } 47 GetRef() const48WeakReference WeakReferenceOwner::GetRef() const { 49 if (!flag_) 50 flag_ = new WeakReference::Flag(&flag_); 51 return WeakReference(flag_); 52 } 53 Invalidate()54void WeakReferenceOwner::Invalidate() { 55 if (flag_) { 56 flag_->Invalidate(); 57 flag_ = NULL; 58 } 59 } 60 WeakPtrBase()61WeakPtrBase::WeakPtrBase() { 62 } 63 ~WeakPtrBase()64WeakPtrBase::~WeakPtrBase() { 65 } 66 WeakPtrBase(const WeakReference & ref)67WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) { 68 } 69 70 } // namespace internal 71 } // namespace base 72