summaryrefslogtreecommitdiff
path: root/maybe.h
blob: 95a26b84a66548b0a5bb9b8c6e657fd86badabb6 (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
#pragma once

template <typename T>
class Maybe{
	T *value;

public:
	Maybe(void):value(NULL){} // 'Nothing' constructor
	Maybe(T v):value(new T(move(v))){}

	~Maybe(void){
		if(value)delete value;
	}

	T fromJust(void) const {
		return *value;
	}

	T fromMaybe(T &def) const {
		if(value)return *value;
		else return def;
	}

	bool isJust(void) const {
		return (bool)value;
	}

	bool isNothing(void) const {
		return !value;
	}

	static Maybe<T> Nothing(void){
		return Maybe<T>();
	}
};