utils: string format for rect, aabb and quat

This commit is contained in:
2019-02-15 07:59:11 +07:00
parent 8f56b58092
commit 8484d21468
2 changed files with 148 additions and 0 deletions

View File

@@ -138,6 +138,139 @@ namespace e2d { namespace strings
}
};
//
// quat
//
template < typename T >
class format_arg<quat<T>, std::enable_if_t<std::is_integral<T>::value>> {
quat<T> value_;
u8 width_;
public:
template < typename U >
explicit format_arg(U&& value, u8 width = 0) noexcept
: value_(std::forward<U>(value)), width_(width) {}
std::ptrdiff_t write(char* dst, size_t size) const {
return math::numeric_cast<std::ptrdiff_t>(
format(dst, size, "(%0,%1,%2,%3)",
make_format_arg(value_.x, width_),
make_format_arg(value_.y, width_),
make_format_arg(value_.z, width_),
make_format_arg(value_.w, width_)));
}
};
template < typename T >
class format_arg<quat<T>, std::enable_if_t<std::is_floating_point<T>::value>> {
quat<T> value_;
u8 width_;
u8 precision_;
public:
template < typename U >
explicit format_arg(U&& value, u8 width = 0, u8 precision = 6) noexcept
: value_(std::forward<U>(value)), width_(width), precision_(precision) {}
std::ptrdiff_t write(char* dst, size_t size) const {
return math::numeric_cast<std::ptrdiff_t>(
format(dst, size, "(%0,%1,%2,%3)",
make_format_arg(value_.x, width_, precision_),
make_format_arg(value_.y, width_, precision_),
make_format_arg(value_.z, width_, precision_),
make_format_arg(value_.w, width_, precision_)));
}
};
//
// rect
//
template < typename T >
class format_arg<rect<T>, std::enable_if_t<std::is_integral<T>::value>> {
rect<T> value_;
u8 width_;
public:
template < typename U >
explicit format_arg(U&& value, u8 width = 0) noexcept
: value_(std::forward<U>(value)), width_(width) {}
std::ptrdiff_t write(char* dst, size_t size) const {
return math::numeric_cast<std::ptrdiff_t>(
format(dst, size, "(%0,%1,%2,%3)",
make_format_arg(value_.position.x, width_),
make_format_arg(value_.position.y, width_),
make_format_arg(value_.size.x, width_),
make_format_arg(value_.size.y, width_)));
}
};
template < typename T >
class format_arg<rect<T>, std::enable_if_t<std::is_floating_point<T>::value>> {
rect<T> value_;
u8 width_;
u8 precision_;
public:
template < typename U >
explicit format_arg(U&& value, u8 width = 0, u8 precision = 6) noexcept
: value_(std::forward<U>(value)), width_(width), precision_(precision) {}
std::ptrdiff_t write(char* dst, size_t size) const {
return math::numeric_cast<std::ptrdiff_t>(
format(dst, size, "(%0,%1,%2,%3)",
make_format_arg(value_.position.x, width_, precision_),
make_format_arg(value_.position.y, width_, precision_),
make_format_arg(value_.size.x, width_, precision_),
make_format_arg(value_.size.y, width_, precision_)));
}
};
//
// aabb
//
template < typename T >
class format_arg<aabb<T>, std::enable_if_t<std::is_integral<T>::value>> {
aabb<T> value_;
u8 width_;
public:
template < typename U >
explicit format_arg(U&& value, u8 width = 0) noexcept
: value_(std::forward<U>(value)), width_(width) {}
std::ptrdiff_t write(char* dst, size_t size) const {
return math::numeric_cast<std::ptrdiff_t>(
format(dst, size, "(%0,%1,%2,%3,%4,%5)",
make_format_arg(value_.position.x, width_),
make_format_arg(value_.position.y, width_),
make_format_arg(value_.position.z, width_),
make_format_arg(value_.size.x, width_),
make_format_arg(value_.size.y, width_),
make_format_arg(value_.size.z, width_)));
}
};
template < typename T >
class format_arg<aabb<T>, std::enable_if_t<std::is_floating_point<T>::value>> {
aabb<T> value_;
u8 width_;
u8 precision_;
public:
template < typename U >
explicit format_arg(U&& value, u8 width = 0, u8 precision = 6) noexcept
: value_(std::forward<U>(value)), width_(width), precision_(precision) {}
std::ptrdiff_t write(char* dst, size_t size) const {
return math::numeric_cast<std::ptrdiff_t>(
format(dst, size, "(%0,%1,%2,%3,%4,%5)",
make_format_arg(value_.position.x, width_, precision_),
make_format_arg(value_.position.y, width_, precision_),
make_format_arg(value_.position.z, width_, precision_),
make_format_arg(value_.size.x, width_, precision_),
make_format_arg(value_.size.y, width_, precision_),
make_format_arg(value_.size.z, width_, precision_)));
}
};
//
// mat2
//

View File

@@ -155,4 +155,19 @@ TEST_CASE("strfmts") {
strings::make_format_arg(make_microseconds(4.f), u8(5), u8(2))) ==
" 4.00us");
}
{
REQUIRE(strings::rformat("%0", make_rect(1,2,3,4)) == "(1,2,3,4)");
REQUIRE(strings::rformat("%0", make_quat(1,2,3,4)) == "(1,2,3,4)");
REQUIRE(strings::rformat("%0", make_aabb(1,2,3,4,5,6)) == "(1,2,3,4,5,6)");
REQUIRE(strings::rformat(
"%0",
strings::make_format_arg(make_rect(1.f,2.f,3.f,4.f), u8(5), u8(2))) == "( 1.00, 2.00, 3.00, 4.00)");
REQUIRE(strings::rformat(
"%0",
strings::make_format_arg(make_quat(1.f,2.f,3.f,4.f), u8(5), u8(2))) == "( 1.00, 2.00, 3.00, 4.00)");
REQUIRE(strings::rformat(
"%0",
strings::make_format_arg(make_aabb(1.f,2.f,3.f,4.f,5.f,6.f), u8(5), u8(2))) == "( 1.00, 2.00, 3.00, 4.00, 5.00, 6.00)");
}
}