2 * Copyright 2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Adrien Destugues, pulkomandy@pulkomandy.tk
9 * headers/os/support/Referenceable.h hrev48725
10 * src/kits/support/Referenceable.cpp hrev48725
18 \brief Provides the BReferenceable interface and declares the BReferenceable
19 and BReference classes.
27 \brief Implementation of reference-counted memory management.
29 The C++ language provides two main ways of allocating objects: on the stack,
30 and on the heap. Objects created on the heap must be managed by the
31 application and deleted when they are not needed. Objects allocated on the
32 stack have a lifetime limited to the execution of the block they are
35 This approach is simple, but in some cases it can become quite difficult to
36 track ownership and lifetime of objects. The BReferenceable class allows to
37 implement reference counting, which allows a partially automated memory
38 management and some safety checks. It can also be used to implement pools
41 As the name implies, reference counting consists of keeping track, inside an
42 object, of how much references there are to it in the running application.
43 When the object is not referenced anymore, it can't be reached or used from
44 other parts of the application, so it is safe to delete or recycle the
47 To use reference counting for a particular class, you make it inherit
48 BReferenceable. This provides all the support for reference counting.
49 Objects are created as usual, on the stack or using the new operator.
51 The object can then be referenced from other places. Each time a reference
52 to it is kept, the owner of the reference should call AcquireReference.
53 When the reference is not needed, it should call ReleaseReference.
60 \fn BReferenceable::BReferenceable()
61 \brief Initialize the object with a reference count of 1.
63 The creator of the object is assumed to owns the first reference to it.
68 \fn BReferenceable::~BReferenceable()
71 The destructor should not be called directly, as the object is destructed
72 automatically when the last reference is released. This destructor is
73 public because you may still need to destroy the objects in the case where
74 LastReferenceReleased is overriden and you handle object destruction in
77 In debug mode, the destructor checks that no references to the object are
78 still held. If the object was allocated on the stack, it allows exactly one
79 reference to be kept, which makes it possible to allocate BReferenceable
80 on the stack without needing to release the single reference to it.
85 \fn int32 BReferenceable::AcquireReference()
86 \brief Acquire a reference to the object.
87 \returns the previous reference count
89 If the reference count was previously 0, this will call
90 FirstReferenceAcquired.
95 \fn int32 BReferenceable::ReleaseReference()
96 \brief Release a reference to the object.
97 \returns the previous reference count
99 If the reference count was previously 1 (and is now 0), this will call
100 LastReferenceReleased.
105 \fn int32 BReferenceable::CountReferences()
106 \brief Return the number of references to the object.
107 \returns the reference count.
112 \fn void BReferenceable::FirstReferenceAcquired()
113 \brief Called when the first reference to the object is reacquired.
115 The default implementation does nothing. This method can be overriden to
116 implement reinitialization of the object, when using BReferenceable to
117 keep track of a pool of reusable objects.
119 In this use case, the objects are created (at initialization or lazily on an
120 as-needed basis) and stored in a pool. When an object is needed, if there
121 is an unused one waiting in the pool, it is reused instead of creating a
122 new one. Once a reference to it is acquired, this method is called and the
123 object can initialize itself to a clean state.
125 \warning This is currently not called when the object is first created, but
126 only on subsequent reuses.
131 \fn void BReferenceable::LastReferenceReleased()
132 \brief Called when the last reference to the object is released.
134 This function is called when the object is not referenced anymore. The
135 default implementation deletes the object using the delete operator.
137 This behavior can be overriden. For example, to implement an object pool,
138 this method would not delete the object, but instead put it back in the
139 free object pool, ready for reuse at a later point.
149 \brief A reference to a BReferenceable object.
151 BReference simplifies the use of BReferenceable and makes it more
152 transparent. It automatically acquires and release references to the pointed
153 objects. It provides an API similar to a standard C++ pointer, allowing use
154 of assignment and comparison operators and direct access to the object with
160 \fn BReference::BReference()
161 \brief Creates a reference without initializing it
163 An uninitialized references behaves similarly to a NULL pointer.
168 \fn BReference::BReference(Type*, bool)
169 \brief Creates and initialize a reference
171 The reference is set to the pointed object. If the parameter is set to
172 true, the reference count is not incremented, this should only be used when
173 referencing a freshly constructed object.
178 \fn BReference::BReference(const BReference& other)
179 \brief Copy constructor
181 The reference is set to the same object as the source. The reference count
182 of the target object is incremented.
187 \fn BReference::~BReference
190 The reference count of the target object is decremented.
195 \fn void BReference::SetTo(Type*, bool)
196 \brief Sets a new target for the reference.
198 The reference to the previously targetted object is released.
199 A reference to the new object is acquired only if \p alreadyHasReference
205 \fn void BReference::Unset()
206 \brief Unsets the reference.
208 The targetted object is released. The reference is unset and can't be used
209 to access it anymore.
214 \fn ConstType* BReference::Get() const
215 \brief Get the target object
216 \returns a raw pointer to the target object.
221 \fn ConstType* BReference::Detach() const
222 \brief Detach the pointed object from the reference
223 \returns a raw pointer to the target object.
225 This returns the pointed object and unsets the reference, without
226 decrementing the object reference count. It is used to transfer ownership of
227 the reference to something else.