|
| 1 | +#pragma once |
| 2 | +#include "Partial.h" |
| 3 | + |
| 4 | +/// note: for this header you need fmt library (not included as library dependency) |
| 5 | +#include <fmt/format.h> |
| 6 | + |
| 7 | +/// note: for this header you need strong19 (not included as library dependency) |
| 8 | +#include <strong19/Strong.h> |
| 9 | + |
| 10 | +template<class... Ts, class Char> struct fmt::formatter<partial19::PartialWhich<Ts...>, Char> { |
| 11 | + constexpr auto parse(fmt::basic_format_parse_context<Char>& ctx) { return ctx.begin(); } |
| 12 | + |
| 13 | + template<size_t I, class T> static auto format_type_index(bool hit, auto& first, auto& out) -> bool { |
| 14 | + if (hit) { |
| 15 | + if constexpr (strong19::is_strong<T>) { |
| 16 | + if (first) { |
| 17 | + out = fmt::format_to(out, "{}", strong19::strong_name<T>); |
| 18 | + first = false; |
| 19 | + } |
| 20 | + else { |
| 21 | + out = fmt::format_to(out, ", {}", strong19::strong_name<T>); |
| 22 | + } |
| 23 | + } |
| 24 | + else { |
| 25 | + if (first) { |
| 26 | + out = fmt::format_to(out, "{}", I); |
| 27 | + first = false; |
| 28 | + } |
| 29 | + else { |
| 30 | + out = fmt::format_to(out, ", {}", I); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + return true; |
| 35 | + } |
| 36 | + |
| 37 | + template<typename FormatContext> auto format(partial19::PartialWhich<Ts...> const& v, FormatContext& ctx) const { |
| 38 | + if constexpr (sizeof...(Ts) == 0) { |
| 39 | + return fmt::format_to(ctx.out(), "Which()"); |
| 40 | + } |
| 41 | + else { |
| 42 | + return [&]<size_t... Is>(std::index_sequence<Is...> const&) { |
| 43 | + auto out = fmt::format_to(ctx.out(), "Which<"); |
| 44 | + bool first = true; |
| 45 | + ((format_type_index<Is, Ts>(v[Is], first, out)), ...); |
| 46 | + return fmt::format_to(out, ">"); |
| 47 | + }(std::make_index_sequence<sizeof...(Ts)>{}); |
| 48 | + } |
| 49 | + } |
| 50 | +}; |
0 commit comments