2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file memutil/MEM_RefCountPtr.h
33 * @file MEM_RefCountPtr.h
34 * Declaration of MEM_RefCounted and MEM_RefCountable classes.
38 #ifndef __MEM_REFCOUNTPTR_H__
39 #define __MEM_REFCOUNTPTR_H__
41 #include <stdlib.h> // for NULL !
44 * @section MEM_RefCountable
45 * This is a base class for reference countable objects.
46 * If you want an object to be shared using a reference
47 * counted system derrivce from this class. All subclasses
48 * should insist that they are created on the heap, this
49 * can be done by makeing all constructors private and
50 * defining a static New() method that returns a ref counted
51 * ptr to a new()ly allocated instance.
53 * @section Example subclass
56 * class MySharedObject : public MEM_RefCountable {
59 * MySharedObject() : MEM_RefCountable() { //class specific initialization};
60 * MySharedObject(const MySharedObject &other) // not implemented
63 * MEM_RefCountPtr<MySharedObject>
66 * return MEM_RefCountPtr<MySharedObject>( new MySharedObject());
69 * // other member functions
72 * Alternitively you may first wish to define a fully functional
73 * class and then define a reference counting wrapper for this class.
74 * This is useful when the base type can be used without reference
78 * class UsefullClass {
85 * AnotherUsefullMethod(...)
88 * class RcUsefullClass : public UsefullClass, public MEM_RefCountable
91 * // Override base class public constructor --- forces
96 * // Override each public constructor of UsefullClass with
97 * // an equivalent static New method returning a MEM_RefCountPtr
100 * MEM_RefCountPtr<RcUsefullClass>
102 * return MEM_RefCountPtr<RcUsefullClass> output(
103 * new UsefullClass(...)
107 * // warning never call destructor directly allow ref counting
108 * // mechanism to handle object lifetime.
115 class MEM_RefCountable {
119 * The reference count!
120 * We use mutable here because we would like to
121 * share references of const objects!
122 * Maybe should think about having decRef()
123 * another value because we should not be deleting
132 * Protected constructors
133 * This class is not for direct instantiation. Sub classes
134 * should only be allocated on the heap.
144 const MEM_RefCountable &
171 * @section MEM_RefCountPtr
176 class MEM_RefCountPtr {
181 * Construction from reference - share ownership with
182 * the right hand side.
186 const MEM_RefCountPtr &rhs
188 ShareOwnership(rhs.m_val);
192 * Construction from ptr - this class shares
193 * ownership of object val.
205 * Default constructor
215 * Type conversion from this class to the type
216 * of a pointer to the template parameter.
217 * This means you can pass an instance of this class
218 * to a function expecting a ptr of type T.
221 operator T * () const {
226 MEM_RefCountPtr & operator=(
227 const MEM_RefCountPtr &rhs
229 if (this->m_val != rhs.m_val) {
231 ShareOwnership(rhs.m_val);
237 * Overload the operator -> so that it's possible to access
238 * all the normal methods of the internal ptr.
241 T * operator->() const {
246 * Returrn a reference to the shared object.
257 * Destructor - deletes object if it's ref count is zero.
267 /// The ptr owned by this class.
277 m_val = const_cast<T *>(val);
284 if (m_val->DecRef() == 0) {