result: add some more functionality
[staging/windowmanager.git] / src / result.hpp
1 //
2 // Created by mfritzsc on 7/12/17.
3 //
4
5 #ifndef TMCAGLWM_RESULT_HPP
6 #define TMCAGLWM_RESULT_HPP
7
8 #include <experimental/optional>
9 #include <functional>
10
11 namespace wm {
12
13 using std::experimental::optional;
14 using std::experimental::nullopt;
15
16 // We only ever return a string as an error - so just parametrize
17 // this over result type T
18 template <typename T>
19 struct result {
20    char const *e;
21    optional<T> t;
22
23    bool is_ok() const { return this->t != nullopt; }
24    bool is_err() const { return this->e != nullptr; }
25
26    T unwrap() {
27       if (this->e != nullptr) {
28          throw std::logic_error(this->e);
29       }
30       return this->t.value();
31    }
32
33    operator T() {
34       return this->unwrap();
35    }
36
37    char const *unwrap_err() { return this->e; }
38
39    optional<T> ok() const { return this->t; }
40    optional<char const *> err() const { return optional<char const *>(this->e); }
41
42    template <typename U>
43    result<U> map(std::function<U(T)> f);
44
45    result<T> map_err(std::function<char const *(char const *)> f);
46 };
47
48 template <typename T>
49 struct result<T> Err(char const *e) {
50    return result<T>{e, nullopt};
51 }
52
53 template <typename T>
54 struct result<T> Ok(T t) {
55    return result<T>{nullptr, t};
56 }
57
58 template <typename T>
59 template <typename U>
60 result<U> result<T>::map(std::function<U(T)> f) {
61    if (this->is_ok()) {
62       return Ok<U>(f(this->unwrap()));
63    }
64    return Err<U>(this->e);
65 }
66
67 template <typename T>
68 result<T> result<T>::map_err(std::function<char const *(char const *)> f) {
69    if (this->is_err()) {
70       return Err<T>(f(this->e));
71    }
72    return *this;
73 }
74
75 }  // namespace wm
76
77 #endif  // TMCAGLWM_RESULT_HPP