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_SmartPtr_h
34 #define NAN_INCLUDED_MEM_SmartPtr_h
38 * Copyright (C) 2001 NaN Technologies B.V.
43 #include <stdlib.h> // for NULL !
46 * @section MEM_SmartPtr
47 * This class defines a smart pointer similar to that defined in
48 * the Standard Template Library but without the painful get()
49 * semantics to access the internal c style pointer.
51 * It is often useful to explicitely decalre ownership of memory
52 * allocated on the heap within class or function scope. This
53 * class helps you to encapsulate this ownership within a value
54 * type. When an instance of this class goes out of scope it
55 * makes sure that any memory associated with it's internal pointer
56 * is deleted. It can help to inform users of an aggregate class
57 * that it owns instances of it's members and these instances
58 * should not be shared. This is not reliably enforcable in C++
59 * but this class attempts to make the 1-1 relationship clear.
61 * @section Example usage
64 * ...constructors accessors etc.
74 * MEM_SmartPtr<foo> afoo = new foo();
75 * MEM_SmartPtr<bar> that = new bar();
77 * if (foo == NULL || that == NULL) return NULL;
79 * that->m_foo = afoo.Release();
80 * return that.Release();
84 * // smart ptr takes care of deletion
87 * MEM_SmartPtr<foo> m_foo;
90 * You my also safely construct vectors of MEM_SmartPtrs and
91 * have the vector own stuff you put into it.
95 * std::vector<MEM_SmartPtr<foo> > foo_vector;
96 * foo_vector.push_back( new foo());
97 * foo_vector.push_back( new foo());
99 * foo_vector[0]->bla();
100 * } // foo_vector out of scope => heap memory freed for both foos
102 * @warning this class should only be used for objects created
103 * on the heap via the new function. It will not behave correctly
104 * if you pass ptrs to objects created with new[] nor with
105 * objects declared on the stack. Doing this is likely to crash
106 * the program or lead to memory leaks.
116 * Construction from reference - this class
117 * always assumes ownership from the rhs.
121 const MEM_SmartPtr &rhs
123 m_val = rhs.Release();
127 * Construction from ptr - this class always
128 * assumes that it now owns the memory associated with the
140 * Defalut constructor
150 * Type conversion from this class to the type
151 * of a pointer to the template parameter.
152 * This means you can pass an instance of this class
153 * to a function expecting a ptr of type T.
156 operator T * () const {
161 * Return a reference to the internal ptr class.
162 * Use with care when you now that the internal ptr
173 * Assignment operator - ownership is transfered from rhs to lhs.
174 * There is an intenional side-effect of function of transferring
175 * ownership from the const parameter rhs. This is to insure
176 * the 1-1 relationship.
177 * The object associated with this instance is deleted if it
178 * is not the same as that contained in the rhs.
181 MEM_SmartPtr & operator=(
182 const MEM_SmartPtr &rhs
184 if (this->m_val != rhs.m_val) {
188 this->m_val = rhs.Release();
193 * Overload the operator -> so that it's possible to access
194 * all the normal methods of the internal ptr.
197 T * operator->() const {
202 * Caller takes ownership of the object - the object will not
203 * be deleted when the ptr goes out of scope.
210 (const_cast<MEM_SmartPtr *>(this))->m_val = NULL;
215 * Force destruction of the internal object.
226 * Destructor - deletes object if it exists
236 /// The ptr owned by this class.