1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mfbt/WeakPtr.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,200 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* Weak pointer functionality, implemented as a mixin for use with any class. */ 1.11 + 1.12 +/** 1.13 + * SupportsWeakPtr lets you have a pointer to an object 'Foo' without affecting 1.14 + * its lifetime. It works by creating a single shared reference counted object 1.15 + * (WeakReference) that each WeakPtr will access 'Foo' through. This lets 'Foo' 1.16 + * clear the pointer in the WeakReference without having to know about all of 1.17 + * the WeakPtrs to it and allows the WeakReference to live beyond the lifetime 1.18 + * of 'Foo'. 1.19 + * 1.20 + * PLEASE NOTE: This weak pointer implementation is not thread-safe. 1.21 + * 1.22 + * Note that when deriving from SupportsWeakPtr you should add 1.23 + * MOZ_DECLARE_REFCOUNTED_TYPENAME(ClassName) to the public section of your 1.24 + * class, where ClassName is the name of your class. 1.25 + * 1.26 + * The overhead of WeakPtr is that accesses to 'Foo' becomes an additional 1.27 + * dereference, and an additional heap allocated pointer sized object shared 1.28 + * between all of the WeakPtrs. 1.29 + * 1.30 + * Example of usage: 1.31 + * 1.32 + * // To have a class C support weak pointers, inherit from SupportsWeakPtr<C>. 1.33 + * class C : public SupportsWeakPtr<C> 1.34 + * { 1.35 + * public: 1.36 + * MOZ_DECLARE_REFCOUNTED_TYPENAME(C) 1.37 + * int num; 1.38 + * void act(); 1.39 + * }; 1.40 + * 1.41 + * C* ptr = new C(); 1.42 + * 1.43 + * // Get weak pointers to ptr. The first time asWeakPtr is called 1.44 + * // a reference counted WeakReference object is created that 1.45 + * // can live beyond the lifetime of 'ptr'. The WeakReference 1.46 + * // object will be notified of 'ptr's destruction. 1.47 + * WeakPtr<C> weak = ptr->asWeakPtr(); 1.48 + * WeakPtr<C> other = ptr->asWeakPtr(); 1.49 + * 1.50 + * // Test a weak pointer for validity before using it. 1.51 + * if (weak) { 1.52 + * weak->num = 17; 1.53 + * weak->act(); 1.54 + * } 1.55 + * 1.56 + * // Destroying the underlying object clears weak pointers to it. 1.57 + * delete ptr; 1.58 + * 1.59 + * MOZ_ASSERT(!weak, "Deleting |ptr| clears weak pointers to it."); 1.60 + * MOZ_ASSERT(!other, "Deleting |ptr| clears all weak pointers to it."); 1.61 + * 1.62 + * WeakPtr is typesafe and may be used with any class. It is not required that 1.63 + * the class be reference-counted or allocated in any particular way. 1.64 + * 1.65 + * The API was loosely inspired by Chromium's weak_ptr.h: 1.66 + * http://src.chromium.org/svn/trunk/src/base/memory/weak_ptr.h 1.67 + */ 1.68 + 1.69 +#ifndef mozilla_WeakPtr_h 1.70 +#define mozilla_WeakPtr_h 1.71 + 1.72 +#include "mozilla/ArrayUtils.h" 1.73 +#include "mozilla/Assertions.h" 1.74 +#include "mozilla/NullPtr.h" 1.75 +#include "mozilla/RefPtr.h" 1.76 +#include "mozilla/TypeTraits.h" 1.77 + 1.78 +#include <string.h> 1.79 + 1.80 +namespace mozilla { 1.81 + 1.82 +template <typename T, class WeakReference> class WeakPtrBase; 1.83 +template <typename T, class WeakReference> class SupportsWeakPtrBase; 1.84 + 1.85 +namespace detail { 1.86 + 1.87 +// This can live beyond the lifetime of the class derived from SupportsWeakPtrBase. 1.88 +template<class T> 1.89 +class WeakReference : public ::mozilla::RefCounted<WeakReference<T> > 1.90 +{ 1.91 + public: 1.92 + explicit WeakReference(T* p) : ptr(p) {} 1.93 + T* get() const { 1.94 + return ptr; 1.95 + } 1.96 + 1.97 +#ifdef MOZ_REFCOUNTED_LEAK_CHECKING 1.98 +#ifdef XP_WIN 1.99 +#define snprintf _snprintf 1.100 +#endif 1.101 + const char* typeName() const { 1.102 + static char nameBuffer[1024]; 1.103 + const char* innerType = ptr->typeName(); 1.104 + // We could do fancier length checks at runtime, but innerType is 1.105 + // controlled by us so we can ensure that this never causes a buffer 1.106 + // overflow by this assertion. 1.107 + MOZ_ASSERT(strlen(innerType) + sizeof("WeakReference<>") < ArrayLength(nameBuffer), 1.108 + "Exceedingly large type name"); 1.109 + snprintf(nameBuffer, ArrayLength(nameBuffer), "WeakReference<%s>", innerType); 1.110 + // This is usually not OK, but here we are returning a pointer to a static 1.111 + // buffer which will immediately be used by the caller. 1.112 + return nameBuffer; 1.113 + } 1.114 + size_t typeSize() const { 1.115 + return sizeof(*this); 1.116 + } 1.117 +#undef snprintf 1.118 +#endif 1.119 + 1.120 + private: 1.121 + friend class WeakPtrBase<T, WeakReference<T> >; 1.122 + friend class SupportsWeakPtrBase<T, WeakReference<T> >; 1.123 + void detach() { 1.124 + ptr = nullptr; 1.125 + } 1.126 + T* ptr; 1.127 +}; 1.128 + 1.129 +} // namespace detail 1.130 + 1.131 +template <typename T, class WeakReference> 1.132 +class SupportsWeakPtrBase 1.133 +{ 1.134 + public: 1.135 + WeakPtrBase<T, WeakReference> asWeakPtr() { 1.136 + if (!weakRef) 1.137 + weakRef = new WeakReference(static_cast<T*>(this)); 1.138 + return WeakPtrBase<T, WeakReference>(weakRef); 1.139 + } 1.140 + 1.141 + protected: 1.142 + ~SupportsWeakPtrBase() { 1.143 + static_assert(IsBaseOf<SupportsWeakPtrBase<T, WeakReference>, T>::value, 1.144 + "T must derive from SupportsWeakPtrBase<T, WeakReference>"); 1.145 + if (weakRef) 1.146 + weakRef->detach(); 1.147 + } 1.148 + 1.149 + private: 1.150 + friend class WeakPtrBase<T, WeakReference>; 1.151 + 1.152 + RefPtr<WeakReference> weakRef; 1.153 +}; 1.154 + 1.155 +template <typename T> 1.156 +class SupportsWeakPtr : public SupportsWeakPtrBase<T, detail::WeakReference<T> > 1.157 +{ 1.158 +}; 1.159 + 1.160 +template <typename T, class WeakReference> 1.161 +class WeakPtrBase 1.162 +{ 1.163 + public: 1.164 + WeakPtrBase(const WeakPtrBase<T, WeakReference>& o) : ref(o.ref) {} 1.165 + // Ensure that ref is dereferenceable in the uninitialized state 1.166 + WeakPtrBase() : ref(new WeakReference(nullptr)) {} 1.167 + 1.168 + operator T*() const { 1.169 + return ref->get(); 1.170 + } 1.171 + T& operator*() const { 1.172 + return *ref->get(); 1.173 + } 1.174 + 1.175 + T* operator->() const { 1.176 + return ref->get(); 1.177 + } 1.178 + 1.179 + T* get() const { 1.180 + return ref->get(); 1.181 + } 1.182 + 1.183 + private: 1.184 + friend class SupportsWeakPtrBase<T, WeakReference>; 1.185 + 1.186 + explicit WeakPtrBase(const RefPtr<WeakReference> &o) : ref(o) {} 1.187 + 1.188 + RefPtr<WeakReference> ref; 1.189 +}; 1.190 + 1.191 +template <typename T> 1.192 +class WeakPtr : public WeakPtrBase<T, detail::WeakReference<T> > 1.193 +{ 1.194 + typedef WeakPtrBase<T, detail::WeakReference<T> > Base; 1.195 + public: 1.196 + WeakPtr(const WeakPtr<T>& o) : Base(o) {} 1.197 + WeakPtr(const Base& o) : Base(o) {} 1.198 + WeakPtr() {} 1.199 +}; 1.200 + 1.201 +} // namespace mozilla 1.202 + 1.203 +#endif /* mozilla_WeakPtr_h */