From 31c220c7178f3a8ae7a803e46d3d568d7ff56848 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Wed, 24 Apr 2024 20:47:57 +0200 Subject: Reword custom formatter to be simpler --- deimos/core/format.h | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'deimos/core/format.h') diff --git a/deimos/core/format.h b/deimos/core/format.h index ba1a3e2..8a2c850 100644 --- a/deimos/core/format.h +++ b/deimos/core/format.h @@ -15,6 +15,12 @@ struct CustomFormatter Callback callback; }; +template +concept CustomFormattable = requires(IWriter* writer, const T& value) +{ + DeimosFormat(writer, value); +}; + struct FormatArg { enum Type : uint8_t @@ -50,29 +56,26 @@ struct FormatArg string{value} {} - template - constexpr explicit FormatArg(const T& payload, CustomFormatter::Callback callback) : + template + constexpr explicit FormatArg(const T& payload) : type{kCustom}, - custom{CustomFormatter{&payload, callback}} - {} + custom{CustomFormatter{&payload, [](IWriter* writer, const void* raw) + { + DeimosFormat(writer, *(const T*)raw); + }}} + { + } }; template -deimos::FormatArg DeimosMakeFormatArg(T&& value) - requires std::is_constructible_v -{ - return deimos::FormatArg(value); -} - -template -concept Formattable = std::same_as()))>; +concept Formattable = std::is_constructible_v; void FormatVa(IWriter*, gsl::czstring fmt, Span); template void Format(IWriter* writer, gsl::czstring fmt, Args&&... args) { - FormatVa(writer, fmt, { DeimosMakeFormatArg(std::forward(args))... }); + FormatVa(writer, fmt, { FormatArg(std::forward(args))... }); } } // namespace deimos -- cgit