use more constexpr

This commit is contained in:
Maxime Coste 2012-09-04 23:54:10 +02:00
parent 63f467081a
commit aac30a27e7
5 changed files with 42 additions and 46 deletions

View File

@ -19,11 +19,11 @@ class Window;
struct BufferCoord : LineAndColumn<BufferCoord>
{
BufferCoord(LineCount line = 0, CharCount column = 0)
constexpr BufferCoord(LineCount line = 0, CharCount column = 0)
: LineAndColumn(line, column) {}
template<typename T>
explicit BufferCoord(const LineAndColumn<T>& other)
explicit constexpr BufferCoord(const LineAndColumn<T>& other)
: LineAndColumn(other.line, other.column) {}
};

View File

@ -12,11 +12,11 @@ namespace Kakoune
struct DisplayCoord : LineAndColumn<DisplayCoord>
{
DisplayCoord(LineCount line = 0, CharCount column = 0)
constexpr DisplayCoord(LineCount line = 0, CharCount column = 0)
: LineAndColumn(line, column) {}
template<typename T>
explicit DisplayCoord(const LineAndColumn<T>& other)
explicit constexpr DisplayCoord(const LineAndColumn<T>& other)
: LineAndColumn(other.line, other.column) {}
};

View File

@ -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; }
};

View File

@ -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<EffectiveType*>(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<EffectiveType*>(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;
}

View File

@ -8,19 +8,19 @@ template<typename RealType, typename ValueType = int>
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>
{
LineCount(int value) : StronglyTypedInteger<LineCount>(value) {}
constexpr LineCount(int value) : StronglyTypedInteger<LineCount>(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>
{
CharCount(int value) : StronglyTypedInteger<CharCount>(value) {}
constexpr CharCount(int value) : StronglyTypedInteger<CharCount>(value) {}
};
inline CharCount operator"" _char(unsigned long long int value)
inline constexpr CharCount operator"" _char(unsigned long long int value)
{
return CharCount(value);
}