Skip to content

Commit

Permalink
add std::format support for unnamed types
Browse files Browse the repository at this point in the history
  • Loading branch information
SizzinSeal committed Jan 8, 2025
1 parent f802474 commit 05fac66
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
43 changes: 43 additions & 0 deletions include/units/units.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,49 @@ template <isQuantity Q, typename quotient> using Rooted = Named<
std::ratio_divide<typename Q::angle, quotient>, std::ratio_divide<typename Q::temperature, quotient>,
std::ratio_divide<typename Q::luminosity, quotient>, std::ratio_divide<typename Q::moles, quotient>>>;

template <isQuantity Q>
requires(!std::is_same_v<Named<Q>, Q>)
struct std::formatter<Q> : std::formatter<double> {
auto format(const Q& quantity, std::format_context& ctx) const {
// return std::format_to(ctx.out(), "{}", quantity.internal());
return std::formatter<Named<Q>>::format(quantity, ctx);
}
};

template <isQuantity Q>
requires std::is_same_v<Named<Q>, Q>
struct std::formatter<Q> : std::formatter<double> {
auto format(const Q& quantity, std::format_context& ctx) const {
constinit static std::array<std::pair<intmax_t, intmax_t>, 8> dims {{
{Q::mass::num, Q::mass::den},
{Q::length::num, Q::length::den},
{Q::time::num, Q::time::den},
{Q::current::num, Q::current::den},
{Q::angle::num, Q::angle::den},
{Q::temperature::num, Q::temperature::den},
{Q::luminosity::num, Q::luminosity::den},
{Q::moles::num, Q::moles::den},
}};
std::array<const char*, 8> prefixes {"_kg", "_m", "_s", "_A", "_rad", "_K", "_cd", "_mol"};

auto out = ctx.out();

// Format the quantity value
out = std::formatter<double>::format(quantity.internal(), ctx);

// Add dimensions and prefixes
for (size_t i = 0; i != 8; i++) {
if (dims[i].first != 0) {
out = std::format_to(out, "{}", prefixes[i]);
if (dims[i].first != 1 || dims[i].second != 1) { out = std::format_to(out, "^{}", dims[i].first); }
if (dims[i].second != 1) { out = std::format_to(out, "/{}", dims[i].second); }
}
}

return out;
}
};

inline void unit_printer_helper(std::ostream& os, double quantity,
const std::array<std::pair<intmax_t, intmax_t>, 8>& dims) {
static constinit std::array<const char*, 8> prefixes {"_kg", "_m", "_s", "_A", "_rad", "_K", "_cd", "_mol"};
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ void initialize() {
std::cout << std::format("{:.2f}", 180_stDeg) << std::endl; // should output 3.14_stRad
std::cout << std::format("{:.2f}", 0_celsius) << std::endl; // should output 273.15
std::cout << std::format("{:.2f}", 1.2345) << std::endl;
std::cout << units::pow<5>(505_cm) * 15_celsius << std::endl;
std::cout << std::format("{:.2f}", units::pow<5>(505_cm) * 15_celsius) << std::endl;
}

constexpr void miscTests() {
Expand Down

0 comments on commit 05fac66

Please sign in to comment.