3 * ***** BEGIN GPL/BL DUAL 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. The Blender
9 * Foundation also sells licenses for use in proprietary software under
10 * the Blender License. See http://www.blender.org/BL/ for information
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL/BL DUAL LICENSE BLOCK *****
32 #ifndef NAN_INCLUDED_MEM_RefCountPtr_h
34 #define NAN_INCLUDED_MEM_RefCountPtr_h
38 * Copyright (C) 2001 NaN Technologies B.V.
43 #include <stdlib.h> // for NULL !
46 * @section MEM_RefCountable
47 * This is a base class for reference countable objects.
48 * If you want an object to be shared using a reference
49 * counted system derrivce from this class. All subclasses
50 * should insist that they are created on the heap, this
51 * can be done by makeing all constructors private and
52 * defining a static New() method that returns a ref counted
53 * ptr to a new()ly allocated instance.
55 * @section Example subclass
58 * class MySharedObject : public MEM_RefCountable {
61 * MySharedObject() : MEM_RefCountable() { //class specific initialization};
62 * MySharedObject(const MySharedObject &other) // not implemented
65 * MEM_RefCountPtr<MySharedObject>
68 * return MEM_RefCountPtr<MySharedObject>( new MySharedObject());
71 * // other member functions
74 * Alternitively you may first wish to define a fully functional
75 * class and then define a reference counting wrapper for this class.
76 * This is useful when the base type can be used without reference
80 * class UsefullClass {
87 * AnotherUsefullMethod(...)
90 * class RcUsefullClass : public UsefullClass, public MEM_RefCountable
93 * // Override base class public constructor --- forces
98 * // Override each public constructor of UsefullClass with
99 * // an equivalent static New method returning a MEM_RefCountPtr
102 * MEM_RefCountPtr<RcUsefullClass>
104 * return MEM_RefCountPtr<RcUsefullClass> output(
105 * new UsefullClass(...)
109 * // warning never call destructor directly allow ref counting
110 * // mechanism to handle object lifetime.
117 class MEM_RefCountable {
121 * The reference count!
122 * We use mutable here because we would like to
123 * share references of const objects!
124 * Maybe should think about having decRef()
125 * another value because we should not be deleting
134 * Protected constructors
135 * This class is not for direct instanciation. Sub classes
136 * should only be allocated on the heap.
146 const MEM_RefCountable & other
173 * @section MEM_RefCountPtr
178 class MEM_RefCountPtr {
183 * Construction from reference - share ownership with
184 * the right hand side.
188 const MEM_RefCountPtr &rhs
190 ShareOwnership(rhs.m_val);
194 * Construction from ptr - this class shares
195 * ownership of object val.
207 * Defalut constructor
217 * Type conversion from this class to the type
218 * of a pointer to the template parameter.
219 * This means you can pass an instance of this class
220 * to a function expecting a ptr of type T.
223 operator T * () const {
228 MEM_RefCountPtr & operator=(
229 const MEM_RefCountPtr &rhs
231 if (this->m_val != rhs.m_val) {
233 ShareOwnership(rhs.m_val);
239 * Overload the operator -> so that it's possible to access
240 * all the normal methods of the internal ptr.
243 T * operator->() const {
248 * Returrn a reference to the shared object.
258 * Destructor - deletes object if it's ref count is zero.
268 /// The ptr owned by this class.
278 m_val = const_cast<T *>(val);
285 if (m_val->DecRef() == 0) {