Migrate prefixes to Magnitude

This commit is contained in:
Chip Hogg committed 2022-07-07 17:09:42 +00:00
1 parent 92750072b0
commit e1a173a02d
5 files changed
+41 -47

No files matched your search

+3 -3
View File
@@ -40,11 +40,11 @@ namespace units {
// Prefix
namespace detail {
template<ratio R>
template<Magnitude auto M>
struct prefix_base;
template<ratio R>
void to_prefix_base(const volatile prefix_base<R>*);
template<Magnitude auto M>
void to_prefix_base(const volatile prefix_base<M>*);
} // namespace detail
+7 -13
View File
@@ -69,28 +69,22 @@ constexpr auto magnitude_text()
}
}
template<ratio R>
constexpr auto ratio_text()
template<Unit U, Magnitude auto M, std::size_t SymbolLen>
constexpr auto prefix_or_magnitude_text()
{
return magnitude_text<as_magnitude<R>()>();
}
template<Unit U, ratio R, std::size_t SymbolLen>
constexpr auto prefix_or_ratio_text()
{
if constexpr (R.num == 1 && R.den == 1 && R.exp == 0) {
if constexpr (M == as_magnitude<1>()) {
// no ratio/prefix
return basic_fixed_string("");
} else {
// try to form a prefix
using prefix = downcast<detail::prefix_base<R>>;
using prefix = downcast<detail::prefix_base<M>>;
if constexpr (can_be_prefixed<U> && !is_same_v<prefix, prefix_base<R>>) {
if constexpr (can_be_prefixed<U> && !is_same_v<prefix, prefix_base<M>>) {
// print as a prefixed unit
return prefix::symbol;
} else {
// print as a ratio of the coherent unit
constexpr auto txt = ratio_text<R>();
constexpr auto txt = magnitude_text<M>();
if constexpr (SymbolLen > 0 && txt.standard().size() > 0)
return txt + basic_fixed_string(" ");
else
@@ -164,7 +158,7 @@ constexpr auto unit_text()
}();
constexpr auto prefix_txt =
prefix_or_ratio_text<U, as_ratio(U::mag / coherent_unit::mag), symbol_text.standard().size()>();
prefix_or_magnitude_text<U, U::mag / coherent_unit::mag, symbol_text.standard().size()>();
return prefix_txt + symbol_text;
}
}
+5 -5
View File
@@ -34,9 +34,9 @@ namespace units {
namespace detail {
template<ratio R>
struct prefix_base : downcast_base<prefix_base<R>> {
static constexpr UNITS_MSVC_WORKAROUND(Magnitude) auto mag = as_magnitude<R>();
template<Magnitude auto M>
struct prefix_base : downcast_base<prefix_base<M>> {
static constexpr UNITS_MSVC_WORKAROUND(Magnitude) auto mag = M;
};
} // namespace detail
@@ -54,8 +54,8 @@ struct prefix_base : downcast_base<prefix_base<R>> {
* @tparam Symbol a text representation of the prefix
* @tparam R factor to be used to scale a unit
*/
template<typename Child, basic_symbol_text Symbol, ratio R>
struct prefix : downcast_dispatch<Child, detail::prefix_base<R>, downcast_mode::on> {
template<typename Child, basic_symbol_text Symbol, Magnitude auto M>
struct prefix : downcast_dispatch<Child, detail::prefix_base<M>, downcast_mode::on> {
static constexpr auto symbol = Symbol;
};