summaryrefslogtreecommitdiffstats
path: root/src/result.hpp
diff options
context:
space:
mode:
authorMarcus Fritzsch <marcus_fritzsch@mentor.com>2017-07-27 15:50:10 +0200
committerMarcus Fritzsch <marcus_fritzsch@mentor.com>2017-08-08 17:24:00 +0200
commitf618aa82d6c5dbfc815b4f5f79c75325c9ee7544 (patch)
tree7f8b9238f7ba3880e5158b524eaeea7fcd44eb23 /src/result.hpp
parent462202c1187be467b8f002c8eaec1a3d42d01a1a (diff)
result: add some more functionality
Signed-off-by: Marcus Fritzsch <marcus_fritzsch@mentor.com>
Diffstat (limited to 'src/result.hpp')
-rw-r--r--src/result.hpp26
1 files changed, 26 insertions, 0 deletions
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 <experimental/optional>
+#include <functional>
namespace wm {
@@ -34,6 +35,14 @@ struct result {
}
char const *unwrap_err() { return this->e; }
+
+ optional<T> ok() const { return this->t; }
+ optional<char const *> err() const { return optional<char const *>(this->e); }
+
+ template <typename U>
+ result<U> map(std::function<U(T)> f);
+
+ result<T> map_err(std::function<char const *(char const *)> f);
};
template <typename T>
@@ -46,6 +55,23 @@ struct result<T> Ok(T t) {
return result<T>{nullptr, t};
}
+template <typename T>
+template <typename U>
+result<U> result<T>::map(std::function<U(T)> f) {
+ if (this->is_ok()) {
+ return Ok<U>(f(this->unwrap()));
+ }
+ return Err<U>(this->e);
+}
+
+template <typename T>
+result<T> result<T>::map_err(std::function<char const *(char const *)> f) {
+ if (this->is_err()) {
+ return Err<T>(f(this->e));
+ }
+ return *this;
+}
+
} // namespace wm
#endif // TMCAGLWM_RESULT_HPP