Orange/include/Orange/Math/Unit.h

114 lines
2.9 KiB
C
Raw Normal View History

2022-08-12 21:18:11 +01:00
#pragma once
namespace orange
{
template <template <typename> class Derived, typename T>
class Unit
{
template <template <typename> class, typename>
friend class Unit;
public:
using Type = T;
2022-08-13 12:07:07 +01:00
constexpr Unit()
2022-08-12 21:18:11 +01:00
: m_value{T{0}}
{
}
2022-08-13 12:07:07 +01:00
constexpr explicit Unit(T value)
2022-08-12 21:18:11 +01:00
: m_value{value}
{
}
template <typename U>
2022-08-13 12:07:07 +01:00
constexpr explicit Unit(Unit<Derived, U> value)
2022-08-12 21:18:11 +01:00
: m_value{T{value.m_value}}
{
}
2022-08-13 12:07:07 +01:00
constexpr explicit operator T() const { return m_value; }
2022-08-12 21:18:11 +01:00
2022-08-13 12:07:07 +01:00
constexpr bool operator==(Unit<Derived, T> other) const
2022-08-12 21:18:11 +01:00
{
return m_value == other.m_value;
}
2022-08-13 12:07:07 +01:00
constexpr bool operator!=(Unit<Derived, T> other) const { return !operator==(other); }
2022-08-12 21:18:11 +01:00
2022-08-13 12:07:07 +01:00
constexpr bool operator<(Unit<Derived, T> other) const
2022-08-12 21:18:11 +01:00
{
return m_value < other.m_value;
}
2022-08-13 12:07:07 +01:00
constexpr bool operator>(Unit<Derived, T> other) const
2022-08-12 21:18:11 +01:00
{
return m_value > other.m_value;
}
2022-08-13 12:07:07 +01:00
constexpr bool operator<=(Unit<Derived, T> other) const { return !operator>(other); }
2022-08-12 21:18:11 +01:00
2022-08-13 12:07:07 +01:00
constexpr bool operator>=(Unit<Derived, T> other) const { return !operator<(other); }
2022-08-12 21:18:11 +01:00
2022-08-13 12:07:07 +01:00
constexpr Unit<Derived, T> operator-() const { return Unit<Derived, T>{-m_value}; }
2022-08-12 21:18:11 +01:00
2022-08-13 12:07:07 +01:00
constexpr Unit<Derived, T>& operator+=(Unit<Derived, T> other)
2022-08-12 21:18:11 +01:00
{
m_value += other.m_value;
return *this;
}
2022-08-13 12:07:07 +01:00
constexpr Unit<Derived, T> operator+(Unit<Derived, T> other) const
2022-08-12 21:18:11 +01:00
{
return Unit<Derived, T>{m_value + other.m_value};
}
2022-08-13 12:07:07 +01:00
constexpr Unit<Derived, T>& operator-=(Unit<Derived, T> other)
2022-08-12 21:18:11 +01:00
{
m_value -= other.m_value;
return *this;
}
2022-08-13 12:07:07 +01:00
constexpr Unit<Derived, T> operator-(Unit<Derived, T> other) const
2022-08-12 21:18:11 +01:00
{
return Unit<Derived, T>{m_value - other.m_value};
}
2022-08-13 12:07:07 +01:00
constexpr Unit<Derived, T>& operator*=(T number)
2022-08-12 21:18:11 +01:00
{
m_value *= number;
return *this;
}
2022-08-13 12:07:07 +01:00
constexpr Unit<Derived, T> operator*(T number) const
2022-08-12 21:18:11 +01:00
{
return Unit<Derived, T>{m_value * number};
}
2022-08-13 12:07:07 +01:00
constexpr Unit<Derived, T>& operator/=(T number)
2022-08-12 21:18:11 +01:00
{
m_value /= number;
return *this;
}
2022-08-13 12:07:07 +01:00
constexpr Unit<Derived, T> operator/(T number) const
2022-08-12 21:18:11 +01:00
{
return Unit<Derived, T>{m_value / number};
}
2022-08-13 12:07:07 +01:00
constexpr T operator/(Unit<Derived, T> other) const { return m_value / other.value; }
2022-08-12 21:18:11 +01:00
private:
T m_value;
};
template <template <typename> class Derived, typename T>
2022-08-13 12:07:07 +01:00
constexpr Unit<Derived, T> operator*(typename std::common_type<T>::type number,
2022-08-12 21:18:11 +01:00
const Unit<Derived, T>& value)
{
return value * number;
}
}