-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTuple.fmt.h
More file actions
35 lines (29 loc) · 1.33 KB
/
Tuple.fmt.h
File metadata and controls
35 lines (29 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma once
#include "Tuple.h"
/// note: for this header you need fmt library (not included as library dependency)
#include <fmt/format.h>
namespace tuple19 {
/// use values of the tuple as format arguments
/// note: you still have to craft the proper format string!
/// usage: vformat(formatStr, tuple19::vargs(argsTuple));
template<class... Ts> constexpr auto vargs(Tuple<Ts...> const& args) {
return [&]<size_t... Is>(std::index_sequence<Is...> const&) {
return fmt::vargs<Ts...>{{args.template at<Is>()...}};
}(std::make_index_sequence<sizeof...(Ts)>{});
}
} // namespace tuple19
template<class... Ts, class Char> struct fmt::formatter<tuple19::Tuple<Ts...>, Char> {
constexpr auto parse(fmt::basic_format_parse_context<Char>& ctx) { return ctx.begin(); }
template<typename FormatContext> auto format(tuple19::Tuple<Ts...> const& v, FormatContext& ctx) const {
if constexpr (sizeof...(Ts) == 0) {
return fmt::format_to(ctx.out(), "Tuple<>");
}
else {
return [&]<size_t... Is>(std::index_sequence<0, Is...> const&) {
auto out = fmt::format_to(ctx.out(), "{}", v.template at<0>());
((out = fmt::format_to(out, ", {}", v.template at<Is>())), ...);
return out;
}(std::make_index_sequence<sizeof...(Ts)>{});
}
}
};