@@ -45,42 +45,23 @@ struct formatter<P>
45
45
46
46
template <typename FormatContext>
47
47
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);
84
65
85
66
}
86
67
};
0 commit comments