Compare commits

..

3 Commits

Author SHA1 Message Date
a097c0a2f4 Merge pull request #57 from orange-cpp/feature/added_formatters
Adds std::format support for math types
2025-08-26 12:37:13 +03:00
20ae2b4dd1 Adds formatters for Angle and Color
Adds formatters for `omath::Angle` and `omath::Color` to allow for easy formatting using `std::format`.

This allows users to easily output Angle and Color values in a human-readable format.
2025-08-26 12:35:40 +03:00
115b5a3471 Adds std::format support for math types
Adds `std::formatter` specializations for `Mat`, `Vector2`, `Vector3`, and `Vector4` types, enabling the use of `std::format` for these types.

This simplifies formatting and printing of these mathematical objects, improving code readability and consistency.

Also adds a hash function specialization for Vector3.
2025-08-26 12:22:33 +03:00
7 changed files with 110 additions and 18 deletions

View File

@@ -6,6 +6,7 @@
#include "omath/angles.hpp" #include "omath/angles.hpp"
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
#include <format>
namespace omath namespace omath
{ {
@@ -149,3 +150,17 @@ namespace omath
} }
}; };
} // namespace omath } // namespace omath
template<class Type, Type min, Type max, omath::AngleFlags flags>
struct std::formatter<omath::Angle<Type, min, max, flags>> // NOLINT(*-dcl58-cpp)
{
[[nodiscard]]
static constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}
[[nodiscard]]
static auto format(const omath::Angle<Type, min, max, flags>& deg, std::format_context& ctx)
{
return std::format_to(ctx.out(), "{}deg", deg.as_degrees());
}
};

View File

@@ -167,3 +167,21 @@ namespace omath
#endif #endif
}; };
} // namespace omath } // namespace omath
template<>
struct std::formatter<omath::Color> // 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<int>(col.x * 255.f),
static_cast<int>(col.y * 255.f),
static_cast<int>(col.z * 255.f),
static_cast<int>(col.w * 255.f));
}
};

View File

@@ -480,3 +480,19 @@ namespace omath
{0.f, 0.f, -1.f, 0.f}}; {0.f, 0.f, -1.f, 0.f}};
} }
} // namespace omath } // namespace omath
template<size_t Rows, size_t Columns, class Type, omath::MatStoreType StoreType>
struct std::formatter<omath::Mat<Rows, Columns, Type, StoreType>> // NOLINT(*-dcl58-cpp)
{
using MatType = omath::Mat<Rows, Columns, Type, StoreType>;
[[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());
}
};

View File

@@ -3,7 +3,9 @@
// //
#pragma once #pragma once
#include "vector3.hpp"
#include <cmath> #include <cmath>
#include <format>
#include <tuple> #include <tuple>
#ifdef OMATH_IMGUI_INTEGRATION #ifdef OMATH_IMGUI_INTEGRATION
@@ -218,7 +220,6 @@ namespace omath
{ {
return std::make_tuple(x, y); return std::make_tuple(x, y);
} }
#ifdef OMATH_IMGUI_INTEGRATION #ifdef OMATH_IMGUI_INTEGRATION
[[nodiscard]] [[nodiscard]]
ImVec2 to_im_vec2() const noexcept ImVec2 to_im_vec2() const noexcept
@@ -233,3 +234,18 @@ namespace omath
#endif #endif
}; };
} // namespace omath } // namespace omath
template<class Type>
struct std::formatter<omath::Vector2<Type>> // NOLINT(*-dcl58-cpp)
{
[[nodiscard]]
static constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}
[[nodiscard]]
static auto format(const omath::Vector2<Type>& vec, std::format_context& ctx)
{
return std::format_to(ctx.out(), "[{}, {}]", vec.x, vec.y);
}
};

View File

@@ -159,9 +159,9 @@ namespace omath
return Vector2<Type>::length(); return Vector2<Type>::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 #endif
@@ -279,21 +279,33 @@ namespace omath
} }
}; };
} // namespace omath } // namespace omath
// ReSharper disable once CppRedundantNamespaceDefinition
namespace std template<> struct std::hash<omath::Vector3<float>>
{ {
template<> struct hash<omath::Vector3<float>> std::size_t operator()(const omath::Vector3<float>& vec) const noexcept
{ {
std::size_t operator()(const omath::Vector3<float>& vec) const noexcept std::size_t hash = 0;
{ constexpr std::hash<float> hasher;
std::size_t hash = 0;
constexpr std::hash<float> hasher;
hash ^= hasher(vec.x) + 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.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
hash ^= hasher(vec.z) + 0x9e3779b9 + (hash << 6) + (hash >> 2); hash ^= hasher(vec.z) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
return hash; return hash;
} }
}; };
} // namespace std
template<class Type>
struct std::formatter<omath::Vector3<Type>> // NOLINT(*-dcl58-cpp)
{
[[nodiscard]]
static constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}
[[nodiscard]]
static auto format(const omath::Vector3<Type>& vec, std::format_context& ctx)
{
return std::format_to(ctx.out(), "[{}, {}, {}]", vec.x, vec.y, vec.z);
}
};

View File

@@ -201,3 +201,18 @@ namespace omath
#endif #endif
}; };
} // namespace omath } // namespace omath
template<class Type>
struct std::formatter<omath::Vector4<Type>> // NOLINT(*-dcl58-cpp)
{
[[nodiscard]]
static constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}
[[nodiscard]]
static auto format(const omath::Vector4<Type>& vec, std::format_context& ctx)
{
return std::format_to(ctx.out(), "[{}, {}, {}, {}]", vec.x, vec.y, vec.z, vec.w);
}
};

View File

@@ -125,7 +125,7 @@ TEST_F(UnitTestMat, Clear)
TEST_F(UnitTestMat, ToString) TEST_F(UnitTestMat, ToString)
{ {
const std::string str = m2.to_string(); const std::string str = std::format("{}", m2);
EXPECT_FALSE(str.empty()); EXPECT_FALSE(str.empty());
EXPECT_EQ(str, "[[ 1.000, 2.000]\n [ 3.000, 4.000]]"); EXPECT_EQ(str, "[[ 1.000, 2.000]\n [ 3.000, 4.000]]");
} }