2012-08-22 23:33:52 +02:00
|
|
|
#ifndef units_hh_INCLUDED
|
|
|
|
#define units_hh_INCLUDED
|
|
|
|
|
2016-04-22 21:48:42 +02:00
|
|
|
#include "assert.hh"
|
2014-12-16 19:57:19 +01:00
|
|
|
#include "hash.hh"
|
|
|
|
|
2013-01-14 18:51:45 +01:00
|
|
|
#include <type_traits>
|
2023-01-21 01:27:05 +01:00
|
|
|
#include <compare>
|
2013-01-14 18:51:45 +01:00
|
|
|
|
2012-08-22 23:33:52 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename RealType, typename ValueType = int>
|
2013-01-14 18:51:45 +01:00
|
|
|
class StronglyTypedNumber
|
2012-08-22 23:33:52 +02:00
|
|
|
{
|
|
|
|
public:
|
2017-01-29 14:49:45 +01:00
|
|
|
StronglyTypedNumber() = default;
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2013-01-14 18:51:45 +01:00
|
|
|
explicit constexpr StronglyTypedNumber(ValueType value)
|
|
|
|
: m_value(value)
|
|
|
|
{
|
|
|
|
static_assert(std::is_base_of<StronglyTypedNumber, RealType>::value,
|
|
|
|
"RealType is not derived from StronglyTypedNumber");
|
|
|
|
}
|
2012-08-22 23:33:52 +02:00
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2015-05-13 22:30:23 +02:00
|
|
|
constexpr friend RealType operator+(RealType lhs, RealType rhs)
|
|
|
|
{ return RealType(lhs.m_value + rhs.m_value); }
|
2012-08-22 23:33:52 +02:00
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2015-05-13 22:30:23 +02:00
|
|
|
constexpr friend RealType operator-(RealType lhs, RealType rhs)
|
|
|
|
{ return RealType(lhs.m_value - rhs.m_value); }
|
2012-08-22 23:33:52 +02:00
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2015-05-13 22:30:23 +02:00
|
|
|
constexpr friend RealType operator*(RealType lhs, RealType rhs)
|
|
|
|
{ return RealType(lhs.m_value * rhs.m_value); }
|
2012-08-22 23:33:52 +02:00
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2015-05-13 22:30:23 +02:00
|
|
|
constexpr friend RealType operator/(RealType lhs, RealType rhs)
|
|
|
|
{ return RealType(lhs.m_value / rhs.m_value); }
|
2012-08-22 23:33:52 +02:00
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2013-07-26 00:49:04 +02:00
|
|
|
RealType& operator+=(RealType other)
|
2012-08-22 23:33:52 +02:00
|
|
|
{ m_value += other.m_value; return static_cast<RealType&>(*this); }
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2013-07-26 00:49:04 +02:00
|
|
|
RealType& operator-=(RealType other)
|
2012-08-22 23:33:52 +02:00
|
|
|
{ m_value -= other.m_value; return static_cast<RealType&>(*this); }
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2013-07-26 00:49:04 +02:00
|
|
|
RealType& operator*=(RealType other)
|
2012-08-22 23:33:52 +02:00
|
|
|
{ m_value *= other.m_value; return static_cast<RealType&>(*this); }
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2013-07-26 00:49:04 +02:00
|
|
|
RealType& operator/=(RealType other)
|
2012-08-22 23:33:52 +02:00
|
|
|
{ m_value /= other.m_value; return static_cast<RealType&>(*this); }
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2012-08-22 23:33:52 +02:00
|
|
|
RealType& operator++()
|
|
|
|
{ ++m_value; return static_cast<RealType&>(*this); }
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2012-08-22 23:33:52 +02:00
|
|
|
RealType& operator--()
|
|
|
|
{ --m_value; return static_cast<RealType&>(*this); }
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2012-08-22 23:33:52 +02:00
|
|
|
RealType operator++(int)
|
2012-10-02 14:19:45 +02:00
|
|
|
{ RealType backup(static_cast<RealType&>(*this)); ++m_value; return backup; }
|
2012-08-22 23:33:52 +02:00
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2012-08-22 23:33:52 +02:00
|
|
|
RealType operator--(int)
|
2012-10-02 14:19:45 +02:00
|
|
|
{ RealType backup(static_cast<RealType&>(*this)); --m_value; return backup; }
|
2012-08-22 23:33:52 +02:00
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2013-11-11 20:11:17 +01:00
|
|
|
constexpr RealType operator-() const { return RealType(-m_value); }
|
2012-08-22 23:33:52 +02:00
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2015-05-13 22:30:23 +02:00
|
|
|
constexpr friend RealType operator%(RealType lhs, RealType rhs)
|
|
|
|
{ return RealType(lhs.m_value % rhs.m_value); }
|
2013-11-06 20:10:15 +01:00
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2013-11-11 20:11:17 +01:00
|
|
|
RealType& operator%=(RealType other)
|
2013-11-06 20:10:15 +01:00
|
|
|
{ m_value %= other.m_value; return static_cast<RealType&>(*this); }
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2021-11-21 10:06:14 +01:00
|
|
|
constexpr friend bool operator==(RealType lhs, RealType rhs)
|
2015-05-13 22:30:23 +02:00
|
|
|
{ return lhs.m_value == rhs.m_value; }
|
2012-08-22 23:33:52 +02:00
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2020-11-14 22:57:19 +01:00
|
|
|
constexpr friend auto operator<=>(RealType lhs, RealType rhs)
|
|
|
|
{ return lhs.m_value <=> rhs.m_value; }
|
2012-08-22 23:33:52 +02:00
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2012-09-04 23:54:10 +02:00
|
|
|
constexpr bool operator!() const
|
2015-12-28 00:28:34 +01:00
|
|
|
{ return not m_value; }
|
2012-08-23 23:56:35 +02:00
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2012-09-04 23:54:10 +02:00
|
|
|
explicit constexpr operator ValueType() const { return m_value; }
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2012-10-02 14:18:34 +02:00
|
|
|
explicit constexpr operator bool() const { return m_value; }
|
2015-03-11 14:59:25 +01:00
|
|
|
|
2017-10-17 16:45:17 +02:00
|
|
|
friend constexpr size_t hash_value(RealType val) { return hash_value(val.m_value); }
|
|
|
|
friend constexpr size_t abs(RealType val) { return val.m_value < ValueType(0) ? -val.m_value : val.m_value; }
|
2015-03-11 14:59:25 +01:00
|
|
|
|
2016-04-22 21:48:42 +02:00
|
|
|
explicit operator size_t() { kak_assert(m_value >= 0); return (size_t)m_value; }
|
|
|
|
|
|
|
|
protected:
|
2013-11-14 01:12:15 +01:00
|
|
|
ValueType m_value;
|
2012-08-22 23:33:52 +02:00
|
|
|
};
|
|
|
|
|
2013-01-14 18:51:45 +01:00
|
|
|
struct LineCount : public StronglyTypedNumber<LineCount, int>
|
2012-08-22 23:33:52 +02:00
|
|
|
{
|
2017-01-29 14:49:45 +01:00
|
|
|
LineCount() = default;
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2017-01-29 14:49:45 +01:00
|
|
|
constexpr LineCount(int value) : StronglyTypedNumber<LineCount>(value) {}
|
2012-08-22 23:33:52 +02:00
|
|
|
};
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2012-09-04 23:54:10 +02:00
|
|
|
inline constexpr LineCount operator"" _line(unsigned long long int value)
|
2012-08-22 23:33:52 +02:00
|
|
|
{
|
|
|
|
return LineCount(value);
|
|
|
|
}
|
|
|
|
|
2013-01-14 18:51:45 +01:00
|
|
|
struct ByteCount : public StronglyTypedNumber<ByteCount, int>
|
2012-10-11 00:13:31 +02:00
|
|
|
{
|
2017-01-29 14:49:45 +01:00
|
|
|
ByteCount() = default;
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2017-01-29 14:49:45 +01:00
|
|
|
constexpr ByteCount(int value) : StronglyTypedNumber<ByteCount>(value) {}
|
2012-10-11 00:13:31 +02:00
|
|
|
};
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2012-10-11 00:13:31 +02:00
|
|
|
inline constexpr ByteCount operator"" _byte(unsigned long long int value)
|
|
|
|
{
|
|
|
|
return ByteCount(value);
|
|
|
|
}
|
|
|
|
|
2022-12-15 03:29:45 +01:00
|
|
|
template<typename Byte>
|
|
|
|
requires (std::is_same_v<std::remove_cv_t<Byte>, char> or std::is_same_v<std::remove_cv_t<Byte>, void>)
|
|
|
|
Byte* operator+(Byte* ptr, ByteCount count) { return ptr + (int)count; }
|
|
|
|
|
2013-01-14 18:51:45 +01:00
|
|
|
struct CharCount : public StronglyTypedNumber<CharCount, int>
|
2012-08-23 23:56:35 +02:00
|
|
|
{
|
2017-01-29 14:49:45 +01:00
|
|
|
CharCount() = default;
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2017-01-29 14:49:45 +01:00
|
|
|
constexpr CharCount(int value) : StronglyTypedNumber<CharCount>(value) {}
|
2012-08-23 23:56:35 +02:00
|
|
|
};
|
|
|
|
|
2014-05-17 11:17:28 +02:00
|
|
|
[[gnu::always_inline]]
|
2012-09-04 23:54:10 +02:00
|
|
|
inline constexpr CharCount operator"" _char(unsigned long long int value)
|
2012-08-23 23:56:35 +02:00
|
|
|
{
|
|
|
|
return CharCount(value);
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
struct ColumnCount : public StronglyTypedNumber<ColumnCount, int>
|
|
|
|
{
|
2017-01-29 14:49:45 +01:00
|
|
|
ColumnCount() = default;
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
[[gnu::always_inline]]
|
2017-01-29 14:49:45 +01:00
|
|
|
constexpr ColumnCount(int value) : StronglyTypedNumber<ColumnCount>(value) {}
|
2016-09-22 21:36:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline constexpr ColumnCount operator"" _col(unsigned long long int value)
|
|
|
|
{
|
|
|
|
return ColumnCount(value);
|
|
|
|
}
|
|
|
|
|
2012-08-22 23:33:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // units_hh_INCLUDED
|