From 4c86214a30654528273da34bd2612fc150ac7830 Mon Sep 17 00:00:00 2001 From: Davide Basso <davide.basso@skywarder.eu> Date: Tue, 12 Dec 2023 20:03:38 +0100 Subject: [PATCH] [Units] Add UnitKind --- src/shared/units/Length.h | 5 +- src/shared/units/Pressure.h | 5 +- src/shared/units/Time.h | 4 +- src/shared/units/Units.h | 103 ++++++++++++++++++++---------------- 4 files changed, 61 insertions(+), 56 deletions(-) diff --git a/src/shared/units/Length.h b/src/shared/units/Length.h index 4408326e4..6afec19cc 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 8f5e76b81..ac65bf249 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 a56760b9b..41c7bb44b 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 20499a3b6..64e37e9e8 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; -- GitLab