summaryrefslogtreecommitdiff
path: root/maybe.h
diff options
context:
space:
mode:
Diffstat (limited to 'maybe.h')
-rw-r--r--maybe.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/maybe.h b/maybe.h
new file mode 100644
index 0000000..95a26b8
--- /dev/null
+++ b/maybe.h
@@ -0,0 +1,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>();
+ }
+};