Skip to content

Commit

Permalink
Need to use std::visit to make compiler happy
Browse files Browse the repository at this point in the history
  • Loading branch information
jsallay committed Jul 6, 2024
1 parent 1a6e0e0 commit 14b85d9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
53 changes: 17 additions & 36 deletions include/pmtv/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,23 @@ struct formatter<P>

template <typename FormatContext>
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<std::monostate>(value)) return fmt::format_to(ctx.out(), "null");
else if (std::holds_alternative<bool>(value)) return fmt::format_to(ctx.out(), "{}", std::get<bool>(value));
else if (std::holds_alternative<uint8_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<uint8_t>(value));
else if (std::holds_alternative<uint16_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<uint16_t>(value));
else if (std::holds_alternative<uint32_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<uint32_t>(value));
else if (std::holds_alternative<uint64_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<uint64_t>(value));
else if (std::holds_alternative<int8_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<int8_t>(value));
else if (std::holds_alternative<int16_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<int16_t>(value));
else if (std::holds_alternative<int32_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<int32_t>(value));
else if (std::holds_alternative<int64_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<int64_t>(value));
else if (std::holds_alternative<float>(value)) return fmt::format_to(ctx.out(), "{}", std::get<float>(value));
else if (std::holds_alternative<double>(value)) return fmt::format_to(ctx.out(), "{}", std::get<double>(value));
else if (std::holds_alternative<std::complex<float>>(value)) return fmt::format_to(ctx.out(), "{}", std::get<std::complex<float>>(value));
else if (std::holds_alternative<std::complex<double>>(value)) return fmt::format_to(ctx.out(), "{}", std::get<std::complex<double>>(value));
else if (std::holds_alternative<std::vector<bool>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<bool>>(value), ", "));
else if (std::holds_alternative<std::vector<uint8_t>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<uint8_t>>(value), ", "));
else if (std::holds_alternative<std::vector<uint16_t>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<uint16_t>>(value), ", "));
else if (std::holds_alternative<std::vector<uint32_t>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<uint32_t>>(value), ", "));
else if (std::holds_alternative<std::vector<uint64_t>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<uint64_t>>(value), ", "));
else if (std::holds_alternative<std::vector<int8_t>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<int8_t>>(value), ", "));
else if (std::holds_alternative<std::vector<int16_t>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<int16_t>>(value), ", "));
else if (std::holds_alternative<std::vector<int32_t>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<int32_t>>(value), ", "));
else if (std::holds_alternative<std::vector<int64_t>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<int64_t>>(value), ", "));
else if (std::holds_alternative<std::vector<float>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<float>>(value), ", "));
else if (std::holds_alternative<std::vector<double>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<double>>(value), ", "));
else if (std::holds_alternative<std::vector<std::complex<float>>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<std::complex<float>>>(value), ", "));
else if (std::holds_alternative<std::vector<std::complex<double>>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<std::complex<double>>>(value), ", "));
else if (std::holds_alternative<std::string>(value)) return fmt::format_to(ctx.out(), "{}", std::get<std::string>(value));
else if (std::holds_alternative<std::vector<std::string>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<std::string>>(value), ", "));
else if (std::holds_alternative<std::vector<pmt>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<pmt>>(value), ", "));
else if (std::holds_alternative<map_t>(value)) return fmt::format_to(ctx.out(), "{{{}}}", fmt::join(std::get<map_t>(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<decltype(arg)>;
if constexpr (Scalar<T> || Complex<T>)
return fmt::format_to(ctx.out(), "{}", arg);
else if constexpr (std::same_as<T, std::string>)
return fmt::format_to(ctx.out(), "{}", arg);
else if constexpr (UniformVector<T> || UniformStringVector<T>)
return fmt::format_to(ctx.out(), "[{}]", fmt::join(arg, ", "));
else if constexpr (std::same_as<T, std::vector<pmt>>) {
return fmt::format_to(ctx.out(), "[{}]", fmt::join(arg, ", "));
} else if constexpr (PmtMap<T>) {
return fmt::format_to(ctx.out(), "{{{}}}", fmt::join(arg, ", "));
} else if constexpr (std::same_as<std::monostate, T>)
return fmt::format_to(ctx.out(), "null");
return fmt::format_to(ctx.out(), "unknown type {}", typeid(T).name());
}, value);

}
};
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 14b85d9

Please sign in to comment.