TOptDelPtr.h
Go to the documentation of this file.
1 #ifndef istd_TOptDelPtr_included
2 #define istd_TOptDelPtr_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, bool DelArray = false>
20 class TOptDelPtr: public TPointerBase<Type>
21 {
22 public:
24 
30  TOptDelPtr(Type* ptr = NULL, bool releaseFlag = false);
31 
37  TOptDelPtr(const TOptDelPtr& ptr);
38 
43  ~TOptDelPtr();
44 
50  bool IsToRelase() const;
51 
55  void Reset();
56 
62  void SetPtr(Type* ptr, bool releaseFlag = false);
63 
67  Type* PopPtr();
72  void TakeOver(TOptDelPtr& sourcePtr);
73 
79  TOptDelPtr& operator=(const TOptDelPtr& ptr);
85  TOptDelPtr& operator=(Type* ptr);
86 
92  template <class SourceType>
93  bool SetCastedOrRemove(SourceType* ptr, bool releaseFlag = false)
94  {
95  Type* castedPtr = dynamic_cast<Type*>(ptr);
96 
97  SetPtr(castedPtr, releaseFlag);
98 
99  if (castedPtr != NULL){
100  return true;
101  }
102  else if (releaseFlag){
103  delete ptr;
104  }
105 
106  return false;
107  }
108 
109 protected:
114  void Detach();
115 
116 private:
117  bool m_releaseFlag;
118 };
119 
120 
121 // inline methods
122 
123 template <class Type, bool DelArray>
124 inline TOptDelPtr<Type, DelArray>::TOptDelPtr(Type* ptr, bool releaseFlag)
125 : BaseClass(ptr), m_releaseFlag(releaseFlag)
126 {
127 }
128 
129 
130 template <class Type, bool DelArray>
132 : BaseClass(NULL), m_releaseFlag(false)
133 {
134  if (!ptr.m_releaseFlag){
135  SetPtr(ptr.GetPtr(), false);
136 
137  return;
138  }
139 
140  Q_ASSERT(ptr.GetPtr() == NULL);
141 }
142 
143 
144 template <class Type, bool DelArray>
146 {
147  Detach();
148 }
149 
150 
151 template <class Type, bool DelArray>
153 {
154  SetPtr(NULL);
155 }
156 
157 
158 template <class Type, bool DelArray>
160 {
161  return m_releaseFlag;
162 }
163 
164 
165 template <class Type, bool DelArray>
166 inline void TOptDelPtr<Type, DelArray>::SetPtr(Type* ptr, bool releaseFlag)
167 {
168  Detach();
169 
170  BaseClass::SetPtr(ptr);
171 
172  m_releaseFlag = releaseFlag;
173 }
174 
175 
176 template <class Type, bool DelArray>
178 {
179  Type* slavePtr = BaseClass::GetPtr();
180  BaseClass::SetPtr(NULL);
181 
182  return slavePtr;
183 }
184 
185 
186 template <class Type, bool DelArray>
188 {
189  SetPtr(sourcePtr.PopPtr());
190 }
191 
192 
193 // public methods
194 
195 template <class Type, bool DelArray>
197 {
198  if (m_releaseFlag){
199  I_IF_DEBUG(Q_ASSERT(ptr.GetPtr() == NULL));
200  }
201 
202  Reset();
203 
204  return *this;
205 }
206 
207 
208 template <class Type, bool DelArray>
210 {
211  Detach();
212 
213  BaseClass::SetPtr(ptr);
214 
215  return *this;
216 }
217 
218 
219 // protected methods
220 
221 template <class Type, bool DelArray>
223 {
224  if (m_releaseFlag){
225  Type* ptr = BaseClass::GetPtr();
226  if (ptr == NULL){
227  return;
228  }
229 
230  if (DelArray){
231  delete[] ptr;
232  }
233  else{
234  delete ptr;
235  }
236  }
237 }
238 
239 
240 } // namespace istd
241 
242 
243 #endif // !istd_TOptDelPtr_included
244 
245 
TPointerBase< Type > BaseClass
Definition: TOptDelPtr.h:23
#define I_IF_DEBUG(instructions)
Definition: istd.h:49
~TOptDelPtr()
Destructor.
Definition: TOptDelPtr.h:145
Implementation of pointer wrapper.
Definition: TPointerBase.h:49
void Reset()
Remove object pointed by internal pointer and set this pointer to NULL.
Definition: TOptDelPtr.h:152
Type * PopPtr()
Reset internal pointer value without deleting instance and return previos value.
Definition: TOptDelPtr.h:177
bool IsToRelase() const
Get state of release flag.
Definition: TOptDelPtr.h:159
Pointer wrapper providing activatable deleting pointed object during destruction. ...
Definition: TOptDelPtr.h:20
bool SetCastedOrRemove(SourceType *ptr, bool releaseFlag=false)
Set internal pointer using casted pointer of other type.
Definition: TOptDelPtr.h:93
void TakeOver(TOptDelPtr &sourcePtr)
Take internal pointer over.
Definition: TOptDelPtr.h:187
void SetPtr(Type *ptr, bool releaseFlag=false)
Set new value of internal pointer.
Definition: TOptDelPtr.h:166
Type * GetPtr() const
Return access to internal stored pointer.
Definition: TPointerBase.h:129
TOptDelPtr(Type *ptr=NULL, bool releaseFlag=false)
Construct and init this pointer.
Definition: TOptDelPtr.h:124
#define NULL
Definition: istd.h:64
TOptDelPtr & operator=(const TOptDelPtr &ptr)
Assign operator.
void Detach()
Remove referenced object.
Definition: TOptDelPtr.h:222

© 2007-2017 Witold Gantzke and Kirill Lepskiy