diff options
Diffstat (limited to 'src/result.hpp')
-rw-r--r-- | src/result.hpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/result.hpp b/src/result.hpp new file mode 100644 index 0000000..fe9956b --- /dev/null +++ b/src/result.hpp @@ -0,0 +1,38 @@ +// +// Created by mfritzsc on 7/12/17. +// + +#ifndef TMCAGLWM_RESULT_HPP +#define TMCAGLWM_RESULT_HPP + +#include <experimental/optional> + +namespace wm { + + using std::experimental::optional; + using std::experimental::nullopt; + + // We only ever return a string as an error - so just parametrize + // this over result type T + template<typename T> + struct result { + char const *e; + optional<T> t; + + bool is_ok() const { return this->t != nullopt; } + bool is_err() const { return this->e != nullptr; } + + T unwrap() { return this->t.value(); } + + char const *unwrap_err() { return this->e; } + }; + + template<typename T> + struct result<T> Err(char const *e) { return result<T>{e, nullopt}; } + + template<typename T> + struct result<T> Ok(T t) { return result<T>{nullptr, t}; } + +} // namespace wm + +#endif //TMCAGLWM_RESULT_HPP |