Skip to content

Add a custom allocator(like polymorphic_allocator) when some RAII methods return std containers. #2523

Description

@xmmmmmovo

Thank you for open-sourcing this modern C++ Vulkan wrapper library. Our project is currently trying to use vulkan-hpp, but I noticed that some methods which return std containers use std::allocator, as shown below.

// wrapper function for command vkEnumerateInstanceExtensionProperties, see
// https://registry.khronos.org/vulkan/specs/latest/man/html/vkEnumerateInstanceExtensionProperties.html
VULKAN_HPP_NODISCARD typename ResultValueType<std::vector<ExtensionProperties>>::type
  enumerateInstanceExtensionProperties( Optional<std::string const> layerName VULKAN_HPP_DEFAULT_ASSIGNMENT( nullptr ) ) const;

// wrapper function for command vkEnumerateInstanceLayerProperties, see
// https://registry.khronos.org/vulkan/specs/latest/man/html/vkEnumerateInstanceLayerProperties.html
VULKAN_HPP_NODISCARD typename ResultValueType<std::vector<LayerProperties>>::type enumerateInstanceLayerProperties() const;

This means that if I need to store this data, I have to copy it into our pmr containers to keep the types consistent. It would be ideal if a macro could be added to enable or disable a custom allocator. My personal idea is as follows:

template<T>
using VKHppStdContainerAllocator =
#if defined(VULKAN_HPP_USE_PMR_ALLOCATOR)
	std::polymorphic_allocator<T>;
#else
	std::allocator<T>;
#endif

// wrapper function for command vkEnumerateInstanceExtensionProperties, see
// https://registry.khronos.org/vulkan/specs/latest/man/html/vkEnumerateInstanceExtensionProperties.html
// add allocator
VULKAN_HPP_NODISCARD typename ResultValueType<std::vector<ExtensionProperties, VKHppStdContainerAllocator<ExtensionProperties>>>::type
  enumerateInstanceExtensionProperties( Optional<std::string const> layerName VULKAN_HPP_DEFAULT_ASSIGNMENT( nullptr ) ) const;

// wrapper function for command vkEnumerateInstanceLayerProperties, see
// https://registry.khronos.org/vulkan/specs/latest/man/html/vkEnumerateInstanceLayerProperties.html
VULKAN_HPP_NODISCARD typename ResultValueType<std::vector<LayerProperties, VKHppStdContainerAllocator<LayerProperties>>>::type enumerateInstanceLayerProperties() const;

PS. If this issue is acceptable and this repository welcomes PRs, I would be happy to try contributing this part of the code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions