Orange/include/Orange/Math/Unit.h

114 lines
2.9 KiB
C++

#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;
constexpr Unit()
: m_value{T{0}}
{
}
constexpr explicit Unit(T value)
: m_value{value}
{
}
template <typename U>
constexpr explicit Unit(Unit<Derived, U> value)
: m_value{T{value.m_value}}
{
}
constexpr explicit operator T() const { return m_value; }
constexpr bool operator==(Unit<Derived, T> other) const
{
return m_value == other.m_value;
}
constexpr bool operator!=(Unit<Derived, T> other) const { return !operator==(other); }
constexpr bool operator<(Unit<Derived, T> other) const
{
return m_value < other.m_value;
}
constexpr bool operator>(Unit<Derived, T> other) const
{
return m_value > other.m_value;
}
constexpr bool operator<=(Unit<Derived, T> other) const { return !operator>(other); }
constexpr bool operator>=(Unit<Derived, T> other) const { return !operator<(other); }
constexpr Unit<Derived, T> operator-() const { return Unit<Derived, T>{-m_value}; }
constexpr Unit<Derived, T>& operator+=(Unit<Derived, T> other)
{
m_value += other.m_value;
return *this;
}
constexpr Unit<Derived, T> operator+(Unit<Derived, T> other) const
{
return Unit<Derived, T>{m_value + other.m_value};
}
constexpr Unit<Derived, T>& operator-=(Unit<Derived, T> other)
{
m_value -= other.m_value;
return *this;
}
constexpr Unit<Derived, T> operator-(Unit<Derived, T> other) const
{
return Unit<Derived, T>{m_value - other.m_value};
}
constexpr Unit<Derived, T>& operator*=(T number)
{
m_value *= number;
return *this;
}
constexpr Unit<Derived, T> operator*(T number) const
{
return Unit<Derived, T>{m_value * number};
}
constexpr Unit<Derived, T>& operator/=(T number)
{
m_value /= number;
return *this;
}
constexpr Unit<Derived, T> operator/(T number) const
{
return Unit<Derived, T>{m_value / number};
}
constexpr T operator/(Unit<Derived, T> other) const { return m_value / other.value; }
private:
T m_value;
};
template <template <typename> class Derived, typename T>
constexpr Unit<Derived, T> operator*(typename std::common_type<T>::type number,
const Unit<Derived, T>& value)
{
return value * number;
}
}