Home | History | Annotate | Download | only in memory
      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() : is_valid_(true) {
     11 }
     12 
     13 void WeakReference::Flag::Invalidate() {
     14   is_valid_ = false;
     15 }
     16 
     17 bool WeakReference::Flag::IsValid() const {
     18   return is_valid_;
     19 }
     20 
     21 WeakReference::Flag::~Flag() {
     22 }
     23 
     24 WeakReference::WeakReference() {
     25 }
     26 
     27 WeakReference::WeakReference(const Flag* flag) : flag_(flag) {
     28 }
     29 
     30 WeakReference::~WeakReference() {
     31 }
     32 
     33 bool WeakReference::is_valid() const { return flag_.get() && flag_->IsValid(); }
     34 
     35 WeakReferenceOwner::WeakReferenceOwner() {
     36 }
     37 
     38 WeakReferenceOwner::~WeakReferenceOwner() {
     39   Invalidate();
     40 }
     41 
     42 WeakReference WeakReferenceOwner::GetRef() const {
     43   // If we hold the last reference to the Flag then create a new one.
     44   if (!HasRefs())
     45     flag_ = new WeakReference::Flag();
     46 
     47   return WeakReference(flag_.get());
     48 }
     49 
     50 void WeakReferenceOwner::Invalidate() {
     51   if (flag_.get()) {
     52     flag_->Invalidate();
     53     flag_ = NULL;
     54   }
     55 }
     56 
     57 WeakPtrBase::WeakPtrBase() {
     58 }
     59 
     60 WeakPtrBase::~WeakPtrBase() {
     61 }
     62 
     63 WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) {
     64 }
     65 
     66 }  // namespace internal
     67 }  // namespace base
     68