summaryrefslogtreecommitdiff
path: root/src/units.hh
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2012-08-22 23:33:52 +0200
committerMaxime Coste <frrrwww@gmail.com>2012-08-22 23:33:52 +0200
commit0d8cce272831cd896d0e69d7c86cc9afc521eb11 (patch)
treed60ec676564cf7fbec9ae2b3b9b1f6d6d7902d0b /src/units.hh
parentc6e8080426b24271a23f68a697b5a6944811a4bc (diff)
use a strongly typed int LineCount for line counts
Diffstat (limited to 'src/units.hh')
-rw-r--r--src/units.hh88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/units.hh b/src/units.hh
new file mode 100644
index 00000000..8a1180de
--- /dev/null
+++ b/src/units.hh
@@ -0,0 +1,88 @@
+#ifndef units_hh_INCLUDED
+#define units_hh_INCLUDED
+
+namespace Kakoune
+{
+
+template<typename RealType, typename ValueType = int>
+class StronglyTypedInteger
+{
+public:
+ explicit StronglyTypedInteger(ValueType value)
+ : m_value(value) {}
+
+ RealType operator+(const RealType& other) const
+ { return RealType(m_value + other.m_value); }
+
+ RealType operator-(const RealType& other) const
+ { return RealType(m_value - other.m_value); }
+
+ RealType operator*(const RealType& other) const
+ { return RealType(m_value * other.m_value); }
+
+ 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(*this); ++m_value; return backup; }
+
+ RealType operator--(int)
+ { RealType backup(*this); --m_value; return backup; }
+
+ RealType operator-() { return RealType(-m_value); }
+
+ bool operator==(const RealType& other) const
+ { return m_value == other.m_value; }
+
+ bool operator!=(const RealType& other) const
+ { return m_value != other.m_value; }
+
+ bool operator<(const RealType& other) const
+ { return m_value < other.m_value; }
+
+ bool operator<=(const RealType& other) const
+ { return m_value <= other.m_value; }
+
+ bool operator>(const RealType& other) const
+ { return m_value > other.m_value; }
+
+ bool operator>=(const RealType& other) const
+ { return m_value >= other.m_value; }
+
+ explicit operator ValueType() const { return m_value; }
+private:
+ ValueType m_value;
+};
+
+struct LineCount : public StronglyTypedInteger<LineCount, int>
+{
+ LineCount(int value) : StronglyTypedInteger<LineCount>(value) {}
+};
+
+inline LineCount operator"" _line(unsigned long long int value)
+{
+ return LineCount(value);
+}
+
+}
+
+#endif // units_hh_INCLUDED
+