ADTF  3.18.2
d_ptr.h
Go to the documentation of this file.
1 
7 #ifndef BASE_D_POINTER_H
8 #define BASE_D_POINTER_H
9 
10 namespace A_UTILS_NS
11 {
20  template <class _PARENT, class _PRIVATE_D>
21  class d_ptr_impl
22  {
23  protected:
25  _PARENT* _p = nullptr;
26 
27  public:
29  d_ptr_impl() = default;
30 
32  virtual ~d_ptr_impl()
33  {
34  Release_d();
35  _p = nullptr;
36  }
37 
39  d_ptr_impl(const d_ptr_impl&) = delete;
40  d_ptr_impl& operator=(const d_ptr_impl&) = delete;
41 
43  d_ptr_impl(d_ptr_impl&&) = delete;
44  d_ptr_impl& operator=(d_ptr_impl&&) = delete;
45 
52  tVoid Set_p(_PARENT* _pInst)
53  {
54  _p = _pInst;
55  Create_d();
56  }
57 
61  inline _PRIVATE_D* operator->() const
62  {
63  return (_PRIVATE_D*)(this);
64  }
65 
70  inline _PRIVATE_D* Get_d() const
71  {
72  return (_PRIVATE_D*)(this);
73  }
74 
78  inline operator _PRIVATE_D*() const
79  {
80  return (_PRIVATE_D*)(this);
81  }
82 
87  {
88  return (d_ptr_impl< _PARENT, _PRIVATE_D >*)(&*this);
89  }
90 
91  protected:
95  virtual tVoid Create_d()
96  {
97  //nothing to do this is to overwrite
98  }
102  virtual tVoid Release_d()
103  {
104  //nothing to do this is to overwrite
105  }
106  };
107 
115  template <class _PARENT, class _PRIVATE_D>
116  class d_ptr
117  {
118  private:
120 
121  protected:
123  internal_type* _dRef = nullptr;
124 
125  public:
127 
129  d_ptr() = default;
130 
132  virtual ~d_ptr()
133  {
134  if (_dRef)
135  {
136  delete _dRef;
137  _dRef = nullptr;
138  }
139  }
140 
141  //No copying allowed - this is no shared ptr!
142  d_ptr(const d_ptr&) = delete;
143  d_ptr& operator=(const d_ptr&) = delete;
144 
145  //moving allowed
146  d_ptr(d_ptr&& o_oOther) : _dRef(nullptr)
147  { //leave move-from-object in valid but unspecified state
148  Swap(o_oOther);
149  }
150 
151  d_ptr& operator=(d_ptr&& o_oOther)
152  { //leave move-from-object in valid but unspecified state
153  if (this != &o_oOther)
154  {
155  Swap(o_oOther);
156  //Reset move-from-object
157  self_type().Swap(o_oOther);
158  }
159  return *this;
160  }
161 
167  tVoid Set_p(_PARENT* _pInst)
168  {
169  if (_dRef)
170  {
171  _dRef->Set_p(_pInst);
172  }
173  }
174 
178  _PRIVATE_D* operator->() const
179  {
180  return Get_d();
181  }
182 
187  _PRIVATE_D* Get_d() const
188  {
189  return (_PRIVATE_D*)(_dRef);
190  }
191 
195  operator _PRIVATE_D*() const
196  {
197  return Get_d();
198  }
199 
203  self_type* operator=(_PRIVATE_D* _dInst)
204  {
205  if (_dRef)
206  {
207  delete _dRef;
208  }
209  _dRef = (internal_type*)_dInst;
210  return this;
211  }
212 
217  tVoid Swap(self_type& o_oOther)
218  { //std::swap not present, do it yourself...
219  const auto _dRefTmp = _dRef;
220  _dRef = o_oOther._dRef;
221  o_oOther._dRef = _dRefTmp;
222  }
223  };//class class cAUTILSDPtr
224 } //namespace
225 
226 namespace std
227 {
236  template<typename _PARENT, typename _PRIVATE_D>
239  {
240  i_oLHS.Swap(i_oRHS);
241  }
242 }//ns std
243 
244 
256 #define A_UTILS_D_CLASS(__dclassname_, __pclassname_) \
257 private: \
258  class __dclassname_; \
259  A_UTILS_NS::d_ptr<__pclassname_, __dclassname_> _d; \
260  friend class __dclassname_;
261 
270 #define A_UTILS_D(__pclassname_) A_UTILS_D_CLASS(__pclassname_ ## Private, __pclassname_)
271 
277 #define A_UTILS_D_CREATE_CLASS(__classname_) \
278  _d = new __classname_(); \
279  _d->Set_p(this)
280 
286 #define A_UTILS_D_CREATE(__classname_) A_UTILS_D_CREATE_CLASS(__classname_ ## Private)
287 
293 #define A_UTILS_DECLARE_LOCAL_D(__classname_) __classname_ ## Private* _D = _d.Get_d()
294 
300 #define A_UTILS_P_DECLARE_CLASS(__d_classname_, __p_classname_) class __p_classname_::__d_classname_ : public A_UTILS_NS::d_ptr_impl<__p_classname_, __p_classname_::__d_classname_>
301 
306 #define A_UTILS_P_DECLARE( __p_classname_) A_UTILS_P_DECLARE_CLASS(__p_classname_ ## Private, __p_classname_)
307 
312 #define A_UTILS_P_CLASS( __p_classname_) __p_classname_::__p_classname_ ## Private
313 
319 #define A_UTILS_P_DECLARE_CLASS_PRE(__d_classname_, __p_classname_) \
320  A_UTILS_P_DECLARE_CLASS(__d_classname_, __p_classname_)\
321  { \
322  friend class __p_classname_; \
323  public: \
324  __d_classname_(){}; \
325  virtual ~__d_classname_(){}; \
326  };
327 
333 #define A_UTILS_P_DECLARE_PRE(__p_classname_) A_UTILS_P_DECLARE_CLASS_PRE(__p_classname_ ## Private, __p_classname_)
334 
442 #endif /* BASE_D_POINTER_H */
void tVoid
The tVoid is always the definition for the void (non-type).
Template to implement the Private class of the global d_pointer definitions.
Definition: d_ptr.h:22
virtual tVoid Create_d()
Virtual function call to overwrite it something needs to be done on creation time.
Definition: d_ptr.h:95
virtual tVoid Release_d()
Virtual function call to overwrite it something needs to be done before destroying the object.
Definition: d_ptr.h:102
_PARENT * _p
the reference to the parent class
Definition: d_ptr.h:25
d_ptr_impl(d_ptr_impl &&)=delete
moving forbidden
_PRIVATE_D * operator->() const
Overwrites the pointer operator to return the Private class reference.
Definition: d_ptr.h:61
d_ptr_impl(const d_ptr_impl &)=delete
copying forbidden
_PRIVATE_D * Get_d() const
Gets the Private class reference.
Definition: d_ptr.h:70
virtual ~d_ptr_impl()
destructor
Definition: d_ptr.h:32
tVoid Set_p(_PARENT *_pInst)
Sets the parent Reference.
Definition: d_ptr.h:52
d_ptr_impl()=default
Default constructor.
Template class for the d-pointer Reference class withing the Parent Class.
Definition: d_ptr.h:117
_PRIVATE_D * operator->() const
Pointer operator enable access to the internal pointer.
Definition: d_ptr.h:178
tVoid Swap(self_type &o_oOther)
Swap *this d_ptr with another one.
Definition: d_ptr.h:217
internal_type * _dRef
The internal pointer to the private class.
Definition: d_ptr.h:123
_PRIVATE_D * Get_d() const
Gets the internal pointer.
Definition: d_ptr.h:187
d_ptr()=default
Default constructor.
d_ptr_impl< _PARENT, _PRIVATE_D > internal_type
internally held type
Definition: d_ptr.h:119
virtual ~d_ptr()
Destructor.
Definition: d_ptr.h:132
self_type * operator=(_PRIVATE_D *_dInst)
Assignment operator allows to set the internal pointer value.
Definition: d_ptr.h:203
d_ptr< _PARENT, _PRIVATE_D > self_type
self type
Definition: d_ptr.h:126
tVoid Set_p(_PARENT *_pInst)
Sets the internal pointer.
Definition: d_ptr.h:167
tVoid swap(A_UTILS_NS::d_ptr< _PARENT, _PRIVATE_D > &i_oLHS, A_UTILS_NS::d_ptr< _PARENT, _PRIVATE_D > &i_oRHS)
std::swap specialization for AUTILSDPtr for perfect fit on ADL
Definition: d_ptr.h:237
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.
Definition: d_ptr.h:11