From 14b85d9cb6c4da5f0abc4544b348ffd51b698969 Mon Sep 17 00:00:00 2001 From: John Sallay Date: Fri, 5 Jul 2024 21:27:25 -0400 Subject: [PATCH] Need to use std::visit to make compiler happy --- include/pmtv/format.hpp | 53 +++++++++++++---------------------------- meson.build | 2 +- 2 files changed, 18 insertions(+), 37 deletions(-) diff --git a/include/pmtv/format.hpp b/include/pmtv/format.hpp index dd770d8..dc5dc3d 100644 --- a/include/pmtv/format.hpp +++ b/include/pmtv/format.hpp @@ -45,42 +45,23 @@ struct formatter

template auto format(const P& value, FormatContext& ctx) const { - // Using visit here is fairly slow. It is probably because of the recursive nature of it. - // It is really simple to enumerate the possibilities here. - using namespace pmtv; - if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "null"); - else if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "{}", std::get(value)); - else if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "{}", std::get(value)); - else if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "{}", std::get(value)); - else if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "{}", std::get(value)); - else if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "{}", std::get(value)); - else if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "{}", std::get(value)); - else if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "{}", std::get(value)); - else if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "{}", std::get(value)); - else if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "{}", std::get(value)); - else if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "{}", std::get(value)); - else if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "{}", std::get(value)); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "{}", std::get>(value)); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "{}", std::get>(value)); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>(value), ", ")); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>(value), ", ")); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>(value), ", ")); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>(value), ", ")); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>(value), ", ")); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>(value), ", ")); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>(value), ", ")); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>(value), ", ")); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>(value), ", ")); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>(value), ", ")); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>(value), ", ")); - else if (std::holds_alternative>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>>(value), ", ")); - else if (std::holds_alternative>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>>(value), ", ")); - else if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "{}", std::get(value)); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>(value), ", ")); - else if (std::holds_alternative>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get>(value), ", ")); - else if (std::holds_alternative(value)) return fmt::format_to(ctx.out(), "{{{}}}", fmt::join(std::get(value), ", ")); - //static_assert(false); - return fmt::format_to(ctx.out(), "error"); + return std::visit([&ctx](const auto arg) { + using namespace pmtv; + using T = std::decay_t; + if constexpr (Scalar || Complex) + return fmt::format_to(ctx.out(), "{}", arg); + else if constexpr (std::same_as) + return fmt::format_to(ctx.out(), "{}", arg); + else if constexpr (UniformVector || UniformStringVector) + return fmt::format_to(ctx.out(), "[{}]", fmt::join(arg, ", ")); + else if constexpr (std::same_as>) { + return fmt::format_to(ctx.out(), "[{}]", fmt::join(arg, ", ")); + } else if constexpr (PmtMap) { + return fmt::format_to(ctx.out(), "{{{}}}", fmt::join(arg, ", ")); + } else if constexpr (std::same_as) + return fmt::format_to(ctx.out(), "null"); + return fmt::format_to(ctx.out(), "unknown type {}", typeid(T).name()); + }, value); } }; diff --git a/meson.build b/meson.build index 8b1d886..08bd4f2 100644 --- a/meson.build +++ b/meson.build @@ -2,7 +2,7 @@ project('pmt', 'cpp', version : '0.0.2', meson_version: '>=0.63.0', license : 'GPLv3', - default_options : ['cpp_std=c++20', 'warning_level=3']) + default_options : ['cpp_std=c++23', 'warning_level=3']) cc = meson.get_compiler('cpp') warnings_as_errors = get_option('warnings_as_errors') # Define this option in your meson_options.txt