From 98c8fd5d39ee645922f071b6433308a813245500 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Wed, 24 Apr 2024 00:29:16 +0200 Subject: Add custom formatter & use it on Status --- deimos/core/format.h | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'deimos/core/format.h') diff --git a/deimos/core/format.h b/deimos/core/format.h index 3424dc2..ba1a3e2 100644 --- a/deimos/core/format.h +++ b/deimos/core/format.h @@ -7,6 +7,14 @@ namespace deimos class IWriter; +struct CustomFormatter +{ + using Callback = void (*)(IWriter*, const void* payload); + + const void* payload; + Callback callback; +}; + struct FormatArg { enum Type : uint8_t @@ -14,42 +22,57 @@ struct FormatArg kInteger, kUnsignedInteger, kString, + kCustom, }; Type type; union { - int64_t integer; - uint64_t unsigned_integer; - StringView string; + int64_t integer; + uint64_t unsigned_integer; + StringView string; + CustomFormatter custom; }; - explicit FormatArg(std::signed_integral auto value) : + constexpr explicit FormatArg(std::signed_integral auto value) : type{kInteger}, integer{value} {} - explicit FormatArg(std::unsigned_integral auto value) : + constexpr explicit FormatArg(std::unsigned_integral auto value) : type{kUnsignedInteger}, unsigned_integer{value} {} - explicit FormatArg(StringView value) : + constexpr explicit FormatArg(StringView value) : type{kString}, string{value} {} + + template + constexpr explicit FormatArg(const T& payload, CustomFormatter::Callback callback) : + type{kCustom}, + custom{CustomFormatter{&payload, callback}} + {} }; template -concept Formattable = requires (T value) { FormatArg(value); }; +deimos::FormatArg DeimosMakeFormatArg(T&& value) + requires std::is_constructible_v +{ + return deimos::FormatArg(value); +} + +template +concept Formattable = std::same_as()))>; void FormatVa(IWriter*, gsl::czstring fmt, Span); template void Format(IWriter* writer, gsl::czstring fmt, Args&&... args) { - FormatVa(writer, fmt, { FormatArg(std::forward(args))... }); + FormatVa(writer, fmt, { DeimosMakeFormatArg(std::forward(args))... }); } } // namespace deimos -- cgit