add ignore pattern for git
[web/konrad/chester.git] / dptr_shared.h
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
15 /// \cond never
16 namespace Chester_0_1{
17 /// \endcond
18
19 /** \brief Base class of shared d-pointers.
20
21 Use in conjunction with DECLARE_SHARED_DPTR and DEFINE_SHARED_DPTR */
22 class SharedDPtr
23 {
24         private:
25                 int cnt;
26         public:
27                 ///instantiates a shared d-pointer
28                 SharedDPtr(){cnt=1;}
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;}
35 };
36
37 //hide the namespace
38 /// \cond never
39 };
40 using namespace Chester_0_1;
41 /// \endcond
42
43 #endif
44
45 #ifdef DEFINE_SHARED_DPTR
46 #undef DEFINE_SHARED_DPTR
47 #endif
48
49 /** \brief Defines the methods of the shared d-pointer wrapper.
50
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.
53
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  {if(d!=dp.d){d->detach();d=dp.d;d->attach();}return *this;}
62