--- parser3/src/include/pa_pool.h 2003/01/23 11:37:55 1.86.2.2 +++ parser3/src/include/pa_pool.h 2003/01/24 14:36:10 1.86.2.8 @@ -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/24 14:36:10 $"; #include "pa_config_includes.h" @@ -247,10 +247,10 @@ public: PA_Object(): references(0) {} virtual ~PA_Object()=0; - void ref() { + void ref() const { references++; } - void unref() { + void unref() const { if(references) { if(--references==0) delete this; @@ -259,21 +259,21 @@ public: }; -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 +282,9 @@ public: if(ptr) ptr->ref(); } + return *this; } - ~object_auto_ptr() { + ~object_ptr() { if(ptr) ptr->unref(); } @@ -296,18 +297,30 @@ public: T *get() const { return ptr; } + // so one could assign object_ptr to 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; + +/// 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 +330,31 @@ 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 assign smart_ptr to smart_ptr + operator smart_ptr() const { + return *this; + } + operator bool() const { + return get(); + } private: bool _Owns; T *_Ptr; }; +// convinient types + +typedef smart_ptr CharPtr; +typedef smart_ptr ConstCharPtr; #endif