1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=99 ft=cpp:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at:
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is Mozilla Code.
19 * The Initial Developer of the Original Code is
20 * The Mozilla Foundation
21 * Portions created by the Initial Developer are Copyright (C) 2011
22 * the Initial Developer. All Rights Reserved.
25 * Chris Jones <jones.chris.g@gmail.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef mozilla_RefPtr_h_
42 #define mozilla_RefPtr_h_
44 #include "mozilla/Util.h"
47 * Helpers for defining and using refcounted objects.
52 template<typename T
> class RefCounted
;
53 template<typename T
> class RefPtr
;
54 template<typename T
> class TemporaryRef
;
55 template<typename T
> class OutParamRef
;
56 template<typename T
> OutParamRef
<T
> byRef(RefPtr
<T
>&);
59 * RefCounted<T> is a sort of a "mixin" for a class T. RefCounted
60 * manages, well, refcounting for T, and because RefCounted is
61 * parameterized on T, RefCounted<T> can call T's destructor directly.
62 * This means T doesn't need to have a virtual dtor and so doesn't
65 * RefCounted<T> is created with refcount == 0. Newly-allocated
66 * RefCounted<T> must immediately be assigned to a RefPtr to make the
67 * refcount > 0. It's an error to allocate and free a bare
68 * RefCounted<T>, i.e. outside of the RefPtr machinery. Attempts to
69 * do so will abort DEBUG builds.
71 * Live RefCounted<T> have refcount > 0. The lifetime (refcounts) of
72 * live RefCounted<T> are controlled by RefPtr<T> and
73 * RefPtr<super/subclass of T>. Upon a transition from refcounted==1
74 * to 0, the RefCounted<T> "dies" and is destroyed. The "destroyed"
75 * state is represented in DEBUG builds by refcount==-0xdead. This
76 * state distinguishes use-before-ref (refcount==0) from
77 * use-after-destroy (refcount==-0xdead).
82 friend class RefPtr
<T
>;
85 RefCounted() : refCnt(0) { }
86 ~RefCounted() { MOZ_ASSERT(refCnt
== -0xdead); }
88 // Compatibility with nsRefPtr.
90 MOZ_ASSERT(refCnt
>= 0);
95 MOZ_ASSERT(refCnt
> 0);
100 delete static_cast<T
*>(this);
104 // Compatibility with wtf::RefPtr.
105 void ref() { AddRef(); }
106 void deref() { Release(); }
107 int refCount() const { return refCnt
; }
108 bool hasOneRef() const {
109 MOZ_ASSERT(refCnt
> 0);
118 * RefPtr points to a refcounted thing that has AddRef and Release
119 * methods to increase/decrease the refcount, respectively. After a
120 * RefPtr<T> is assigned a T*, the T* can be used through the RefPtr
121 * as if it were a T*.
123 * A RefPtr can forget its underlying T*, which results in the T*
124 * being wrapped in a temporary object until the T* is either
125 * re-adopted from or released by the temporary.
130 // To allow them to use unref()
131 friend class TemporaryRef
<T
>;
132 friend class OutParamRef
<T
>;
137 RefPtr() : ptr(0) { }
138 RefPtr(const RefPtr
& o
) : ptr(ref(o
.ptr
)) {}
139 RefPtr(const TemporaryRef
<T
>& o
) : ptr(o
.drop()) {}
140 RefPtr(T
* t
) : ptr(ref(t
)) {}
143 RefPtr(const RefPtr
<U
>& o
) : ptr(ref(o
.get())) {}
145 ~RefPtr() { unref(ptr
); }
147 RefPtr
& operator=(const RefPtr
& o
) {
151 RefPtr
& operator=(const TemporaryRef
<T
>& o
) {
155 RefPtr
& operator=(T
* t
) {
161 RefPtr
& operator=(const RefPtr
<U
>& o
) {
162 assign(ref(o
.get()));
166 TemporaryRef
<T
> forget() {
169 return TemporaryRef
<T
>(tmp
, dontRef());
172 T
* get() const { return ptr
; }
173 operator T
*() const { return ptr
; }
174 T
* operator->() const { return ptr
; }
175 T
& operator*() const { return *ptr
; }
177 operator TemporaryRef
<U
>() { return forget(); }
187 static MOZ_ALWAYS_INLINE T
* ref(T
* t
) {
194 static MOZ_ALWAYS_INLINE
void unref(T
* t
) {
202 * TemporaryRef<T> represents an object that holds a temporary
203 * reference to a T. TemporaryRef objects can't be manually ref'd or
204 * unref'd (being temporaries, not lvalues), so can only relinquish
205 * references to other objects, or unref on destruction.
210 // To allow it to construct TemporaryRef from a bare T*
211 friend class RefPtr
<T
>;
213 typedef typename RefPtr
<T
>::dontRef dontRef
;
216 TemporaryRef(T
* t
) : ptr(RefPtr
<T
>::ref(t
)) {}
217 TemporaryRef(const TemporaryRef
& o
) : ptr(o
.drop()) {}
220 TemporaryRef(const TemporaryRef
<U
>& o
) : ptr(o
.drop()) {}
222 ~TemporaryRef() { RefPtr
<T
>::unref(ptr
); }
231 TemporaryRef(T
* t
, const dontRef
&) : ptr(t
) {}
236 TemporaryRef
& operator=(const TemporaryRef
&);
240 * OutParamRef is a wrapper that tracks a refcounted pointer passed as
241 * an outparam argument to a function. OutParamRef implements COM T**
242 * outparam semantics: this requires the callee to AddRef() the T*
243 * returned through the T** outparam on behalf of the caller. This
244 * means the caller (through OutParamRef) must Release() the old
245 * object contained in the tracked RefPtr. It's OK if the callee
246 * returns the same T* passed to it through the T** outparam, as long
247 * as the callee obeys the COM discipline.
249 * Prefer returning TemporaryRef<T> from functions over creating T**
250 * outparams and passing OutParamRef<T> to T**. Prefer RefPtr<T>*
251 * outparams over T** outparams.
256 friend OutParamRef byRef
<T
>(RefPtr
<T
>&);
260 RefPtr
<T
>::unref(refPtr
.ptr
);
264 operator T
**() { return &tmp
; }
267 OutParamRef(RefPtr
<T
>& p
) : refPtr(p
), tmp(p
.get()) {}
272 OutParamRef() MOZ_DELETE
;
273 OutParamRef
& operator=(const OutParamRef
&) MOZ_DELETE
;
277 * byRef cooperates with OutParamRef to implement COM outparam semantics.
281 byRef(RefPtr
<T
>& ptr
)
283 return OutParamRef
<T
>(ptr
);
286 } // namespace mozilla
288 #endif // mozilla_RefPtr_h_
293 // Command line that builds these tests
295 // cp RefPtr.h test.cc && g++ -g -Wall -pedantic -DDEBUG -o test test.cc && ./test
297 using namespace mozilla
;
299 struct Foo
: public RefCounted
<Foo
>
301 Foo() : dead(false) { }
309 static int numDestroyed
;
311 int Foo::numDestroyed
;
313 struct Bar
: public Foo
{ };
318 return RefPtr
<Foo
>(new Foo());
331 // Kids, don't try this at home
336 GetPassedFoo(Foo
** f
)
338 // Kids, don't try this at home
343 GetNewFoo(RefPtr
<Foo
>* f
)
349 GetPassedFoo(RefPtr
<Foo
>* f
)
359 main(int argc
, char** argv
)
361 // This should blow up
362 // Foo* f = new Foo(); delete f;
364 MOZ_ASSERT(0 == Foo::numDestroyed
);
366 RefPtr
<Foo
> f
= new Foo();
367 MOZ_ASSERT(f
->refCount() == 1);
369 MOZ_ASSERT(1 == Foo::numDestroyed
);
372 RefPtr
<Foo
> f1
= NewFoo();
373 RefPtr
<Foo
> f2(NewFoo());
374 MOZ_ASSERT(1 == Foo::numDestroyed
);
376 MOZ_ASSERT(3 == Foo::numDestroyed
);
379 RefPtr
<Foo
> b
= NewBar();
380 MOZ_ASSERT(3 == Foo::numDestroyed
);
382 MOZ_ASSERT(4 == Foo::numDestroyed
);
390 MOZ_ASSERT(4 == Foo::numDestroyed
);
392 MOZ_ASSERT(4 == Foo::numDestroyed
);
394 MOZ_ASSERT(5 == Foo::numDestroyed
);
397 RefPtr
<Foo
> f
= new Foo();
399 MOZ_ASSERT(6 == Foo::numDestroyed
);
403 RefPtr
<Foo
> f
= new Foo();
405 MOZ_ASSERT(7 == Foo::numDestroyed
);
407 MOZ_ASSERT(8 == Foo::numDestroyed
);
410 RefPtr
<Foo
> f
= new Foo();
411 GetPassedFoo(byRef(f
));
412 MOZ_ASSERT(8 == Foo::numDestroyed
);
414 MOZ_ASSERT(9 == Foo::numDestroyed
);
417 RefPtr
<Foo
> f
= new Foo();
419 MOZ_ASSERT(10 == Foo::numDestroyed
);
421 MOZ_ASSERT(11 == Foo::numDestroyed
);
424 RefPtr
<Foo
> f
= new Foo();
426 MOZ_ASSERT(11 == Foo::numDestroyed
);
428 MOZ_ASSERT(12 == Foo::numDestroyed
);
431 RefPtr
<Foo
> f1
= new Bar();
433 MOZ_ASSERT(13 == Foo::numDestroyed
);
436 RefPtr
<Foo
> f
= GetNullFoo();
437 MOZ_ASSERT(13 == Foo::numDestroyed
);
439 MOZ_ASSERT(13 == Foo::numDestroyed
);