1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* Weak pointer functionality, implemented as a mixin for use with any class. */
9 * SupportsWeakPtr lets you have a pointer to an object 'Foo' without affecting
10 * its lifetime. It works by creating a single shared reference counted object
11 * (WeakReference) that each WeakPtr will access 'Foo' through. This lets 'Foo'
12 * clear the pointer in the WeakReference without having to know about all of
13 * the WeakPtrs to it and allows the WeakReference to live beyond the lifetime
16 * The overhead of WeakPtr is that accesses to 'Foo' becomes an additional
17 * dereference, and an additional heap allocated pointer sized object shared
18 * between all of the WeakPtrs.
22 * // To have a class C support weak pointers, inherit from SupportsWeakPtr<C>.
23 * class C : public SupportsWeakPtr<C>
32 * // Get weak pointers to ptr. The first time asWeakPtr is called
33 * // a reference counted WeakReference object is created that
34 * // can live beyond the lifetime of 'ptr'. The WeakReference
35 * // object will be notified of 'ptr's destruction.
36 * WeakPtr<C> weak = ptr->asWeakPtr();
37 * WeakPtr<C> other = ptr->asWeakPtr();
39 * // Test a weak pointer for validity before using it.
45 * // Destroying the underlying object clears weak pointers to it.
48 * MOZ_ASSERT(!weak, "Deleting |ptr| clears weak pointers to it.");
49 * MOZ_ASSERT(!other, "Deleting |ptr| clears all weak pointers to it.");
51 * WeakPtr is typesafe and may be used with any class. It is not required that
52 * the class be reference-counted or allocated in any particular way.
54 * The API was loosely inspired by Chromium's weak_ptr.h:
55 * http://src.chromium.org/svn/trunk/src/base/memory/weak_ptr.h
58 #ifndef mozilla_WeakPtr_h_
59 #define mozilla_WeakPtr_h_
61 #include "mozilla/Assertions.h"
62 #include "mozilla/NullPtr.h"
63 #include "mozilla/RefPtr.h"
64 #include "mozilla/TypeTraits.h"
68 template <typename T
> class WeakPtr
;
74 WeakPtr
<T
> asWeakPtr() {
76 weakRef
= new WeakReference(static_cast<T
*>(this));
77 return WeakPtr
<T
>(weakRef
);
82 MOZ_STATIC_ASSERT((IsBaseOf
<SupportsWeakPtr
<T
>, T
>::value
), "T must derive from SupportsWeakPtr<T>");
88 friend class WeakPtr
<T
>;
90 // This can live beyond the lifetime of the class derived from SupportsWeakPtr.
91 class WeakReference
: public RefCounted
<WeakReference
>
94 explicit WeakReference(T
* p
) : ptr(p
) {}
100 friend class WeakPtr
<T
>;
101 friend class SupportsWeakPtr
<T
>;
108 RefPtr
<WeakReference
> weakRef
;
111 template <typename T
>
115 WeakPtr(const WeakPtr
<T
>& o
) : ref(o
.ref
) {}
116 // Ensure that ref is dereferenceable in the uninitialized state
117 WeakPtr() : ref(new typename SupportsWeakPtr
<T
>::WeakReference(nullptr)) {}
119 operator T
*() const {
122 T
& operator*() const {
126 T
* operator->() const {
135 friend class SupportsWeakPtr
<T
>;
137 explicit WeakPtr(const RefPtr
<typename SupportsWeakPtr
<T
>::WeakReference
> &o
) : ref(o
) {}
139 RefPtr
<typename SupportsWeakPtr
<T
>::WeakReference
> ref
;
142 } // namespace mozilla
144 #endif /* ifdef mozilla_WeakPtr_h_ */