AutoPtr.h

Go to the documentation of this file.
00001 #ifndef __AUTO_PTR_H
00002 #define __AUTO_PTR_H
00003 
00004 #include <set>
00005 #include <esg/Export>
00006 
00007 namespace esg {
00008 
00009 #define INSTANTIATE_AUTOPTR_STATIC_MEMBERS(T)             \
00010     template<> std::set<AutoPtr<T>*> AutoPtr<T>::_repos = \
00011         std::set<AutoPtr<T>*>();
00012 
00042 template <class T> class OGSCENE_EXPORT AutoPtr {
00043 protected:
00044         T *                          _pObject; 
00045         std::set<void*>              _ref;     
00046         static std::set<AutoPtr<T>*> _repos;   
00047 
00048 protected:
00049         void _unregister (void * pR) { _ref.erase(pR); }
00050 
00051         //    AutoPtr<T> (const AutoPtr<T>&);
00052 
00053         ~AutoPtr<T> () {
00054             if (_pObject) { delete _pObject; _pObject = NULL; }
00055             if (_ref.size()) {
00056                 fprintf(stderr,"AutoPtr Error: Have registered references\n");
00057                 _ref.clear();
00058             }
00059         }
00060     
00061 public:
00065         AutoPtr<T>(T * t) : _pObject(t) { _repos.insert(this); }
00066 
00076         static bool destroy (AutoPtr<T>* pA, void* pR) {
00077             if (pA && pR) {
00078                 pA->_unregister(pR);
00079                 if (!pA->hasReferer()) {
00080                     _repos.erase(pA);
00081                     delete pA;
00082                     return true;
00083                 }
00084             }
00085             return false;
00086         }
00087 
00093         void registerReferer (void * pR) { _ref.insert(pR); }
00094 
00100         int hasReferer () const { return _ref.size(); }
00101 
00107         T* origObject() { return _pObject; }
00108 };
00109 
00110 }; // namespace
00111 
00112 #endif // __AUTO_PTR_H

Generated on Wed Jun 28 12:24:28 2006 for esg by  doxygen 1.4.6