Blender V2.61 - r43446

MEM_SmartPtr.h

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