00001 #ifndef MANAGED_OBJECT_IMPLEMENTATION 00002 #define MANAGED_OBJECT_IMPLEMENTATION 00003 00004 /*****************************************************************************\ 00005 * * 00006 * Name : managed_object * 00007 * Author : Aaron Buchanan * 00008 * * 00009 ******************************************************************************* 00010 * Copyright (c) 2001-$now By Author. This program is free software; you can * 00011 * redistribute it and/or modify it under the terms of the GNU General Public * 00012 * License as published by the Free Software Foundation; either version 2 of * 00013 * the License or (at your option) any later version. This is online at: * 00014 * http://www.fsf.org/copyleft/gpl.html * 00015 * Please send any updates to: fred@gruntose.com * 00016 \*****************************************************************************/ 00017 00018 #include "managed_object.h" 00019 00020 template <class contents> 00021 class object_storer 00022 { 00023 public: 00024 object_storer(contents *_object) 00025 : m_Object(_object), m_refCount(1) {} 00026 // Constructor stores the original object and intializes the 00027 // reference count to one. 00028 00029 // When the storer is deleted we delete the original object 00030 ~object_storer() 00031 { 00032 delete m_Object; 00033 m_Object = NULL; 00034 } 00035 00036 contents *GetObject() const { return m_Object; } 00037 // Returns the originally stored object 00038 00039 // Reference count for the number of references to the object 00040 void addRef() { m_refCount++; } 00041 void removeRef() 00042 { 00043 // When the reference count reaches zero we delete the object_storer, 00044 // this in turn deletes the original object. 00045 m_refCount--; 00046 if( m_refCount <= 0 ) 00047 delete this; 00048 } 00049 00050 private: 00051 contents *m_Object; 00052 int m_refCount; 00053 }; 00054 00056 00057 template <class contents> 00058 managed_object<contents>::managed_object() 00059 : storer(NULL) 00060 {} 00061 00062 template <class contents> 00063 managed_object<contents>::managed_object(contents *&_object) 00064 : storer(new object_storer<contents>(_object)) 00065 { _object = NULL; } 00066 00067 template <class contents> 00068 managed_object<contents>::managed_object(const managed_object<contents> &_rhs) 00069 : storer(NULL) 00070 { *this = _rhs; } 00071 00072 template <class contents> 00073 managed_object<contents> &managed_object<contents>::operator =(const managed_object<contents> &rhs) 00074 { 00075 if (this == &rhs) return *this; 00076 if(NULL != storer) storer->removeRef(); 00077 storer = rhs.storer; 00078 if(NULL != storer) storer->addRef(); 00079 return *this; 00080 } 00081 00082 template <class contents> 00083 managed_object<contents>::~managed_object() 00084 { if(NULL != storer) storer->removeRef(); } 00085 00086 template <class contents> 00087 bool managed_object<contents>::operator ==(const managed_object<contents> &_compare) const 00088 { 00089 if( (NULL == storer) || (NULL == _compare.storer) ) 00090 { 00091 if( (NULL == storer) && (NULL == _compare.storer) ) 00092 return true; 00093 return false; 00094 } 00095 return( *(storer->GetObject()) == *(_compare.storer->GetObject()) ); 00096 } 00097 00098 template <class contents> 00099 managed_object<contents> *managed_object<contents>::new_reference() 00100 { return new managed_object<contents>(*this); } 00101 00102 template <class contents> 00103 contents *managed_object<contents>::access() 00104 { return( (NULL == storer) ? NULL : storer->GetObject() ); } 00105 00106 template <class contents> 00107 const contents *managed_object<contents>::observe() const 00108 { return( (NULL == storer) ? NULL : storer->GetObject() ); } 00109 00110 #endif 00111
1.5.1