ACF $AcfVersion:0$
TOptDelPtr.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// ACF includes
6#include <istd/istd.h>
7#include <istd/TPointerBase.h>
8
9
10namespace istd
11{
12
13
19template <class Type, bool DelArray = false>
20class TOptDelPtr: public TPointerBase<Type>
21{
22public:
24
30 TOptDelPtr(Type* ptr = NULL, bool releaseFlag = false);
31
38
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
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 && (ptr != nullptr)){
103 delete ptr;
104 }
105
106 return false;
107 }
108
109protected:
114 void Detach();
115
116private:
117 bool m_releaseFlag = false;
118};
119
120
121// inline methods
122
123template <class Type, bool DelArray>
124inline TOptDelPtr<Type, DelArray>::TOptDelPtr(Type* ptr, bool releaseFlag)
125: BaseClass(ptr), m_releaseFlag(releaseFlag)
126{
127}
128
129
130template <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
144template <class Type, bool DelArray>
146{
147 Detach();
148}
149
150
151template <class Type, bool DelArray>
153{
154 SetPtr(NULL);
155}
156
157
158template <class Type, bool DelArray>
160{
161 return m_releaseFlag;
162}
163
164
165template <class Type, bool DelArray>
166inline 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
176template <class Type, bool DelArray>
178{
179 Type* slavePtr = BaseClass::GetPtr();
180 BaseClass::SetPtr(NULL);
181
182 return slavePtr;
183}
184
185
186template <class Type, bool DelArray>
188{
189 SetPtr(sourcePtr.PopPtr(), sourcePtr.IsToRelase());
190}
191
192
193// public methods
194
195template <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
208template <class Type, bool DelArray>
210{
211 Detach();
212
213 BaseClass::SetPtr(ptr);
214
215 return *this;
216}
217
218
219// protected methods
220
221template <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
244
Pointer wrapper providing activatable deleting pointed object during destruction.
Definition TOptDelPtr.h:21
bool IsToRelase() const
Get state of release flag.
Definition TOptDelPtr.h:159
void Reset()
Remove object pointed by internal pointer and set this pointer to NULL.
Definition TOptDelPtr.h:152
TOptDelPtr(const TOptDelPtr &ptr)
Copy constructor.
Definition TOptDelPtr.h:131
void TakeOver(TOptDelPtr &sourcePtr)
Take internal pointer over.
Definition TOptDelPtr.h:187
TOptDelPtr & operator=(Type *ptr)
Assign operator.
Definition TOptDelPtr.h:209
TPointerBase< Type > BaseClass
Definition TOptDelPtr.h:23
TOptDelPtr & operator=(const TOptDelPtr &ptr)
Assign operator.
bool SetCastedOrRemove(SourceType *ptr, bool releaseFlag=false)
Set internal pointer using casted pointer of other type.
Definition TOptDelPtr.h:93
Type * PopPtr()
Reset internal pointer value without deleting instance and return previos value.
Definition TOptDelPtr.h:177
void Detach()
Remove referenced object.
Definition TOptDelPtr.h:222
void SetPtr(Type *ptr, bool releaseFlag=false)
Set new value of internal pointer.
Definition TOptDelPtr.h:166
~TOptDelPtr()
Destructor.
Definition TOptDelPtr.h:145
TOptDelPtr(Type *ptr=NULL, bool releaseFlag=false)
Construct and init this pointer.
Definition TOptDelPtr.h:124
Implementation of pointer wrapper.
Type * GetPtr() const
Return access to internal stored pointer.
#define NULL
Definition istd.h:74
#define I_IF_DEBUG(instructions)
Definition istd.h:59
Standard library.
Definition IComponent.h:17