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