aboutsummaryrefslogtreecommitdiff
path: root/src/optional.hpp
blob: 5e1c6300591d6a0e56c04c217762a0f95c925cc3 (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
37
38
39
40
41
42
43
44
45
46
47
/**
 * @file optional.hpp
 * @author Felix Morgner (felix.morgner@gmail.com)
 * @since 1.0.0
 */

#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();
}

} // namespace wanda::std_ext

#endif