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();
33 operator T() { return this->unwrap(); }
35 char const *unwrap_err() { return this->e; }
37 optional<T> const &ok() const { return this->t; }
38 optional<char const *> err() const {
39 return this->e ? optional<char const *>(this->e) : nullopt;
42 result<T> map_err(std::function<char const *(char const *)> f);
46 struct result<T> Err(char const *e) {
47 return result<T>{e, nullopt};
51 struct result<T> Ok(T t) {
52 return result<T>{nullptr, t};
56 result<T> result<T>::map_err(std::function<char const *(char const *)> f) {
58 return Err<T>(f(this->e));
65 #endif // TMCAGLWM_RESULT_HPP