michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Weak pointer functionality, implemented as a mixin for use with any class. */ michael@0: michael@0: /** michael@0: * SupportsWeakPtr lets you have a pointer to an object 'Foo' without affecting michael@0: * its lifetime. It works by creating a single shared reference counted object michael@0: * (WeakReference) that each WeakPtr will access 'Foo' through. This lets 'Foo' michael@0: * clear the pointer in the WeakReference without having to know about all of michael@0: * the WeakPtrs to it and allows the WeakReference to live beyond the lifetime michael@0: * of 'Foo'. michael@0: * michael@0: * PLEASE NOTE: This weak pointer implementation is not thread-safe. michael@0: * michael@0: * Note that when deriving from SupportsWeakPtr you should add michael@0: * MOZ_DECLARE_REFCOUNTED_TYPENAME(ClassName) to the public section of your michael@0: * class, where ClassName is the name of your class. michael@0: * michael@0: * The overhead of WeakPtr is that accesses to 'Foo' becomes an additional michael@0: * dereference, and an additional heap allocated pointer sized object shared michael@0: * between all of the WeakPtrs. michael@0: * michael@0: * Example of usage: michael@0: * michael@0: * // To have a class C support weak pointers, inherit from SupportsWeakPtr. michael@0: * class C : public SupportsWeakPtr michael@0: * { michael@0: * public: michael@0: * MOZ_DECLARE_REFCOUNTED_TYPENAME(C) michael@0: * int num; michael@0: * void act(); michael@0: * }; michael@0: * michael@0: * C* ptr = new C(); michael@0: * michael@0: * // Get weak pointers to ptr. The first time asWeakPtr is called michael@0: * // a reference counted WeakReference object is created that michael@0: * // can live beyond the lifetime of 'ptr'. The WeakReference michael@0: * // object will be notified of 'ptr's destruction. michael@0: * WeakPtr weak = ptr->asWeakPtr(); michael@0: * WeakPtr other = ptr->asWeakPtr(); michael@0: * michael@0: * // Test a weak pointer for validity before using it. michael@0: * if (weak) { michael@0: * weak->num = 17; michael@0: * weak->act(); michael@0: * } michael@0: * michael@0: * // Destroying the underlying object clears weak pointers to it. michael@0: * delete ptr; michael@0: * michael@0: * MOZ_ASSERT(!weak, "Deleting |ptr| clears weak pointers to it."); michael@0: * MOZ_ASSERT(!other, "Deleting |ptr| clears all weak pointers to it."); michael@0: * michael@0: * WeakPtr is typesafe and may be used with any class. It is not required that michael@0: * the class be reference-counted or allocated in any particular way. michael@0: * michael@0: * The API was loosely inspired by Chromium's weak_ptr.h: michael@0: * http://src.chromium.org/svn/trunk/src/base/memory/weak_ptr.h michael@0: */ michael@0: michael@0: #ifndef mozilla_WeakPtr_h michael@0: #define mozilla_WeakPtr_h michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/NullPtr.h" michael@0: #include "mozilla/RefPtr.h" michael@0: #include "mozilla/TypeTraits.h" michael@0: michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: michael@0: template class WeakPtrBase; michael@0: template class SupportsWeakPtrBase; michael@0: michael@0: namespace detail { michael@0: michael@0: // This can live beyond the lifetime of the class derived from SupportsWeakPtrBase. michael@0: template michael@0: class WeakReference : public ::mozilla::RefCounted > michael@0: { michael@0: public: michael@0: explicit WeakReference(T* p) : ptr(p) {} michael@0: T* get() const { michael@0: return ptr; michael@0: } michael@0: michael@0: #ifdef MOZ_REFCOUNTED_LEAK_CHECKING michael@0: #ifdef XP_WIN michael@0: #define snprintf _snprintf michael@0: #endif michael@0: const char* typeName() const { michael@0: static char nameBuffer[1024]; michael@0: const char* innerType = ptr->typeName(); michael@0: // We could do fancier length checks at runtime, but innerType is michael@0: // controlled by us so we can ensure that this never causes a buffer michael@0: // overflow by this assertion. michael@0: MOZ_ASSERT(strlen(innerType) + sizeof("WeakReference<>") < ArrayLength(nameBuffer), michael@0: "Exceedingly large type name"); michael@0: snprintf(nameBuffer, ArrayLength(nameBuffer), "WeakReference<%s>", innerType); michael@0: // This is usually not OK, but here we are returning a pointer to a static michael@0: // buffer which will immediately be used by the caller. michael@0: return nameBuffer; michael@0: } michael@0: size_t typeSize() const { michael@0: return sizeof(*this); michael@0: } michael@0: #undef snprintf michael@0: #endif michael@0: michael@0: private: michael@0: friend class WeakPtrBase >; michael@0: friend class SupportsWeakPtrBase >; michael@0: void detach() { michael@0: ptr = nullptr; michael@0: } michael@0: T* ptr; michael@0: }; michael@0: michael@0: } // namespace detail michael@0: michael@0: template michael@0: class SupportsWeakPtrBase michael@0: { michael@0: public: michael@0: WeakPtrBase asWeakPtr() { michael@0: if (!weakRef) michael@0: weakRef = new WeakReference(static_cast(this)); michael@0: return WeakPtrBase(weakRef); michael@0: } michael@0: michael@0: protected: michael@0: ~SupportsWeakPtrBase() { michael@0: static_assert(IsBaseOf, T>::value, michael@0: "T must derive from SupportsWeakPtrBase"); michael@0: if (weakRef) michael@0: weakRef->detach(); michael@0: } michael@0: michael@0: private: michael@0: friend class WeakPtrBase; michael@0: michael@0: RefPtr weakRef; michael@0: }; michael@0: michael@0: template michael@0: class SupportsWeakPtr : public SupportsWeakPtrBase > michael@0: { michael@0: }; michael@0: michael@0: template michael@0: class WeakPtrBase michael@0: { michael@0: public: michael@0: WeakPtrBase(const WeakPtrBase& o) : ref(o.ref) {} michael@0: // Ensure that ref is dereferenceable in the uninitialized state michael@0: WeakPtrBase() : ref(new WeakReference(nullptr)) {} michael@0: michael@0: operator T*() const { michael@0: return ref->get(); michael@0: } michael@0: T& operator*() const { michael@0: return *ref->get(); michael@0: } michael@0: michael@0: T* operator->() const { michael@0: return ref->get(); michael@0: } michael@0: michael@0: T* get() const { michael@0: return ref->get(); michael@0: } michael@0: michael@0: private: michael@0: friend class SupportsWeakPtrBase; michael@0: michael@0: explicit WeakPtrBase(const RefPtr &o) : ref(o) {} michael@0: michael@0: RefPtr ref; michael@0: }; michael@0: michael@0: template michael@0: class WeakPtr : public WeakPtrBase > michael@0: { michael@0: typedef WeakPtrBase > Base; michael@0: public: michael@0: WeakPtr(const WeakPtr& o) : Base(o) {} michael@0: WeakPtr(const Base& o) : Base(o) {} michael@0: WeakPtr() {} michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif /* mozilla_WeakPtr_h */