Add std::format formatters for basic math types
This commit is contained in:
parent
6a8a896aba
commit
57f9f7331c
5 changed files with 126 additions and 0 deletions
|
|
@ -5,6 +5,7 @@
|
||||||
#include <psemek/math/point.hpp>
|
#include <psemek/math/point.hpp>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <format>
|
||||||
|
|
||||||
namespace psemek::math
|
namespace psemek::math
|
||||||
{
|
{
|
||||||
|
|
@ -348,3 +349,34 @@ namespace psemek::math
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
|
||||||
|
template <typename T, std::size_t N, typename Char>
|
||||||
|
struct formatter<::psemek::math::box<T, N>, Char>
|
||||||
|
{
|
||||||
|
constexpr auto parse(std::format_parse_context & ctx)
|
||||||
|
{
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto format(::psemek::math::point<T, N> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <format>
|
||||||
|
|
||||||
namespace psemek::math
|
namespace psemek::math
|
||||||
{
|
{
|
||||||
|
|
@ -302,3 +303,22 @@ namespace psemek::math
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
|
||||||
|
template <typename T, typename Char>
|
||||||
|
struct formatter<::psemek::math::interval<T>, Char>
|
||||||
|
{
|
||||||
|
constexpr auto parse(std::format_parse_context & ctx)
|
||||||
|
{
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto format(::psemek::math::interval<T> const & i, std::format_context & ctx) const
|
||||||
|
{
|
||||||
|
return std::format_to(ctx.out(), "[{} .. {}]", i.min, i.max);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
#include <psemek/math/vector.hpp>
|
#include <psemek/math/vector.hpp>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <format>
|
||||||
|
|
||||||
namespace psemek::math
|
namespace psemek::math
|
||||||
{
|
{
|
||||||
|
|
@ -232,4 +233,30 @@ namespace std
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T, std::size_t N, typename Char>
|
||||||
|
struct formatter<::psemek::math::point<T, N>, Char>
|
||||||
|
{
|
||||||
|
constexpr auto parse(std::format_parse_context & ctx)
|
||||||
|
{
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto format(::psemek::math::point<T, N> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
#include <psemek/math/matrix.hpp>
|
#include <psemek/math/matrix.hpp>
|
||||||
#include <psemek/math/interval.hpp>
|
#include <psemek/math/interval.hpp>
|
||||||
|
|
||||||
|
#include <format>
|
||||||
|
|
||||||
namespace psemek::math
|
namespace psemek::math
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -338,3 +340,22 @@ namespace psemek::math
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
|
||||||
|
template <typename T, typename Char>
|
||||||
|
struct formatter<::psemek::math::quaternion<T>, Char>
|
||||||
|
{
|
||||||
|
constexpr auto parse(std::format_parse_context & ctx)
|
||||||
|
{
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto format(::psemek::math::quaternion<T> const & q, std::format_context & ctx) const
|
||||||
|
{
|
||||||
|
return std::format_to(ctx.out(), "{}", q.coords);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -522,4 +522,30 @@ namespace std
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T, std::size_t N, typename Char>
|
||||||
|
struct formatter<::psemek::math::vector<T, N>, Char>
|
||||||
|
{
|
||||||
|
constexpr auto parse(std::format_parse_context & ctx)
|
||||||
|
{
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto format(::psemek::math::vector<T, N> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue