2 // Created by mfritzsc on 7/12/17.
5 #ifndef TMCAGLWM_RESULT_HPP
6 #define TMCAGLWM_RESULT_HPP
8 #include <experimental/optional>
13 using std::experimental::optional;
14 using std::experimental::nullopt;
16 // We only ever return a string as an error - so just parametrize
17 // this over result type T
23 bool is_ok() const { return this->t != nullopt; }
24 bool is_err() const { return this->e != nullptr; }
27 if (this->e != nullptr) {
28 throw std::logic_error(this->e);
30 return this->t.value();
34 return this->unwrap();
37 char const *unwrap_err() { return this->e; }
39 optional<T> ok() const { return this->t; }
40 optional<char const *> err() const { return optional<char const *>(this->e); }
43 result<U> map(std::function<U(T)> f);
45 result<T> map_err(std::function<char const *(char const *)> f);
49 struct result<T> Err(char const *e) {
50 return result<T>{e, nullopt};
54 struct result<T> Ok(T t) {
55 return result<T>{nullptr, t};
60 result<U> result<T>::map(std::function<U(T)> f) {
62 return Ok<U>(f(this->unwrap()));
64 return Err<U>(this->e);
68 result<T> result<T>::map_err(std::function<char const *(char const *)> f) {
70 return Err<T>(f(this->e));
77 #endif // TMCAGLWM_RESULT_HPP