Bug 835381 - Unit tests for the MediaSniffer to make sure Matroska files are not...
[gecko.git] / mfbt / WeakPtr.h
blobb8437f5bcf6dc2ea2fb1930292f69669a3b3db9c
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. */
8 /**
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
14 * of 'Foo'.
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.
20 * Example of usage:
22 * // To have a class C support weak pointers, inherit from SupportsWeakPtr<C>.
23 * class C : public SupportsWeakPtr<C>
24 * {
25 * public:
26 * int num;
27 * void act();
28 * };
30 * C* ptr = new 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.
40 * if (weak) {
41 * weak->num = 17;
42 * weak->act();
43 * }
45 * // Destroying the underlying object clears weak pointers to it.
46 * delete ptr;
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"
66 namespace mozilla {
68 template <typename T> class WeakPtr;
70 template <typename T>
71 class SupportsWeakPtr
73 public:
74 WeakPtr<T> asWeakPtr() {
75 if (!weakRef)
76 weakRef = new WeakReference(static_cast<T*>(this));
77 return WeakPtr<T>(weakRef);
80 protected:
81 ~SupportsWeakPtr() {
82 MOZ_STATIC_ASSERT((IsBaseOf<SupportsWeakPtr<T>, T>::value), "T must derive from SupportsWeakPtr<T>");
83 if (weakRef)
84 weakRef->detach();
87 private:
88 friend class WeakPtr<T>;
90 // This can live beyond the lifetime of the class derived from SupportsWeakPtr.
91 class WeakReference : public RefCounted<WeakReference>
93 public:
94 explicit WeakReference(T* p) : ptr(p) {}
95 T* get() const {
96 return ptr;
99 private:
100 friend class WeakPtr<T>;
101 friend class SupportsWeakPtr<T>;
102 void detach() {
103 ptr = nullptr;
105 T* ptr;
108 RefPtr<WeakReference> weakRef;
111 template <typename T>
112 class WeakPtr
114 public:
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 {
120 return ref->get();
122 T& operator*() const {
123 return *ref->get();
126 T* operator->() const {
127 return ref->get();
130 T* get() const {
131 return ref->get();
134 private:
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_ */