Basic std::fmt support added
This commit is contained in:
1 parent
4d3e4aa1fc
commit
b5b2c54fe8
5 files changed
+613
-380
No files matched your search
+159
-5
@@ -22,7 +22,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/bits/format_utils.h>
|
||||
#include <units/dimension.h>
|
||||
#include <units/prefix.h>
|
||||
#include <units/ratio.h>
|
||||
@@ -115,9 +114,164 @@ namespace units {
|
||||
|
||||
}
|
||||
|
||||
// derived_unit
|
||||
namespace detail {
|
||||
|
||||
struct no_prefix;
|
||||
template<int Value>
|
||||
requires (0 <= Value) && (Value < 10)
|
||||
inline constexpr basic_fixed_string superscript_number = "\u2070";
|
||||
|
||||
// template<> inline constexpr basic_fixed_string superscript_number<0> = "\u2070";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<1> = "\u00b9";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<2> = "\u00b2";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<3> = "\u00b3";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<4> = "\u2074";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<5> = "\u2075";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<6> = "\u2076";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<7> = "\u2077";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<8> = "\u2078";
|
||||
template<> inline constexpr basic_fixed_string superscript_number<9> = "\u2079";
|
||||
|
||||
template<int Value>
|
||||
requires (Value >= 0)
|
||||
constexpr auto superscript()
|
||||
{
|
||||
if constexpr(Value < 10)
|
||||
return superscript_number<Value>;
|
||||
else
|
||||
return superscript<Value / 10>() + superscript<Value % 10>();
|
||||
}
|
||||
|
||||
template<int Value>
|
||||
requires (Value >= 0)
|
||||
constexpr auto regular()
|
||||
{
|
||||
if constexpr(Value < 10)
|
||||
return basic_fixed_string(static_cast<char>('0' + Value));
|
||||
else
|
||||
return regular<Value / 10>() + regular<Value % 10>();
|
||||
}
|
||||
|
||||
|
||||
template<typename Ratio>
|
||||
constexpr auto ratio_text()
|
||||
{
|
||||
if constexpr(Ratio::num != 1 || Ratio::den != 1) {
|
||||
auto txt = basic_fixed_string("[") + regular<Ratio::num>();
|
||||
if constexpr(Ratio::den == 1) {
|
||||
return txt + basic_fixed_string("]");
|
||||
}
|
||||
else {
|
||||
return txt + basic_fixed_string("/") + regular<Ratio::den>() + basic_fixed_string("]");
|
||||
}
|
||||
}
|
||||
else {
|
||||
return basic_fixed_string("");
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Ratio, typename PrefixType>
|
||||
constexpr auto prefix_or_ratio_text()
|
||||
{
|
||||
if constexpr(Ratio::num != 1 || Ratio::den != 1) {
|
||||
if constexpr (!std::same_as<PrefixType, no_prefix>) {
|
||||
using prefix = downcast<detail::prefix_base<PrefixType, Ratio>>;
|
||||
|
||||
if constexpr(!std::same_as<prefix, prefix_base<PrefixType, Ratio>>) {
|
||||
// print as a prefixed unit
|
||||
return prefix::symbol;
|
||||
}
|
||||
else {
|
||||
// print as a ratio of the coherent unit
|
||||
return ratio_text<Ratio>();
|
||||
}
|
||||
}
|
||||
else {
|
||||
// print as a ratio of the coherent unit
|
||||
return ratio_text<Ratio>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<bool Divide, std::size_t Idx>
|
||||
constexpr auto operator_text()
|
||||
{
|
||||
if constexpr(Idx == 0) {
|
||||
if constexpr(Divide) {
|
||||
return basic_fixed_string("1/");
|
||||
}
|
||||
else {
|
||||
return basic_fixed_string("");
|
||||
}
|
||||
}
|
||||
else {
|
||||
if constexpr(Divide) {
|
||||
return basic_fixed_string("/");
|
||||
}
|
||||
else {
|
||||
return basic_fixed_string("⋅");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename E, std::size_t Idx>
|
||||
constexpr auto exp_text()
|
||||
{
|
||||
// get calculation operator + symbol
|
||||
const auto txt = operator_text<E::num < 0, Idx>() + E::dimension::symbol;
|
||||
if constexpr(E::den != 1) {
|
||||
// add root part
|
||||
return txt + basic_fixed_string("^(") + regular<abs(E::num)>() + basic_fixed_string("/") + regular<E::den>() + basic_fixed_string(")");
|
||||
}
|
||||
else if constexpr(abs(E::num) != 1) {
|
||||
// add exponent part
|
||||
return txt + superscript<abs(E::num)>();
|
||||
}
|
||||
else {
|
||||
return txt;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... Es, std::size_t... Idxs>
|
||||
constexpr auto symbol_text_impl(dimension<Es...>, std::index_sequence<Idxs...>)
|
||||
{
|
||||
return (exp_text<Es, Idxs>() + ...);
|
||||
}
|
||||
|
||||
template<typename... Es>
|
||||
constexpr auto symbol_text(dimension<Es...> d)
|
||||
{
|
||||
return symbol_text_impl<>(d, std::index_sequence_for<Es...>());
|
||||
}
|
||||
|
||||
template<typename Unit>
|
||||
constexpr auto unit_text()
|
||||
{
|
||||
if constexpr(!is_unit<Unit>) {
|
||||
// Unit is a downcasted derived unit child class already so just print defined symbol immediately
|
||||
return Unit::symbol;
|
||||
}
|
||||
else {
|
||||
// we are dealing with a non-user-defined unit here
|
||||
using ratio = Unit::ratio;
|
||||
using dim = Unit::dimension;
|
||||
if constexpr(!is_dimension<dim>) {
|
||||
// downcasted user-defined dimension
|
||||
// print as a prefix or ratio of a coherent unit symbol defined by the user
|
||||
using coherent_unit = downcast<units::unit<dim, units::ratio<1>>>;
|
||||
return prefix_or_ratio_text<ratio, typename coherent_unit::prefix_type>() + coherent_unit::symbol;
|
||||
}
|
||||
else {
|
||||
// print as a ratio of a coherent unit + coherent unit dimensions and their exponents
|
||||
return ratio_text<ratio>() + symbol_text(dim{});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
|
||||
// derived_unit
|
||||
|
||||
template<typename Child, basic_fixed_string Symbol, Dimension D, typename PrefixType = no_prefix>
|
||||
struct named_coherent_derived_unit : downcast_child<Child, unit<D, ratio<1>>> {
|
||||
@@ -138,7 +292,7 @@ namespace units {
|
||||
|
||||
template<typename Child, Dimension D, Ratio R>
|
||||
struct derived_unit : downcast_child<Child, unit<D, R>> {
|
||||
static constexpr auto symbol = "aaa";
|
||||
static constexpr auto symbol = basic_fixed_string("aaa");
|
||||
};
|
||||
|
||||
template<typename Child, Prefix P, Unit U>
|
||||
@@ -155,7 +309,7 @@ namespace units {
|
||||
|
||||
template<typename Child, Dimension D, Unit U, Unit... Us>
|
||||
struct deduced_derived_unit : downcast_child<Child, detail::make_derived_unit<D, U, Us...>> {
|
||||
static constexpr auto symbol = "bbb";
|
||||
static constexpr auto symbol = basic_fixed_string("bbb");
|
||||
};
|
||||
|
||||
} // namespace units
|
||||
Reference in new issue
Block a user