Generating binding API glue code using generate-binding.py
[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) { return result<T>{e, nullopt}; }
32
33     template<typename T>
34     struct result<T> Ok(T t) { return result<T>{nullptr, t}; }
35
36 } // namespace wm
37
38 #endif //TMCAGLWM_RESULT_HPP