summaryrefslogtreecommitdiff
path: root/deimos/core/format.h
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2024-04-24 20:47:57 +0200
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2024-04-24 20:47:57 +0200
commit31c220c7178f3a8ae7a803e46d3d568d7ff56848 (patch)
treef1fc84611cb2403ff8f7d34b9590f284e1d6a1f6 /deimos/core/format.h
parent98c8fd5d39ee645922f071b6433308a813245500 (diff)
Reword custom formatter to be simpler
Diffstat (limited to 'deimos/core/format.h')
-rw-r--r--deimos/core/format.h29
1 files changed, 16 insertions, 13 deletions
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<typename T>
+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<typename T>
- constexpr explicit FormatArg(const T& payload, CustomFormatter::Callback callback) :
+ template<CustomFormattable T>
+ 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<typename T>
-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>()))>;
+concept Formattable = std::is_constructible_v<FormatArg, 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, { DeimosMakeFormatArg(std::forward<Args>(args))... });
+ FormatVa(writer, fmt, { FormatArg(std::forward<Args>(args))... });
}
} // namespace deimos