-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNamedParameter.h
executable file
·233 lines (177 loc) · 6.38 KB
/
NamedParameter.h
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifndef AETHER_NAMED_PARAMETER_H
#define AETHER_NAMED_PARAMETER_H
#include <utility>
#include <boost/hana.hpp>
#include "aether/typelist/TypeList.h"
#include "aether/typelist/Contains.h"
#include "aether/typelist/IndexOf.h"
namespace aether {
using namespace boost;
template <typename T>
struct named_parameter {
using Name = T;
template <typename V>
constexpr auto operator=(V&&) const;
};
template <typename N, typename T>
struct bound_named_parameter {
using Name = N;
using result_type = T;
T value;
constexpr Name GetName() const {
return {};
}
const T& Value() const {
return value;
}
T& Value() {
return value;
}
};
template <typename N, typename T>
struct named_field {
using Name = N;
using Type = T;
};
template <typename Keys, typename Store>
struct named_struct {
Store store{};
using K = Keys;
template <typename Name>
constexpr const auto& operator[](Name) const {
static_assert(contains(Name{}, Keys{}), "Field not found in named_struct");
constexpr auto index = index_of(Name{}, Keys{});
return hana::at_c<index>(store);
}
template <typename Name>
auto& operator[](Name) {
static_assert(contains(Name{}, Keys{}), "Field not found in named_struct");
constexpr auto index = index_of(Name{}, Keys{});
return hana::at_c<index>(store);
}
template <typename Name>
constexpr bool HasKey(Name) const {
return contains(Name{}, Keys{});
}
};
using empty_named_struct = named_struct<TypeSet<>, hana::tuple<>>;
template <typename Name, typename T>
struct HasKey : std::false_type {};
template <typename Name, typename Keys, typename Store>
struct HasKey<Name, named_struct<Keys, Store>> {
static constexpr bool value = contains(Name{}, Keys{});
};
template <typename Name, typename A, typename B, EnableIf<!HasKey<Name, A>::value || !HasKey<Name, B>::value> = 0>
constexpr bool TypesMatch() {
return false;
};
template <typename Name, typename A, typename B, EnableIf<HasKey<Name, A>::value && HasKey<Name, B>::value> = 0>
constexpr bool TypesMatch() {
return std::is_same<
decltype(std::declval<typename A::Store>()[std::declval<Name>()])
, decltype(std::declval<typename B::Store>()[std::declval<Name>()])
>::value;
};
template <typename K, typename To, typename From, EnableIf<!TypesMatch<K, To, From>()> = 0>
void copy_fields_single(K, To&, const From&) {
}
template <typename K, typename To, typename From, EnableIf<TypesMatch<K, To, From>()> = 0>
void copy_fields_single(K, To& to, const From& from) {
constexpr auto key = K{};
to[key] = from[key];
}
template <typename... Ks, typename To, typename From>
void copy_fields_impl(TypeSet<Ks...>, To& to, const From& from) {
using consume = int[];
(void)consume{1, (copy_fields_single(Ks{}, to, from), 1)...};
}
template <typename To, typename From>
void copy_fields(To& to, const From& from) {
copy_fields_impl(to.Keys(), to, from);
}
template <typename T, typename Name>
constexpr auto field(Name) {
return named_field<Name, T>{};
}
template <typename... Vs>
constexpr auto make_named_struct(Vs...) {
return named_struct<TypeSet<typename Vs::Name...>, hana::tuple<typename Vs::Type...>>{};
}
template <typename Name, typename T>
constexpr auto make_bound_named_parameter(Name, T&& t) {
return bound_named_parameter<Name, std::decay_t<T>>{std::forward<T>(t)};
}
template <typename T>
template <typename V>
constexpr auto named_parameter<T>::operator=(V&& v) const {
return bound_named_parameter<T, std::decay_t<V>>{std::forward<V>(v)};
}
template <typename Name, typename Tuple>
struct has_name_impl {
static constexpr bool value = false;
};
template <typename Name, typename T, typename... Ts>
struct has_name_impl<Name, TypeList<bound_named_parameter<Name, T>, Ts...>> {
static constexpr bool value = true;
};
template <typename Name, typename T, typename... Ts>
struct has_name_impl<Name, TypeList<T, Ts...>> {
static constexpr bool value = has_name_impl<Name, TypeList<Ts...>>::value;
};
template <typename Name, typename... Ts>
struct has_name_impl<Name, hana::tuple<Ts...>> {
static constexpr bool value = has_name_impl<Name, TypeList<Ts...>>::value;
};
template <typename Name, typename Tuple>
struct has_name {
static constexpr bool value = has_name_impl<Name, Tuple>::value;
};
template <std::size_t I, typename Name, typename Tuple>
struct index_of_name_impl;
template <std::size_t I, typename Name>
struct index_of_name_impl<I, Name, TypeList<>> {
static_assert(!std::is_same<Name, Name>::value, "Named parameter not found.");
};
template <std::size_t I, typename Name, typename T, typename... Ts>
struct index_of_name_impl<I, Name, TypeList<bound_named_parameter<Name, T>, Ts...>> {
static constexpr std::size_t value = I;
};
template <std::size_t I, typename Name, typename T, typename... Ts>
struct index_of_name_impl<I, Name, TypeList<T, Ts...>> {
static constexpr std::size_t value = index_of_name_impl<I + 1, Name, TypeList<Ts...>>::value;
};
template <typename Name, typename... Ts>
struct index_of_name_impl<0, Name, hana::tuple<Ts...>> {
static constexpr std::size_t value = index_of_name_impl<0, Name, TypeList<Ts...>>::value;
};
template <typename Name, typename Tuple>
struct index_of_name {
static constexpr std::size_t value = index_of_name_impl<0, Name, Tuple>::value;
};
template <std::size_t I, typename Name, typename Tuple>
struct type_of_name_impl;
template <std::size_t I, typename Name>
struct type_of_name_impl<I, Name, TypeList<>> {
static_assert(!std::is_same<Name, Name>::value, "Named parameter not found.");
};
template <std::size_t I, typename Name, typename T, typename... Ts>
struct type_of_name_impl<I, Name, TypeList<bound_named_parameter<Name, T>, Ts...>> {
using type = T;
};
template <std::size_t I, typename Name, typename T, typename... Ts>
struct type_of_name_impl<I, Name, TypeList<T, Ts...>> {
using type = typename type_of_name_impl<I + 1, Name, TypeList<Ts...>>::type;
};
template <typename Name, typename... Ts>
struct type_of_name_impl<0, Name, hana::tuple<Ts...>> {
using type = typename type_of_name_impl<0, Name, TypeList<Ts...>>::type;
};
template <typename Name, typename Tuple>
struct type_of_name {
using type = typename type_of_name_impl<0, Name, Tuple>::type;
};
template <typename Name, typename Tuple>
using type_of_name_t = typename type_of_name<Name, Tuple>::type;
#define NAMED_PARAM(name) struct name##_t {}; constexpr named_parameter<name##_t> name##_{};
} // end namespace aether
#endif