aboutsummaryrefslogtreecommitdiff
path: root/src/optional.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/optional.hpp')
-rw-r--r--src/optional.hpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/optional.hpp b/src/optional.hpp
new file mode 100644
index 0000000..c178d59
--- /dev/null
+++ b/src/optional.hpp
@@ -0,0 +1,36 @@
+#ifndef WANDA_OPTIONAL_HPP
+#define WANDA_OPTIONAL_HPP
+
+#include <optional>
+
+namespace wanda::std_ext {
+
+struct failable {
+
+ constexpr static auto success() { return failable{false}; }
+ constexpr static auto failure() { return failable{true}; }
+
+ template<typename Handler>
+ constexpr auto operator ||(Handler handler) const {
+ if(m_failed) {
+ handler();
+ }
+ }
+
+private:
+ constexpr explicit failable(bool failed) : m_failed{failed} { };
+ bool const m_failed;
+};
+
+template<typename ObjectType, typename HandlerType>
+auto with(std::optional<ObjectType> && object, HandlerType handler) {
+ if(object) {
+ handler(object.value());
+ return failable::success();
+ }
+ return failable::failure();
+}
+
+}
+
+#endif \ No newline at end of file