Blender  V2.59
MEM_SmartPtr.h
Go to the documentation of this file.
00001 /*
00002  * $Id: MEM_SmartPtr.h 35156 2011-02-25 11:47:18Z jesterking $
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): none yet.
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00039 #ifndef NAN_INCLUDED_MEM_SmartPtr_h
00040 #define NAN_INCLUDED_MEM_SmartPtr_h
00041 
00042 
00043 #include <stdlib.h> // for NULL !
00044 
00045 
00110 template 
00111         < class T >
00112 class MEM_SmartPtr {
00113 
00114 public :
00115 
00121         MEM_SmartPtr(
00122                 const MEM_SmartPtr &rhs
00123         ){
00124                 m_val = rhs.Release();
00125         }
00126 
00133         MEM_SmartPtr(
00134                 T* val
00135         ) :
00136                 m_val (val)
00137         {
00138         }
00139         
00144         MEM_SmartPtr(
00145         ) :
00146                 m_val (NULL)
00147         {
00148         }
00149 
00157         operator T * () const {
00158                 return m_val;
00159         }
00160 
00167                 T &
00168         Ref(
00169         ) const {
00170                 return *m_val;
00171         }       
00172 
00182         MEM_SmartPtr & operator=(
00183                 const MEM_SmartPtr &rhs
00184         ) {
00185                 if (this->m_val != rhs.m_val) {
00186                         delete this->m_val;
00187                 }
00188 
00189                 this->m_val = rhs.Release();
00190                 return *this;
00191         }
00192         
00198         T * operator->() const {
00199                 return m_val;
00200         }
00201 
00207                 T *
00208         Release(
00209         ) const {
00210                 T* temp = m_val;
00211                 (const_cast<MEM_SmartPtr *>(this))->m_val = NULL;       
00212                 return temp;
00213         }
00214 
00219                 void
00220         Delete(
00221         ) {
00222                 delete (m_val);
00223                 m_val = NULL;
00224         }
00225 
00230         ~MEM_SmartPtr(
00231         ) {
00232                 delete (m_val);
00233         }
00234 
00235 private :
00236         
00238         T * m_val;
00239 };
00240 
00241 #endif
00242