From 4d7a478bfe6a0abfae0ce4472aac5f908fcb8ae6 Mon Sep 17 00:00:00 2001 From: Danil Belov Date: Sun, 10 Mar 2024 21:53:58 +0200 Subject: [PATCH 1/2] Changed units outputting & fixed UnitT concept --- include/units.hpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/include/units.hpp b/include/units.hpp index 503cd82..05ba55d 100644 --- a/include/units.hpp +++ b/include/units.hpp @@ -10,7 +10,10 @@ namespace mr template concept UnitT = requires(T unit) { - requires ArithmeticT; + // member check + typename T::ValueT; + sizeof(T) == sizeof(typename T::ValueT); + requires ArithmeticT; // constructor check T{typename T::ValueT{}}; @@ -18,8 +21,8 @@ namespace mr // operations check {unit + unit} -> std::same_as; {unit - unit} -> std::same_as; - {unit * ArithmeticT} -> std::same_as; - {unit / ArithmeticT} -> std::same_as; + {unit * typename T::ValueT{}} -> std::same_as; + {unit / typename T::ValueT{}} -> std::same_as; }; // extract value from unit type (used for units compatibility) @@ -48,9 +51,6 @@ namespace mr public: using ValueT = T; - static constexpr auto strname = "rad"; - static constexpr std::size_t size = 1; - T _data; constexpr Radians() noexcept {}; @@ -69,6 +69,11 @@ namespace mr // comparison operator [[nodiscard]] friend constexpr auto operator<=>(Radians lhs, Radians rhs) = default; + + friend std::ostream & operator<<(std::ostream &os, const Radians &r) noexcept { + os << r._data << "rad"; + return os; + } }; template @@ -76,9 +81,6 @@ namespace mr public: using ValueT = T; - static constexpr auto strname = "deg"; - static constexpr std::size_t size = 1; - T _data; explicit constexpr Degrees(T x) noexcept : _data(x) {}; @@ -97,6 +99,11 @@ namespace mr // comparison operator [[nodiscard]] friend constexpr auto operator<=>(Degrees lhs, Degrees rhs) = default; + + friend std::ostream & operator<<(std::ostream &os, const Degrees &d) noexcept { + os << d._data << "deg"; + return os; + } }; From 788299fb4d8538f44da116d27f44b7b7846c599e Mon Sep 17 00:00:00 2001 From: Danil Belov Date: Sun, 10 Mar 2024 22:04:50 +0200 Subject: [PATCH 2/2] Added std::format support --- include/def.hpp | 2 ++ include/matr.hpp | 23 +++++++++++++++++++++++ include/row.hpp | 42 +++++++++++++++++++++++++++++------------- include/units.hpp | 18 ++++++++++++++++++ 4 files changed, 72 insertions(+), 13 deletions(-) diff --git a/include/def.hpp b/include/def.hpp index f11075a..2c6fc5c 100644 --- a/include/def.hpp +++ b/include/def.hpp @@ -7,9 +7,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include diff --git a/include/matr.hpp b/include/matr.hpp index 9b5dd41..eca009f 100644 --- a/include/matr.hpp +++ b/include/matr.hpp @@ -301,4 +301,27 @@ namespace mr }; } // namespace mr +// std::format support +namespace std { + template + struct formatter> { + template + constexpr auto parse(ParseContext& ctx) { + // skip all format specifiers + return ctx.end(); + } + + template + auto format(const mr::Matr &m, FmtContext& ctx) const { + ostringstream out; + out << '(' << m[0] << ",\n"; + for (size_t i = 1; i < N - 1; i++) + out << ' ' << m[i] << ",\n"; + out << ' ' << m[N - 1] << ')'; + + return ranges::copy(move(out).str(), ctx.out()).out; + } + }; +} // namespace std + #endif // __Matr_hpp_ diff --git a/include/row.hpp b/include/row.hpp index 494aff7..5620077 100644 --- a/include/row.hpp +++ b/include/row.hpp @@ -62,16 +62,6 @@ namespace mr { operator-(const DerivedT &rhs) noexcept { return -rhs._data; } - - friend std::ostream & - operator<<(std::ostream &s, const DerivedT &v) noexcept { - s << '('; - for (size_t i = 0; i < DerivedT::size; i++) { - s << v[i] << (char)(',' * (i < DerivedT::size - 1)) << (char)(' ' * (i < DerivedT::size - 1)); - } - s << ')' << DerivedT::strname; - return s; - } }; template @@ -119,15 +109,22 @@ namespace mr { lhs._data >>= rhs._data; return lhs; } + + friend std::ostream & + operator<<(std::ostream &s, const DerivedT &v) noexcept { + s << '('; + for (size_t i = 0; i < DerivedT::size - 1; i++) { + s << v[i] << ", "; + } + s << v[DerivedT::size - 1] << ')'; + return s; + } }; template struct Row : RowOperators> { - public: using SimdT = SimdImpl; - - static constexpr auto strname = "row"; static constexpr size_t size = N; SimdT _data{}; @@ -203,4 +200,23 @@ namespace mr { }; } // namespace mr +// std::format support +namespace std { + template + struct formatter> { + template + constexpr auto parse(ParseContext& ctx) { + // skip all format specifiers + return ctx.end(); + } + + template + auto format(const mr::Row &r, FmtContext& ctx) const { + ostringstream out; + out << r; + return ranges::copy(move(out).str(), ctx.out()).out; + } + }; +} // namespace std + #endif // __Row_hpp_ diff --git a/include/units.hpp b/include/units.hpp index 05ba55d..09d81d6 100644 --- a/include/units.hpp +++ b/include/units.hpp @@ -149,4 +149,22 @@ namespace mr } } +// std::format support +namespace std { + template + struct formatter { + template + constexpr auto parse(ParseContext& ctx) { + // skip all format specifiers + return ctx.end(); + } + + template + auto format(U u, FmtContext& ctx) const { + ostringstream out; + out << u; + return ranges::copy(move(out).str(), ctx.out()).out; + } + }; +} // namespace std #endif // __units_hpp_