feat(math): add std::formatter specializations

This commit is contained in:
light7734 2025-09-18 19:21:52 +03:30
parent bb6485488c
commit 3020f17507
Signed by: light7734
GPG key ID: 8C30176798F1A6BA
4 changed files with 71 additions and 14 deletions

View file

@ -9,7 +9,7 @@ template<typename T = float>
struct mat4_impl struct mat4_impl
{ {
using Column_T = vec4_impl<T>; using Column_T = vec4_impl<T>;
explicit mat4_impl(T scalar = 0) constexpr explicit mat4_impl(T scalar = 0)
: values( : values(
{ {
Column_T { scalar }, Column_T { scalar },
@ -22,7 +22,7 @@ struct mat4_impl
} }
// clang-format off // clang-format off
mat4_impl( constexpr mat4_impl(
const T& x0, const T& y0, const T& z0, const T& w0, const T& x0, const T& y0, const T& z0, const T& w0,
const T& x1, const T& y1, const T& z1, const T& w1, const T& x1, const T& y1, const T& z1, const T& w1,
const T& x2, const T& y2, const T& z2, const T& w2, const T& x2, const T& y2, const T& z2, const T& w2,
@ -33,7 +33,7 @@ struct mat4_impl
{ {
} }
mat4_impl( constexpr mat4_impl(
const Column_T &column_x, const Column_T &column_x,
const Column_T &column_y, const Column_T &column_y,
const Column_T &column_z, const Column_T &column_z,
@ -53,22 +53,22 @@ struct mat4_impl
}; };
} }
[[nodiscard]] auto operator[](size_t idx) -> Column_T & [[nodiscard]] constexpr auto operator[](size_t idx) -> Column_T &
{ {
return values[idx]; return values[idx];
} }
[[nodiscard]] auto operator[](size_t idx) const -> const Column_T & [[nodiscard]] constexpr auto operator[](size_t idx) const -> const Column_T &
{ {
return values[idx]; return values[idx];
} }
[[nodiscard]] auto operator*(const mat4_impl<T> &other) const -> mat4_impl<T> [[nodiscard]] constexpr auto operator*(const mat4_impl<T> &other) const -> mat4_impl<T>
{ {
return mat4_impl<T> {}; return mat4_impl<T> {};
} }
[[nodiscard]] auto operator*(const vec4_impl<T> &other) const -> vec4_impl<T> [[nodiscard]] constexpr auto operator*(const vec4_impl<T> &other) const -> vec4_impl<T>
{ {
return vec4_impl<T> {}; return vec4_impl<T> {};
} }

View file

@ -51,7 +51,6 @@ struct vec2_impl
}; };
} }
T x; // NOLINT T x; // NOLINT
T y; // NOLINT T y; // NOLINT
@ -65,3 +64,17 @@ using ivec2 = vec2_impl<int32_t>;
using uvec2 = vec2_impl<uint32_t>; using uvec2 = vec2_impl<uint32_t>;
} // namespace lt::math } // namespace lt::math
template<typename T>
struct std::formatter<lt::math::vec2_impl<T>>
{
constexpr auto parse(std::format_parse_context &context)
{
return context.begin();
}
auto format(const lt::math::vec2_impl<T> &val, std::format_context &context) const
{
return std::format_to(context.out(), "{}, {}", val.x, val.y);
}
};

View file

@ -47,6 +47,12 @@ struct vec3_impl
}; };
} }
friend auto operator<<(std::ostream &stream, vec3_impl<T> value) -> std::ostream &
{
stream << value.x << ", " << value.y << ", " << value.z;
return stream;
}
T x; // NOLINT T x; // NOLINT
T y; // NOLINT T y; // NOLINT
@ -61,3 +67,17 @@ using ivec3 = vec3_impl<int32_t>;
using uvec3 = vec3_impl<uint32_t>; using uvec3 = vec3_impl<uint32_t>;
} // namespace lt::math } // namespace lt::math
template<typename T>
struct std::formatter<lt::math::vec3_impl<T>>
{
constexpr auto parse(std::format_parse_context &context)
{
return context.begin();
}
auto format(const lt::math::vec3_impl<T> &val, std::format_context &context) const
{
return std::format_to(context.out(), "{}, {}, {}", val.x, val.y, val.z);
}
};

View file

@ -7,19 +7,29 @@ namespace lt::math {
template<typename T = float> template<typename T = float>
struct vec4_impl struct vec4_impl
{ {
vec4_impl(): x(), y(), z(), w() constexpr vec4_impl(): x(), y(), z(), w()
{ {
} }
explicit vec4_impl(T scalar): x(scalar), y(scalar), z(scalar), w(scalar) constexpr explicit vec4_impl(T scalar): x(scalar), y(scalar), z(scalar), w(scalar)
{ {
} }
vec4_impl(T x, T y, T z, T w): x(x), y(y), z(z), w(w) constexpr vec4_impl(T x, T y, T z, T w): x(x), y(y), z(z), w(w)
{ {
} }
[[nodiscard]] auto operator-(const vec4_impl<T> &other) const -> vec4_impl [[nodiscard]] auto operator==(const vec4_impl<T> &other) const -> bool
{
return x == other.x && y == other.y && z == other.z && w == other.w;
}
[[nodiscard]] auto operator!=(const vec4_impl<T> &other) const -> bool
{
return !(*this == other);
}
[[nodiscard]] constexpr auto operator-(const vec4_impl<T> &other) const -> vec4_impl
{ {
return { return {
x - other.x, x - other.x,
@ -29,12 +39,12 @@ struct vec4_impl
}; };
} }
[[nodiscard]] auto operator[](size_t idx) -> T & [[nodiscard]] constexpr auto operator[](size_t idx) -> T &
{ {
return values[idx]; return values[idx];
} }
[[nodiscard]] auto operator[](size_t idx) const -> const T & [[nodiscard]] constexpr auto operator[](size_t idx) const -> const T &
{ {
return values[idx]; return values[idx];
} }
@ -82,3 +92,17 @@ using ivec4 = vec4_impl<int32_t>;
using uvec4 = vec4_impl<uint32_t>; using uvec4 = vec4_impl<uint32_t>;
} // namespace lt::math } // namespace lt::math
template<typename T>
struct std::formatter<lt::math::vec4_impl<T>>
{
constexpr auto parse(std::format_parse_context &context)
{
return context.begin();
}
auto format(const lt::math::vec4_impl<T> &val, std::format_context &context) const
{
return std::format_to(context.out(), "{}, {}, {}, {}", val.x, val.y, val.z, val.w);
}
};