RefCountedClass.h

00001 
00002 //    Copyright 2004, SenseGraphics AB
00003 //
00004 //    This file is part of H3D API.
00005 //
00006 //    H3D API is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    H3D API is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with H3D API; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 //
00020 //    A commercial license is also available. Please contact us at 
00021 //    www.sensegraphics.com for more information.
00022 //
00023 //
00024 //
00025 //
00027 #ifndef __REFCOUNTEDCLASS_H__
00028 #define __REFCOUNTEDCLASS_H__
00029 
00030 #include "H3DApi.h"
00031 #include "Console.h"
00032 #include <string>
00033 #include <iostream>
00034 using namespace std;
00035 
00036 namespace H3D {
00037 
00038   class H3DAPI_API RefCountedClass {
00039   public:
00040 
00042     RefCountedClass( ):
00043       ref_count( 0 ),
00044       name( "" ),
00045       type_name( "RefCountedClass" ),
00046       is_initialized( false ),
00047       manual_initialize( false ){}
00048 
00049 
00051     virtual ~RefCountedClass() {
00052 #ifdef REF_COUNT_DEBUG
00053       Console(1) << "~RefCountedClass: " << this << endl;
00054 #endif
00055     }
00056 
00059     virtual void initialize() {
00060       is_initialized = true;
00061     }
00062 
00064     inline void ref() { 
00065       ref_count++;
00066 #ifdef REF_COUNT_DEBUG
00067       Console(1) << "Ref " << getName() << " " << this << ": " 
00068                  << ref_count << endl;
00069 #endif
00070       if( !manual_initialize && ref_count == 1 ) {
00071         initialize();
00072       }
00073     }
00074 
00078     inline void setManualInitialize( bool b ) {
00079       manual_initialize = b;
00080     }
00081 
00083     inline bool getManualInitialize() {
00084       return manual_initialize;
00085     }
00086 
00089     inline void unref() {
00090       ref_count--;
00091 #ifdef REF_COUNT_DEBUG
00092       Console(1) << "Unref " << getName() << " " << this << ": " 
00093                  << ref_count << endl;
00094 #endif
00095       if( ref_count == 0 ) {
00096         delete this;
00097       }
00098     }
00099 
00101     inline string getName() { 
00102       if( name == "" )
00103         return string( "Unnamed " ) + getTypeName();
00104       else 
00105         return name; 
00106     }
00107 
00109     inline void setName( const string &_name ) { 
00110       name = _name;
00111     }
00112     
00114     inline bool hasName() { return name != ""; }
00115 
00118     string getTypeName() {
00119       return type_name;
00120     }
00121 
00123     inline bool isInitialized() {
00124       return is_initialized;
00125     }
00126 
00127   protected:
00129     unsigned int ref_count; 
00130 
00132     string name;
00133 
00135     string type_name;
00136 
00138     bool is_initialized;
00139 
00143     bool manual_initialize;
00144   };
00145     
00146 }
00147 
00148 #endif

Generated on Thu Aug 24 12:38:34 2006 for H3D API by  doxygen 1.4.5