Skip to content

Commit 011cadf

Browse files
committed
Need to use std::visit to make compiler happy
Signed-off-by: John Sallay <[email protected]>
1 parent 7e5ea97 commit 011cadf

File tree

2 files changed

+18
-37
lines changed

2 files changed

+18
-37
lines changed

include/pmtv/format.hpp

+17-36
Original file line numberDiff line numberDiff line change
@@ -45,42 +45,23 @@ struct formatter<P>
4545

4646
template <typename FormatContext>
4747
auto format(const P& value, FormatContext& ctx) const {
48-
// Using visit here is fairly slow. It is probably because of the recursive nature of it.
49-
// It is really simple to enumerate the possibilities here.
50-
using namespace pmtv;
51-
if (std::holds_alternative<std::monostate>(value)) return fmt::format_to(ctx.out(), "null");
52-
else if (std::holds_alternative<bool>(value)) return fmt::format_to(ctx.out(), "{}", std::get<bool>(value));
53-
else if (std::holds_alternative<uint8_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<uint8_t>(value));
54-
else if (std::holds_alternative<uint16_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<uint16_t>(value));
55-
else if (std::holds_alternative<uint32_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<uint32_t>(value));
56-
else if (std::holds_alternative<uint64_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<uint64_t>(value));
57-
else if (std::holds_alternative<int8_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<int8_t>(value));
58-
else if (std::holds_alternative<int16_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<int16_t>(value));
59-
else if (std::holds_alternative<int32_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<int32_t>(value));
60-
else if (std::holds_alternative<int64_t>(value)) return fmt::format_to(ctx.out(), "{}", std::get<int64_t>(value));
61-
else if (std::holds_alternative<float>(value)) return fmt::format_to(ctx.out(), "{}", std::get<float>(value));
62-
else if (std::holds_alternative<double>(value)) return fmt::format_to(ctx.out(), "{}", std::get<double>(value));
63-
else if (std::holds_alternative<std::complex<float>>(value)) return fmt::format_to(ctx.out(), "{}", std::get<std::complex<float>>(value));
64-
else if (std::holds_alternative<std::complex<double>>(value)) return fmt::format_to(ctx.out(), "{}", std::get<std::complex<double>>(value));
65-
else if (std::holds_alternative<std::vector<bool>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<bool>>(value), ", "));
66-
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), ", "));
67-
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), ", "));
68-
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), ", "));
69-
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), ", "));
70-
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), ", "));
71-
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), ", "));
72-
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), ", "));
73-
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), ", "));
74-
else if (std::holds_alternative<std::vector<float>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<float>>(value), ", "));
75-
else if (std::holds_alternative<std::vector<double>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<double>>(value), ", "));
76-
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), ", "));
77-
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), ", "));
78-
else if (std::holds_alternative<std::string>(value)) return fmt::format_to(ctx.out(), "{}", std::get<std::string>(value));
79-
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), ", "));
80-
else if (std::holds_alternative<std::vector<pmt>>(value)) return fmt::format_to(ctx.out(), "[{}]", fmt::join(std::get<std::vector<pmt>>(value), ", "));
81-
else if (std::holds_alternative<map_t>(value)) return fmt::format_to(ctx.out(), "{{{}}}", fmt::join(std::get<map_t>(value), ", "));
82-
//static_assert(false);
83-
return fmt::format_to(ctx.out(), "error");
48+
return std::visit([&ctx](const auto arg) {
49+
using namespace pmtv;
50+
using T = std::decay_t<decltype(arg)>;
51+
if constexpr (Scalar<T> || Complex<T>)
52+
return fmt::format_to(ctx.out(), "{}", arg);
53+
else if constexpr (std::same_as<T, std::string>)
54+
return fmt::format_to(ctx.out(), "{}", arg);
55+
else if constexpr (UniformVector<T> || UniformStringVector<T>)
56+
return fmt::format_to(ctx.out(), "[{}]", fmt::join(arg, ", "));
57+
else if constexpr (std::same_as<T, std::vector<pmt>>) {
58+
return fmt::format_to(ctx.out(), "[{}]", fmt::join(arg, ", "));
59+
} else if constexpr (PmtMap<T>) {
60+
return fmt::format_to(ctx.out(), "{{{}}}", fmt::join(arg, ", "));
61+
} else if constexpr (std::same_as<std::monostate, T>)
62+
return fmt::format_to(ctx.out(), "null");
63+
return fmt::format_to(ctx.out(), "unknown type {}", typeid(T).name());
64+
}, value);
8465

8566
}
8667
};

meson.build

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ project('pmt', 'cpp',
22
version : '0.0.2',
33
meson_version: '>=0.63.0',
44
license : 'GPLv3',
5-
default_options : ['cpp_std=c++20', 'warning_level=3'])
5+
default_options : ['cpp_std=c++23', 'warning_level=3'])
66

77
cc = meson.get_compiler('cpp')
88
warnings_as_errors = get_option('warnings_as_errors') # Define this option in your meson_options.txt

0 commit comments

Comments
 (0)