ICF 3.0.5.47
Technical documentation of ICF Libraries
TOptDelPtr.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#pragma once
7
8
9// ICF includes
10#include <istd/istd.h>
11#include <istd/TPointerBase.h>
12
13
14namespace istd
15{
16
17
21template <class Type, bool DelArray = false>
22class TOptDelPtr: public TPointerBase<Type>
23{
24public:
26
27 typedef std::function<void (Type*)> ReleaseFn;
28
29 TOptDelPtr(const TOptDelPtr& ptr) = delete;
30
36 TOptDelPtr(Type* ptr = nullptr, ReleaseFn releaseFn = nullptr);
37
43 TOptDelPtr(Type* ptr, bool releaseFlag);
44
49
54 TOptDelPtr(std::unique_ptr<Type>&& ptr);
55
61
67 bool IsToRelase() const;
68
72 void Reset();
73
80 void SetPtr(Type* ptr, ReleaseFn releaseFn = nullptr);
81
85 Type* GetFreePtr() const;
86
90 Type* release();
91
95 void Swap(TOptDelPtr& ptr);
96
103
109
114 TOptDelPtr& operator=(std::unique_ptr<Type>&& ptr);
121
127 template <class SourceType>
128 bool SetCastedOrRemove(SourceType* ptr, ReleaseFn releaseFn = nullptr)
129 {
130 Type* castedPtr = dynamic_cast<Type*>(ptr);
131
132 if (castedPtr != nullptr){
133 SetPtr(castedPtr, releaseFn);
134
135 return true;
136 }
137 else {
138 SetPtr(nullptr);
139
140 delete ptr;
141
142 return false;
143 }
144 }
145
151 template <class SourcePtrType>
152 bool SetCastedOrRemove(std::unique_ptr<SourcePtrType> ptr)
153 {
154 Type* castedPtr = dynamic_cast<Type*>(ptr.get());
155
156 if (castedPtr != nullptr) {
157 ptr.release();
158 SetPtr(castedPtr, SimpleDeleter);
159
160 return true;
161 }
162 else {
163 SetPtr(nullptr);
164
165 return false;
166 }
167 }
168
169 // static methods
173 static void SimpleDeleter(Type* ptr);
174
175protected:
180 void Detach();
181
182private:
183 ReleaseFn m_releaseFn;
184};
185
186
187// inline methods
188
189template <class Type, bool DelArray>
191: BaseClass(ptr), m_releaseFn(releaseFn)
192{
193}
194
195
196template <class Type, bool DelArray>
197inline TOptDelPtr<Type, DelArray>::TOptDelPtr(Type* ptr, bool releaseFlag)
198: BaseClass(ptr), m_releaseFn(releaseFlag? SimpleDeleter: nullptr)
199{
200}
201
202
203template <class Type, bool DelArray>
205: BaseClass(ptr.get()), m_releaseFn(std::move(ptr.m_releaseFn))
206{
207 ptr.BaseClass::SetPtr(nullptr);
208}
209
210
211template <class Type, bool DelArray>
212inline TOptDelPtr<Type, DelArray>::TOptDelPtr(std::unique_ptr<Type>&& ptr)
213: BaseClass(ptr.release()), m_releaseFn(SimpleDeleter)
214{
215}
216
217
218template <class Type, bool DelArray>
220{
221 Detach();
222}
223
224
225template <class Type, bool DelArray>
227{
228 SetPtr(nullptr);
229}
230
231
232template <class Type, bool DelArray>
234{
235 return m_releaseFn != nullptr;
236}
237
238
239template <class Type, bool DelArray>
240inline void TOptDelPtr<Type, DelArray>::SetPtr(Type* ptr, ReleaseFn releaseFn)
241{
242 Detach();
243
244 BaseClass::SetPtr(ptr);
245
246 m_releaseFn = releaseFn;
247}
248
249
250template <class Type, bool DelArray>
252{
253 if (m_releaseFn == nullptr) {
254 return BaseClass::get();
255 }
256
257 return nullptr;
258}
259
260
261template <class Type, bool DelArray>
263{
264 Type* slavePtr = BaseClass::get();
265
266 BaseClass::SetPtr(nullptr);
267 m_releaseFn = nullptr;
268
269 return slavePtr;
270}
271
272
273// public methods
274
275template <class Type, bool DelArray>
277{
278 BaseClass::Swap(ptr);
279 std::swap(m_releaseFn, ptr.m_releaseFn);
280}
281
282
283template <class Type, bool DelArray>
285{
286 if (ptr.m_releaseFn == nullptr){
287 SetPtr(ptr.m_ptr, nullptr);
288
289 return *this;
290 }
291
292 Q_ASSERT(ptr.m_ptr == nullptr);
293
294 return *this;
295}
296
297
298template <class Type, bool DelArray>
300{
301 SetPtr(ptr.m_ptr, std::move(ptr.m_releaseFn));
302
303 ptr.m_releaseFn = nullptr;
304 ptr.m_ptr = nullptr;
305
306 return *this;
307}
308
309
310template <class Type, bool DelArray>
312{
313 SetPtr(ptr.release(), SimpleDeleter);
314
315 return *this;
316}
317
318
319template <class Type, bool DelArray>
321{
322 SetPtr(ptr);
323
324 return *this;
325}
326
327
328// static methods
329
330template <class Type, bool DelArray>
332{
333 if (DelArray) {
334 delete[] ptr;
335 }
336 else {
337 delete ptr;
338 }
339}
340
341
342// protected methods
343
344template <class Type, bool DelArray>
346{
347 if (m_releaseFn != nullptr){
348 Type* ptr = BaseClass::get();
349 if (ptr != nullptr) {
350 m_releaseFn(ptr);
351 }
352 }
353}
354
355
356} // namespace istd
357
358
Pointer wrapper providing activatable deleting pointed object during destruction.
Definition TOptDelPtr.h:23
void Swap(TOptDelPtr &ptr)
Swap two pointers.
Definition TOptDelPtr.h:276
TPointerBase< Type > BaseClass
Definition TOptDelPtr.h:25
void SetPtr(Type *ptr, ReleaseFn releaseFn=nullptr)
Set new value of internal pointer.
Definition TOptDelPtr.h:240
bool IsToRelase() const
Get state of release flag.
Definition TOptDelPtr.h:233
std::function< void(Type *)> ReleaseFn
Definition TOptDelPtr.h:27
void Reset()
Remove object pointed by internal pointer and set this pointer to nullptr.
Definition TOptDelPtr.h:226
TOptDelPtr & operator=(TOptDelPtr &&ptr)
Assign move operator.
Definition TOptDelPtr.h:299
TOptDelPtr & operator=(const TOptDelPtr &ptr)
Assign operator.
Definition TOptDelPtr.h:284
static void SimpleDeleter(Type *ptr)
Function calling delete operator.
Definition TOptDelPtr.h:331
TOptDelPtr & operator=(Type *ptr)
Assign operator.
Definition TOptDelPtr.h:320
TOptDelPtr(TOptDelPtr &&ptr)
Move constructor.
Definition TOptDelPtr.h:204
TOptDelPtr(const TOptDelPtr &ptr)=delete
TOptDelPtr(std::unique_ptr< Type > &&ptr)
Construct and init this pointer using unique pointer.
Definition TOptDelPtr.h:212
TOptDelPtr(Type *ptr, bool releaseFlag)
Construct and init this pointer.
Definition TOptDelPtr.h:197
Type * release()
Reset internal pointer value without deleting instance and return previos value.
Definition TOptDelPtr.h:262
TOptDelPtr(Type *ptr=nullptr, ReleaseFn releaseFn=nullptr)
Construct and init this pointer.
Definition TOptDelPtr.h:190
void Detach()
Remove referenced object.
Definition TOptDelPtr.h:345
TOptDelPtr & operator=(std::unique_ptr< Type > &&ptr)
Assign operator.
Definition TOptDelPtr.h:311
bool SetCastedOrRemove(SourceType *ptr, ReleaseFn releaseFn=nullptr)
Set internal pointer using casted pointer of other type.
Definition TOptDelPtr.h:128
Type * GetFreePtr() const
Get internal pointer only if it is free, it means will be not released.
Definition TOptDelPtr.h:251
~TOptDelPtr()
Destructor.
Definition TOptDelPtr.h:219
bool SetCastedOrRemove(std::unique_ptr< SourcePtrType > ptr)
Set internal pointer using casted pointer of other type.
Definition TOptDelPtr.h:152
Implementation of pointer wrapper.
Standard library.
Definition IComponent.h:17

© Witold Gantzke and Kirill Lepskiy