TDelPtr.h
Go to the documentation of this file.
1 #ifndef istd_TDelPtr_included
2 #define istd_TDelPtr_included
3 
4 
5 // ACF includes
6 #include <istd/istd.h>
7 #include <istd/TPointerBase.h>
8 
9 
10 namespace istd
11 {
12 
13 
19 template <class Type, class Accessor = DefaultAccessor<Type> >
20 class TDelPtr: public TPointerBase<Type>
21 {
22 public:
24 
29  TDelPtr(Type* ptr = NULL);
30 
36  TDelPtr(const TDelPtr& ptr);
37 
42  ~TDelPtr();
43 
47  void Reset();
48 
53  void SetPtr(Type* ptr);
54 
58  Type* PopPtr();
63  void TakeOver(TDelPtr& sourcePtr);
64 
70  TDelPtr& operator=(const TDelPtr& ptr);
76  TDelPtr& operator=(Type* ptr);
77 
82  template <class SourceType>
83  bool SetCastedOrRemove(SourceType* ptr)
84  {
85  Type* castedPtr = dynamic_cast<Type*>(ptr);
86 
87  SetPtr(castedPtr);
88 
89  if (castedPtr != NULL){
90  return true;
91  }
92  else{
93  delete ptr;
94 
95  return false;
96  }
97  }
98 
99 protected:
104  void Detach();
105 };
106 
107 
108 // inline methods
109 
110 template <class Type, class Accessor>
112 : BaseClass(ptr)
113 {
114 }
115 
116 
117 template <class Type, class Accessor>
119 : BaseClass(NULL)
120 {
121  I_IF_DEBUG(Q_ASSERT(ptr.GetPtr() == NULL));
122 }
123 
124 
125 template <class Type, class Accessor>
127 {
128  TDelPtr::Detach();
129 }
130 
131 
132 template <class Type, class Accessor>
134 {
135  SetPtr(NULL);
136 }
137 
138 
139 template <class Type, class Accessor>
140 inline void TDelPtr<Type, Accessor>::SetPtr(Type* ptr)
141 {
142  Detach();
143 
144  BaseClass::SetPtr(ptr);
145 }
146 
147 
148 template <class Type, class Accessor>
150 {
151  Type* slavePtr = BaseClass::GetPtr();
152  BaseClass::SetPtr(NULL);
153 
154  return slavePtr;
155 }
156 
157 
158 template <class Type, class Accessor>
160 {
161  SetPtr(sourcePtr.PopPtr());
162 }
163 
164 
165 // public methods
166 
167 template <class Type, class Accessor>
169 {
170  I_IF_DEBUG(Q_ASSERT(ptr.GetPtr() == NULL));
171 
172  Reset();
173 
174  return *this;
175 }
176 
177 
178 template <class Type, class Accessor>
180 {
181  Detach();
182 
183  BaseClass::SetPtr(ptr);
184 
185  return *this;
186 }
187 
188 
189 // protected methods
190 
191 template <class Type, class Accessor>
193 {
194  Type* ptr = BaseClass::GetPtr();
195  if (ptr == NULL){
196  return;
197  }
198 
199  Accessor::Delete(ptr);
200 }
201 
202 
203 } // namespace istd
204 
205 
206 #endif // !istd_TDelPtr_included
207 
208 
#define I_IF_DEBUG(instructions)
Definition: istd.h:49
Implementation of pointer wrapper.
Definition: TPointerBase.h:49
void Detach()
Remove referenced object.
Definition: TDelPtr.h:192
~TDelPtr()
Destructor.
Definition: TDelPtr.h:126
void Reset()
Remove object pointed by internal pointer and set this pointer to NULL.
Definition: TDelPtr.h:133
TDelPtr(Type *ptr=NULL)
Construct and init this pointer.
Definition: TDelPtr.h:111
Type * PopPtr()
Reset internal pointer value without deleting instance and return previos value.
Definition: TDelPtr.h:149
void TakeOver(TDelPtr &sourcePtr)
Take internal pointer over.
Definition: TDelPtr.h:159
TPointerBase< Type > BaseClass
Definition: TDelPtr.h:23
#define NULL
Definition: istd.h:64
TDelPtr & operator=(const TDelPtr &ptr)
Assign operator.
bool SetCastedOrRemove(SourceType *ptr)
Set internal pointer using casted pointer of other type.
Definition: TDelPtr.h:83
Pointer wrapper providing automatic deleting pointed object during destruction.
Definition: TDelPtr.h:20
void SetPtr(Type *ptr)
Set new value of internal pointer.
Definition: TDelPtr.h:140

© 2007-2017 Witold Gantzke and Kirill Lepskiy