From: Samuel Just Date: Thu, 12 Oct 2023 05:13:10 +0000 (-0700) Subject: common/fmt_common: add has_fmt_print_ctx concept and formatter X-Git-Tag: v19.3.0~296^2~11 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=3a1640b12d26d18f975240bdfc5a1418603f6e4a;p=ceph.git common/fmt_common: add has_fmt_print_ctx concept and formatter fmt_print_ctx avoids creating a string. Signed-off-by: Samuel Just --- diff --git a/src/common/fmt_common.h b/src/common/fmt_common.h index 27e488aedceb..5db8bac43cd7 100644 --- a/src/common/fmt_common.h +++ b/src/common/fmt_common.h @@ -22,6 +22,8 @@ * *or* * std::string alt_fmt_print(bool short_format) const * as public member functions. + * *or* + * auto fmt_print_ctx(auto &ctx) -> decltype(ctx.out()); */ template concept has_fmt_print = requires(T t) { @@ -31,6 +33,11 @@ template concept has_alt_fmt_print = requires(T t) { { t.alt_fmt_print(bool{}) } -> std::same_as; }; +template +concept has_fmt_print_ctx = requires( + T t, fmt::buffer_context &ctx) { + { t.fmt_print_ctx(ctx) } -> std::same_as; +}; namespace fmt { @@ -64,6 +71,16 @@ struct formatter { bool verbose{true}; }; +template +struct formatter { + template + constexpr auto parse(ParseContext& ctx) { return ctx.begin(); } + template + auto format(const T& k, FormatContext& ctx) const { + return k.fmt_print_ctx(ctx); + } +}; + template struct formatter> { constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }