Use expected size for xrLocateViews

This commit is contained in:
Patrick Nicolas
2024-03-30 17:00:34 +01:00
committed by Guillaume Meunier
parent 9cceaac648
commit 146dd841ab
2 changed files with 41 additions and 1 deletions
+40
View File
@@ -115,4 +115,44 @@ auto enumerate(F f, Args &&... args) -> auto
return array;
}
template <typename T, typename F, typename... Args>
auto enumerate2(F f, uint32_t count, Args &&... args) -> auto
{
assert(count);
using array_type = std::conditional_t<std::is_same_v<T, char>, std::string, std::vector<T>>;
T default_value{};
uint32_t size_offset = 0;
if constexpr (std::is_same_v<T, char>)
{
size_offset = 1;
}
else if constexpr (structure_type<T> != XR_TYPE_UNKNOWN)
{
default_value.type = structure_type<T>;
}
array_type array(count - size_offset, default_value);
XrResult result = f(std::forward<Args>(args)..., count, &count, reinterpret_cast<typename structure_traits<T>::base *>(array.data()));
if (XR_SUCCEEDED(result))
{
assert(count >= size_offset);
array.resize(count - size_offset, default_value);
return array;
}
if (result == XR_ERROR_SIZE_INSUFFICIENT)
{
assert(count >= size_offset);
array.resize(count - size_offset, default_value);
result = f(std::forward<Args>(args)..., count, &count, reinterpret_cast<typename structure_traits<T>::base *>(array.data()));
}
if (!XR_SUCCEEDED(result))
{
throw std::system_error(result, xr::error_category(), "enumerating " + type_name<T>());
}
return array;
}
} // namespace xr::details
+1 -1
View File
@@ -174,7 +174,7 @@ std::pair<XrViewStateFlags, std::vector<XrView>> xr::session::locate_views(XrVie
.type = XR_TYPE_VIEW_STATE,
};
auto views = details::enumerate<XrView>(xrLocateViews, id, &view_locate_info, &view_state);
auto views = details::enumerate2<XrView>(xrLocateViews, 2, id, &view_locate_info, &view_state);
return {view_state.viewStateFlags, views};
}