Skip to content

Commit 826cd46

Browse files
committed
Extend fmt support for partial19 and variant19
1 parent 3be8555 commit 826cd46

File tree

7 files changed

+148
-0
lines changed

7 files changed

+148
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
template<class... Ts, class Char> struct fmt::formatter<partial19::Partial<Ts...>, Char> {
8+
constexpr auto parse(fmt::basic_format_parse_context<Char>& ctx) { return ctx.begin(); }
9+
10+
template<class FormatContext> auto format(partial19::Partial<Ts...> const& t, FormatContext& ctx) const {
11+
bool first = true;
12+
auto out = ctx.out();
13+
t.visitInitialized([&](auto& v) {
14+
if (first) {
15+
out = fmt::format_to(out, "<[{}", v);
16+
first = false;
17+
}
18+
else {
19+
out = fmt::format_to(out, "; {}", v);
20+
}
21+
});
22+
return fmt::format_to(out, "]>");
23+
}
24+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
};

src/partial19.lib/partial19/partial19.qbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ Product {
1111
files: [
1212
"Bitset.h",
1313
"Partial.equals.h",
14+
"Partial.fmt.h",
1415
"Partial.h",
1516
"Partial.ostream.h",
17+
"PartialWhich.fmt.h",
1618
"align.h",
1719
]
1820
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
#include "None.h"
3+
4+
/// note: for this header you need fmt library (not included as library dependency)
5+
#include <fmt/format.h>
6+
7+
template<class... Ts, class Char> struct fmt::formatter<variant19::None, Char> {
8+
constexpr auto parse(fmt::basic_format_parse_context<Char>& ctx) { return ctx.begin(); }
9+
10+
template<typename FormatContext> auto format(variant19::None const&, FormatContext& ctx) const {
11+
return fmt::format_to(ctx.out(), "");
12+
}
13+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
#include "Variant.h"
3+
4+
/// note: for this header you need fmt library (not included as library dependency)
5+
#include <fmt/format.h>
6+
7+
template<class... Ts, class Char> struct fmt::formatter<variant19::Variant<Ts...>, Char> {
8+
constexpr auto parse(fmt::basic_format_parse_context<Char>& ctx) { return ctx.begin(); }
9+
10+
template<class FormatContext> auto format(variant19::Variant<Ts...> const& v, FormatContext& ctx) const {
11+
if constexpr (sizeof...(Ts) == 0) {
12+
return fmt::format_to(ctx.out(), "Variant()");
13+
}
14+
else {
15+
return v.visit([&](auto const& elem) { return fmt::format_to(ctx.out(), "Variant({})", elem); });
16+
}
17+
}
18+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
#include "Variant.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<variant19::VariantWhich<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(size_t i, auto& out) -> bool {
14+
if (i == I) {
15+
if constexpr (strong19::is_strong<T>) {
16+
out = fmt::format_to(out, "Which<{}>({})", strong19::strong_name<T>, i);
17+
}
18+
else {
19+
out = fmt::format_to(out, "Which({})", i);
20+
}
21+
}
22+
return (i == I);
23+
}
24+
25+
template<typename FormatContext> auto format(variant19::VariantWhich<Ts...> const& v, FormatContext& ctx) const {
26+
if constexpr (sizeof...(Ts) == 0) {
27+
return fmt::format_to(ctx.out(), "Which()");
28+
}
29+
else {
30+
return [&]<size_t... Is>(std::index_sequence<Is...> const&) {
31+
auto out = ctx.out();
32+
((format_type_index<Is, Ts>(v, out)) || ... ||
33+
(out = fmt::format_to(out, "Which({})", static_cast<size_t>(v)), true));
34+
return out;
35+
}(std::make_index_sequence<sizeof...(Ts)>{});
36+
}
37+
}
38+
};

src/variant19.lib/variant19/variant19.qbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ Product {
99
}
1010

1111
files: [
12+
"None.fmt.h",
1213
"None.h",
1314
"None.ostream.h",
1415
"Variant.equals.h",
16+
"Variant.fmt.h",
1517
"Variant.h",
1618
"Variant.ostream.h",
19+
"VariantWhich.fmt.h",
1720
]
1821
}

0 commit comments

Comments
 (0)