Skip to content

Commit

Permalink
Merge pull request #550 from mpusz/typed_prefixes
Browse files Browse the repository at this point in the history
feat: improve types readability by eliminating extraneous `()` for a value of a type for prefixes
  • Loading branch information
mpusz authored Feb 13, 2024
2 parents f1353a7 + 8b5d2ea commit 26a7b35
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 76 deletions.
32 changes: 16 additions & 16 deletions src/systems/include/mp-units/systems/iec80000/binary_prefixes.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@
namespace mp_units::iec80000 {

// clang-format off
template<PrefixableUnit auto U> struct kibi_ : prefixed_unit<"Ki", mag_power<2, 10>, U> {};
template<PrefixableUnit auto U> struct mebi_ : prefixed_unit<"Mi", mag_power<2, 20>, U> {};
template<PrefixableUnit auto U> struct gibi_ : prefixed_unit<"Gi", mag_power<2, 30>, U> {};
template<PrefixableUnit auto U> struct tebi_ : prefixed_unit<"Ti", mag_power<2, 40>, U> {};
template<PrefixableUnit auto U> struct pebi_ : prefixed_unit<"Pi", mag_power<2, 50>, U> {};
template<PrefixableUnit auto U> struct exbi_ : prefixed_unit<"Ei", mag_power<2, 60>, U> {};
template<PrefixableUnit auto U> struct zebi_ : prefixed_unit<"Zi", mag_power<2, 70>, U> {};
template<PrefixableUnit auto U> struct yobi_ : prefixed_unit<"Yi", mag_power<2, 80>, U> {};
template<PrefixableUnit U> struct kibi_ : prefixed_unit<"Ki", mag_power<2, 10>, U{}> {};
template<PrefixableUnit U> struct mebi_ : prefixed_unit<"Mi", mag_power<2, 20>, U{}> {};
template<PrefixableUnit U> struct gibi_ : prefixed_unit<"Gi", mag_power<2, 30>, U{}> {};
template<PrefixableUnit U> struct tebi_ : prefixed_unit<"Ti", mag_power<2, 40>, U{}> {};
template<PrefixableUnit U> struct pebi_ : prefixed_unit<"Pi", mag_power<2, 50>, U{}> {};
template<PrefixableUnit U> struct exbi_ : prefixed_unit<"Ei", mag_power<2, 60>, U{}> {};
template<PrefixableUnit U> struct zebi_ : prefixed_unit<"Zi", mag_power<2, 70>, U{}> {};
template<PrefixableUnit U> struct yobi_ : prefixed_unit<"Yi", mag_power<2, 80>, U{}> {};

template<PrefixableUnit auto U> inline constexpr kibi_<U> kibi;
template<PrefixableUnit auto U> inline constexpr mebi_<U> mebi;
template<PrefixableUnit auto U> inline constexpr gibi_<U> gibi;
template<PrefixableUnit auto U> inline constexpr tebi_<U> tebi;
template<PrefixableUnit auto U> inline constexpr pebi_<U> pebi;
template<PrefixableUnit auto U> inline constexpr exbi_<U> exbi;
template<PrefixableUnit auto U> inline constexpr zebi_<U> zebi;
template<PrefixableUnit auto U> inline constexpr yobi_<U> yobi;
template<PrefixableUnit auto U> inline constexpr kibi_<std::remove_const_t<decltype(U)>> kibi;
template<PrefixableUnit auto U> inline constexpr mebi_<std::remove_const_t<decltype(U)>> mebi;
template<PrefixableUnit auto U> inline constexpr gibi_<std::remove_const_t<decltype(U)>> gibi;
template<PrefixableUnit auto U> inline constexpr tebi_<std::remove_const_t<decltype(U)>> tebi;
template<PrefixableUnit auto U> inline constexpr pebi_<std::remove_const_t<decltype(U)>> pebi;
template<PrefixableUnit auto U> inline constexpr exbi_<std::remove_const_t<decltype(U)>> exbi;
template<PrefixableUnit auto U> inline constexpr zebi_<std::remove_const_t<decltype(U)>> zebi;
template<PrefixableUnit auto U> inline constexpr yobi_<std::remove_const_t<decltype(U)>> yobi;
// clang-format on

} // namespace mp_units::iec80000
96 changes: 48 additions & 48 deletions src/systems/include/mp-units/systems/si/prefixes.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,55 +27,55 @@
namespace mp_units::si {

// clang-format off
template<PrefixableUnit auto U> struct quecto_ : prefixed_unit<"q", mag_power<10, -30>, U> {};
template<PrefixableUnit auto U> struct ronto_ : prefixed_unit<"r", mag_power<10, -27>, U> {};
template<PrefixableUnit auto U> struct yocto_ : prefixed_unit<"y", mag_power<10, -24>, U> {};
template<PrefixableUnit auto U> struct zepto_ : prefixed_unit<"z", mag_power<10, -21>, U> {};
template<PrefixableUnit auto U> struct atto_ : prefixed_unit<"a", mag_power<10, -18>, U> {};
template<PrefixableUnit auto U> struct femto_ : prefixed_unit<"f", mag_power<10, -15>, U> {};
template<PrefixableUnit auto U> struct pico_ : prefixed_unit<"p", mag_power<10, -12>, U> {};
template<PrefixableUnit auto U> struct nano_ : prefixed_unit<"n", mag_power<10, -9>, U> {};
template<PrefixableUnit auto U> struct micro_ : prefixed_unit<basic_symbol_text{"µ", "u"}, mag_power<10, -6>, U> {};
template<PrefixableUnit auto U> struct milli_ : prefixed_unit<"m", mag_power<10, -3>, U> {};
template<PrefixableUnit auto U> struct centi_ : prefixed_unit<"c", mag_power<10, -2>, U> {};
template<PrefixableUnit auto U> struct deci_ : prefixed_unit<"d", mag_power<10, -1>, U> {};
template<PrefixableUnit auto U> struct deca_ : prefixed_unit<"da", mag_power<10, 1>, U> {};
template<PrefixableUnit auto U> struct hecto_ : prefixed_unit<"h", mag_power<10, 2>, U> {};
template<PrefixableUnit auto U> struct kilo_ : prefixed_unit<"k", mag_power<10, 3>, U> {};
template<PrefixableUnit auto U> struct mega_ : prefixed_unit<"M", mag_power<10, 6>, U> {};
template<PrefixableUnit auto U> struct giga_ : prefixed_unit<"G", mag_power<10, 9>, U> {};
template<PrefixableUnit auto U> struct tera_ : prefixed_unit<"T", mag_power<10, 12>, U> {};
template<PrefixableUnit auto U> struct peta_ : prefixed_unit<"P", mag_power<10, 15>, U> {};
template<PrefixableUnit auto U> struct exa_ : prefixed_unit<"E", mag_power<10, 18>, U> {};
template<PrefixableUnit auto U> struct zetta_ : prefixed_unit<"Z", mag_power<10, 21>, U> {};
template<PrefixableUnit auto U> struct yotta_ : prefixed_unit<"Y", mag_power<10, 24>, U> {};
template<PrefixableUnit auto U> struct ronna_ : prefixed_unit<"R", mag_power<10, 27>, U> {};
template<PrefixableUnit auto U> struct quetta_ : prefixed_unit<"Q", mag_power<10, 30>, U> {};
template<PrefixableUnit U> struct quecto_ : prefixed_unit<"q", mag_power<10, -30>, U{}> {};
template<PrefixableUnit U> struct ronto_ : prefixed_unit<"r", mag_power<10, -27>, U{}> {};
template<PrefixableUnit U> struct yocto_ : prefixed_unit<"y", mag_power<10, -24>, U{}> {};
template<PrefixableUnit U> struct zepto_ : prefixed_unit<"z", mag_power<10, -21>, U{}> {};
template<PrefixableUnit U> struct atto_ : prefixed_unit<"a", mag_power<10, -18>, U{}> {};
template<PrefixableUnit U> struct femto_ : prefixed_unit<"f", mag_power<10, -15>, U{}> {};
template<PrefixableUnit U> struct pico_ : prefixed_unit<"p", mag_power<10, -12>, U{}> {};
template<PrefixableUnit U> struct nano_ : prefixed_unit<"n", mag_power<10, -9>, U{}> {};
template<PrefixableUnit U> struct micro_ : prefixed_unit<basic_symbol_text{"µ", "u"}, mag_power<10, -6>, U{}> {};
template<PrefixableUnit U> struct milli_ : prefixed_unit<"m", mag_power<10, -3>, U{}> {};
template<PrefixableUnit U> struct centi_ : prefixed_unit<"c", mag_power<10, -2>, U{}> {};
template<PrefixableUnit U> struct deci_ : prefixed_unit<"d", mag_power<10, -1>, U{}> {};
template<PrefixableUnit U> struct deca_ : prefixed_unit<"da", mag_power<10, 1>, U{}> {};
template<PrefixableUnit U> struct hecto_ : prefixed_unit<"h", mag_power<10, 2>, U{}> {};
template<PrefixableUnit U> struct kilo_ : prefixed_unit<"k", mag_power<10, 3>, U{}> {};
template<PrefixableUnit U> struct mega_ : prefixed_unit<"M", mag_power<10, 6>, U{}> {};
template<PrefixableUnit U> struct giga_ : prefixed_unit<"G", mag_power<10, 9>, U{}> {};
template<PrefixableUnit U> struct tera_ : prefixed_unit<"T", mag_power<10, 12>, U{}> {};
template<PrefixableUnit U> struct peta_ : prefixed_unit<"P", mag_power<10, 15>, U{}> {};
template<PrefixableUnit U> struct exa_ : prefixed_unit<"E", mag_power<10, 18>, U{}> {};
template<PrefixableUnit U> struct zetta_ : prefixed_unit<"Z", mag_power<10, 21>, U{}> {};
template<PrefixableUnit U> struct yotta_ : prefixed_unit<"Y", mag_power<10, 24>, U{}> {};
template<PrefixableUnit U> struct ronna_ : prefixed_unit<"R", mag_power<10, 27>, U{}> {};
template<PrefixableUnit U> struct quetta_ : prefixed_unit<"Q", mag_power<10, 30>, U{}> {};

template<PrefixableUnit auto U> inline constexpr quecto_<U> quecto;
template<PrefixableUnit auto U> inline constexpr ronto_<U> ronto;
template<PrefixableUnit auto U> inline constexpr yocto_<U> yocto;
template<PrefixableUnit auto U> inline constexpr zepto_<U> zepto;
template<PrefixableUnit auto U> inline constexpr atto_<U> atto;
template<PrefixableUnit auto U> inline constexpr femto_<U> femto;
template<PrefixableUnit auto U> inline constexpr pico_<U> pico;
template<PrefixableUnit auto U> inline constexpr nano_<U> nano;
template<PrefixableUnit auto U> inline constexpr micro_<U> micro;
template<PrefixableUnit auto U> inline constexpr milli_<U> milli;
template<PrefixableUnit auto U> inline constexpr centi_<U> centi;
template<PrefixableUnit auto U> inline constexpr deci_<U> deci;
template<PrefixableUnit auto U> inline constexpr deca_<U> deca;
template<PrefixableUnit auto U> inline constexpr hecto_<U> hecto;
template<PrefixableUnit auto U> inline constexpr kilo_<U> kilo;
template<PrefixableUnit auto U> inline constexpr mega_<U> mega;
template<PrefixableUnit auto U> inline constexpr giga_<U> giga;
template<PrefixableUnit auto U> inline constexpr tera_<U> tera;
template<PrefixableUnit auto U> inline constexpr peta_<U> peta;
template<PrefixableUnit auto U> inline constexpr exa_<U> exa;
template<PrefixableUnit auto U> inline constexpr zetta_<U> zetta;
template<PrefixableUnit auto U> inline constexpr yotta_<U> yotta;
template<PrefixableUnit auto U> inline constexpr ronna_<U> ronna;
template<PrefixableUnit auto U> inline constexpr quetta_<U> quetta;
template<PrefixableUnit auto U> inline constexpr quecto_<std::remove_const_t<decltype(U)>> quecto;
template<PrefixableUnit auto U> inline constexpr ronto_<std::remove_const_t<decltype(U)>> ronto;
template<PrefixableUnit auto U> inline constexpr yocto_<std::remove_const_t<decltype(U)>> yocto;
template<PrefixableUnit auto U> inline constexpr zepto_<std::remove_const_t<decltype(U)>> zepto;
template<PrefixableUnit auto U> inline constexpr atto_<std::remove_const_t<decltype(U)>> atto;
template<PrefixableUnit auto U> inline constexpr femto_<std::remove_const_t<decltype(U)>> femto;
template<PrefixableUnit auto U> inline constexpr pico_<std::remove_const_t<decltype(U)>> pico;
template<PrefixableUnit auto U> inline constexpr nano_<std::remove_const_t<decltype(U)>> nano;
template<PrefixableUnit auto U> inline constexpr micro_<std::remove_const_t<decltype(U)>> micro;
template<PrefixableUnit auto U> inline constexpr milli_<std::remove_const_t<decltype(U)>> milli;
template<PrefixableUnit auto U> inline constexpr centi_<std::remove_const_t<decltype(U)>> centi;
template<PrefixableUnit auto U> inline constexpr deci_<std::remove_const_t<decltype(U)>> deci;
template<PrefixableUnit auto U> inline constexpr deca_<std::remove_const_t<decltype(U)>> deca;
template<PrefixableUnit auto U> inline constexpr hecto_<std::remove_const_t<decltype(U)>> hecto;
template<PrefixableUnit auto U> inline constexpr kilo_<std::remove_const_t<decltype(U)>> kilo;
template<PrefixableUnit auto U> inline constexpr mega_<std::remove_const_t<decltype(U)>> mega;
template<PrefixableUnit auto U> inline constexpr giga_<std::remove_const_t<decltype(U)>> giga;
template<PrefixableUnit auto U> inline constexpr tera_<std::remove_const_t<decltype(U)>> tera;
template<PrefixableUnit auto U> inline constexpr peta_<std::remove_const_t<decltype(U)>> peta;
template<PrefixableUnit auto U> inline constexpr exa_<std::remove_const_t<decltype(U)>> exa;
template<PrefixableUnit auto U> inline constexpr zetta_<std::remove_const_t<decltype(U)>> zetta;
template<PrefixableUnit auto U> inline constexpr yotta_<std::remove_const_t<decltype(U)>> yotta;
template<PrefixableUnit auto U> inline constexpr ronna_<std::remove_const_t<decltype(U)>> ronna;
template<PrefixableUnit auto U> inline constexpr quetta_<std::remove_const_t<decltype(U)>> quetta;
// clang-format on

} // namespace mp_units::si
12 changes: 7 additions & 5 deletions test/static/quantity_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,8 @@ static_assert(is_of_type<1 * percent * (1 * m), quantity<derived_unit<struct per

static_assert(is_of_type<1 * m / (1 * s), quantity<derived_unit<struct si::metre, per<struct si::second>>{}, int>>);
static_assert(is_of_type<1 * m / (1 * m), quantity<one, int>>);
static_assert(is_of_type<1 * km / (1 * m), quantity<derived_unit<si::kilo_<si::metre>, per<struct si::metre>>{}, int>>);
static_assert(
is_of_type<1 * km / (1 * m), quantity<derived_unit<si::kilo_<struct si::metre>, per<struct si::metre>>{}, int>>);

static_assert(is_of_type<1 * m / 1, quantity<si::metre, int>>);
static_assert(is_of_type<1 * m / (1 * one), quantity<si::metre, int>>);
Expand Down Expand Up @@ -568,7 +569,7 @@ static_assert(is_of_type<1 * percent * (1. * m), quantity<derived_unit<struct pe
static_assert(is_of_type<1 * m / (1. * s), quantity<derived_unit<struct si::metre, per<struct si::second>>{}, double>>);
static_assert(is_of_type<1. * m / (1 * m), quantity<one, double>>);
static_assert(
is_of_type<1. * km / (1 * m), quantity<derived_unit<si::kilo_<si::metre>, per<struct si::metre>>{}, double>>);
is_of_type<1. * km / (1 * m), quantity<derived_unit<si::kilo_<struct si::metre>, per<struct si::metre>>{}, double>>);

static_assert(is_of_type<1. * m / 1, quantity<si::metre, double>>);
static_assert(is_of_type<1 * m / (1. * one), quantity<si::metre, double>>);
Expand Down Expand Up @@ -612,11 +613,12 @@ static_assert(is_of_type<1 * m * (1 * min), quantity<derived_unit<struct si::met
static_assert(is_of_type<1 * s * (1 * Hz), quantity<derived_unit<struct si::hertz, struct si::second>{}, int>>);
static_assert(is_of_type<1 / (1 * min), quantity<derived_unit<struct one, per<struct si::minute>>{}, int>>);
static_assert(is_of_type<1 / (1 * Hz), quantity<derived_unit<struct one, per<struct si::hertz>>{}, int>>);
static_assert(is_of_type<1 / (1 * km), quantity<derived_unit<struct one, per<si::kilo_<si::metre>>>{}, int>>);
static_assert(is_of_type<1 / (1 * km), quantity<derived_unit<struct one, per<si::kilo_<struct si::metre>>>{}, int>>);
static_assert(is_of_type<1 / min, quantity<derived_unit<struct one, per<struct si::minute>>{}, int>>);
static_assert(is_of_type<1 / Hz, quantity<derived_unit<struct one, per<struct si::hertz>>{}, int>>);
static_assert(is_of_type<1 / km, quantity<derived_unit<struct one, per<si::kilo_<si::metre>>>{}, int>>);
static_assert(is_of_type<1 * km / (1 * m), quantity<derived_unit<si::kilo_<si::metre>, per<struct si::metre>>{}, int>>);
static_assert(is_of_type<1 / km, quantity<derived_unit<struct one, per<si::kilo_<struct si::metre>>>{}, int>>);
static_assert(
is_of_type<1 * km / (1 * m), quantity<derived_unit<si::kilo_<struct si::metre>, per<struct si::metre>>{}, int>>);
static_assert(is_of_type<1 * m / (1 * s), quantity<derived_unit<struct si::metre, per<struct si::second>>{}, int>>);
static_assert(is_of_type<1 * m / (1 * min), quantity<derived_unit<struct si::metre, per<struct si::minute>>{}, int>>);
static_assert(is_of_type<1 * min / (1 * m), quantity<derived_unit<struct si::minute, per<struct si::metre>>{}, int>>);
Expand Down
5 changes: 3 additions & 2 deletions test/static/si_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ static_assert(1 * Rm == 1'000'000'000'000'000'000 * Gm);
static_assert(1 * Qm == 1'000'000'000'000'000'000 * Tm);

// check for invalid prefixes
template<template<auto U> typename prefix, auto V1>
concept can_not_be_prefixed = Unit<std::remove_const_t<decltype(V1)>> && !requires { typename prefix<V1>; };
template<template<typename U> typename prefix, auto V1>
concept can_not_be_prefixed =
Unit<std::remove_const_t<decltype(V1)>> && !requires { typename prefix<std::remove_const_t<decltype(V1)>>; };

static_assert(can_not_be_prefixed<si::milli_, si::degree_Celsius>);
static_assert(can_not_be_prefixed<si::milli_, si::minute>);
Expand Down
10 changes: 5 additions & 5 deletions test/static/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ static_assert(convertible(kilojoule, joule));
static_assert(kilojoule != joule);
static_assert(kilojoule.symbol == "kJ");

static_assert(is_of_type<si::kilo<metre>, si::kilo_<metre>>);
static_assert(is_of_type<si::kilo<joule>, si::kilo_<joule>>);
static_assert(is_of_type<si::kilo<metre>, si::kilo_<metre_>>);
static_assert(is_of_type<si::kilo<joule>, si::kilo_<joule_>>);

// TODO Should the below be a scaled version of metre^2?
static_assert(is_of_type<kilometre * metre, derived_unit<kilometre_, metre_>>); // !!!
Expand Down Expand Up @@ -264,7 +264,7 @@ static_assert(is_of_type<get_canonical_unit(km_2).reference_unit, metre_>);
static_assert(get_canonical_unit(km_2).mag == mag<2000>);

constexpr auto kJ_42 = mag<42> * si::kilo<joule>;
static_assert(is_of_type<kJ_42, scaled_unit<mag<42>, si::kilo_<joule>>>);
static_assert(is_of_type<kJ_42, scaled_unit<mag<42>, si::kilo_<joule_>>>);
static_assert(
is_of_type<get_canonical_unit(kJ_42).reference_unit, derived_unit<gram_, power<metre_, 2>, per<power<second_, 2>>>>);
static_assert(get_canonical_unit(kJ_42).mag == mag<42'000'000>);
Expand Down Expand Up @@ -467,7 +467,7 @@ static_assert(!convertible(metre, metre* metre));

// one
static_assert(is_of_type<metre / metre, one_>);
static_assert(is_of_type<si::kilo<metre> / metre, derived_unit<si::kilo_<metre>, per<metre_>>>);
static_assert(is_of_type<si::kilo<metre> / metre, derived_unit<si::kilo_<metre_>, per<metre_>>>);
static_assert(metre / metre == one);
static_assert(hertz * second == one);
static_assert(one * one == one);
Expand Down Expand Up @@ -509,7 +509,7 @@ static_assert(is_of_type<pow<1, 2>(metre / (second * second)), derived_unit<powe
static_assert(is_of_type<kilometre * kilometre, derived_unit<power<kilometre_, 2>>>);

static_assert(is_of_type<pow<2>(kilometre), derived_unit<power<kilometre_, 2>>>);
static_assert(is_of_type<pow<2>(si::kilo<metre>), derived_unit<power<si::kilo_<metre>, 2>>>);
static_assert(is_of_type<pow<2>(si::kilo<metre>), derived_unit<power<si::kilo_<metre_>, 2>>>);
static_assert(is_of_type<pow<2>(hour), derived_unit<power<hour_, 2>>>);
static_assert(
is_of_type<pow<2>(mag<3600>* second), scaled_unit<mag<3600> * mag<3600>, derived_unit<power<second_, 2>>>>);
Expand Down

0 comments on commit 26a7b35

Please sign in to comment.