--- parser3/src/include/pa_pool.h 2003/01/22 15:39:07 1.86.2.1 +++ parser3/src/include/pa_pool.h 2003/01/23 15:38:05 1.86.2.3 @@ -9,7 +9,7 @@ #ifndef PA_POOL_H #define PA_POOL_H -static const char* IDENT_POOL_H="$Date: 2003/01/22 15:39:07 $"; +static const char* IDENT_POOL_H="$Date: 2003/01/23 15:38:05 $"; #include "pa_config_includes.h" @@ -218,25 +218,18 @@ public: #define NEW new(pool()) */ -/** - Base for all Parser classes, memory allocation/dallocation goes via pa_malloc/pa_free. -*/ -class PA_Object { - mutable unsigned long references_count; -public: - PA_Object(): references_count(0) {} - void add_ref() { - references_count++; - } - void release() { - if(references_count) { - if(--references_count==0) - delete this; - } - } +#define override + +void *operator new(size_t size) { + return pa_malloc(size); +} +void operator delete(void *ptr) { + pa_free(ptr); +} +class PA_Allocated { public: - /// the PA_Object -sole: instances allocated using our functions + /// the sole: instances allocated using our functions static void *operator new(size_t size) { return pa_malloc(size); } @@ -245,32 +238,54 @@ public: } }; -template class auto_ptr { +/** + Base for all Parser classes, memory allocation/dallocation goes via pa_malloc/pa_free. +*/ +class PA_Object: public PA_Allocated { + mutable unsigned long references; +public: + PA_Object(): references(0) {} + virtual ~PA_Object()=0; + + void ref() { + references++; + } + void unref() { + if(references) { + if(--references==0) + delete this; + } + } +}; + + +template class object_ptr { T *ptr; public: typedef T element_type; - explicit auto_ptr(T *ptr = 0) { + + explicit object_ptr(T *ptr = 0) { this->ptr=ptr; if(ptr) - ptr->add_ref(); + ptr->ref(); } - auto_ptr(const auto_ptr& src) { + object_ptr(const object_ptr& src) { ptr=src.get(); - ptr->add_ref(); + ptr->ref(); } - auto_ptr& operator=(auto_ptr& src) { + object_ptr& operator=(const object_ptr& src) { if(this!=&src) if(ptr!=src.get()) { if(ptr) - ptr->release(); + ptr->unref(); ptr=src.get(); if(ptr) - ptr->add_ref(); + ptr->ref(); } } - ~auto_ptr() { + ~object_ptr() { if(ptr) - ptr->release(); + ptr->unref(); } T& operator*() const { return *get(); @@ -283,4 +298,39 @@ public: } }; +/// TEMPLATE CLASS smart_ptr, stolen from stl:auto_ptr +template + class smart_ptr { +public: + typedef T element_type; + explicit smart_ptr(T *_P = 0) + : _Owns(_P != 0), _Ptr(_P) {} + smart_ptr(const smart_ptr& _Y) + : _Owns(_Y._Owns), _Ptr(_Y.release()) {} + smart_ptr& operator=(const smart_ptr& _Y) + {if (this != &_Y) + {if (_Ptr != _Y.get()) + {if (_Owns) + delete _Ptr; + _Owns = _Y._Owns; } + else if (_Y._Owns) + _Owns = true; + _Ptr = _Y.release(); } + return (*this); } + ~smart_ptr() + {if (_Owns) + delete _Ptr; } + T& operator*() const + {return (*get()); } + T *get() const + {return (_Ptr); } + T *release() const + {((smart_ptr *)this)->_Owns = false; + return (_Ptr); } +private: + bool _Owns; + T *_Ptr; + }; + + #endif