aboutsummaryrefslogtreecommitdiff
path: root/src/optional.hpp
blob: c178d59a0aca50ff3f7b3762864fdb3fb5bdd937 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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