From 3320960992afe36f4b6306130c6327e084c381b2 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Thu, 4 Apr 2024 18:37:13 +0200 Subject: Add format --- deimos/core/format.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 deimos/core/format.h (limited to 'deimos/core/format.h') 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 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 +concept Formattable = requires (T value) { FormatArg(value); }; + +void FormatVa(IWriter*, gsl::czstring fmt, Span); + +template +void Format(IWriter* writer, gsl::czstring fmt, Args&&... args) +{ + FormatVa(writer, fmt, { FormatArg(std::forward(args))... }); +} + +} // namespace deimos + -- cgit