/* * WiVRn VR streaming * Copyright (C) 2026 Guillaume Meunier * * 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 #include #include #include #include #include #if __has_include() #define BOOST_HAS_CORE_NAME #endif namespace details { template struct is_stdarray : public std::false_type {}; template struct is_stdarray> : public std::true_type {}; template inline constexpr bool is_stdarray_v = is_stdarray::value; template struct csv_logger_traits; template struct csv_logger_traits && !is_stdarray_v>> { #ifdef BOOST_HAS_CORE_NAME static void write_header(std::ostream & out, const std::string & name, bool first = true) { boost::pfr::for_each_field_with_name(T{}, [&](std::string_view member_name, const auto & value) { csv_logger_traits>::write_header(out, name + "." + (std::string)member_name, first); first = false; }); } #endif static void write_line(std::ostream & out, const T & value, bool first = true) { boost::pfr::for_each_field(value, [&](const auto & member) { csv_logger_traits>::write_line(out, member, first); first = false; }); } }; template struct csv_logger_traits>> { static void write_header(std::ostream & out, const std::string & name, bool first = true) { for (int i = 0; i < std::tuple_size_v; i++) { csv_logger_traits>::write_header(out, name + "[" + std::to_string(i) + "]", first and i == 0); } } static void write_line(std::ostream & out, const T & value, bool first = true) { for (int i = 0; i < std::tuple_size_v; i++) { csv_logger_traits>::write_line(out, value[i], first and i == 0); } } }; template struct csv_logger_traits>> { static void write_header(std::ostream & out, std::string_view name, bool first = true) { if (not first) out << ","; out << name.substr(1); } static void write_line(std::ostream & out, const T & value, bool first = true) { if (not first) out << ","; out << value; } }; } // namespace details template requires std::derived_from class csv_logger { Stream stream; public: template csv_logger(Args &&... args) : stream(std::forward(args)...) { #ifdef BOOST_HAS_CORE_NAME details::csv_logger_traits::write_header(stream, ""); stream << std::endl; #endif } void write(const T & data) { details::csv_logger_traits::write_line(stream, data); stream << std::endl; } };