1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-22 11:01:48 +03:00
kakoune/src/units.hh

118 lines
3.4 KiB
C++
Raw Normal View History

#ifndef units_hh_INCLUDED
#define units_hh_INCLUDED
#include <type_traits>
namespace Kakoune
{
template<typename RealType, typename ValueType = int>
class StronglyTypedNumber
{
public:
explicit constexpr StronglyTypedNumber(ValueType value)
: m_value(value)
{
static_assert(std::is_base_of<StronglyTypedNumber, RealType>::value,
"RealType is not derived from StronglyTypedNumber");
}
2012-09-05 01:54:10 +04:00
constexpr RealType operator+(const RealType& other) const
{ return RealType(m_value + other.m_value); }
2012-09-05 01:54:10 +04:00
constexpr RealType operator-(const RealType& other) const
{ return RealType(m_value - other.m_value); }
2012-09-05 01:54:10 +04:00
constexpr RealType operator*(const RealType& other) const
{ return RealType(m_value * other.m_value); }
2012-09-05 01:54:10 +04:00
constexpr RealType operator/(const RealType& other) const
{ return RealType(m_value / other.m_value); }
RealType& operator+=(const RealType& other)
{ m_value += other.m_value; return static_cast<RealType&>(*this); }
RealType& operator-=(const RealType& other)
{ m_value -= other.m_value; return static_cast<RealType&>(*this); }
RealType& operator*=(const RealType& other)
{ m_value *= other.m_value; return static_cast<RealType&>(*this); }
RealType& operator/=(const RealType& other)
{ m_value /= other.m_value; return static_cast<RealType&>(*this); }
RealType& operator++()
{ ++m_value; return static_cast<RealType&>(*this); }
RealType& operator--()
{ --m_value; return static_cast<RealType&>(*this); }
RealType operator++(int)
{ RealType backup(static_cast<RealType&>(*this)); ++m_value; return backup; }
RealType operator--(int)
{ RealType backup(static_cast<RealType&>(*this)); --m_value; return backup; }
2012-09-05 01:54:10 +04:00
constexpr RealType operator-() { return RealType(-m_value); }
2012-09-05 01:54:10 +04:00
constexpr bool operator==(const RealType& other) const
{ return m_value == other.m_value; }
2012-09-05 01:54:10 +04:00
constexpr bool operator!=(const RealType& other) const
{ return m_value != other.m_value; }
2012-09-05 01:54:10 +04:00
constexpr bool operator<(const RealType& other) const
{ return m_value < other.m_value; }
2012-09-05 01:54:10 +04:00
constexpr bool operator<=(const RealType& other) const
{ return m_value <= other.m_value; }
2012-09-05 01:54:10 +04:00
constexpr bool operator>(const RealType& other) const
{ return m_value > other.m_value; }
2012-09-05 01:54:10 +04:00
constexpr bool operator>=(const RealType& other) const
{ return m_value >= other.m_value; }
2012-09-05 01:54:10 +04:00
constexpr bool operator!() const
{ return !m_value; }
2012-09-05 01:54:10 +04:00
explicit constexpr operator ValueType() const { return m_value; }
explicit constexpr operator bool() const { return m_value; }
private:
ValueType m_value;
};
struct LineCount : public StronglyTypedNumber<LineCount, int>
{
constexpr LineCount(int value = 0) : StronglyTypedNumber<LineCount>(value) {}
};
2012-09-05 01:54:10 +04:00
inline constexpr LineCount operator"" _line(unsigned long long int value)
{
return LineCount(value);
}
struct ByteCount : public StronglyTypedNumber<ByteCount, int>
2012-10-11 02:13:31 +04:00
{
constexpr ByteCount(int value = 0) : StronglyTypedNumber<ByteCount>(value) {}
2012-10-11 02:13:31 +04:00
};
inline constexpr ByteCount operator"" _byte(unsigned long long int value)
{
return ByteCount(value);
}
struct CharCount : public StronglyTypedNumber<CharCount, int>
{
constexpr CharCount(int value = 0) : StronglyTypedNumber<CharCount>(value) {}
};
2012-09-05 01:54:10 +04:00
inline constexpr CharCount operator"" _char(unsigned long long int value)
{
return CharCount(value);
}
}
#endif // units_hh_INCLUDED