summaryrefslogtreecommitdiff
path: root/deimos/core/format.h
diff options
context:
space:
mode:
Diffstat (limited to 'deimos/core/format.h')
-rw-r--r--deimos/core/format.h39
1 files changed, 31 insertions, 8 deletions
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<typename T>
+ constexpr explicit FormatArg(const T& payload, CustomFormatter::Callback callback) :
+ type{kCustom},
+ custom{CustomFormatter{&payload, callback}}
+ {}
};
template<typename T>
-concept Formattable = requires (T value) { FormatArg(value); };
+deimos::FormatArg DeimosMakeFormatArg(T&& value)
+ requires std::is_constructible_v<deimos::FormatArg, T>
+{
+ return deimos::FormatArg(value);
+}
+
+template<typename T>
+concept Formattable = std::same_as<deimos::FormatArg, decltype(DeimosMakeFormatArg(std::declval<T>()))>;
void FormatVa(IWriter*, gsl::czstring fmt, Span<const FormatArg>);
template<Formattable... Args>
void Format(IWriter* writer, gsl::czstring fmt, Args&&... args)
{
- FormatVa(writer, fmt, { FormatArg(std::forward<Args>(args))... });
+ FormatVa(writer, fmt, { DeimosMakeFormatArg(std::forward<Args>(args))... });
}
} // namespace deimos