ACF $AcfVersion:0$
TDelPtr.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, class Accessor = DefaultAccessor<Type> >
20class TDelPtr: public TPointerBase<Type>
21{
22public:
24
29 TDelPtr(Type* ptr = NULL);
30
36 TDelPtr(const TDelPtr& ptr);
37
42
48
52 void Reset();
53
58 void SetPtr(Type* ptr);
59
63 Type* PopPtr();
68 void TakeOver(TDelPtr& sourcePtr);
69
76
81
87 TDelPtr& operator=(Type* ptr);
88
93 template <class SourceType>
94 bool SetCastedOrRemove(SourceType* ptr)
95 {
96 Type* castedPtr = dynamic_cast<Type*>(ptr);
97
98 SetPtr(castedPtr);
99
100 if (castedPtr != NULL){
101 return true;
102 }
103 else{
104 if (ptr != nullptr){
105 delete ptr;
106 }
107
108 return false;
109 }
110 }
111
112protected:
117 void Detach();
118};
119
120
121// inline methods
122
123template <class Type, class Accessor>
125: BaseClass(ptr)
126{
127}
128
129
130template <class Type, class Accessor>
132: BaseClass(NULL)
133{
134 I_IF_DEBUG(Q_ASSERT(ptr.GetPtr() == NULL));
135}
136
137
138template <class Type, class Accessor>
141{
142 SetPtr(ptr.PopPtr());
143}
144
145
146template <class Type, class Accessor>
151
152
153template <class Type, class Accessor>
155{
156 SetPtr(NULL);
157}
158
159
160template <class Type, class Accessor>
162{
163 Detach();
164
165 BaseClass::SetPtr(ptr);
166}
167
168
169template <class Type, class Accessor>
171{
172 Type* slavePtr = BaseClass::GetPtr();
173 BaseClass::SetPtr(NULL);
174
175 return slavePtr;
176}
177
178
179template <class Type, class Accessor>
181{
182 SetPtr(sourcePtr.PopPtr());
183}
184
185
186// public methods
187
188template <class Type, class Accessor>
190{
191 I_IF_DEBUG(Q_ASSERT(ptr.GetPtr() == NULL));
192
193 Reset();
194
195 return *this;
196}
197
198
199template <class Type, class Accessor>
201{
202 SetPtr(ptr.PopPtr());
203
204 return *this;
205}
206
207
208template <class Type, class Accessor>
210{
211 Detach();
212
213 BaseClass::SetPtr(ptr);
214
215 return *this;
216}
217
218
219// protected methods
220
221template <class Type, class Accessor>
223{
224 Type* ptr = BaseClass::GetPtr();
225 if (ptr == NULL){
226 return;
227 }
228
229 Accessor::Delete(ptr);
230}
231
232
233} // namespace istd
234
235
236
237
Pointer wrapper providing automatic deleting pointed object during destruction.
Definition TDelPtr.h:21
TDelPtr(const TDelPtr &ptr)
Copy constructor.
void Detach()
Remove referenced object.
Definition TDelPtr.h:222
~TDelPtr()
Destructor.
Definition TDelPtr.h:147
void Reset()
Remove object pointed by internal pointer and set this pointer to NULL.
Definition TDelPtr.h:154
void TakeOver(TDelPtr &sourcePtr)
Take internal pointer over.
Definition TDelPtr.h:180
TDelPtr & operator=(Type *ptr)
Assign operator.
Definition TDelPtr.h:209
Type * PopPtr()
Reset internal pointer value without deleting instance and return previos value.
Definition TDelPtr.h:170
TPointerBase< Type > BaseClass
Definition TDelPtr.h:23
TDelPtr(Type *ptr=NULL)
Construct and init this pointer.
Definition TDelPtr.h:124
TDelPtr(TDelPtr &&ptr)
Move constructor.
Definition TDelPtr.h:139
bool SetCastedOrRemove(SourceType *ptr)
Set internal pointer using casted pointer of other type.
Definition TDelPtr.h:94
TDelPtr & operator=(const TDelPtr &ptr)
Assign operator.
void SetPtr(Type *ptr)
Set new value of internal pointer.
Definition TDelPtr.h:161
TDelPtr & operator=(TDelPtr &&ptr)
Move operator.
Definition TDelPtr.h:200
Implementation of pointer wrapper.
#define NULL
Definition istd.h:74
#define I_IF_DEBUG(instructions)
Definition istd.h:59
Standard library.
Definition IComponent.h:17