diff --git a/include/vsg/vk/DeviceFeatures.h b/include/vsg/vk/DeviceFeatures.h index 925bd2500..0632d1fb6 100644 --- a/include/vsg/vk/DeviceFeatures.h +++ b/include/vsg/vk/DeviceFeatures.h @@ -14,6 +14,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include #include +#include namespace vsg { @@ -36,14 +37,15 @@ namespace vsg template FeatureStruct& get() { - if (auto itr = _features.find(type); itr != _features.end()) return *reinterpret_cast(itr->second); + if (auto itr = _features.find(type); itr != _features.end()) return *reinterpret_cast(itr->second.first); FeatureStruct* feature = new FeatureStruct{}; feature->sType = type; feature->pNext = nullptr; - _features[type] = reinterpret_cast(feature); + _features[type].first = reinterpret_cast(feature); + _features[type].second = [](FeatureHeader* ptr) { delete reinterpret_cast(ptr); }; return *feature; } @@ -70,7 +72,9 @@ namespace vsg void* pNext; }; - std::map _features; + using DeleteHandler = void (*)(FeatureHeader* ptr); + + std::map> _features; }; VSG_type_name(vsg::DeviceFeatures); diff --git a/src/vsg/vk/DeviceFeatures.cpp b/src/vsg/vk/DeviceFeatures.cpp index 1c34931f8..b9386b261 100644 --- a/src/vsg/vk/DeviceFeatures.cpp +++ b/src/vsg/vk/DeviceFeatures.cpp @@ -26,25 +26,15 @@ DeviceFeatures::~DeviceFeatures() VkPhysicalDeviceFeatures& DeviceFeatures::get() { - if (auto itr = _features.find(VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2); itr != _features.end()) - { - return reinterpret_cast(itr->second)->features; - } - - VkPhysicalDeviceFeatures2* feature = new VkPhysicalDeviceFeatures2{}; - feature->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; - feature->pNext = nullptr; - - _features[VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2] = reinterpret_cast(feature); + return get().features; - return feature->features; } void DeviceFeatures::clear() { for (auto& feature : _features) { - delete feature.second; + feature.second.second(feature.second.first); } _features.clear(); @@ -58,10 +48,10 @@ void* DeviceFeatures::data() const FeatureHeader* previous = nullptr; for (auto itr = _features.rbegin(); itr != _features.rend(); ++itr) { - itr->second->pNext = previous; - previous = itr->second; + itr->second.first->pNext = previous; + previous = itr->second.first; } // return head of the chain - return const_cast(reinterpret_cast(_features.begin()->second)); + return const_cast(reinterpret_cast(_features.begin()->second.first)); }