xref: /haiku/src/kits/support/Referenceable.cpp (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
1 /*
2  * Copyright 2005-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <Referenceable.h>
7 
8 
9 BReferenceable::BReferenceable(bool deleteWhenUnreferenced)
10 	: fReferenceCount(1),
11 	  fDeleteWhenUnreferenced(deleteWhenUnreferenced)
12 {
13 }
14 
15 
16 BReferenceable::~BReferenceable()
17 {
18 }
19 
20 
21 void
22 BReferenceable::AcquireReference()
23 {
24 	if (atomic_add(&fReferenceCount, 1) == 0)
25 		FirstReferenceAcquired();
26 }
27 
28 
29 bool
30 BReferenceable::ReleaseReference()
31 {
32 	bool unreferenced = (atomic_add(&fReferenceCount, -1) == 1);
33 	if (unreferenced)
34 		LastReferenceReleased();
35 	return unreferenced;
36 }
37 
38 
39 void
40 BReferenceable::FirstReferenceAcquired()
41 {
42 }
43 
44 
45 void
46 BReferenceable::LastReferenceReleased()
47 {
48 	if (fDeleteWhenUnreferenced)
49 		delete this;
50 }
51