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
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* Helpers for defining and using refcounted objects. */
8 #ifndef mozilla_RefPtr_h_
9 #define mozilla_RefPtr_h_
11 #include "mozilla/Assertions.h"
12 #include "mozilla/Attributes.h"
16 template<typename T
> class RefCounted
;
17 template<typename T
> class RefPtr
;
18 template<typename T
> class TemporaryRef
;
19 template<typename T
> class OutParamRef
;
20 template<typename T
> OutParamRef
<T
> byRef(RefPtr
<T
>&);
23 * RefCounted<T> is a sort of a "mixin" for a class T. RefCounted
24 * manages, well, refcounting for T, and because RefCounted is
25 * parameterized on T, RefCounted<T> can call T's destructor directly.
26 * This means T doesn't need to have a virtual dtor and so doesn't
29 * RefCounted<T> is created with refcount == 0. Newly-allocated
30 * RefCounted<T> must immediately be assigned to a RefPtr to make the
31 * refcount > 0. It's an error to allocate and free a bare
32 * RefCounted<T>, i.e. outside of the RefPtr machinery. Attempts to
33 * do so will abort DEBUG builds.
35 * Live RefCounted<T> have refcount > 0. The lifetime (refcounts) of
36 * live RefCounted<T> are controlled by RefPtr<T> and
37 * RefPtr<super/subclass of T>. Upon a transition from refcounted==1
38 * to 0, the RefCounted<T> "dies" and is destroyed. The "destroyed"
39 * state is represented in DEBUG builds by refcount==0xffffdead. This
40 * state distinguishes use-before-ref (refcount==0) from
41 * use-after-destroy (refcount==0xffffdead).
45 static const int DEAD
= 0xffffdead;
52 friend class RefPtr
<T
>;
55 RefCounted() : refCnt(0) { }
56 ~RefCounted() { MOZ_ASSERT(refCnt
== detail::DEAD
); }
59 // Compatibility with nsRefPtr.
61 MOZ_ASSERT(refCnt
>= 0);
66 MOZ_ASSERT(refCnt
> 0);
69 refCnt
= detail::DEAD
;
71 delete static_cast<T
*>(this);
75 // Compatibility with wtf::RefPtr.
76 void ref() { AddRef(); }
77 void deref() { Release(); }
78 int refCount() const { return refCnt
; }
79 bool hasOneRef() const {
80 MOZ_ASSERT(refCnt
> 0);
89 * RefPtr points to a refcounted thing that has AddRef and Release
90 * methods to increase/decrease the refcount, respectively. After a
91 * RefPtr<T> is assigned a T*, the T* can be used through the RefPtr
94 * A RefPtr can forget its underlying T*, which results in the T*
95 * being wrapped in a temporary object until the T* is either
96 * re-adopted from or released by the temporary.
101 // To allow them to use unref()
102 friend class TemporaryRef
<T
>;
103 friend class OutParamRef
<T
>;
108 RefPtr() : ptr(0) { }
109 RefPtr(const RefPtr
& o
) : ptr(ref(o
.ptr
)) {}
110 RefPtr(const TemporaryRef
<T
>& o
) : ptr(o
.drop()) {}
111 RefPtr(T
* t
) : ptr(ref(t
)) {}
114 RefPtr(const RefPtr
<U
>& o
) : ptr(ref(o
.get())) {}
116 ~RefPtr() { unref(ptr
); }
118 RefPtr
& operator=(const RefPtr
& o
) {
122 RefPtr
& operator=(const TemporaryRef
<T
>& o
) {
126 RefPtr
& operator=(T
* t
) {
132 RefPtr
& operator=(const RefPtr
<U
>& o
) {
133 assign(ref(o
.get()));
137 TemporaryRef
<T
> forget() {
140 return TemporaryRef
<T
>(tmp
, DontRef());
143 T
* get() const { return ptr
; }
144 operator T
*() const { return ptr
; }
145 T
* operator->() const { return ptr
; }
146 T
& operator*() const { return *ptr
; }
148 operator TemporaryRef
<U
>() { return TemporaryRef
<U
>(ptr
); }
158 static MOZ_ALWAYS_INLINE T
* ref(T
* t
) {
164 static MOZ_ALWAYS_INLINE
void unref(T
* t
) {
171 * TemporaryRef<T> represents an object that holds a temporary
172 * reference to a T. TemporaryRef objects can't be manually ref'd or
173 * unref'd (being temporaries, not lvalues), so can only relinquish
174 * references to other objects, or unref on destruction.
179 // To allow it to construct TemporaryRef from a bare T*
180 friend class RefPtr
<T
>;
182 typedef typename RefPtr
<T
>::DontRef DontRef
;
185 TemporaryRef(T
* t
) : ptr(RefPtr
<T
>::ref(t
)) {}
186 TemporaryRef(const TemporaryRef
& o
) : ptr(o
.drop()) {}
189 TemporaryRef(const TemporaryRef
<U
>& o
) : ptr(o
.drop()) {}
191 ~TemporaryRef() { RefPtr
<T
>::unref(ptr
); }
200 TemporaryRef(T
* t
, const DontRef
&) : ptr(t
) {}
204 TemporaryRef() MOZ_DELETE
;
205 void operator=(const TemporaryRef
&) MOZ_DELETE
;
209 * OutParamRef is a wrapper that tracks a refcounted pointer passed as
210 * an outparam argument to a function. OutParamRef implements COM T**
211 * outparam semantics: this requires the callee to AddRef() the T*
212 * returned through the T** outparam on behalf of the caller. This
213 * means the caller (through OutParamRef) must Release() the old
214 * object contained in the tracked RefPtr. It's OK if the callee
215 * returns the same T* passed to it through the T** outparam, as long
216 * as the callee obeys the COM discipline.
218 * Prefer returning TemporaryRef<T> from functions over creating T**
219 * outparams and passing OutParamRef<T> to T**. Prefer RefPtr<T>*
220 * outparams over T** outparams.
225 friend OutParamRef byRef
<T
>(RefPtr
<T
>&);
229 RefPtr
<T
>::unref(refPtr
.ptr
);
233 operator T
**() { return &tmp
; }
236 OutParamRef(RefPtr
<T
>& p
) : refPtr(p
), tmp(p
.get()) {}
241 OutParamRef() MOZ_DELETE
;
242 OutParamRef
& operator=(const OutParamRef
&) MOZ_DELETE
;
246 * byRef cooperates with OutParamRef to implement COM outparam semantics.
250 byRef(RefPtr
<T
>& ptr
)
252 return OutParamRef
<T
>(ptr
);
255 } // namespace mozilla
257 #endif // mozilla_RefPtr_h_
262 // Command line that builds these tests
264 // cp RefPtr.h test.cc && g++ -g -Wall -pedantic -DDEBUG -o test test.cc && ./test
266 using namespace mozilla
;
268 struct Foo
: public RefCounted
<Foo
>
270 Foo() : dead(false) { }
278 static int numDestroyed
;
280 int Foo::numDestroyed
;
282 struct Bar
: public Foo
{ };
287 return RefPtr
<Foo
>(new Foo());
300 // Kids, don't try this at home
305 GetPassedFoo(Foo
** f
)
307 // Kids, don't try this at home
312 GetNewFoo(RefPtr
<Foo
>* f
)
318 GetPassedFoo(RefPtr
<Foo
>* f
)
328 main(int argc
, char** argv
)
330 // This should blow up
331 // Foo* f = new Foo(); delete f;
333 MOZ_ASSERT(0 == Foo::numDestroyed
);
335 RefPtr
<Foo
> f
= new Foo();
336 MOZ_ASSERT(f
->refCount() == 1);
338 MOZ_ASSERT(1 == Foo::numDestroyed
);
341 RefPtr
<Foo
> f1
= NewFoo();
342 RefPtr
<Foo
> f2(NewFoo());
343 MOZ_ASSERT(1 == Foo::numDestroyed
);
345 MOZ_ASSERT(3 == Foo::numDestroyed
);
348 RefPtr
<Foo
> b
= NewBar();
349 MOZ_ASSERT(3 == Foo::numDestroyed
);
351 MOZ_ASSERT(4 == Foo::numDestroyed
);
359 MOZ_ASSERT(4 == Foo::numDestroyed
);
361 MOZ_ASSERT(4 == Foo::numDestroyed
);
363 MOZ_ASSERT(5 == Foo::numDestroyed
);
366 RefPtr
<Foo
> f
= new Foo();
368 MOZ_ASSERT(6 == Foo::numDestroyed
);
372 RefPtr
<Foo
> f
= new Foo();
374 MOZ_ASSERT(7 == Foo::numDestroyed
);
376 MOZ_ASSERT(8 == Foo::numDestroyed
);
379 RefPtr
<Foo
> f
= new Foo();
380 GetPassedFoo(byRef(f
));
381 MOZ_ASSERT(8 == Foo::numDestroyed
);
383 MOZ_ASSERT(9 == Foo::numDestroyed
);
386 RefPtr
<Foo
> f
= new Foo();
388 MOZ_ASSERT(10 == Foo::numDestroyed
);
390 MOZ_ASSERT(11 == Foo::numDestroyed
);
393 RefPtr
<Foo
> f
= new Foo();
395 MOZ_ASSERT(11 == Foo::numDestroyed
);
397 MOZ_ASSERT(12 == Foo::numDestroyed
);
400 RefPtr
<Foo
> f1
= new Bar();
402 MOZ_ASSERT(13 == Foo::numDestroyed
);
405 RefPtr
<Foo
> f
= GetNullFoo();
406 MOZ_ASSERT(13 == Foo::numDestroyed
);
408 MOZ_ASSERT(13 == Foo::numDestroyed
);