/* * WiVRn VR streaming * Copyright (C) 2022 Guillaume Meunier * Copyright (C) 2022 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once #ifdef __ANDROID__ #include #endif #include "utils/typename.h" #include "xr/check.h" #include #include #include #include namespace xr::details { template struct structure_traits_base { static constexpr inline XrStructureType type = XR_TYPE_UNKNOWN; using base = T; }; template struct structure_traits : structure_traits_base {}; template <> struct structure_traits : structure_traits_base { static constexpr inline XrStructureType type = XR_TYPE_VIEW_CONFIGURATION_VIEW; }; template <> struct structure_traits : structure_traits_base { static constexpr inline XrStructureType type = XR_TYPE_VIEW; }; template <> struct structure_traits : structure_traits_base { static constexpr inline XrStructureType type = XR_TYPE_API_LAYER_PROPERTIES; }; template <> struct structure_traits : structure_traits_base { static constexpr inline XrStructureType type = XR_TYPE_EXTENSION_PROPERTIES; }; template <> struct structure_traits : structure_traits_base { static constexpr inline XrStructureType type = XR_TYPE_SWAPCHAIN_IMAGE_VULKAN_KHR; using base = XrSwapchainImageBaseHeader; }; template constexpr inline XrStructureType structure_type = structure_traits::type; template void enumerate(F f, std::conditional_t, std::string, std::vector> & data, Args &&... args) { uint32_t count; uint32_t capacity = data.size(); // For strings, add the null byte as capacity if (std::is_same_v and capacity) capacity++; XrResult result = f(std::forward(args)..., capacity, &count, reinterpret_cast::base *>(data.data())); if (result == XR_ERROR_SIZE_INSUFFICIENT or (data.empty() and XR_SUCCEEDED(result))) { if constexpr (structure_type != XR_TYPE_UNKNOWN) data.resize(count, T{.type = structure_type}); else if constexpr (std::is_same_v) data.resize(count - 1); // count includes the null terminator else data.resize(count); return enumerate(f, data, std::forward(args)...); } if (not XR_SUCCEEDED(result)) throw std::system_error(result, xr::error_category(), "enumerating " + type_name()); data.resize(count - std::is_same_v); } template auto enumerate(F f, Args &&... args) -> auto { using array_type = std::conditional_t, std::string, std::vector>; array_type array; uint32_t count; XrResult result; result = f(std::forward(args)..., 0, &count, nullptr); if (XR_SUCCEEDED(result)) { if constexpr (std::is_same_v) { array.resize(count - 1); // count includes the null terminator } else if constexpr (structure_type == XR_TYPE_UNKNOWN) { array.resize(count); } else { T default_value{}; default_value.type = structure_type; array.resize(count, default_value); } result = f(std::forward(args)..., count, &count, reinterpret_cast::base *>(array.data())); } if (!XR_SUCCEEDED(result)) { throw std::system_error(result, xr::error_category(), "enumerating " + type_name()); } return array; } } // namespace xr::details