From e333d81b814ed5575d53adf34e422dea25419cd8 Mon Sep 17 00:00:00 2001 From: Orange Date: Sun, 14 Sep 2025 04:43:01 +0300 Subject: [PATCH] Enables formatting support for Angle objects Adds a partial specialization of `std::formatter` for `omath::Angle` to provide formatting support using `std::format`. This allows `Angle` objects to be easily formatted as strings, including degree symbol representation, using standard formatting techniques. --- include/omath/angle.hpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/include/omath/angle.hpp b/include/omath/angle.hpp index ee53921..0126158 100644 --- a/include/omath/angle.hpp +++ b/include/omath/angle.hpp @@ -5,8 +5,8 @@ #pragma once #include "omath/angles.hpp" #include -#include #include +#include namespace omath { @@ -150,17 +150,28 @@ namespace omath } }; } // namespace omath -template -struct std::formatter> // NOLINT(*-dcl58-cpp) +// partial specialization of std::formatter for omath::Angle +template +struct std::formatter, CharT> { + using AngleT = omath::Angle; + + // required: default ctor is implicitly OK + + // parse with the correct CharT-aware context [[nodiscard]] - static constexpr auto parse(std::format_parse_context& ctx) + static constexpr auto parse(std::basic_format_parse_context& ctx) + -> std::basic_format_parse_context::iterator { - return ctx.begin(); + return ctx.begin(); // no custom format specifiers } - [[nodiscard]] - static auto format(const omath::Angle& deg, std::format_context& ctx) + + // format; here we only implement for narrow char to keep it simple + template + auto format(const AngleT& deg, FormatContext& ctx) const { - return std::format_to(ctx.out(), "{}deg", deg.as_degrees()); + if constexpr (std::is_same_v) + return std::format_to(ctx.out(), "{}deg", deg.as_degrees()); + return std::format_to(ctx.out(), L"{}deg", deg.as_degrees()); } }; \ No newline at end of file