From 57f9f7331cb73a128752fce070ecbe18dcb9843b Mon Sep 17 00:00:00 2001 From: lisyarus Date: Fri, 4 Apr 2025 21:07:19 +0300 Subject: [PATCH] Add std::format formatters for basic math types --- libs/math/include/psemek/math/box.hpp | 32 ++++++++++++++++++++ libs/math/include/psemek/math/interval.hpp | 20 ++++++++++++ libs/math/include/psemek/math/point.hpp | 27 +++++++++++++++++ libs/math/include/psemek/math/quaternion.hpp | 21 +++++++++++++ libs/math/include/psemek/math/vector.hpp | 26 ++++++++++++++++ 5 files changed, 126 insertions(+) diff --git a/libs/math/include/psemek/math/box.hpp b/libs/math/include/psemek/math/box.hpp index 18038144..09aa56f9 100644 --- a/libs/math/include/psemek/math/box.hpp +++ b/libs/math/include/psemek/math/box.hpp @@ -5,6 +5,7 @@ #include #include +#include namespace psemek::math { @@ -348,3 +349,34 @@ namespace psemek::math } } + +namespace std +{ + + template + struct formatter<::psemek::math::box, Char> + { + constexpr auto parse(std::format_parse_context & ctx) + { + return ctx.begin(); + } + + auto format(::psemek::math::point const & p, std::format_context & ctx) const + { + if constexpr (N == 0) + { + return std::format_to(ctx.out(), "()"); + } + else + { + auto out = ctx.out(); + out = std::format_to(out, "({}", p[0]); + for (std::size_t i = 1; i < p.dimension(); ++i) + out = std::format_to(out, ", {}", p[i]); + out = std::format_to(out, ")"); + return out; + } + } + }; + +} diff --git a/libs/math/include/psemek/math/interval.hpp b/libs/math/include/psemek/math/interval.hpp index 1cff3ffa..d2a55d4e 100644 --- a/libs/math/include/psemek/math/interval.hpp +++ b/libs/math/include/psemek/math/interval.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace psemek::math { @@ -302,3 +303,22 @@ namespace psemek::math } } + +namespace std +{ + + template + struct formatter<::psemek::math::interval, Char> + { + constexpr auto parse(std::format_parse_context & ctx) + { + return ctx.begin(); + } + + auto format(::psemek::math::interval const & i, std::format_context & ctx) const + { + return std::format_to(ctx.out(), "[{} .. {}]", i.min, i.max); + } + }; + +} diff --git a/libs/math/include/psemek/math/point.hpp b/libs/math/include/psemek/math/point.hpp index 461d6cec..7cb5d2bf 100644 --- a/libs/math/include/psemek/math/point.hpp +++ b/libs/math/include/psemek/math/point.hpp @@ -6,6 +6,7 @@ #include #include +#include namespace psemek::math { @@ -232,4 +233,30 @@ namespace std } }; + template + struct formatter<::psemek::math::point, Char> + { + constexpr auto parse(std::format_parse_context & ctx) + { + return ctx.begin(); + } + + auto format(::psemek::math::point const & p, std::format_context & ctx) const + { + if constexpr (N == 0) + { + return std::format_to(ctx.out(), "()"); + } + else + { + auto out = ctx.out(); + out = std::format_to(out, "({}", p[0]); + for (std::size_t i = 1; i < p.dimension(); ++i) + out = std::format_to(out, ", {}", p[i]); + out = std::format_to(out, ")"); + return out; + } + } + }; + } diff --git a/libs/math/include/psemek/math/quaternion.hpp b/libs/math/include/psemek/math/quaternion.hpp index a86b7303..3b644990 100644 --- a/libs/math/include/psemek/math/quaternion.hpp +++ b/libs/math/include/psemek/math/quaternion.hpp @@ -5,6 +5,8 @@ #include #include +#include + namespace psemek::math { @@ -338,3 +340,22 @@ namespace psemek::math } } + +namespace std +{ + + template + struct formatter<::psemek::math::quaternion, Char> + { + constexpr auto parse(std::format_parse_context & ctx) + { + return ctx.begin(); + } + + auto format(::psemek::math::quaternion const & q, std::format_context & ctx) const + { + return std::format_to(ctx.out(), "{}", q.coords); + } + }; + +} diff --git a/libs/math/include/psemek/math/vector.hpp b/libs/math/include/psemek/math/vector.hpp index a3c79def..5c9d9709 100644 --- a/libs/math/include/psemek/math/vector.hpp +++ b/libs/math/include/psemek/math/vector.hpp @@ -522,4 +522,30 @@ namespace std } }; + template + struct formatter<::psemek::math::vector, Char> + { + constexpr auto parse(std::format_parse_context & ctx) + { + return ctx.begin(); + } + + auto format(::psemek::math::vector const & v, std::format_context & ctx) const + { + if constexpr (N == 0) + { + return std::format_to(ctx.out(), "()"); + } + else + { + auto out = ctx.out(); + out = std::format_to(out, "({}", v[0]); + for (std::size_t i = 1; i < v.dimension(); ++i) + out = std::format_to(out, ", {}", v[i]); + out = std::format_to(out, ")"); + return out; + } + } + }; + }