diff --git a/src/shared/units/Length.h b/src/shared/units/Length.h index 4408326e4cd4123b9bb6f333406c0a1ac8541968..6afec19cc16310bb09bbfe2437437a9d85fdbcdc 100644 --- a/src/shared/units/Length.h +++ b/src/shared/units/Length.h @@ -34,10 +34,7 @@ namespace Length { template <class Ratio = std::ratio<1>> -class Length : public Unit<Ratio> -{ - using Unit<Ratio>::Unit; -}; +using Length = Unit<UnitKind::Length, Ratio>; template <class ToLength, class FromLength> ToLength length_cast(FromLength const &from) diff --git a/src/shared/units/Pressure.h b/src/shared/units/Pressure.h index 8f5e76b81ca028b58d1db87a07b898cd8693e0d3..ac65bf249227a4b5f29927fc8fc238377a036313 100644 --- a/src/shared/units/Pressure.h +++ b/src/shared/units/Pressure.h @@ -34,10 +34,7 @@ namespace Pressure { template <class Ratio = std::ratio<1>> -class Pressure : public Unit<Ratio> -{ - using Unit<Ratio>::Unit; -}; +using Pressure = Unit<UnitKind::Pressure, Ratio>; template <class ToPressure, class FromPressure> ToPressure pressure_cast(FromPressure const &from) diff --git a/src/shared/units/Time.h b/src/shared/units/Time.h index a56760b9b7981f0a7fa7e0ade47d91c98c2ee0c2..41c7bb44b5ef4436305530b044a7784b8ca100cf 100644 --- a/src/shared/units/Time.h +++ b/src/shared/units/Time.h @@ -35,9 +35,9 @@ namespace Time { template <class Ratio = std::ratio<1>> -class Time : public Unit<Ratio> +class Time : public Unit<UnitKind::Time, Ratio> { - using Unit<Ratio>::Unit; + using Unit<UnitKind::Time, Ratio>::Unit; public: std::chrono::duration<float> chrono() const diff --git a/src/shared/units/Units.h b/src/shared/units/Units.h index 20499a3b62c8ff55c840c8c1e70e3f653d9d483b..64e37e9e8ef3b4f68d0f539190bece5f77351b17 100644 --- a/src/shared/units/Units.h +++ b/src/shared/units/Units.h @@ -35,17 +35,19 @@ namespace Units enum class UnitKind { Angle, - Length + Length, + Pressure, + Time }; -template <UnitKind Kind, class Ratio = std::ratio<1>> // Base class to implement custom measurement units logic. +template <UnitKind Kind, class Ratio = std::ratio<1>> class Unit { public: Unit(float val) : _value(val){}; - template <class FromRatio> - constexpr explicit Unit(Unit<Kind, FromRatio> const &from) + template <UnitKind FromKind, class FromRatio> + constexpr explicit Unit(Unit<FromKind, FromRatio> const &from) : _value(from.template value<Ratio>()) { } @@ -54,18 +56,18 @@ public: template <class TargetRatio = Ratio> float value() const { - const auto currentRatio = + constexpr auto currentRatio = static_cast<float>(Ratio::num) / static_cast<float>(Ratio::den); - const auto targetRatio = static_cast<float>(TargetRatio::num) / - static_cast<float>(TargetRatio::den); + constexpr auto targetRatio = static_cast<float>(TargetRatio::num) / + static_cast<float>(TargetRatio::den); return _value * currentRatio / targetRatio; } - template <class TargetRatio = Ratio> - constexpr explicit operator Unit<TargetRatio>() const + template <UnitKind TargetKind, class TargetRatio = Ratio> + constexpr explicit operator Unit<TargetKind, TargetRatio>() const { - return Unit<TargetRatio>(value<TargetRatio>()); + return Unit<TargetKind, TargetRatio>(value<TargetRatio>()); } private: @@ -77,100 +79,109 @@ template <UnitKind Kind, class Ratio> constexpr auto operator+(const Unit<Kind, Ratio> &lhs, const Unit<Kind, Ratio> &rhs) { - return Unit(lhs.template value() + rhs.template value()); + return Unit<Kind, Ratio>(lhs.template value() + rhs.template value()); } -template <class DerivedUnit> -constexpr auto operator-(const DerivedUnit &lhs, const DerivedUnit &rhs) +template <UnitKind Kind, class Ratio> +constexpr auto operator-(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) { - return DerivedUnit(lhs.template value() - rhs.template value()); + return Unit<Kind, Ratio>(lhs.template value() - rhs.template value()); } -template <class DerivedUnit> -constexpr auto operator*(const DerivedUnit &lhs, float rhs) +template <UnitKind Kind, class Ratio> +constexpr auto operator*(const Unit<Kind, Ratio> &lhs, float rhs) { - return DerivedUnit(lhs.template value() * rhs); + return Unit<Kind, Ratio>(lhs.template value() * rhs); } -template <class DerivedUnit> -constexpr auto operator*(float lhs, const DerivedUnit &rhs) +template <UnitKind Kind, class Ratio> +constexpr auto operator*(float lhs, const Unit<Kind, Ratio> &rhs) { - return DerivedUnit(lhs * rhs.template value()); + return Unit<Kind, Ratio>(lhs * rhs.template value()); } -template <class DerivedUnit> -constexpr auto operator/(const DerivedUnit &lhs, float rhs) +template <UnitKind Kind, class Ratio> +constexpr auto operator/(const Unit<Kind, Ratio> &lhs, float rhs) { - return DerivedUnit(lhs.template value() / rhs); + return Unit<Kind, Ratio>(lhs.template value() / rhs); } -template <class DerivedUnit> -constexpr auto operator/(float lhs, const DerivedUnit &rhs) +template <UnitKind Kind, class Ratio> +constexpr auto operator/(float lhs, const Unit<Kind, Ratio> &rhs) { - return DerivedUnit(lhs / rhs.template value()); + return Unit<Kind, Ratio>(lhs / rhs.template value()); } // Comparison operators -template <class DerivedUnit> -constexpr bool operator==(const DerivedUnit &lhs, const DerivedUnit &rhs) +template <UnitKind Kind, class Ratio> +constexpr bool operator==(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) { return lhs.template value() == rhs.template value(); } -template <class DerivedUnit> -constexpr bool operator!=(const DerivedUnit &lhs, const DerivedUnit &rhs) +template <UnitKind Kind, class Ratio> +constexpr bool operator!=(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) { return lhs.template value() != rhs.template value(); } -template <class DerivedUnit> -constexpr bool operator<(const DerivedUnit &lhs, const DerivedUnit &rhs) +template <UnitKind Kind, class Ratio> +constexpr bool operator<(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) { return lhs.template value() < rhs.template value(); } -template <class DerivedUnit> -constexpr bool operator>(const DerivedUnit &lhs, const DerivedUnit &rhs) +template <UnitKind Kind, class Ratio> +constexpr bool operator>(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) { return lhs.template value() > rhs.template value(); } -template <class DerivedUnit> -constexpr bool operator<=(const DerivedUnit &lhs, const DerivedUnit &rhs) +template <UnitKind Kind, class Ratio> +constexpr bool operator<=(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) { return lhs.template value() <= rhs.template value(); } -template <class DerivedUnit> -constexpr bool operator>=(const DerivedUnit &lhs, const DerivedUnit &rhs) +template <UnitKind Kind, class Ratio> +constexpr bool operator>=(const Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) { return lhs.template value() >= rhs.template value(); } // Direct assignment operators -template <class DerivedUnit> -constexpr DerivedUnit &operator+=(DerivedUnit &lhs, const DerivedUnit &rhs) +template <UnitKind Kind, class Ratio> +constexpr Unit<Kind, Ratio> &operator+=(Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) { lhs = lhs + rhs; return lhs; } -template <class DerivedUnit> -constexpr DerivedUnit &operator-=(DerivedUnit &lhs, const DerivedUnit &rhs) +template <UnitKind Kind, class Ratio> +constexpr Unit<Kind, Ratio> &operator-=(Unit<Kind, Ratio> &lhs, + const Unit<Kind, Ratio> &rhs) { lhs = lhs - rhs; return lhs; } -template <class DerivedUnit> -constexpr DerivedUnit &operator*=(DerivedUnit &lhs, float rhs) +template <UnitKind Kind, class Ratio> +constexpr Unit<Kind, Ratio> &operator*=(Unit<Kind, Ratio> &lhs, float rhs) { lhs = lhs * rhs; return lhs; } -template <class DerivedUnit> -constexpr DerivedUnit &operator/=(DerivedUnit &lhs, float rhs) +template <UnitKind Kind, class Ratio> +constexpr Unit<Kind, Ratio> &operator/=(Unit<Kind, Ratio> &lhs, float rhs) { lhs = lhs / rhs; return lhs;