-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathoptional.hpp
165 lines (142 loc) · 3.83 KB
/
optional.hpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#pragma once
#include <lager/lenses.hpp>
#include <lager/util.hpp>
#include <zug/compose.hpp>
#include <zug/meta/detected.hpp>
#include <zug/meta/util.hpp>
#include <optional>
#include <type_traits>
#include <utility>
namespace lager {
namespace lenses {
namespace detail {
template <template <typename> typename PartMeta, typename Lens>
auto opt_impl(Lens&& lens)
{
return zug::comp([lens = std::forward<Lens>(lens)](auto&& f) {
return [&, f = LAGER_FWD(f)](auto&& whole) {
using Part = typename PartMeta<std::decay_t<decltype(::lager::view(
lens,
std::declval<std::decay_t<decltype(whole.value())>>()))>>::type;
if (whole.has_value()) {
return f(Part{::lager::view(lens, LAGER_FWD(whole).value())})(
[&](Part part) {
if (part.has_value()) {
return std::decay_t<decltype(whole)>{
::lager::set(lens,
LAGER_FWD(whole).value(),
std::move(part).value())};
} else {
return LAGER_FWD(whole);
}
});
} else {
return f(Part{std::nullopt})(
[&](auto&&) { return LAGER_FWD(whole); });
}
};
});
}
template <typename T>
struct remove_opt
{
using type = T;
};
template <typename T>
struct remove_opt<std::optional<T>>
{
using type = T;
};
template <typename T>
using remove_opt_t = typename remove_opt<T>::type;
template <typename T>
struct to_opt
{
using type = std::optional<remove_opt_t<std::decay_t<T>>>;
};
template <typename T>
struct add_opt
{
using type = std::optional<std::decay_t<T>>;
};
template <typename T>
struct is_optional : std::false_type
{};
template <typename T>
struct is_optional<std::optional<T>> : std::true_type
{};
} // namespace detail
//! @defgroup lenses
//! @{
/*!
* `Lens<W, P> -> Lens<[W], [P]>`
*/
template <typename Lens>
auto map_opt(Lens&& lens)
{
return detail::opt_impl<detail::add_opt>(std::forward<Lens>(lens));
}
/*!
* `Lens<W, [P]> -> Lens<[W], [P]>`
*/
template <typename Lens>
auto bind_opt(Lens&& lens)
{
return detail::opt_impl<zug::meta::identity>(std::forward<Lens>(lens));
}
/*!
* `(Lens<W, P> | Lens<W, [P]>) -> Lens<[W], [P]>`
*/
template <typename Lens>
auto with_opt(Lens&& lens)
{
return detail::opt_impl<detail::to_opt>(std::forward<Lens>(lens));
}
/*!
* `X -> Lens<[X], X>`
*/
template <typename T>
auto value_or(T&& t)
{
return zug::comp([t = std::forward<T>(t)](auto&& f) {
return [&, f = LAGER_FWD(f)](auto&& whole) {
return f(LAGER_FWD(whole).value_or(t))(
[&](auto&& x) { return LAGER_FWD(x); });
};
});
}
/*!
* `() -> Lens<[X], X>`
*/
ZUG_INLINE_CONSTEXPR auto value_or()
{
return zug::comp([](auto&& f) {
return [&, f = LAGER_FWD(f)](auto&& whole) {
using T = std::decay_t<decltype(whole.value())>;
return f(LAGER_FWD(whole).value_or(T{}))(
[&](auto&& x) { return LAGER_FWD(x); });
};
});
}
/*!
* `() -> Lens<[X], X>`
*/
ZUG_INLINE_CONSTEXPR auto or_default = value_or();
/*!
* `Lens<T, [T]>`
*/
ZUG_INLINE_CONSTEXPR auto force_opt = zug::comp([](auto&& f) {
return [f = LAGER_FWD(f)](auto&& p) {
using opt_t = std::optional<std::decay_t<decltype(p)>>;
auto opt = opt_t{LAGER_FWD(p)};
return f(std::move(opt))([&](auto&& x) -> decltype(auto) {
if constexpr (detail::is_optional<std::decay_t<decltype(x)>>::value)
return LAGER_FWD(x).value_or(*std::move(opt));
else
return LAGER_FWD(x);
});
};
});
//! @}
} // namespace lenses
} // namespace lager