From e70f0b4de81d24d22a29a2af03c669368fce6af2 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 24 Nov 2018 20:38:48 +0100 Subject: wanda: initial commit --- src/optional.hpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/optional.hpp (limited to 'src/optional.hpp') 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 + +namespace wanda::std_ext { + +struct failable { + + constexpr static auto success() { return failable{false}; } + constexpr static auto failure() { return failable{true}; } + + template + constexpr auto operator ||(Handler handler) const { + if(m_failed) { + handler(); + } + } + +private: + constexpr explicit failable(bool failed) : m_failed{failed} { }; + bool const m_failed; +}; + +template +auto with(std::optional && object, HandlerType handler) { + if(object) { + handler(object.value()); + return failable::success(); + } + return failable::failure(); +} + +} + +#endif \ No newline at end of file -- cgit v1.2.3