summaryrefslogtreecommitdiff
path: root/either.h
diff options
context:
space:
mode:
Diffstat (limited to 'either.h')
-rw-r--r--either.h43
1 files changed, 43 insertions, 0 deletions
diff --git a/either.h b/either.h
new file mode 100644
index 0000000..33808a0
--- /dev/null
+++ b/either.h
@@ -0,0 +1,43 @@
+#pragma once
+
+struct Left{};
+struct Right{};
+
+template <typename T,typename U>
+class Either{
+ T *left;
+ U *right;
+
+public:
+ Either(Left,const T &l)
+ :left(new T(l)),right(nullptr){}
+
+ Either(Right,const U &r)
+ :left(nullptr),right(new U(r)){}
+
+ Either(const Either<T,U> &other){
+ if(other.left)left=new T(*other.left);
+ if(other.right)right=new U(*other.right);
+ }
+
+ ~Either(void){
+ if(left)delete left;
+ if(right)delete right;
+ }
+
+ T fromLeft(void) const {
+ return *left;
+ }
+
+ U fromRight(void) const {
+ return *right;
+ }
+
+ bool isLeft(void) const {
+ return (bool)left;
+ }
+
+ bool isRight(void) const {
+ return (bool)right;
+ }
+};