瀏覽代碼

Add skills for +, -, unary -, *, /, ++, and --.

Sam Jaffe 5 年之前
父節點
當前提交
213a23f3ad
共有 2 個文件被更改,包括 58 次插入1 次删除
  1. 56 0
      include/opaque_typedef/arithmatic.hpp
  2. 2 1
      include/opaque_typedef/opaque_typedef.hpp

+ 56 - 0
include/opaque_typedef/arithmatic.hpp

@@ -0,0 +1,56 @@
+#pragma once
+
+namespace types {
+  template <typename Self>
+  struct Incrementable {
+    friend Self operator++(Self const & self, int) {
+      Self copy = self;
+      self = Self(self.get() + 1);
+      return copy;
+    }
+    friend Self & operator++(Self const & self) {
+      return self = Self(self.get() + 1);
+    }
+  };
+
+  template <typename Self>
+  struct Decrementable {
+    friend Self operator--(Self const & self, int) {
+      Self copy = self;
+      self = Self(self.get() - 1);
+      return copy;
+    }
+    friend Self & operator--(Self const & self) {
+      return self = Self(self.get() - 1);
+    }
+  };
+
+  template <typename Self>
+  struct Addable {
+    friend Self operator+(Self const & lhs, Self const & rhs) {
+      return Self(lhs.get() + rhs.get());
+    }
+  };
+    
+  template <typename Self>
+  struct Arithmatic : Addable<Self> {
+    friend Self operator-(Self const & self) {
+      return Self(-self.get());
+    }
+    
+    friend Self operator-(Self const & lhs, Self const & rhs) {
+      return Self(lhs.get() - rhs.get());
+    }
+  };
+
+  template <typename Self>
+  struct Numeric : Arithmatic<Self> {
+    friend Self operator*(Self const & lhs, Self const & rhs) {
+      return Self(lhs.get() * rhs.get());
+    }
+    
+    friend Self operator/(Self const & lhs, Self const & rhs) {
+      return Self(lhs.get() / rhs.get());
+    }
+  };
+}

+ 2 - 1
include/opaque_typedef/opaque_typedef.hpp

@@ -9,6 +9,7 @@
 
 #pragma once
 
+#include "arithmatic.hpp"
 #include "comparable.hpp"
 
 namespace types {
@@ -26,5 +27,5 @@ namespace types {
     
     Base const & get() const { return value_; }
     explicit operator Base const &() const { return value_; }
-  };
+  };  
 }