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
33 #define NAN_INCLUDED_MEM_SmartPtr_h
41 * Copyright (C) 2001 NaN Technologies B.V.
46 #include <stdlib.h> // for NULL !
49 * @section MEM_SmartPtr
50 * This class defines a smart pointer similar to that defined in
51 * the Standard Template Library but without the painful get()
52 * semantics to access the internal c style pointer.
54 * It is often useful to explicitely decalre ownership of memory
55 * allocated on the heap within class or function scope. This
56 * class helps you to encapsulate this ownership within a value
57 * type. When an instance of this class goes out of scope it
58 * makes sure that any memory associated with it's internal pointer
59 * is deleted. It can help to inform users of an aggregate class
60 * that it owns instances of it's members and these instances
61 * should not be shared. This is not reliably enforcable in C++
62 * but this class attempts to make the 1-1 relationship clear.
64 * @section Example usage
67 * ...constructors accessors etc.
77 * MEM_SmartPtr<foo> afoo = new foo();
78 * MEM_SmartPtr<bar> that = new bar();
80 * if (foo == NULL || that == NULL) return NULL;
82 * that->m_foo = afoo.Release();
83 * return that.Release();
87 * // smart ptr takes care of deletion
90 * MEM_SmartPtr<foo> m_foo;
93 * You my also safely construct vectors of MEM_SmartPtrs and
94 * have the vector own stuff you put into it.
98 * std::vector<MEM_SmartPtr<foo> > foo_vector;
99 * foo_vector.push_back( new foo());
100 * foo_vector.push_back( new foo());
102 * foo_vector[0]->bla();
103 * } // foo_vector out of scope => heap memory freed for both foos
105 * @warning this class should only be used for objects created
106 * on the heap via the new function. It will not behave correctly
107 * if you pass ptrs to objects created with new[] nor with
108 * objects declared on the stack. Doing this is likely to crash
109 * the program or lead to memory leaks.
119 * Construction from reference - this class
120 * always assumes ownership from the rhs.
124 const MEM_SmartPtr &rhs
126 m_val = rhs.Release();
130 * Construction from ptr - this class always
131 * assumes that it now owns the memory associated with the
143 * Defalut constructor
153 * Type conversion from this class to the type
154 * of a pointer to the template parameter.
155 * This means you can pass an instance of this class
156 * to a function expecting a ptr of type T.
159 operator T * () const {
164 * Return a reference to the internal ptr class.
165 * Use with care when you now that the internal ptr
176 * Assignment operator - ownership is transfered from rhs to lhs.
177 * There is an intenional side-effect of function of transferring
178 * ownership from the const parameter rhs. This is to insure
179 * the 1-1 relationship.
180 * The object associated with this instance is deleted if it
181 * is not the same as that contained in the rhs.
184 MEM_SmartPtr & operator=(
185 const MEM_SmartPtr &rhs
187 if (this->m_val != rhs.m_val) {
191 this->m_val = rhs.Release();
196 * Overload the operator -> so that it's possible to access
197 * all the normal methods of the internal ptr.
200 T * operator->() const {
205 * Caller takes ownership of the object - the object will not
206 * be deleted when the ptr goes out of scope.
213 (const_cast<MEM_SmartPtr *>(this))->m_val = NULL;
218 * Force destruction of the internal object.
229 * Destructor - deletes object if it exists
239 /// The ptr owned by this class.