-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvariant.hpp
191 lines (168 loc) · 5.63 KB
/
kvariant.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// KTL single-header library
// Requirements: C 20
#pragma once
#include "fixed_any.hpp"
#include "koverloaded.hpp"
namespace ktl {
namespace detail {
template <typename... T>
struct largest_sizeof;
template <typename... Types>
static constexpr std::size_t largest_sizeof_v = largest_sizeof<Types...>::value;
template <typename Head, typename...>
struct head_of {
using type = Head;
};
template <typename... Types>
using head_of_t = typename head_of<Types...>::type;
template <typename Target, typename... Types>
struct is_type_in;
template <typename Target, typename... Types>
static constexpr bool is_type_in_v = is_type_in<Target, Types...>::value;
} // namespace detail
///
/// \brief Simple sum-type variant/union of Types...
///
template <typename... Types>
requires(sizeof...(Types) > 0)
class kvariant {
template <typename V>
using visit_ret_t = std::invoke_result_t<V, detail::head_of_t<Types...>>;
template <typename V>
static constexpr bool visitable_v = (std::is_invocable_v<V, Types> && ...);
public:
using vtable_t = fixed_any_vtable;
static constexpr std::size_t size_v = detail::largest_sizeof_v<Types...>;
// clang-format off
template <typename T = detail::head_of_t<Types...>>
requires(detail::is_type_in_v<T, Types...>)
kvariant(T t = T{}) noexcept(std::is_nothrow_move_constructible_v<T>) { m_storage = std::move(t); }
// clang-format on
///
/// \brief Check which type is currently held
///
template <typename T>
requires(detail::is_type_in_v<T, Types...>)
bool contains() const noexcept { return m_storage.template contains<T>(); }
///
/// \brief Obtain a const pointer to the desired value type if held
///
template <typename T>
requires(detail::is_type_in_v<T, Types...>)
T const* get_if() const noexcept;
///
/// \brief Obtain a const pointer to the desired value type if held
///
template <typename T>
requires(detail::is_type_in_v<T, Types...>)
T* get_if() noexcept;
///
/// \brief Obtain a const lvalue reference to the desired value type (assumed held)
///
template <typename T>
requires(detail::is_type_in_v<T, Types...>)
T const& get() const& noexcept;
///
/// \brief Obtain an lvalue reference to the desired value type (assumed held)
///
template <typename T>
requires(detail::is_type_in_v<T, Types...>)
T& get() & noexcept;
///
/// \brief Obtain an rvalue reference to the desired value type (assumed held)
///
template <typename T>
requires(detail::is_type_in_v<T, Types...>)
T && get() && noexcept;
vtable_t const* vtable() const { return m_storage.vtable(); }
///
/// \brief Visitor for Types...
///
// clang-format off
template <typename Visitor>
requires(visitable_v<Visitor>)
visit_ret_t<Visitor> visit(Visitor&& visitor) const { return visit<Types...>(m_storage, std::forward<Visitor>(visitor)); }
template <typename Visitor>
requires(visitable_v<Visitor>)
visit_ret_t<Visitor> visit(Visitor&& visitor) { return visit<Types...>(m_storage, std::forward<Visitor>(visitor)); }
private:
template <typename T, typename... Ts, typename Any, typename Visitor>
static constexpr visit_ret_t<Visitor> visit(Any&& any, Visitor&& visitor) noexcept;
fixed_any<size_v> m_storage;
// clang-format on
};
// impl
namespace detail {
template <typename T>
constexpr T largest(T const a, T const b) noexcept {
return a < b ? b : a;
}
template <typename T>
struct largest_sizeof<T> {
static constexpr std::size_t value = sizeof(T);
};
template <typename T, typename... U>
struct largest_sizeof<T, U...> {
static constexpr std::size_t value = largest(sizeof(T), largest_sizeof<U...>::value);
};
template <typename Target, typename T>
struct is_type_in<Target, T> {
static constexpr bool value = std::is_same_v<Target, T>;
};
template <typename Target, typename T, typename... Types>
struct is_type_in<Target, T, Types...> {
static constexpr bool value = is_type_in<Target, T>::value || is_type_in<Target, Types...>::value;
};
} // namespace detail
template <typename... Types>
requires(sizeof...(Types) > 0)
template <typename T>
requires(detail::is_type_in_v<T, Types...>)
T const* kvariant<Types...>::get_if() const noexcept {
if (m_storage.template contains<T>()) { return &m_storage.template get<T>(); }
return nullptr;
}
template <typename... Types>
requires(sizeof...(Types) > 0)
template <typename T>
requires(detail::is_type_in_v<T, Types...>)
T* kvariant<Types...>::get_if() noexcept {
if (m_storage.template contains<T>()) { return &m_storage.template get<T>(); }
return nullptr;
}
template <typename... Types>
requires(sizeof...(Types) > 0)
template <typename T>
requires(detail::is_type_in_v<T, Types...>)
T const& kvariant<Types...>::get() const& noexcept {
auto ret = get_if<T>();
assert(ret);
return *ret;
}
template <typename... Types>
requires(sizeof...(Types) > 0)
template <typename T>
requires(detail::is_type_in_v<T, Types...>)
T& kvariant<Types...>::get() & noexcept {
auto ret = get_if<T>();
assert(ret);
return *ret;
}
template <typename... Types>
requires(sizeof...(Types) > 0)
template <typename T>
requires(detail::is_type_in_v<T, Types...>)
T && kvariant<Types...>::get() && noexcept {
auto ret = get_if<T>();
assert(ret);
return std::move(*ret);
}
template <typename... Types>
requires(sizeof...(Types) > 0)
template <typename T, typename... Ts, typename Any, typename Visitor>
constexpr auto kvariant<Types...>::visit(Any&& any, Visitor&& visitor) noexcept -> visit_ret_t<Visitor> {
if (any.template contains<T>()) { return visitor(any.template get<T>()); }
if constexpr (sizeof...(Ts) > 0) { return visit<Ts...>(std::forward<Any>(any), std::forward<Visitor>(visitor)); }
if constexpr (!std::is_void_v<visit_ret_t<Visitor>>) { return visit_ret_t<Visitor>{}; }
}
} // namespace ktl