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