From f618aa82d6c5dbfc815b4f5f79c75325c9ee7544 Mon Sep 17 00:00:00 2001 From: Marcus Fritzsch Date: Thu, 27 Jul 2017 15:50:10 +0200 Subject: [PATCH] result: add some more functionality Signed-off-by: Marcus Fritzsch --- src/result.hpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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 -- 2.16.6