From: Marcus Fritzsch Date: Thu, 27 Jul 2017 13:50:10 +0000 (+0200) Subject: result: add some more functionality X-Git-Tag: 4.99.1~232 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=commitdiff_plain;h=f618aa82d6c5dbfc815b4f5f79c75325c9ee7544;p=staging%2Fwindowmanager.git result: add some more functionality Signed-off-by: Marcus Fritzsch --- diff --git a/src/result.hpp b/src/result.hpp index e22fde9..4768d9f 100644 --- a/src/result.hpp +++ b/src/result.hpp @@ -6,6 +6,7 @@ #define TMCAGLWM_RESULT_HPP #include +#include namespace wm { @@ -34,6 +35,14 @@ struct result { } char const *unwrap_err() { return this->e; } + + optional ok() const { return this->t; } + optional err() const { return optional(this->e); } + + template + result map(std::function f); + + result map_err(std::function f); }; template @@ -46,6 +55,23 @@ struct result Ok(T t) { return result{nullptr, t}; } +template +template +result result::map(std::function f) { + if (this->is_ok()) { + return Ok(f(this->unwrap())); + } + return Err(this->e); +} + +template +result result::map_err(std::function f) { + if (this->is_err()) { + return Err(f(this->e)); + } + return *this; +} + } // namespace wm #endif // TMCAGLWM_RESULT_HPP