/* Copyright (c) 2018-2024, Arm Limited and Contributors * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 the "License"; * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "common/error.h" #include "common/glm_common.h" #include namespace vkb { template inline void read(std::istringstream &is, T &value) { is.read(reinterpret_cast(&value), sizeof(T)); } inline void read(std::istringstream &is, std::string &value) { std::size_t size; read(is, size); value.resize(size); is.read(const_cast(value.data()), size); } template inline void read(std::istringstream &is, std::set &value) { std::size_t size; read(is, size); for (uint32_t i = 0; i < size; i++) { T item; is.read(reinterpret_cast(&item), sizeof(T)); value.insert(std::move(item)); } } template inline void read(std::istringstream &is, std::vector &value) { std::size_t size; read(is, size); value.resize(size); is.read(reinterpret_cast(value.data()), value.size() * sizeof(T)); } template inline void read(std::istringstream &is, std::map &value) { std::size_t size; read(is, size); for (uint32_t i = 0; i < size; i++) { std::pair item; read(is, item.first); read(is, item.second); value.insert(std::move(item)); } } template inline void read(std::istringstream &is, std::array &value) { is.read(reinterpret_cast(value.data()), N * sizeof(T)); } template inline void read(std::istringstream &is, T &first_arg, Args &...args) { read(is, first_arg); read(is, args...); } template inline void write(std::ostringstream &os, const T &value) { os.write(reinterpret_cast(&value), sizeof(T)); } inline void write(std::ostringstream &os, const std::string &value) { write(os, value.size()); os.write(value.data(), value.size()); } template inline void write(std::ostringstream &os, const std::set &value) { write(os, value.size()); for (const T &item : value) { os.write(reinterpret_cast(&item), sizeof(T)); } } template inline void write(std::ostringstream &os, const std::vector &value) { write(os, value.size()); os.write(reinterpret_cast(value.data()), value.size() * sizeof(T)); } template inline void write(std::ostringstream &os, const std::map &value) { write(os, value.size()); for (const std::pair &item : value) { write(os, item.first); write(os, item.second); } } template inline void write(std::ostringstream &os, const std::array &value) { os.write(reinterpret_cast(value.data()), N * sizeof(T)); } template inline void write(std::ostringstream &os, const T &first_arg, const Args &...args) { write(os, first_arg); write(os, args...); } /** * @brief Helper function to combine a given hash * with a generated hash for the input param. */ template inline void hash_combine(size_t &seed, const T &v) { std::hash hasher; glm::detail::hash_combine(seed, hasher(v)); } /** * @brief Helper function to convert a data type * to string using output stream operator. * @param value The object to be converted to string * @return String version of the given object */ template inline std::string to_string(const T &value) { std::stringstream ss; ss << std::fixed << value; return ss.str(); } /** * @brief Helper function to check size_t is correctly converted to uint32_t * @param value Value of type size_t to convert * @return An uint32_t representation of the same value */ template uint32_t to_u32(T value) { static_assert(std::is_arithmetic::value, "T must be numeric"); if (static_cast(value) > static_cast(std::numeric_limits::max())) { throw std::runtime_error("to_u32() failed, value is too big to be converted to uint32_t"); } return static_cast(value); } template inline std::vector to_bytes(const T &value) { return std::vector{reinterpret_cast(&value), reinterpret_cast(&value) + sizeof(T)}; } } // namespace vkb