clang-format, mostly indenting.
[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
10 namespace wm {
11
12 using std::experimental::optional;
13 using std::experimental::nullopt;
14
15 // We only ever return a string as an error - so just parametrize
16 // this over result type T
17 template <typename T>
18 struct result {
19    char const *e;
20    optional<T> t;
21
22    bool is_ok() const { return this->t != nullopt; }
23    bool is_err() const { return this->e != nullptr; }
24
25    T unwrap() { return this->t.value(); }
26
27    char const *unwrap_err() { return this->e; }
28 };
29
30 template <typename T>
31 struct result<T> Err(char const *e) {
32    return result<T>{e, nullopt};
33 }
34
35 template <typename T>
36 struct result<T> Ok(T t) {
37    return result<T>{nullptr, t};
38 }
39
40 }  // namespace wm
41
42 #endif  // TMCAGLWM_RESULT_HPP