X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;ds=inline;f=src%2Fresult.hpp;h=5701f243a754206e0261957c4a829433aa0076b3;hb=bd3363946030c35ed5b795d5b30d5d2a34aaad84;hp=e22fde9872260e05700d0b880277bacad54f6fad;hpb=5463a34f0136555cee0bfad9a1cb85e93aeb635c;p=staging%2Fwindowmanager.git diff --git a/src/result.hpp b/src/result.hpp index e22fde9..5701f24 100644 --- a/src/result.hpp +++ b/src/result.hpp @@ -6,6 +6,7 @@ #define TMCAGLWM_RESULT_HPP #include +#include namespace wm { @@ -29,11 +30,16 @@ struct result { return this->t.value(); } - operator T() { - return this->unwrap(); - } + operator T() { return this->unwrap(); } char const *unwrap_err() { return this->e; } + + optional const &ok() const { return this->t; } + optional err() const { + return this->e ? optional(this->e) : nullopt; + } + + result map_err(std::function f); }; template @@ -46,6 +52,14 @@ struct result Ok(T t) { return result{nullptr, t}; } +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