add ignore pattern for git
[web/konrad/chester.git] / dptr_base.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
10 #ifdef DPTR_CLASS_NAME
11 #undef DPTR_CLASS_NAME
12 #endif
13
14 #ifdef DPTR_NAME
15 #undef DPTR_NAME
16 #endif
17
18 #ifdef DPTR_WRAPPER_NAME
19 #undef DPTR_WRAPPER_NAME
20 #endif
21
22 /** \def DPTR_CLASS_NAME(Class)
23 \brief Expands to the fully qualified name of the d-pointer class.
24 \param Class the fully qualified name of the class the d-pointer was declared in.*/
25 #define DPTR_CLASS_NAME(Class) Class::Private
26
27 /** \def DPTR_NAME
28 \brief Expands to the local name of d-pointer classes (Private).
29 */
30 #define DPTR_NAME Private
31
32 /** \def DPTR_WRAPPER_NAME
33 Expands to the local name of the d-pointer wrapper class (DPrivate).
34 */
35 #define DPTR_WRAPPER_NAME DPrivate
36
37 #ifdef DECLARE_DPTR
38 #undef DECLARE_DPTR
39 #endif
40
41 /** \def DECLARE_DPTR(dp)
42 \brief Declares a smart non-shared d-pointer, to be used inside class declaration.
43
44 It also declares the internal nested classes DPrivate and Private - Private is the actual d-pointer class, while DPrivate is a wrapper that automatically allocates, copies and deallocates the d-pointer.
45
46 The wrapper DPointer class contains these methods:
47    - constructor, copy constructor for creating instances of Private
48     - used by automatic and explicit constructors of the containing class
49    - destructor for deallocation of Private
50     - used by the destructor of the containing class
51    - assignment operator to copy the content of Private
52     - used by the assignment operator of the containing class
53    - pointer operator to actually access the Private data
54
55 You can use DEFINE_DPTR to define the necessary methods for a non-shared d-pointer. It is recommended (but not necessary) that non-shared d-pointer classes (Private) are derived from DPtr.
56
57 The d-pointer class Private is only forward declared, you have to fully declare and implement it in the code where you are using it, i.e. where you are implementing the containing class.
58
59 \param dp name of the d-pointer*/
60 #define DECLARE_DPTR(dp) \
61  private:\
62  class Private; \
63  class DPrivate{\
64   public:DPrivate();DPrivate(const DPrivate&);~DPrivate();\
65   DPrivate&operator=(const DPrivate&);\
66   const Private*operator->()const{return d;}\
67   Private*operator->(){return d;}\
68   DPrivate clone()const;\
69   private:Private*d;\
70  }; \
71  DPrivate dp;
72
73 #ifdef DECLARE_SHARED_DPTR
74 #undef DECLARE_SHARED_DPTR
75 #endif
76
77 /** \brief Declares a smart shared d-pointer, to be used inside class declaration.
78
79 It also declares the internal nested classes DPrivate and Private - Private is the actual d-pointer class, while DPrivate is a wrapper that automatically allocates, copies and deallocates the d-pointer.
80
81 The wrapper DPointer class contains these methods:
82    - constructor, copy constructor for creating instances of Private
83     - used by automatic and explicit constructors of the containing class
84    - destructor for deallocation of Private
85     - used by the destructor of the containing class
86    - assignment operator to copy the content of Private
87     - used by the assignment operator of the containing class
88    - pointer operator to actually access the Private data
89
90 You can use DEFINE_DPTR to define the necessary methods for a non-shared d-pointer or DEFINE_SHARED_DPTR if you want to share d-pointer data between instances of the containing class. It is recommended that non-shared d-pointer classes (Private) are derived from DPtr and the shared variants be derived from SharedDPtr.
91
92 The d-pointer class Private is only forward declared, you have to fully declare and implement it in the code where you are using it, i.e. where you are implementing the containing class.
93
94 Warning: shared d-pointers are not thread safe and they are only re-entrant if instances sharing the same d-pointer are only located in one thread, while instances with different d-pointers may be spread over different threads.
95
96 \param dp name of the d-pointer*/
97 #define DECLARE_SHARED_DPTR(dp) DECLARE_DPTR(dp)
98
99
100 #ifdef DECLARE_NONCOPY_DPTR
101 #undef DECLARE_NONCOPY_DPTR
102 #endif
103
104 /** \brief Declares a smart non-shared and non-copyable d-pointer, to be used inside class declaration.
105
106 It also declares the internal nested classes DPrivate and Private - Private is the actual d-pointer class, while DPrivate is a wrapper that automatically allocates, and deallocates the d-pointer. These are usable for content classes that do not allow copying (e.g. Qt's QObject and its subclasses).
107
108 The wrapper DPointer class contains these methods:
109    - constructor for creating instances of Private
110     - used by automatic and explicit constructors of the containing class
111    - destructor for deallocation of Private
112     - used by the destructor of the containing class
113    - private assignment operator and copy constructor
114     - effectively hiding and blocking them
115    - pointer operator to actually access the Private data
116
117 You can use DEFINE_NONCOPY_DPTR to define the necessary methods for a non-shared, non-copy d-pointer. It is recommended that d-pointer classes (Private) are derived from NonCopyDPtr.
118
119 The d-pointer class Private is only forward declared, you have to fully declare and implement it in the code where you are using it, i.e. where you are implementing the containing class.
120
121 \param dp name of the d-pointer*/
122 #define DECLARE_NONCOPY_DPTR(dp) \
123  private:\
124  class Private; \
125  class DPrivate{\
126   public:DPrivate();~DPrivate();\
127   const Private*operator->()const{return d;}\
128   Private*operator->(){return d;}\
129   private:Private*d;\
130   DPrivate(const DPrivate&);DPrivate&operator=(const DPrivate&);\
131  }; \
132  DPrivate dp;