Blender V2.61 - r43446

MEM_RefCountPtr.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_RefCountPtr_h
00039 #define NAN_INCLUDED_MEM_RefCountPtr_h
00040 
00041 #include <stdlib.h> // for NULL !
00042 
00115 class MEM_RefCountable {
00116 private :
00117 
00127     mutable int m_count;
00128 
00129 protected :
00130 
00137     MEM_RefCountable (
00138     ) :
00139         m_count (0)
00140     {
00141     };
00142 
00143     MEM_RefCountable (
00144         const MEM_RefCountable &
00145     ) :
00146         m_count (0)
00147     {
00148     }
00149 
00150 public :
00151 
00152         void
00153     IncRef(
00154     ) const {
00155         m_count++;
00156     }
00157 
00158         int
00159     DecRef(
00160     ) {
00161         return (--m_count);
00162     }
00163 
00164     ~MEM_RefCountable(
00165     ) {
00166         //nothing to do
00167     }
00168 };
00169 
00174 template
00175     < class T >
00176 class MEM_RefCountPtr {
00177 
00178 public :
00179 
00185     MEM_RefCountPtr(
00186         const MEM_RefCountPtr &rhs
00187     ) : m_val (NULL) {
00188         ShareOwnership(rhs.m_val);
00189     }
00190 
00196     MEM_RefCountPtr(
00197         const T* val
00198     ) :
00199         m_val (NULL)
00200     {
00201         ShareOwnership(val);
00202     }
00203 
00208     MEM_RefCountPtr(
00209     ) :
00210         m_val (NULL)
00211     {
00212     }
00213 
00221     operator T * () const {
00222         return m_val;
00223     }
00224 
00225 
00226     MEM_RefCountPtr & operator=(
00227         const MEM_RefCountPtr &rhs
00228     ) {
00229         if (this->m_val != rhs.m_val) {
00230             ReleaseOwnership();
00231             ShareOwnership(rhs.m_val);
00232         }
00233         return *this;
00234     }
00235 
00241     T * operator->() const {
00242         return m_val;
00243     }
00244 
00249         T&
00250     Ref(
00251     ) {
00252         return *m_val;
00253     }
00254 
00255 
00260     ~MEM_RefCountPtr(
00261     ) {
00262         ReleaseOwnership();
00263     }
00264 
00265 private :
00266     
00268     T * m_val;
00269 
00270         void
00271     ShareOwnership(
00272         const T * val
00273     ) {
00274         if (val != NULL) {
00275             val->IncRef();
00276         }
00277         m_val = const_cast<T *>(val);
00278     }
00279         
00280         void
00281     ReleaseOwnership(
00282     ) {
00283         if (m_val) {
00284             if (m_val->DecRef() == 0) {
00285                 delete(m_val);
00286                 m_val = NULL;
00287             }
00288         }
00289     }
00290 
00291 };
00292 
00293 #endif
00294