--- parser3/src/include/pa_pool.h 2003/01/23 11:37:55 1.86.2.2 +++ parser3/src/include/pa_pool.h 2003/01/28 12:11:49 1.86.2.16 @@ -9,7 +9,7 @@ #ifndef PA_POOL_H #define PA_POOL_H -static const char* IDENT_POOL_H="$Date: 2003/01/23 11:37:55 $"; +static const char* IDENT_POOL_H="$Date: 2003/01/28 12:11:49 $"; #include "pa_config_includes.h" @@ -219,6 +219,7 @@ public: */ #define override +#define rethrow throw void *operator new(size_t size) { return pa_malloc(size); @@ -236,44 +237,54 @@ public: static void operator delete(void *ptr) { pa_free(ptr); } + static void *malloc(size_t size) { + return pa_malloc(size); + } + static void *calloc(size_t size) { + return pa_calloc(size); + } + static void *realloc(void *ptr, size_t size) { + return pa_realloc(ptr, size); + } + }; /** Base for all Parser classes, memory allocation/dallocation goes via pa_malloc/pa_free. */ class PA_Object: public PA_Allocated { - mutable unsigned long references; + mutable unsigned long freferences; public: - PA_Object(): references(0) {} + PA_Object(): freferences(0) {} virtual ~PA_Object()=0; - void ref() { - references++; + void ref() const { + freferences++; } - void unref() { - if(references) { - if(--references==0) + void unref() const { + if(freferences) { + if(--freferences==0) delete this; } } }; -template class object_auto_ptr { +template class object_ptr { T *ptr; public: typedef T element_type; - explicit object_auto_ptr(T *ptr = 0) { + explicit object_ptr(T *ptr = 0) { this->ptr=ptr; if(ptr) ptr->ref(); } - object_auto_ptr(const object_auto_ptr& src) { + object_ptr(const object_ptr& src) { ptr=src.get(); ptr->ref(); } - object_auto_ptr& operator=(object_auto_ptr& src) { + object_ptr& operator=(const object_ptr& src) { if(this!=&src) if(ptr!=src.get()) { if(ptr) @@ -282,8 +293,9 @@ public: if(ptr) ptr->ref(); } + return *this; } - ~object_auto_ptr() { + ~object_ptr() { if(ptr) ptr->unref(); } @@ -296,18 +308,32 @@ public: T *get() const { return ptr; } + // so one could object_ptr = object_ptr + operator object_ptr() const{ + return *this; + } + operator bool() const { + return get()!=0; + } + operator !() const { + return get()==0; + } }; -/// TEMPLATE CLASS simple_auto_ptr, stolen from stl:auto_ptr +#define DECLARE_OBJECT_PTR(name) \ + typedef object_ptr name##Ptr; \ + const object_ptr name##PtrZero(0); + +/// TEMPLATE CLASS smart_ptr, stolen from stl:auto_ptr template - class simple_auto_ptr { + class smart_ptr { public: typedef T element_type; - explicit simple_auto_ptr(T *_P = 0) + explicit smart_ptr(T *_P = 0) : _Owns(_P != 0), _Ptr(_P) {} - simple_auto_ptr(const simple_auto_ptr& _Y) + smart_ptr(const smart_ptr& _Y) : _Owns(_Y._Owns), _Ptr(_Y.release()) {} - simple_auto_ptr& operator=(const simple_auto_ptr& _Y) + smart_ptr& operator=(const smart_ptr& _Y) {if (this != &_Y) {if (_Ptr != _Y.get()) {if (_Owns) @@ -317,22 +343,34 @@ public: _Owns = true; _Ptr = _Y.release(); } return (*this); } - ~simple_auto_ptr() + ~smart_ptr() {if (_Owns) delete _Ptr; } T& operator*() const {return (*get()); } - T *operator->() const - {return (get()); } T *get() const {return (_Ptr); } T *release() const - {((simple_auto_ptr *)this)->_Owns = false; + {((smart_ptr *)this)->_Owns = false; return (_Ptr); } + // so one could smart_ptr = smart_ptr + operator smart_ptr() const { + return *this; + } + operator bool() const { + return get()!=0; + } + operator T*() const { + return _Ptr; + } private: bool _Owns; T *_Ptr; }; +// convinient types + +typedef smart_ptr CharPtr; +const CharPtr CharPtrZero(0); #endif