diff --git a/src/coord.hh b/src/coord.hh index daa8bee0..f60be779 100644 --- a/src/coord.hh +++ b/src/coord.hh @@ -12,14 +12,17 @@ struct LineAndColumn LineType line; ColumnType column; + [[gnu::always_inline]] constexpr LineAndColumn(LineType line = 0, ColumnType column = 0) : line(line), column(column) {} + [[gnu::always_inline]] constexpr EffectiveType operator+(EffectiveType other) const { return EffectiveType(line + other.line, column + other.column); } + [[gnu::always_inline]] EffectiveType& operator+=(EffectiveType other) { line += other.line; @@ -27,11 +30,13 @@ struct LineAndColumn return *static_cast(this); } + [[gnu::always_inline]] constexpr EffectiveType operator-(EffectiveType other) const { return EffectiveType(line - other.line, column - other.column); } + [[gnu::always_inline]] EffectiveType& operator-=(EffectiveType other) { line -= other.line; @@ -39,35 +44,41 @@ struct LineAndColumn return *static_cast(this); } + [[gnu::always_inline]] constexpr bool operator< (EffectiveType other) const { return (line != other.line) ? line < other.line : column < other.column; } + [[gnu::always_inline]] constexpr bool operator<= (EffectiveType other) const { return (line != other.line) ? line < other.line : column <= other.column; } + [[gnu::always_inline]] constexpr bool operator> (EffectiveType other) const { return (line != other.line) ? line > other.line : column > other.column; } + [[gnu::always_inline]] constexpr bool operator>= (EffectiveType other) const { return (line != other.line) ? line > other.line : column >= other.column; } + [[gnu::always_inline]] constexpr bool operator== (EffectiveType other) const { return line == other.line and column == other.column; } + [[gnu::always_inline]] constexpr bool operator!= (EffectiveType other) const { return line != other.line or column != other.column; @@ -76,12 +87,14 @@ struct LineAndColumn struct ByteCoord : LineAndColumn { + [[gnu::always_inline]] constexpr ByteCoord(LineCount line = 0, ByteCount column = 0) : LineAndColumn(line, column) {} }; struct CharCoord : LineAndColumn { + [[gnu::always_inline]] constexpr CharCoord(LineCount line = 0, CharCount column = 0) : LineAndColumn(line, column) {} }; diff --git a/src/units.hh b/src/units.hh index 430971e8..69ecffdb 100644 --- a/src/units.hh +++ b/src/units.hh @@ -10,6 +10,7 @@ template class StronglyTypedNumber { public: + [[gnu::always_inline]] explicit constexpr StronglyTypedNumber(ValueType value) : m_value(value) { @@ -17,72 +18,96 @@ public: "RealType is not derived from StronglyTypedNumber"); } + [[gnu::always_inline]] constexpr RealType operator+(RealType other) const { return RealType(m_value + other.m_value); } + [[gnu::always_inline]] constexpr RealType operator-(RealType other) const { return RealType(m_value - other.m_value); } + [[gnu::always_inline]] constexpr RealType operator*(RealType other) const { return RealType(m_value * other.m_value); } + [[gnu::always_inline]] constexpr RealType operator/(RealType other) const { return RealType(m_value / other.m_value); } + [[gnu::always_inline]] RealType& operator+=(RealType other) { m_value += other.m_value; return static_cast(*this); } + [[gnu::always_inline]] RealType& operator-=(RealType other) { m_value -= other.m_value; return static_cast(*this); } + [[gnu::always_inline]] RealType& operator*=(RealType other) { m_value *= other.m_value; return static_cast(*this); } + [[gnu::always_inline]] RealType& operator/=(RealType other) { m_value /= other.m_value; return static_cast(*this); } + [[gnu::always_inline]] RealType& operator++() { ++m_value; return static_cast(*this); } + [[gnu::always_inline]] RealType& operator--() { --m_value; return static_cast(*this); } + [[gnu::always_inline]] RealType operator++(int) { RealType backup(static_cast(*this)); ++m_value; return backup; } + [[gnu::always_inline]] RealType operator--(int) { RealType backup(static_cast(*this)); --m_value; return backup; } + [[gnu::always_inline]] constexpr RealType operator-() const { return RealType(-m_value); } + [[gnu::always_inline]] constexpr RealType operator%(RealType other) const { return RealType(m_value % other.m_value); } + [[gnu::always_inline]] RealType& operator%=(RealType other) { m_value %= other.m_value; return static_cast(*this); } + [[gnu::always_inline]] constexpr bool operator==(RealType other) const { return m_value == other.m_value; } + [[gnu::always_inline]] constexpr bool operator!=(RealType other) const { return m_value != other.m_value; } + [[gnu::always_inline]] constexpr bool operator<(RealType other) const { return m_value < other.m_value; } + [[gnu::always_inline]] constexpr bool operator<=(RealType other) const { return m_value <= other.m_value; } + [[gnu::always_inline]] constexpr bool operator>(RealType other) const { return m_value > other.m_value; } + [[gnu::always_inline]] constexpr bool operator>=(RealType other) const { return m_value >= other.m_value; } + [[gnu::always_inline]] constexpr bool operator!() const { return !m_value; } + [[gnu::always_inline]] explicit constexpr operator ValueType() const { return m_value; } + [[gnu::always_inline]] explicit constexpr operator bool() const { return m_value; } private: ValueType m_value; @@ -90,9 +115,11 @@ private: struct LineCount : public StronglyTypedNumber { + [[gnu::always_inline]] constexpr LineCount(int value = 0) : StronglyTypedNumber(value) {} }; +[[gnu::always_inline]] inline constexpr LineCount operator"" _line(unsigned long long int value) { return LineCount(value); @@ -100,9 +127,11 @@ inline constexpr LineCount operator"" _line(unsigned long long int value) struct ByteCount : public StronglyTypedNumber { + [[gnu::always_inline]] constexpr ByteCount(int value = 0) : StronglyTypedNumber(value) {} }; +[[gnu::always_inline]] inline constexpr ByteCount operator"" _byte(unsigned long long int value) { return ByteCount(value); @@ -110,9 +139,11 @@ inline constexpr ByteCount operator"" _byte(unsigned long long int value) struct CharCount : public StronglyTypedNumber { + [[gnu::always_inline]] constexpr CharCount(int value = 0) : StronglyTypedNumber(value) {} }; +[[gnu::always_inline]] inline constexpr CharCount operator"" _char(unsigned long long int value) { return CharCount(value);