00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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