• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()10 WeakReference::Flag::Flag() : is_valid_(true) {}
11 
Invalidate()12 void WeakReference::Flag::Invalidate() {
13   is_valid_ = false;
14 }
15 
IsValid() const16 bool WeakReference::Flag::IsValid() const {
17   return is_valid_;
18 }
19 
20 WeakReference::Flag::~Flag() = default;
21 
22 WeakReference::WeakReference() = default;
23 
WeakReference(const scoped_refptr<Flag> & flag)24 WeakReference::WeakReference(const scoped_refptr<Flag>& flag) : flag_(flag) {}
25 
26 WeakReference::~WeakReference() = default;
27 
28 WeakReference::WeakReference(WeakReference&& other) = default;
29 
30 WeakReference::WeakReference(const WeakReference& other) = default;
31 
is_valid() const32 bool WeakReference::is_valid() const {
33   return flag_ && flag_->IsValid();
34 }
35 
36 WeakReferenceOwner::WeakReferenceOwner() = default;
37 
~WeakReferenceOwner()38 WeakReferenceOwner::~WeakReferenceOwner() {
39   Invalidate();
40 }
41 
GetRef() const42 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_);
48 }
49 
Invalidate()50 void WeakReferenceOwner::Invalidate() {
51   if (flag_) {
52     flag_->Invalidate();
53     flag_ = nullptr;
54   }
55 }
56 
WeakPtrBase()57 WeakPtrBase::WeakPtrBase() : ptr_(0) {}
58 
59 WeakPtrBase::~WeakPtrBase() = default;
60 
WeakPtrBase(const WeakReference & ref,uintptr_t ptr)61 WeakPtrBase::WeakPtrBase(const WeakReference& ref, uintptr_t ptr)
62     : ref_(ref), ptr_(ptr) {
63   DCHECK(ptr_);
64 }
65 
WeakPtrFactoryBase(uintptr_t ptr)66 WeakPtrFactoryBase::WeakPtrFactoryBase(uintptr_t ptr) : ptr_(ptr) {
67   DCHECK(ptr_);
68 }
69 
~WeakPtrFactoryBase()70 WeakPtrFactoryBase::~WeakPtrFactoryBase() {
71   ptr_ = 0;
72 }
73 
74 }  // namespace internal
75 }  // namespace base
76