Chester
 All Classes Files Functions Variables Macros Pages
dptr_shared.h
Go to the documentation of this file.
1 //d-ptr header
2 //
3 // (c) Konrad Rosenbaum, 2010-2011
4 // Copying and distribution of this file, with or without modification,
5 // are permitted in any medium without royalty provided the copyright
6 // notice and this notice are preserved. This file is offered as-is,
7 // without any warranty.
8 
9 #include "dptr_base.h"
10 
11 
12 #ifndef DPTR_SHAREDCLASS_0_1_H
13 #define DPTR_SHAREDCLASS_0_1_H
14 //hide the namespace
16 namespace Chester_0_1{
18 
23 {
24  private:
25  int cnt;
26  public:
28  SharedDPtr(){cnt=1;}
30  virtual ~SharedDPtr(){}
32  virtual void attach(){cnt++;}
34  virtual void detach(){cnt--;if(cnt==0)delete this;}
36  int currentRefCount()const{return cnt;}
37 };
38 
39 //hide the namespace
41 };
42 using namespace Chester_0_1;
44 
45 #endif
46 
47 #ifdef DEFINE_SHARED_DPTR
48 #undef DEFINE_SHARED_DPTR
49 #endif
50 
57 #define DEFINE_SHARED_DPTR(Class) \
58  Class::DPrivate::DPrivate(){d=new Class::Private;}\
59  Class::DPrivate::DPrivate(const DPrivate&dp){d=dp.d;d->attach();}\
60  Class::DPrivate::~DPrivate(){d->detach();}\
61  Class::DPrivate Class::DPrivate::clone()const{DPrivate r;*(r.d)=*d;return r;}\
62  Class::DPrivate& Class::DPrivate::operator=(const DPrivate&dp)\
63  {if(d!=dp.d){d->detach();d=dp.d;d->attach();}return *this;}\
64  void Class::DPrivate::decouple()\
65  {if(d->currentRefCount()>1){Private *p=new Class::Private(*d);d->detach();d=p;}}
66 
virtual ~SharedDPtr()
deletes a shared d-pointer
Definition: dptr_shared.h:30
virtual void detach()
called by the wrapper to detach from an instance
Definition: dptr_shared.h:34
Base class of shared d-pointers.
Definition: dptr_shared.h:22
virtual void attach()
called by the wrapper to attach to a new instance
Definition: dptr_shared.h:32
SharedDPtr()
instantiates a shared d-pointer
Definition: dptr_shared.h:28
int currentRefCount() const
called by the wrapper to enable copy-on-write logic
Definition: dptr_shared.h:36
int cnt
Definition: dptr_shared.h:25