diff options
Diffstat (limited to 'deimos/core/format.h')
-rw-r--r-- | deimos/core/format.h | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/deimos/core/format.h b/deimos/core/format.h new file mode 100644 index 0000000..98d9001 --- /dev/null +++ b/deimos/core/format.h @@ -0,0 +1,58 @@ +#pragma once
+
+#include "deimos/core/base.h"
+
+namespace deimos
+{
+
+class IWriter;
+
+struct FormatArg
+{
+ enum Type : uint8_t
+ {
+ kInteger,
+ kUnsignedInteger,
+ kString,
+ };
+
+ Type type;
+
+ union
+ {
+ int64_t integer;
+ uint64_t unsigned_integer;
+
+ // @Todo Use string views
+ Span<const char> string;
+ };
+
+ explicit FormatArg(std::signed_integral auto value) :
+ type{kInteger},
+ integer{value}
+ {}
+
+ explicit FormatArg(std::unsigned_integral auto value) :
+ type{kUnsignedInteger},
+ unsigned_integer{value}
+ {}
+
+ explicit FormatArg(gsl::czstring value) :
+ type{kString},
+ string{value, (int64_t)__builtin_strlen(value)}
+ {}
+};
+
+template<typename T>
+concept Formattable = requires (T value) { FormatArg(value); };
+
+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))... });
+}
+
+} // namespace deimos
+
|