blob: 0789f9c24db3dd38b8411ba289bd50f4813f81e1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#pragma once
#include <type_traits>
namespace ksys::util {
/// Deletes an object that was allocated with a new expression and sets the pointer to nullptr,
/// preventing it from being accidentally reused.
/// It is safe for the pointer to be already nullptr.
template <typename T>
inline void safeDelete(T*& pointer) {
delete pointer;
pointer = nullptr;
}
/// Deletes an array that was allocated with a new[] expression and sets the pointer to nullptr,
/// preventing it from being accidentally reused.
/// It is safe for the pointer to be already nullptr.
template <typename T>
inline void safeDeleteArray(T*& pointer) {
delete[] pointer;
pointer = nullptr;
}
template <typename T>
inline void safeDeleteThread(T*& thread, bool is_jam = false) {
if (!thread)
return;
thread->quitAndWaitDoneSingleThread(is_jam);
safeDelete(thread);
}
template <typename T>
inline void safeDeleteHeap(T*& heap) {
if (heap) {
heap->destroy();
heap = nullptr;
}
}
/// @warning This does not call T's destructor.
/// This should only be used if T is trivially destructible (or if you know T's destructor
/// can be skipped).
template <typename T>
inline void deallocateObjectUnsafe(T*& pointer) {
if (pointer) {
operator delete(pointer);
pointer = nullptr;
}
}
/// @warning This does not call T's destructor.
template <typename T>
inline void deallocateObject(T*& pointer) {
static_assert(std::is_trivially_destructible_v<T>,
"T must be trivially destructible: use safeDelete "
"(or use deallocateObjectUnsafe if you know T's destructor can be skipped)");
deallocateObjectUnsafe(pointer);
}
} // namespace ksys::util
|