diff options
author | Edward O'Callaghan <funfunctor@folklore1984.net> | 2017-02-07 22:03:40 +1100 |
---|---|---|
committer | Edward O'Callaghan <funfunctor@folklore1984.net> | 2017-02-07 22:03:40 +1100 |
commit | f5bacf193a77a10b9d69c9e2e8842d0dac34ee56 (patch) | |
tree | 7e0fb98fe438ae95c0dc5d02779e420248b1a56b /src | |
parent | 58044a07dfe9ae1fb21d2dfa4aa779693d68fda4 (diff) |
src/res_mgmr.hcpp: Add RAII helper class
Signed-off-by: Edward O'Callaghan <funfunctor@folklore1984.net>
Diffstat (limited to 'src')
-rw-r--r-- | src/res_mgmr.hcpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/res_mgmr.hcpp b/src/res_mgmr.hcpp new file mode 100644 index 0000000..3827302 --- /dev/null +++ b/src/res_mgmr.hcpp @@ -0,0 +1,57 @@ +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; + } +}; |