-
Notifications
You must be signed in to change notification settings - Fork 0
/
VDeleter.h
73 lines (59 loc) · 1.5 KB
/
VDeleter.h
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
63
64
65
66
67
68
69
70
71
72
73
#ifndef VDELETER
#define VDELETER
#include <vulkan/vulkan.h>
#include <functional>
template <typename T>
class VDeleter {
public:
VDeleter() : VDeleter([](T, VkAllocationCallbacks*) {}) {}
VDeleter(std::function<void(T, VkAllocationCallbacks*)> deletef) {
this->deleter = [=](T obj) {
deletef(obj, nullptr);
};
}
VDeleter(const VDeleter<VkInstance>& instance,
std::function<void(VkInstance, T, VkAllocationCallbacks*)> deletef) {
this->deleter = [&instance, deletef](T obj) {
deletef(instance, obj, nullptr);
};
}
VDeleter(const VDeleter<VkDevice>& device,
std::function<void(VkDevice, T, VkAllocationCallbacks*)> deletef) {
this->deleter = [&device, deletef](T obj) {
deletef(device, obj, nullptr);
};
}
~VDeleter() {
cleanup();
}
const T* operator &() const {
return &object;
}
T* replace() {
cleanup();
return &object;
}
operator T() const {
return object;
}
void operator=(T rhs) {
if (rhs != object) {
cleanup();
object = rhs;
}
}
template<typename V>
bool operator==(V rhs) {
return object == T(rhs);
}
private:
T object{ VK_NULL_HANDLE };
std::function<void(T)> deleter;
void cleanup() {
if (object != VK_NULL_HANDLE) {
deleter(object);
}
object = VK_NULL_HANDLE;
}
};
#endif VDELETER