-
Notifications
You must be signed in to change notification settings - Fork 0
/
magicgetruntime.h
175 lines (142 loc) · 6.23 KB
/
magicgetruntime.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
#pragma once
#include "pfr.hpp"
// #include "boost/type_index.hpp"
#include <QVariant>
#include <utility>
// #include <vcruntime.h>
using index_t = size_t;
template <typename T>
inline constexpr index_t pod_size_v = pfr::tuple_size<std::decay_t<T>>::value;
// #define __TEMPLATES_RECURSION__
// #define __VARIADIC_TEMPLATES_UNPACKING_IF__
// #define __VARIADIC_TEMPLATES_UNPACKING_ARRAY_FUNC__
#define __VARIADIC_TEMPLATES_UNPACKING_ARRAY_VIRT__
#ifdef __TEMPLATES_RECURSION__ // 175.828 ms 99
////////////////////////////////////////////////////////////////////
///
///
template <index_t I>
struct get_visit_impl {
template <typename T, typename F>
constexpr static void visit(T& pod, const index_t idx, F fun) {
(idx == I - 1) ? fun(boost::pfr::get<I - 1>(pod))
: get_visit_impl<I - 1>::visit(pod, idx, fun);
}
template <typename T>
constexpr static auto get(T& pod, const index_t idx) {
return (idx == I - 1) ? boost::pfr::get<I - 1>(pod)
: get_visit_impl<I - 1>::get(pod, idx);
}
};
template <>
struct get_visit_impl<0> {
// clang-format off
template <typename T, typename F>
static void visit(T&, index_t, F) { assert(false); }
template <typename T>
static QVariant get(T&, index_t) { assert(false); return {}; }
// clang-format on
};
template <typename F, typename T>
constexpr void visit_at(T& pod, const index_t idx, F fun) { get_visit_impl<pod_size_v<T>>::visit(pod, idx, fun); }
template <typename F, typename T>
constexpr void visit_at(const T& pod, const index_t idx, F fun) { get_visit_impl<pod_size_v<T>>::visit(pod, idx, fun); }
template <typename T>
constexpr auto get_at(T& pod, const index_t idx) { return get_visit_impl<pod_size_v<T>>::get(pod, idx); }
template <typename T>
constexpr auto get_at(const T& pod, const index_t idx) { return get_visit_impl<pod_size_v<T>>::get(pod, idx); }
#elif defined(__VARIADIC_TEMPLATES_UNPACKING_IF__) // 138.9 ms 99
////////////////////////////////////////////////////////////////////
///
///
template <typename T, typename F, index_t... I>
void visit_impl(T& pod, const index_t idx, F fun, std::index_sequence<I...>) {
((I == idx ? fun(boost::pfr::get<I>(pod)) : void()), ...);
}
template <typename T, index_t... I>
QVariant get_impl(T&& pod, const index_t idx, std::index_sequence<I...>) {
QVariant ret;
return ((I == idx ? ret = boost::pfr::get<I>(pod) : ret), ..., ret);
}
template <typename F, typename T, typename Indices = std::make_index_sequence<pod_size_v<T>>>
constexpr void visit_at(T& pod, const index_t idx, F fun) { visit_impl(pod, idx, fun, Indices{}); }
template <typename F, typename T, typename Indices = std::make_index_sequence<pod_size_v<T>>>
constexpr void visit_at(const T& pod, const index_t idx, F fun) { visit_impl(pod, idx, fun, Indices{}); }
// template <typename T, typename Indices = std::make_index_sequence<pod_size_v<T>>>
// constexpr auto get_at(T& pod, const index_t idx) { return get_impl(pod, idx, Indices {}); }
template <typename T, typename Indices = std::make_index_sequence<pod_size_v<T>>>
constexpr auto get_at(const T& pod, const index_t idx) { return get_impl(pod, idx, Indices{}); }
#elif defined(__VARIADIC_TEMPLATES_UNPACKING_ARRAY_FUNC__) // 122.626 ms 99
////////////////////////////////////////////////////////////////////
///
///
namespace detail {
template <index_t I>
struct getter final {
template <typename T>
auto operator()(T&& pod) { return (boost::pfr::get<I>(pod)); }
};
template <typename T, index_t... Is>
struct array {
static inline std::function<QVariant(T)> data[] = {getter<Is>()...};
};
template <typename T, index_t... I>
auto get_impl(T&& pod, const index_t idx, std::index_sequence<I...>) {
return array<T, I...>::data[idx](pod);
}
template <typename T, typename F, index_t... I>
void visit_impl(T&& pod, const index_t idx, F fun, std::index_sequence<I...>) {
((I == idx ? fun(boost::pfr::get<I>(pod)) : void()), ...);
}
} // namespace detail
template <typename F, typename T, typename Indices = std::make_index_sequence<pod_size_v<T>>>
constexpr void visit_at(T&& pod, const index_t idx, F fun) {
detail::visit_impl(pod, idx, fun, Indices{});
}
template <typename F, typename T, typename Indices = std::make_index_sequence<pod_size_v<T>>>
constexpr void visit_at(const T& pod, const index_t idx, F fun) {
detail::visit_impl(pod, idx, fun, Indices{});
}
template <typename T, typename Indices = std::make_index_sequence<pod_size_v<T>>>
constexpr auto get_at(T&& pod, const index_t idx) { return detail::get_impl(pod, idx, Indices{}); }
template <typename T, typename Indices = std::make_index_sequence<pod_size_v<T>>>
constexpr auto get_at(const T& pod, const index_t idx) { return detail::get_impl(pod, idx, Indices{}); }
#elif defined(__VARIADIC_TEMPLATES_UNPACKING_ARRAY_VIRT__) // 116.363 ms 99
////////////////////////////////////////////////////////////////////
template <typename T>
QVariant toVariant(const T& val) {
return {}; // QVariant::fromValue(val);
}
namespace detail {
template <typename T>
struct getter1 {
virtual QVariant get(T&& pod) = 0;
};
template <index_t I, typename T>
struct getter final : getter1<T> {
QVariant get(T&& pod) override { return toVariant(pfr::get<I>(std::forward<T>(pod))); }
};
template <typename T, index_t... Is>
struct array {
static constexpr size_t size = sizeof(getter<0, T>);
static inline char ptr[size * sizeof...(Is)] = {};
static inline getter1<T>* data2[] = {new(ptr + Is * size) getter<Is, T>()...};
};
template <typename T, index_t... I>
auto get_impl(T&& pod, const index_t idx, std::index_sequence<I...>) {
return array<T, I...>::data2[idx]->get(std::forward<T>(pod));
}
template <typename T, typename F, index_t... I>
void visit_impl(T&& pod, const index_t idx, F fun, std::index_sequence<I...>) {
((I == idx ? fun(pfr::get<I>(std::forward<T>(pod))) : void()), ...);
}
} // namespace detail
template <typename F, typename T>
constexpr void visit_at(T&& pod, const index_t idx, F fun) {
detail::visit_impl(std::forward<T>(pod), idx, fun, std::make_index_sequence<pod_size_v<T>>{});
}
template <typename T>
constexpr auto get_at(T&& pod, const index_t idx) {
return detail::get_impl(std::forward<T>(pod), idx, std::make_index_sequence<pod_size_v<T>>{});
}
#endif