Derived unit factory helpers refactored

This commit is contained in:
Mateusz Pusz
2019-11-08 16:51:45 +00:00
parent ad895b4651
commit 0125bd7762
23 changed files with 116 additions and 92 deletions
+1 -1
View File
@@ -33,7 +33,7 @@ namespace units {
template<typename T>
concept Capacitance = QuantityOf<T, capacitance>;
struct farad : named_coherent_derived_unit<farad, "F", capacitance, si_prefix> {};
struct farad : named_coherent_derived_unit<farad, capacitance, "F", si_prefix> {};
inline namespace literals {
+1 -1
View File
@@ -32,7 +32,7 @@ namespace units {
template<typename T>
concept Current = QuantityOf<T, current>;
struct ampere : named_coherent_derived_unit<ampere, "A", current, si_prefix> {};
struct ampere : named_coherent_derived_unit<ampere, current, "A", si_prefix> {};
inline namespace literals {
@@ -33,7 +33,7 @@ namespace units {
template<typename T>
concept ElectricCharge = QuantityOf<T, electric_charge>;
struct coulomb : named_coherent_derived_unit<coulomb, "C", electric_charge, si_prefix> {};
struct coulomb : named_coherent_derived_unit<coulomb, electric_charge, "C", si_prefix> {};
inline namespace literals {
+1 -1
View File
@@ -34,7 +34,7 @@ namespace units {
template<typename T>
concept Energy = QuantityOf<T, energy>;
struct joule : named_coherent_derived_unit<joule, "J", energy, si_prefix> {};
struct joule : named_coherent_derived_unit<joule, energy, "J", si_prefix> {};
struct millijoule : prefixed_derived_unit<millijoule, milli, joule> {};
struct kilojoule : prefixed_derived_unit<kilojoule, kilo, joule> {};
struct megajoule : prefixed_derived_unit<megajoule, mega, joule> {};
+1 -1
View File
@@ -33,7 +33,7 @@ namespace units {
template<typename T>
concept Force = QuantityOf<T, force>;
struct newton : named_coherent_derived_unit<newton, "N", force, si_prefix> {};
struct newton : named_coherent_derived_unit<newton, force, "N", si_prefix> {};
inline namespace literals {
+1 -1
View File
@@ -33,7 +33,7 @@ namespace units {
template<typename T>
concept Frequency = QuantityOf<T, frequency>;
struct hertz : named_coherent_derived_unit<hertz, "Hz", frequency, si_prefix> {};
struct hertz : named_coherent_derived_unit<hertz, frequency, "Hz", si_prefix> {};
struct millihertz : prefixed_derived_unit<millihertz, milli, hertz> {};
struct kilohertz : prefixed_derived_unit<kilohertz, kilo, hertz> {};
struct megahertz : prefixed_derived_unit<megahertz, mega, hertz> {};
+5 -5
View File
@@ -34,7 +34,7 @@ namespace units {
concept Length = QuantityOf<T, length>;
// SI units
struct metre : named_coherent_derived_unit<metre, "m", length, si_prefix> {};
struct metre : named_coherent_derived_unit<metre, length, "m", si_prefix> {};
struct millimetre : prefixed_derived_unit<millimetre, milli, metre> {};
struct centimetre : prefixed_derived_unit<centimetre, centi, metre> {};
struct kilometre : prefixed_derived_unit<kilometre, kilo, metre> {};
@@ -60,10 +60,10 @@ namespace units {
} // namespace literals
// US customary units
struct yard : named_derived_unit<yard, "yd", length, ratio<9'144, 10'000>> {};
struct foot : named_derived_unit<foot, "ft", length, ratio_multiply<ratio<1, 3>, yard::ratio>> {};
struct inch : named_derived_unit<inch, "in", length, ratio_multiply<ratio<1, 12>, foot::ratio>> {};
struct mile : named_derived_unit<mile, "mi", length, ratio_multiply<ratio<1'760>, yard::ratio>> {};
struct yard : named_scaled_derived_unit<yard, length, "yd", ratio<9'144, 10'000>> {};
struct foot : named_scaled_derived_unit<foot, length, "ft", ratio_multiply<ratio<1, 3>, yard::ratio>> {};
struct inch : named_scaled_derived_unit<inch, length, "in", ratio_multiply<ratio<1, 12>, foot::ratio>> {};
struct mile : named_scaled_derived_unit<mile, length, "mi", ratio_multiply<ratio<1'760>, yard::ratio>> {};
inline namespace literals {
@@ -32,7 +32,7 @@ namespace units {
template<typename T>
concept LuminousIntensity = QuantityOf<T, luminous_intensity>;
struct candela : named_coherent_derived_unit<candela, "cd", luminous_intensity, si_prefix> {};
struct candela : named_coherent_derived_unit<candela, luminous_intensity, "cd", si_prefix> {};
inline namespace literals {
+1 -1
View File
@@ -32,7 +32,7 @@ namespace units {
template<typename T>
concept Mass = QuantityOf<T, mass>;
struct gram : named_derived_unit<gram, "g", mass, ratio<1, 1000>> {};
struct gram : named_scaled_derived_unit<gram, mass, "g", ratio<1, 1000>, si_prefix> {};
struct kilogram : prefixed_derived_unit<kilogram, kilo, gram> {};
inline namespace literals {
+1 -1
View File
@@ -33,7 +33,7 @@ namespace units {
template<typename T>
concept Power = QuantityOf<T, power>;
struct watt : named_coherent_derived_unit<watt, "W", power, si_prefix> {};
struct watt : named_coherent_derived_unit<watt, power, "W", si_prefix> {};
struct milliwatt : prefixed_derived_unit<milliwatt, milli, watt> {};
struct kilowatt : prefixed_derived_unit<kilowatt, kilo, watt> {};
struct megawatt : prefixed_derived_unit<megawatt, mega, watt> {};
+1 -1
View File
@@ -33,7 +33,7 @@ namespace units {
template<typename T>
concept Pressure = QuantityOf<T, pressure>;
struct pascal : named_coherent_derived_unit<pascal, "Pa", pressure, si_prefix> {};
struct pascal : named_coherent_derived_unit<pascal, pressure, "Pa", si_prefix> {};
inline namespace literals {
+1 -1
View File
@@ -28,7 +28,7 @@
namespace units {
// prefix tags
struct si_prefix;
struct si_prefix : prefix_type {};
// SI prefixes
+1 -1
View File
@@ -32,7 +32,7 @@ namespace units {
template<typename T>
concept Substance = QuantityOf<T, substance>;
struct mole : named_coherent_derived_unit<mole, "mol", substance, si_prefix> {};
struct mole : named_coherent_derived_unit<mole, substance, "mol", si_prefix> {};
inline namespace literals {
+1 -1
View File
@@ -32,7 +32,7 @@ namespace units {
template<typename T>
concept ThermodynamicTemperature = QuantityOf<T, temperature>;
struct kelvin : named_coherent_derived_unit<kelvin, "K", temperature> {};
struct kelvin : named_coherent_derived_unit<kelvin, temperature, "K", no_prefix> {};
inline namespace literals {
+3 -3
View File
@@ -33,12 +33,12 @@ namespace units {
template<typename T>
concept Time = QuantityOf<T, time>;
struct second : named_coherent_derived_unit<second, "s", time, si_prefix> {};
struct second : named_coherent_derived_unit<second, time, "s", si_prefix> {};
struct nanosecond : prefixed_derived_unit<nanosecond, nano, second> {};
struct microsecond : prefixed_derived_unit<microsecond, micro, second> {};
struct millisecond : prefixed_derived_unit<millisecond, milli, second> {};
struct minute : named_derived_unit<minute, "min", time, ratio<60>> {};
struct hour : named_derived_unit<hour, "h", time, ratio<3600>> {};
struct minute : named_scaled_derived_unit<minute, time, "min", ratio<60>> {};
struct hour : named_scaled_derived_unit<hour, time, "h", ratio<3600>> {};
inline namespace literals {
+1 -1
View File
@@ -35,7 +35,7 @@ namespace units {
template<typename T>
concept Voltage = QuantityOf<T, voltage>;
struct volt : named_coherent_derived_unit<volt, "V", voltage, si_prefix> {};
struct volt : named_coherent_derived_unit<volt, voltage, "V", si_prefix> {};
inline namespace literals {
+11 -6
View File
@@ -28,18 +28,23 @@
namespace units {
struct prefix_type {};
template<typename T>
concept PrefixType = std::derived_from<T, prefix_type>;
namespace detail {
template<typename PrefixType, Ratio R>
struct prefix_base : downcast_base<prefix_base<PrefixType, R>> {
using prefix_type = PrefixType;
template<PrefixType PT, Ratio R>
struct prefix_base : downcast_base<prefix_base<PT, R>> {
using prefix_type = PT;
using ratio = R;
};
}
template<typename Child, typename PrefixType, Ratio R, basic_fixed_string Symbol>
struct prefix : downcast_child<Child, detail::prefix_base<PrefixType, R>> {
template<typename Child, PrefixType PT, Ratio R, basic_fixed_string Symbol>
struct prefix : downcast_child<Child, detail::prefix_base<PT, R>> {
static constexpr auto symbol = Symbol;
};
@@ -60,6 +65,6 @@ namespace units {
// concept Prefix = detail::is_prefix<T>;
concept Prefix = true;
struct no_prefix;
struct no_prefix : prefix_type {};
} // namespace units
+23 -20
View File
@@ -273,38 +273,41 @@ namespace units {
// 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>>> {
template<typename Child, Dimension Dim, basic_fixed_string Symbol, PrefixType PT>
struct named_coherent_derived_unit : downcast_child<Child, unit<Dim, ratio<1>>> {
static constexpr auto symbol = Symbol;
using prefix_type = PrefixType;
using prefix_type = PT;
};
template<typename Child, Dimension D, typename PrefixType = no_prefix>
struct coherent_derived_unit : downcast_child<Child, unit<D, ratio<1>>> {
static constexpr auto symbol = detail::symbol_text(D());
using prefix_type = PrefixType;
template<typename Child, Dimension Dim>
struct coherent_derived_unit : downcast_child<Child, unit<Dim, ratio<1>>> {
static constexpr auto symbol = detail::symbol_text(Dim());
using prefix_type = no_prefix;
};
template<typename Child, basic_fixed_string Symbol, Dimension D, Ratio R>
struct named_derived_unit : downcast_child<Child, unit<D, R>> {
template<typename Child, Dimension Dim, basic_fixed_string Symbol, Ratio R, PrefixType PT = no_prefix>
struct named_scaled_derived_unit : downcast_child<Child, unit<Dim, R>> {
static constexpr auto symbol = Symbol;
using prefix_type = PT;
};
template<typename Child, Dimension Dim, basic_fixed_string Symbol, PrefixType PT, Unit U, Unit... Us>
struct named_deduced_derived_unit : downcast_child<Child, detail::make_derived_unit<Dim, U, Us...>> {
static constexpr auto symbol = Symbol;
using prefix_type = PT;
};
template<typename Child, Dimension Dim, Unit U, Unit... Us>
struct deduced_derived_unit : downcast_child<Child, detail::make_derived_unit<Dim, U, Us...>> {
static constexpr auto symbol = basic_fixed_string("bbb");
using prefix_type = no_prefix;
};
template<typename Child, Prefix P, Unit U>
requires requires { U::symbol; }
requires (!std::same_as<typename U::prefix_type, no_prefix>)
struct prefixed_derived_unit : downcast_child<Child, unit<typename U::dimension, ratio_multiply<typename P::ratio, typename U::ratio>>> {
static constexpr auto symbol = P::symbol + U::symbol;
using prefix_type = P::prefix_type;
};
template<typename Child, basic_fixed_string Symbol, Dimension D, Unit U, Unit... Us>
struct named_deduced_derived_unit : downcast_child<Child, detail::make_derived_unit<D, U, Us...>> {
static constexpr auto symbol = Symbol;
};
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 = basic_fixed_string("bbb");
};
} // namespace units