diff --git a/include/omath/angle.hpp b/include/omath/angle.hpp index e7d0879..a13b27f 100644 --- a/include/omath/angle.hpp +++ b/include/omath/angle.hpp @@ -6,6 +6,7 @@ #include "omath/angles.hpp" #include #include +#include namespace omath { @@ -149,3 +150,17 @@ namespace omath } }; } // namespace omath +template +struct std::formatter> // NOLINT(*-dcl58-cpp) +{ + [[nodiscard]] + static constexpr auto parse(std::format_parse_context& ctx) + { + return ctx.begin(); + } + [[nodiscard]] + static auto format(const omath::Angle& deg, std::format_context& ctx) + { + return std::format_to(ctx.out(), "{}deg", deg.as_degrees()); + } +}; \ No newline at end of file diff --git a/include/omath/color.hpp b/include/omath/color.hpp index f620807..ba28d08 100644 --- a/include/omath/color.hpp +++ b/include/omath/color.hpp @@ -167,3 +167,21 @@ namespace omath #endif }; } // namespace omath +template<> +struct std::formatter // NOLINT(*-dcl58-cpp) +{ + [[nodiscard]] + static constexpr auto parse(std::format_parse_context& ctx) + { + return ctx.begin(); + } + [[nodiscard]] + static auto format(const omath::Color& col, std::format_context& ctx) + { + return std::format_to(ctx.out(), "[r:{}, g:{}, b:{}, a:{}]", + static_cast(col.x * 255.f), + static_cast(col.y * 255.f), + static_cast(col.z * 255.f), + static_cast(col.w * 255.f)); + } +}; \ No newline at end of file diff --git a/include/omath/mat.hpp b/include/omath/mat.hpp index 4f52734..0ad81f1 100644 --- a/include/omath/mat.hpp +++ b/include/omath/mat.hpp @@ -480,3 +480,19 @@ namespace omath {0.f, 0.f, -1.f, 0.f}}; } } // namespace omath + +template +struct std::formatter> // NOLINT(*-dcl58-cpp) +{ + using MatType = omath::Mat; + [[nodiscard]] + static constexpr auto parse(std::format_parse_context& ctx) + { + return ctx.begin(); + } + [[nodiscard]] + static auto format(const MatType& mat, std::format_context& ctx) + { + return std::format_to(ctx.out(), "{}", mat.to_string()); + } +}; \ No newline at end of file diff --git a/include/omath/vector2.hpp b/include/omath/vector2.hpp index 4123068..d86153c 100644 --- a/include/omath/vector2.hpp +++ b/include/omath/vector2.hpp @@ -3,7 +3,9 @@ // #pragma once +#include "vector3.hpp" #include +#include #include #ifdef OMATH_IMGUI_INTEGRATION @@ -218,7 +220,6 @@ namespace omath { return std::make_tuple(x, y); } - #ifdef OMATH_IMGUI_INTEGRATION [[nodiscard]] ImVec2 to_im_vec2() const noexcept @@ -233,3 +234,18 @@ namespace omath #endif }; } // namespace omath + +template +struct std::formatter> // NOLINT(*-dcl58-cpp) +{ + [[nodiscard]] + static constexpr auto parse(std::format_parse_context& ctx) + { + return ctx.begin(); + } + [[nodiscard]] + static auto format(const omath::Vector2& vec, std::format_context& ctx) + { + return std::format_to(ctx.out(), "[{}, {}]", vec.x, vec.y); + } +}; diff --git a/include/omath/vector3.hpp b/include/omath/vector3.hpp index 3749032..3c37b87 100644 --- a/include/omath/vector3.hpp +++ b/include/omath/vector3.hpp @@ -159,9 +159,9 @@ namespace omath return Vector2::length(); } - [[nodiscard]] Type distance_to(const Vector3& vOther) const noexcept + [[nodiscard]] Type distance_to(const Vector3& v_other) const noexcept { - return (*this - vOther).length(); + return (*this - v_other).length(); } #endif @@ -279,21 +279,33 @@ namespace omath } }; } // namespace omath -// ReSharper disable once CppRedundantNamespaceDefinition -namespace std + +template<> struct std::hash> { - template<> struct hash> + std::size_t operator()(const omath::Vector3& vec) const noexcept { - std::size_t operator()(const omath::Vector3& vec) const noexcept - { - std::size_t hash = 0; - constexpr std::hash hasher; + std::size_t hash = 0; + constexpr std::hash hasher; - hash ^= hasher(vec.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2); - hash ^= hasher(vec.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2); - hash ^= hasher(vec.z) + 0x9e3779b9 + (hash << 6) + (hash >> 2); + hash ^= hasher(vec.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2); + hash ^= hasher(vec.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2); + hash ^= hasher(vec.z) + 0x9e3779b9 + (hash << 6) + (hash >> 2); - return hash; - } - }; -} // namespace std + return hash; + } +}; + +template +struct std::formatter> // NOLINT(*-dcl58-cpp) +{ + [[nodiscard]] + static constexpr auto parse(std::format_parse_context& ctx) + { + return ctx.begin(); + } + [[nodiscard]] + static auto format(const omath::Vector3& vec, std::format_context& ctx) + { + return std::format_to(ctx.out(), "[{}, {}, {}]", vec.x, vec.y, vec.z); + } +}; diff --git a/include/omath/vector4.hpp b/include/omath/vector4.hpp index 815ffcc..2b5d8a0 100644 --- a/include/omath/vector4.hpp +++ b/include/omath/vector4.hpp @@ -201,3 +201,18 @@ namespace omath #endif }; } // namespace omath + +template +struct std::formatter> // NOLINT(*-dcl58-cpp) +{ + [[nodiscard]] + static constexpr auto parse(std::format_parse_context& ctx) + { + return ctx.begin(); + } + [[nodiscard]] + static auto format(const omath::Vector4& vec, std::format_context& ctx) + { + return std::format_to(ctx.out(), "[{}, {}, {}, {}]", vec.x, vec.y, vec.z, vec.w); + } +}; \ No newline at end of file diff --git a/tests/general/unit_test_mat.cpp b/tests/general/unit_test_mat.cpp index 9bae411..3c67882 100644 --- a/tests/general/unit_test_mat.cpp +++ b/tests/general/unit_test_mat.cpp @@ -125,7 +125,7 @@ TEST_F(UnitTestMat, Clear) TEST_F(UnitTestMat, ToString) { - const std::string str = m2.to_string(); + const std::string str = std::format("{}", m2); EXPECT_FALSE(str.empty()); EXPECT_EQ(str, "[[ 1.000, 2.000]\n [ 3.000, 4.000]]"); }