3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
29 * @file MEM_RefCountPtr.h
30 * Declaration of MEM_RefCounted and MEM_RefCountable classes.
34 #ifndef NAN_INCLUDED_MEM_RefCountPtr_h
35 #define NAN_INCLUDED_MEM_RefCountPtr_h
37 #include <stdlib.h> // for NULL !
40 * @section MEM_RefCountable
41 * This is a base class for reference countable objects.
42 * If you want an object to be shared using a reference
43 * counted system derrivce from this class. All subclasses
44 * should insist that they are created on the heap, this
45 * can be done by makeing all constructors private and
46 * defining a static New() method that returns a ref counted
47 * ptr to a new()ly allocated instance.
49 * @section Example subclass
52 * class MySharedObject : public MEM_RefCountable {
55 * MySharedObject() : MEM_RefCountable() { //class specific initialization};
56 * MySharedObject(const MySharedObject &other) // not implemented
59 * MEM_RefCountPtr<MySharedObject>
62 * return MEM_RefCountPtr<MySharedObject>( new MySharedObject());
65 * // other member functions
68 * Alternitively you may first wish to define a fully functional
69 * class and then define a reference counting wrapper for this class.
70 * This is useful when the base type can be used without reference
74 * class UsefullClass {
81 * AnotherUsefullMethod(...)
84 * class RcUsefullClass : public UsefullClass, public MEM_RefCountable
87 * // Override base class public constructor --- forces
92 * // Override each public constructor of UsefullClass with
93 * // an equivalent static New method returning a MEM_RefCountPtr
96 * MEM_RefCountPtr<RcUsefullClass>
98 * return MEM_RefCountPtr<RcUsefullClass> output(
99 * new UsefullClass(...)
103 * // warning never call destructor directly allow ref counting
104 * // mechanism to handle object lifetime.
111 class MEM_RefCountable {
115 * The reference count!
116 * We use mutable here because we would like to
117 * share references of const objects!
118 * Maybe should think about having decRef()
119 * another value because we should not be deleting
128 * Protected constructors
129 * This class is not for direct instanciation. Sub classes
130 * should only be allocated on the heap.
140 const MEM_RefCountable &
167 * @section MEM_RefCountPtr
172 class MEM_RefCountPtr {
177 * Construction from reference - share ownership with
178 * the right hand side.
182 const MEM_RefCountPtr &rhs
184 ShareOwnership(rhs.m_val);
188 * Construction from ptr - this class shares
189 * ownership of object val.
201 * Default constructor
211 * Type conversion from this class to the type
212 * of a pointer to the template parameter.
213 * This means you can pass an instance of this class
214 * to a function expecting a ptr of type T.
217 operator T * () const {
222 MEM_RefCountPtr & operator=(
223 const MEM_RefCountPtr &rhs
225 if (this->m_val != rhs.m_val) {
227 ShareOwnership(rhs.m_val);
233 * Overload the operator -> so that it's possible to access
234 * all the normal methods of the internal ptr.
237 T * operator->() const {
242 * Returrn a reference to the shared object.
253 * Destructor - deletes object if it's ref count is zero.
263 /// The ptr owned by this class.
273 m_val = const_cast<T *>(val);
280 if (m_val->DecRef() == 0) {