From aac30a27e70054799beb0c137e28c9dc9656efec Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 4 Sep 2012 23:54:10 +0200 Subject: [PATCH] use more constexpr --- src/buffer.hh | 4 ++-- src/display_buffer.hh | 4 ++-- src/keys.hh | 4 ++-- src/line_and_column.hh | 40 ++++++++++++++++++---------------------- src/units.hh | 36 ++++++++++++++++++------------------ 5 files changed, 42 insertions(+), 46 deletions(-) diff --git a/src/buffer.hh b/src/buffer.hh index 7f3f95ac..9a5aea1f 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -19,11 +19,11 @@ class Window; struct BufferCoord : LineAndColumn { - BufferCoord(LineCount line = 0, CharCount column = 0) + constexpr BufferCoord(LineCount line = 0, CharCount column = 0) : LineAndColumn(line, column) {} template - explicit BufferCoord(const LineAndColumn& other) + explicit constexpr BufferCoord(const LineAndColumn& other) : LineAndColumn(other.line, other.column) {} }; diff --git a/src/display_buffer.hh b/src/display_buffer.hh index 110b6be2..2fcca28c 100644 --- a/src/display_buffer.hh +++ b/src/display_buffer.hh @@ -12,11 +12,11 @@ namespace Kakoune struct DisplayCoord : LineAndColumn { - DisplayCoord(LineCount line = 0, CharCount column = 0) + constexpr DisplayCoord(LineCount line = 0, CharCount column = 0) : LineAndColumn(line, column) {} template - explicit DisplayCoord(const LineAndColumn& other) + explicit constexpr DisplayCoord(const LineAndColumn& other) : LineAndColumn(other.line, other.column) {} }; diff --git a/src/keys.hh b/src/keys.hh index 0884276e..80593a57 100644 --- a/src/keys.hh +++ b/src/keys.hh @@ -20,10 +20,10 @@ struct Key Modifiers modifiers; Character key; - Key(Modifiers modifiers, Character key) + constexpr Key(Modifiers modifiers, Character key) : modifiers(modifiers), key(key) {} - bool operator==(const Key& other) const + constexpr bool operator==(const Key& other) const { return modifiers == other.modifiers and key == other.key; } }; diff --git a/src/line_and_column.hh b/src/line_and_column.hh index cceaa459..5a278a4f 100644 --- a/src/line_and_column.hh +++ b/src/line_and_column.hh @@ -12,22 +12,22 @@ struct LineAndColumn LineCount line; CharCount column; - LineAndColumn(LineCount line = 0, CharCount column = 0) + constexpr LineAndColumn(LineCount line = 0, CharCount column = 0) : line(line), column(column) {} - EffectiveType operator+(const EffectiveType& other) const + constexpr EffectiveType operator+(const EffectiveType& other) const { return EffectiveType(line + other.line, column + other.column); } - EffectiveType& operator+=(const EffectiveType& other) + constexpr EffectiveType& operator+=(const EffectiveType& other) { line += other.line; column += other.column; return *static_cast(this); } - EffectiveType operator-(const EffectiveType& other) const + constexpr EffectiveType operator-(const EffectiveType& other) const { return EffectiveType(line - other.line, column - other.column); } @@ -39,40 +39,36 @@ struct LineAndColumn return *static_cast(this); } - bool operator< (const EffectiveType& other) const + constexpr bool operator< (const EffectiveType& other) const { - if (line != other.line) - return line < other.line; - return column < other.column; + return (line != other.line) ? line < other.line + : column < other.column; } - bool operator<= (const EffectiveType& other) const + constexpr bool operator<= (const EffectiveType& other) const { - if (line != other.line) - return line < other.line; - return column <= other.column; + return (line != other.line) ? line < other.line + : column <= other.column; } - bool operator> (const EffectiveType& other) const + constexpr bool operator> (const EffectiveType& other) const { - if (line != other.line) - return line > other.line; - return column > other.column; + return (line != other.line) ? line > other.line + : column > other.column; } - bool operator>= (const EffectiveType& other) const + constexpr bool operator>= (const EffectiveType& other) const { - if (line != other.line) - return line > other.line; - return column >= other.column; + return (line != other.line) ? line > other.line + : column >= other.column; } - bool operator== (const EffectiveType& other) const + constexpr bool operator== (const EffectiveType& other) const { return line == other.line and column == other.column; } - bool operator!= (const EffectiveType& other) const + constexpr bool operator!= (const EffectiveType& other) const { return line != other.line or column != other.column; } diff --git a/src/units.hh b/src/units.hh index ca9eb39a..b9def254 100644 --- a/src/units.hh +++ b/src/units.hh @@ -8,19 +8,19 @@ template class StronglyTypedInteger { public: - explicit StronglyTypedInteger(ValueType value) + explicit constexpr StronglyTypedInteger(ValueType value) : m_value(value) {} - RealType operator+(const RealType& other) const + constexpr RealType operator+(const RealType& other) const { return RealType(m_value + other.m_value); } - RealType operator-(const RealType& other) const + constexpr RealType operator-(const RealType& other) const { return RealType(m_value - other.m_value); } - RealType operator*(const RealType& other) const + constexpr RealType operator*(const RealType& other) const { return RealType(m_value * other.m_value); } - RealType operator/(const RealType& other) const + constexpr RealType operator/(const RealType& other) const { return RealType(m_value / other.m_value); } RealType& operator+=(const RealType& other) @@ -47,50 +47,50 @@ public: RealType operator--(int) { RealType backup(*this); --m_value; return backup; } - RealType operator-() { return RealType(-m_value); } + constexpr RealType operator-() { return RealType(-m_value); } - bool operator==(const RealType& other) const + constexpr bool operator==(const RealType& other) const { return m_value == other.m_value; } - bool operator!=(const RealType& other) const + constexpr bool operator!=(const RealType& other) const { return m_value != other.m_value; } - bool operator<(const RealType& other) const + constexpr bool operator<(const RealType& other) const { return m_value < other.m_value; } - bool operator<=(const RealType& other) const + constexpr bool operator<=(const RealType& other) const { return m_value <= other.m_value; } - bool operator>(const RealType& other) const + constexpr bool operator>(const RealType& other) const { return m_value > other.m_value; } - bool operator>=(const RealType& other) const + constexpr bool operator>=(const RealType& other) const { return m_value >= other.m_value; } - bool operator!() const + constexpr bool operator!() const { return !m_value; } - explicit operator ValueType() const { return m_value; } + explicit constexpr operator ValueType() const { return m_value; } private: ValueType m_value; }; struct LineCount : public StronglyTypedInteger { - LineCount(int value) : StronglyTypedInteger(value) {} + constexpr LineCount(int value) : StronglyTypedInteger(value) {} }; -inline LineCount operator"" _line(unsigned long long int value) +inline constexpr LineCount operator"" _line(unsigned long long int value) { return LineCount(value); } struct CharCount : public StronglyTypedInteger { - CharCount(int value) : StronglyTypedInteger(value) {} + constexpr CharCount(int value) : StronglyTypedInteger(value) {} }; -inline CharCount operator"" _char(unsigned long long int value) +inline constexpr CharCount operator"" _char(unsigned long long int value) { return CharCount(value); }