diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2018-11-24 20:38:48 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2018-11-24 20:38:48 +0100 |
| commit | e70f0b4de81d24d22a29a2af03c669368fce6af2 (patch) | |
| tree | f91dfd02434ce460e6120c7747aa0092cbd2327f /src/optional.hpp | |
| download | wanda-e70f0b4de81d24d22a29a2af03c669368fce6af2.tar.xz wanda-e70f0b4de81d24d22a29a2af03c669368fce6af2.zip | |
wanda: initial commit
Diffstat (limited to 'src/optional.hpp')
| -rw-r--r-- | src/optional.hpp | 36 |
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 |
