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.
12 #ifndef DPTR_SHAREDCLASS_0_1_H
13 #define DPTR_SHAREDCLASS_0_1_H
16 namespace Chester_0_1{
19 /** \brief Base class of shared d-pointers.
21 Use in conjunction with DECLARE_SHARED_DPTR and DEFINE_SHARED_DPTR */
27 ///instantiates a shared d-pointer
29 ///deletes a shared d-pointer
30 virtual ~SharedDPtr(){}
31 ///called by the wrapper to attach to a new instance
32 virtual void attach(){cnt++;}
33 ///called by the wrapper to detach from an instance
34 virtual void detach(){cnt--;if(cnt==0)delete this;}
40 using namespace Chester_0_1;
45 #ifdef DEFINE_SHARED_DPTR
46 #undef DEFINE_SHARED_DPTR
49 /** \brief Defines the methods of the shared d-pointer wrapper.
51 This implements the shared version of the d-pointer wrapper.
52 To be used in implementation where the actual d-pointer class is implemented.
54 \param Class the base class within which the d-pointer was declared*/
55 #define DEFINE_SHARED_DPTR(Class) \
56 Class::DPrivate::DPrivate(){d=new Class::Private;}\
57 Class::DPrivate::DPrivate(const DPrivate&dp){d=dp.d;d->attach();}\
58 Class::DPrivate::~DPrivate(){d->detach();}\
59 Class::DPrivate Class::DPrivate::clone()const{DPrivate r;*(r.d)=*d;return r;}\
60 Class::DPrivate& Class::DPrivate::operator=(const DPrivate&dp)\
61 {d->detach();d=dp.d;d->attach();return *this;}