diff --git a/except.hpp b/except.hpp index 562356f..6ad3126 100644 --- a/except.hpp +++ b/except.hpp @@ -101,7 +101,38 @@ namespace cruft::vk { typename ...Args > static auto - try_array (FuncT &&func, Args &&...args) + try_values (FuncT &&func, Args &&...args) + { + // extract the number of elements available + uint32_t expected = 0; + try_func (func, args..., &expected, nullptr); + + // find the type of the last argument, ie. the values we're + // requesting + using ValueT = std::remove_pointer_t< + std::tuple_element_t< + sizeof...(Args) + 1, + typename func_traits::argument_types + > + >; + + ContainerT values (expected); + uint32_t found = expected; + try_func (func, args..., &found, values.data ()); + CHECK_EQ (expected, found); + return values; + } + + + //--------------------------------------------------------------------- + template < + typename ReturnT, + template class ContainerT = std::vector, + typename FuncT, + typename ...Args + > + static auto + try_handles (FuncT &&func, Args &&...args) { // extract the number of elements available uint32_t expected = 0; @@ -118,18 +149,15 @@ namespace cruft::vk { // return the values on the stack temporarily as we may need to // convert them to a seperate result type. - // - // TODO: remove this intermediate step where we don't actually - // have a conversion to do, ie ResultT == ValueT. ValueT values[expected]; uint32_t found = expected; try_func (func, args..., &found, &values[0]); CHECK_EQ (expected, found); // convert to the requested type - return ContainerT { - &values[0], - &values[found] + return ContainerT { + values + 0, + values + found, }; } }; @@ -161,4 +189,5 @@ namespace cruft::vk { }; } + #endif diff --git a/physical_device.cpp b/physical_device.cpp index b6ee8e3..85e9b7e 100644 --- a/physical_device.cpp +++ b/physical_device.cpp @@ -84,7 +84,7 @@ physical_device::memory_properties (void) const std::vector physical_device::queue_families (void) const { - return error::try_array ( + return error::try_values ( vkGetPhysicalDeviceQueueFamilyProperties, native () ); } @@ -94,7 +94,7 @@ physical_device::queue_families (void) const std::vector physical_device::surface_formats (surface &_surface) { - return error::try_array ( + return error::try_values ( vkGetPhysicalDeviceSurfaceFormatsKHR, native (), _surface.native () ); @@ -105,7 +105,7 @@ physical_device::surface_formats (surface &_surface) std::vector physical_device::present_modes (surface &_surface) { - return error::try_array ( + return error::try_values ( vkGetPhysicalDeviceSurfacePresentModesKHR, native (), _surface.native () ); diff --git a/tools/hello.cpp b/tools/hello.cpp index 30796be..a8840dd 100644 --- a/tools/hello.cpp +++ b/tools/hello.cpp @@ -254,7 +254,7 @@ main (void) cruft::vk::error::try_code ( vkGetSwapchainImagesKHR (ldevice.native (), swapchain, &swap_image_count, nullptr) ); - std::vector swap_images = cruft::vk::error::try_array ( + std::vector swap_images = cruft::vk::error::try_values ( vkGetSwapchainImagesKHR, ldevice.native (), swapchain );