summaryrefslogtreecommitdiff
path: root/either.h
blob: 33808a0d4ad2c15dd6445a3e85557ee20adea2e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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;
	}
};