coherent_derived_unit and custom prefixes support added

This commit is contained in:
Mateusz Pusz
2019-10-17 10:16:44 +02:00
parent 9a9abca81d
commit 3e30c779a6
29 changed files with 261 additions and 115 deletions
+1 -1
View File
@@ -31,7 +31,7 @@ namespace units {
template<typename T>
concept Acceleration = QuantityOf<T, acceleration>;
struct metre_per_second_sq : derived_unit<metre_per_second_sq, decltype("m/s^2"_fs), acceleration, metre, second> {};
struct metre_per_second_sq : coherent_derived_unit<metre_per_second_sq, decltype("m/s^2"_fs), acceleration> {};
inline namespace literals {
+5 -5
View File
@@ -31,14 +31,18 @@ namespace units {
template<typename T>
concept Area = QuantityOf<T, area>;
struct square_metre : coherent_derived_unit<square_metre, decltype("m^2"_fs), area> {};
struct square_millimetre : derived_unit<square_millimetre, decltype("mm^2"_fs), area, millimetre> {};
struct square_centimetre : derived_unit<square_centimetre, decltype("cm^2"_fs), area, centimetre> {};
struct square_metre : derived_unit<square_metre, decltype("m^2"_fs), area, metre> {};
struct square_kilometre : derived_unit<square_kilometre, decltype("km^2"_fs), area, kilometre> {};
struct square_foot : derived_unit<square_foot, decltype("ft^2"_fs), area, foot> {};
inline namespace literals {
// sq_m
constexpr auto operator""sq_m(unsigned long long l) { return quantity<square_metre, std::int64_t>(l); }
constexpr auto operator""sq_m(long double l) { return quantity<square_metre, long double>(l); }
// sq_mm
constexpr auto operator""sq_mm(unsigned long long l) { return quantity<square_millimetre, std::int64_t>(l); }
constexpr auto operator""sq_mm(long double l) { return quantity<square_millimetre, long double>(l); }
@@ -47,10 +51,6 @@ namespace units {
constexpr auto operator""sq_cm(unsigned long long l) { return quantity<square_centimetre, std::int64_t>(l); }
constexpr auto operator""sq_cm(long double l) { return quantity<square_centimetre, long double>(l); }
// sq_m
constexpr auto operator""sq_m(unsigned long long l) { return quantity<square_metre, std::int64_t>(l); }
constexpr auto operator""sq_m(long double l) { return quantity<square_metre, long double>(l); }
// sq_km
constexpr auto operator""sq_km(unsigned long long l) { return quantity<square_kilometre, std::int64_t>(l); }
constexpr auto operator""sq_km(long double l) { return quantity<square_kilometre, long double>(l); }
+1 -1
View File
@@ -33,7 +33,7 @@ namespace units {
template<typename T>
concept Capacitance = QuantityOf<T, capacitance>;
struct farad : derived_unit<farad, decltype("F"_fs), capacitance, kilogram, metre, second, ampere> {};
struct farad : coherent_derived_unit<farad, decltype("F"_fs), capacitance, 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 : derived_unit<ampere, decltype("A"_fs), current> {};
struct ampere : coherent_derived_unit<ampere, decltype("A"_fs), current, si_prefix> {};
inline namespace literals {
@@ -33,7 +33,7 @@ namespace units {
template<typename T>
concept ElectricCharge = QuantityOf<T, electric_charge>;
struct coulomb : derived_unit<coulomb, decltype("C"_fs), electric_charge, second, ampere> {};
struct coulomb : coherent_derived_unit<coulomb, decltype("C"_fs), electric_charge, si_prefix> {};
inline namespace literals {
+2 -2
View File
@@ -27,13 +27,13 @@
#include <units/dimensions/pressure.h>
namespace units {
struct energy : derived_dimension<energy, exp<force, 1>, exp<length, 1>> {};
template<typename T>
concept Energy = QuantityOf<T, energy>;
struct joule : derived_unit<joule, decltype("J"_fs), energy, kilogram, metre, second> {};
struct joule : coherent_derived_unit<joule, decltype("J"_fs), energy, si_prefix> {};
struct millijoule : derived_unit<millijoule, decltype("mJ"_fs), milli<joule>> {};
struct kilojoule : derived_unit<kilojoule, decltype("kJ"_fs), kilo<joule>> {};
struct megajoule : derived_unit<megajoule, decltype("MJ"_fs), mega<joule>> {};
+1 -1
View File
@@ -33,7 +33,7 @@ namespace units {
template<typename T>
concept Force = QuantityOf<T, force>;
struct newton : derived_unit<newton, decltype("N"_fs), force, kilogram, metre, second> {};
struct newton : coherent_derived_unit<newton, decltype("N"_fs), force, si_prefix> {};
inline namespace literals {
+1 -1
View File
@@ -32,7 +32,7 @@ namespace units {
template<typename T>
concept Frequency = QuantityOf<T, frequency>;
struct hertz : derived_unit<hertz, decltype("Hz"_fs), frequency, second> {};
struct hertz : coherent_derived_unit<hertz, decltype("Hz"_fs), frequency, si_prefix> {};
struct millihertz : derived_unit<millihertz, decltype("mHz"_fs), milli<hertz>> {};
struct kilohertz : derived_unit<kilohertz, decltype("kHz"_fs), kilo<hertz>> {};
struct megahertz : derived_unit<megahertz, decltype("MHz"_fs), mega<hertz>> {};
+1 -1
View File
@@ -33,7 +33,7 @@ namespace units {
concept Length = QuantityOf<T, length>;
// SI units
struct metre : derived_unit<metre, decltype("m"_fs), length> {};
struct metre : coherent_derived_unit<metre, decltype("m"_fs), length, si_prefix> {};
struct millimetre : derived_unit<millimetre, decltype("mm"_fs), milli<metre>> {};
struct centimetre : derived_unit<centimetre, decltype("cm"_fs), centi<metre>> {};
struct kilometre : derived_unit<kilometre, decltype("km"_fs), kilo<metre>> {};
@@ -32,7 +32,7 @@ namespace units {
template<typename T>
concept LuminousIntensity = QuantityOf<T, luminous_intensity>;
struct candela : derived_unit<candela, decltype("cd"_fs), luminous_intensity> {};
struct candela : coherent_derived_unit<candela, decltype("cd"_fs), luminous_intensity, si_prefix> {};
inline namespace literals {
+1 -1
View File
@@ -32,8 +32,8 @@ namespace units {
template<typename T>
concept Mass = QuantityOf<T, mass>;
struct kilogram : coherent_derived_unit<kilogram, decltype("kg"_fs), mass> {};
struct gram : derived_unit<gram, decltype("g"_fs), mass, ratio<1, 1000>> {};
struct kilogram : derived_unit<kilogram, decltype("kg"_fs), kilo<gram>> {};
inline namespace literals {
+1 -1
View File
@@ -32,7 +32,7 @@ namespace units {
template<typename T>
concept Power = QuantityOf<T, power>;
struct watt : derived_unit<watt, decltype("W"_fs), power, kilogram, metre, second> {};
struct watt : coherent_derived_unit<watt, decltype("W"_fs), power, si_prefix> {};
struct milliwatt : derived_unit<milliwatt, decltype("mW"_fs), milli<watt>> {};
struct kilowatt : derived_unit<kilowatt, decltype("kW"_fs), kilo<watt>> {};
struct megawatt : derived_unit<megawatt, decltype("MW"_fs), mega<watt>> {};
+1 -1
View File
@@ -33,7 +33,7 @@ namespace units {
template<typename T>
concept Pressure = QuantityOf<T, pressure>;
struct pascal : derived_unit<pascal, decltype("Pa"_fs), pressure, kilogram, metre, second> {};
struct pascal : coherent_derived_unit<pascal, decltype("Pa"_fs), pressure, si_prefix> {};
inline namespace literals {
+1 -1
View File
@@ -32,7 +32,7 @@ namespace units {
template<typename T>
concept Substance = QuantityOf<T, substance>;
struct mole : derived_unit<mole, decltype("mol"_fs), substance> {};
struct mole : coherent_derived_unit<mole, decltype("mol"_fs), substance, 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 : derived_unit<kelvin, decltype("K"_fs), temperature> {};
struct kelvin : coherent_derived_unit<kelvin, decltype("K"_fs), temperature> {};
inline namespace literals {
+1 -1
View File
@@ -32,7 +32,7 @@ namespace units {
template<typename T>
concept Time = QuantityOf<T, time>;
struct second : derived_unit<second, decltype("s"_fs), time> {};
struct second : coherent_derived_unit<second, decltype("s"_fs), time, si_prefix> {};
struct nanosecond : derived_unit<nanosecond, decltype("ns"_fs), nano<second>> {};
struct microsecond : derived_unit<microsecond, decltype("us"_fs), micro<second>> {};
struct millisecond : derived_unit<millisecond, decltype("ms"_fs), milli<second>> {};
+1 -1
View File
@@ -32,7 +32,7 @@ namespace units {
template<typename T>
concept Velocity = QuantityOf<T, velocity>;
struct metre_per_second : derived_unit<metre_per_second, decltype("m/s"_fs), velocity, metre, second> {};
struct metre_per_second : coherent_derived_unit<metre_per_second, decltype("m/s"_fs), velocity> {};
struct kilometre_per_hour : derived_unit<kilometre_per_hour, decltype("km/h"_fs), velocity, kilometre, hour> {};
struct mile_per_hour : derived_unit<mile_per_hour, decltype("mi/h"_fs), velocity, mile, hour> {};
+1 -1
View File
@@ -35,7 +35,7 @@ namespace units {
template<typename T>
concept Voltage = QuantityOf<T, voltage>;
struct volt : derived_unit<volt, decltype("V"_fs), voltage, kilogram, metre, second, ampere> {};
struct volt : coherent_derived_unit<volt, decltype("V"_fs), voltage, si_prefix> {};
inline namespace literals {
+1 -1
View File
@@ -31,9 +31,9 @@ namespace units {
template<typename T>
concept Volume = QuantityOf<T, volume>;
struct cubic_metre : coherent_derived_unit<cubic_metre, decltype("m^3"_fs), volume> {};
struct cubic_millimetre : derived_unit<cubic_millimetre, decltype("mm^3"_fs), volume, millimetre> {};
struct cubic_centimetre : derived_unit<cubic_centimetre, decltype("cm^3"_fs), volume, centimetre> {};
struct cubic_metre : derived_unit<cubic_metre, decltype("m^3"_fs), volume, metre> {};
struct cubic_kilometre : derived_unit<cubic_kilometre, decltype("km^3"_fs), volume, kilometre, metre> {};
struct cubic_foot : derived_unit<cubic_foot, decltype("ft^3"_fs), volume, foot> {};
+5 -22
View File
@@ -26,6 +26,9 @@
namespace units {
template<typename Prefix, typename Ratio>
inline constexpr std::string_view prefix_symbol = "";
namespace detail {
template<typename Ratio, typename CharT, typename Traits>
@@ -41,31 +44,11 @@ namespace units {
}
}
template<typename Ratio>
inline constexpr std::string_view ratio_txt = "";
template<> inline constexpr std::string_view ratio_txt<ratio<1, std::atto::den>> = "a";
template<> inline constexpr std::string_view ratio_txt<ratio<1, std::femto::den>> = "f";
template<> inline constexpr std::string_view ratio_txt<ratio<1, std::pico::den>> = "p";
template<> inline constexpr std::string_view ratio_txt<ratio<1, std::nano::den>> = "n";
template<> inline constexpr std::string_view ratio_txt<ratio<1, std::micro::den>> = "\u00b5\u0073";
template<> inline constexpr std::string_view ratio_txt<ratio<1, std::milli::den>> = "m";
template<> inline constexpr std::string_view ratio_txt<ratio<1, std::centi::den>> = "c";
template<> inline constexpr std::string_view ratio_txt<ratio<1, std::deci::den>> = "d";
template<> inline constexpr std::string_view ratio_txt<ratio<std::deca::num>> = "da";
template<> inline constexpr std::string_view ratio_txt<ratio<std::hecto::num>> = "h";
template<> inline constexpr std::string_view ratio_txt<ratio<std::kilo::num>> = "k";
template<> inline constexpr std::string_view ratio_txt<ratio<std::mega::num>> = "M";
template<> inline constexpr std::string_view ratio_txt<ratio<std::giga::num>> = "G";
template<> inline constexpr std::string_view ratio_txt<ratio<std::tera::num>> = "T";
template<> inline constexpr std::string_view ratio_txt<ratio<std::peta::num>> = "P";
template<> inline constexpr std::string_view ratio_txt<ratio<std::exa::num>> = "E";
template<typename Ratio, typename CharT, typename Traits>
template<typename Ratio, typename Prefix, typename CharT, typename Traits>
void print_prefix_or_ratio(std::basic_ostream<CharT, Traits>& os)
{
if constexpr(Ratio::num != 1 || Ratio::den != 1) {
constexpr auto prefix = ratio_txt<Ratio>;
constexpr auto prefix = prefix_symbol<Prefix, Ratio>;
if constexpr(prefix != "") {
// print as a prefixed unit
+68
View File
@@ -0,0 +1,68 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <units/unit.h>
#include <units/format.h>
namespace units {
// prefix tags
struct si_prefix;
// SI prefixes
template<Unit U> using atto = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::atto::den>>>;
template<Unit U> using femto = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::femto::den>>>;
template<Unit U> using pico = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::pico::den>>>;
template<Unit U> using nano = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::nano::den>>>;
template<Unit U> using micro = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::micro::den>>>;
template<Unit U> using milli = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::milli::den>>>;
template<Unit U> using centi = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::centi::den>>>;
template<Unit U> using deca = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::deca::num>>>;
template<Unit U> using hecto = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::hecto::num>>>;
template<Unit U> using kilo = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::kilo::num>>>;
template<Unit U> using mega = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::mega::num>>>;
template<Unit U> using giga = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::giga::num>>>;
template<Unit U> using tera = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::tera::num>>>;
template<Unit U> using peta = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::peta::num>>>;
template<Unit U> using exa = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::exa::num>>>;
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<1, std::atto::den>> = "a";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<1, std::femto::den>> = "f";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<1, std::pico::den>> = "p";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<1, std::nano::den>> = "n";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<1, std::micro::den>> = "\u00b5\u0073"; // µ
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<1, std::milli::den>> = "m";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<1, std::centi::den>> = "c";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<1, std::deci::den>> = "d";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<std::deca::num>> = "da";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<std::hecto::num>> = "h";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<std::kilo::num>> = "k";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<std::mega::num>> = "M";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<std::giga::num>> = "G";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<std::tera::num>> = "T";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<std::peta::num>> = "P";
template<> inline constexpr std::string_view prefix_symbol<si_prefix, ratio<std::exa::num>> = "E";
}
+5 -9
View File
@@ -23,8 +23,7 @@
#pragma once
#include <units/bits/concepts.h>
#include <units/unit.h>
#include <units/format.h>
#include <units/prefix.h>
#include <limits>
#include <ostream>
@@ -290,13 +289,10 @@ namespace units {
using ratio = quantity::unit::ratio;
using dim = quantity::unit::dimension;
if constexpr(!detail::is_dimension<dim>) {
// print as a prefix or ratio of a coherent unit
detail::print_prefix_or_ratio<ratio>(os);
if constexpr(!detail::is_dimension<dim>) {
// print coherent unit symbol defined by the user
os << downcast_target<units::unit<dim>>::symbol::c_str();
}
// print as a prefix or ratio of a coherent unit symbol defined by the user
using coherent_unit = downcast_target<units::unit<dim, units::ratio<1>>>;
detail::print_prefix_or_ratio<ratio, typename coherent_unit::prefix>(os);
os << coherent_unit::symbol::c_str();
}
else {
// print as a ratio of a coherent unit
+9 -24
View File
@@ -28,7 +28,7 @@
namespace units {
template<Dimension D, Ratio R = ratio<1>>
template<Dimension D, Ratio R>
requires (R::num * R::den > 0)
struct unit : downcast_base<unit<D, R>> {
using dimension = D;
@@ -141,14 +141,17 @@ namespace units {
// static constexpr auto symbol = Symbol;
// };
struct no_prefix;
template<typename Child, typename Symbol, Dimension D, typename Prefix = no_prefix>
struct coherent_derived_unit : downcast_helper<Child, unit<D, ratio<1>>> {
using symbol = Symbol;
using prefix = Prefix;
};
template<typename Child, typename Symbol, typename...>
struct derived_unit;
template<typename Child, typename Symbol, Dimension D>
struct derived_unit<Child, Symbol, D> : downcast_helper<Child, unit<D, ratio<1>>> {
using symbol = Symbol;
};
template<typename Child, typename Symbol, Dimension D, Ratio R>
struct derived_unit<Child, Symbol, D, R> : downcast_helper<Child, unit<D, R>> {
using symbol = Symbol;
@@ -164,22 +167,4 @@ namespace units {
using symbol = Symbol;
};
// SI prefixes
template<Unit U> using atto = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::atto::den>>>;
template<Unit U> using femto = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::femto::den>>>;
template<Unit U> using pico = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::pico::den>>>;
template<Unit U> using nano = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::nano::den>>>;
template<Unit U> using micro = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::micro::den>>>;
template<Unit U> using milli = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::milli::den>>>;
template<Unit U> using centi = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<1, std::centi::den>>>;
template<Unit U> using deca = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::deca::num>>>;
template<Unit U> using hecto = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::hecto::num>>>;
template<Unit U> using kilo = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::kilo::num>>>;
template<Unit U> using mega = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::mega::num>>>;
template<Unit U> using giga = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::giga::num>>>;
template<Unit U> using tera = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::tera::num>>>;
template<Unit U> using peta = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::peta::num>>>;
template<Unit U> using exa = unit<typename U::dimension, ratio_multiply<typename U::ratio, ratio<std::exa::num>>>;
} // namespace units