Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "dptr_base.h"
00010
00011
00012 #ifndef DPTR_SHAREDCLASS_0_1_H
00013 #define DPTR_SHAREDCLASS_0_1_H
00014
00016 namespace Chester_0_1{
00018
00022 class SharedDPtr
00023 {
00024 private:
00025 int cnt;
00026 public:
00028 SharedDPtr(){cnt=1;}
00030 virtual ~SharedDPtr(){}
00032 virtual void attach(){cnt++;}
00034 virtual void detach(){cnt--;if(cnt==0)delete this;}
00036 int currentRefCount()const{return cnt;}
00037 };
00038
00039
00041 };
00042 using namespace Chester_0_1;
00044
00045 #endif
00046
00047 #ifdef DEFINE_SHARED_DPTR
00048 #undef DEFINE_SHARED_DPTR
00049 #endif
00050
00057 #define DEFINE_SHARED_DPTR(Class) \
00058 Class::DPrivate::DPrivate(){d=new Class::Private;}\
00059 Class::DPrivate::DPrivate(const DPrivate&dp){d=dp.d;d->attach();}\
00060 Class::DPrivate::~DPrivate(){d->detach();}\
00061 Class::DPrivate Class::DPrivate::clone()const{DPrivate r;*(r.d)=*d;return r;}\
00062 Class::DPrivate& Class::DPrivate::operator=(const DPrivate&dp)\
00063 {if(d!=dp.d){d->detach();d=dp.d;d->attach();}return *this;}\
00064 void Class::DPrivate::decouple()\
00065 {if(d->currentRefCount()>1){Private *p=new Class::Private(*d);d->detach();d=p;}}
00066