-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtype_info.hpp
More file actions
201 lines (178 loc) · 5.86 KB
/
type_info.hpp
File metadata and controls
201 lines (178 loc) · 5.86 KB
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
#pragma once
#include <cassert>
#include <functional>
#include <type_traits>
namespace binsparse {
template <typename T>
struct type_info;
template <typename T>
requires(std::is_const_v<T> || std::is_volatile_v<T>)
struct type_info<T> {
static constexpr auto label() noexcept {
return type_info<std::remove_cv_t<T>>::label();
}
};
template <>
struct type_info<uint8_t> {
static constexpr auto label() noexcept {
return "uint8";
}
};
template <>
struct type_info<uint16_t> {
static constexpr auto label() noexcept {
return "uint16";
}
};
template <>
struct type_info<uint32_t> {
static constexpr auto label() noexcept {
return "uint32";
}
};
template <>
struct type_info<uint64_t> {
static constexpr auto label() noexcept {
return "uint64";
}
};
template <typename T>
requires std::is_same_v<T, std::size_t>
struct type_info<T> {
static constexpr auto label() noexcept {
return "uint64";
}
};
template <>
struct type_info<int8_t> {
static constexpr auto label() noexcept {
return "int8";
}
};
template <>
struct type_info<int16_t> {
static constexpr auto label() noexcept {
return "int16";
}
};
template <>
struct type_info<int32_t> {
static constexpr auto label() noexcept {
return "int32";
}
};
template <>
struct type_info<int64_t> {
static constexpr auto label() noexcept {
return "int64";
}
};
template <>
struct type_info<float> {
static constexpr auto label() noexcept {
return "float32";
}
};
template <>
struct type_info<double> {
static constexpr auto label() noexcept {
return "float64";
}
};
template <>
struct type_info<bool> {
static constexpr auto label() noexcept {
return "bint8";
}
};
namespace __detail {
template <typename Fn, typename... Args>
requires(std::is_invocable_v<Fn, Args...>)
void invoke_if_able(Fn&& fn, Args&&... args) {
std::invoke(std::forward<Fn>(fn), std::forward<Args>(args)...);
}
template <typename Fn, typename... Args>
void invoke_if_able(Fn&& fn, Args&&... args) {}
template <typename Fn, typename... Args>
void invoke_visit_fn_impl_(std::vector<std::string> type_labels, Fn&& fn,
Args&&... args) {
if constexpr (sizeof...(Args) <= 3) {
// The first label we consume is the value label
if (type_labels.size() == 3) {
auto type_label = type_labels.front();
if (type_label == "uint8") {
invoke_if_able(std::forward<Fn>(fn), std::uint8_t(),
std::forward<Args>(args)...);
} else if (type_label == "uint16") {
invoke_if_able(std::forward<Fn>(fn), std::uint16_t(),
std::forward<Args>(args)...);
} else if (type_label == "uint32") {
invoke_if_able(std::forward<Fn>(fn), std::uint32_t(),
std::forward<Args>(args)...);
} else if (type_label == "uint64") {
invoke_if_able(std::forward<Fn>(fn), std::uint64_t(),
std::forward<Args>(args)...);
} else if (type_label == "int8") {
invoke_if_able(std::forward<Fn>(fn), std::int8_t(),
std::forward<Args>(args)...);
} else if (type_label == "int16") {
invoke_if_able(std::forward<Fn>(fn), std::int16_t(),
std::forward<Args>(args)...);
} else if (type_label == "int32") {
invoke_if_able(std::forward<Fn>(fn), std::int32_t(),
std::forward<Args>(args)...);
} else if (type_label == "int64") {
invoke_if_able(std::forward<Fn>(fn), std::int64_t(),
std::forward<Args>(args)...);
} else if (type_label == "float32") {
invoke_if_able(std::forward<Fn>(fn), float(),
std::forward<Args>(args)...);
} else if (type_label == "float64") {
invoke_if_able(std::forward<Fn>(fn), double(),
std::forward<Args>(args)...);
} else if (type_label == "bint8") {
invoke_if_able(std::forward<Fn>(fn), bool(),
std::forward<Args>(args)...);
} else {
assert(false);
}
} else {
// The next two types are index types, which must be integrals.
auto type_label = type_labels.back();
type_labels.pop_back();
if (type_label == "uint8") {
invoke_visit_fn_impl_(type_labels, std::forward<Fn>(fn), std::uint8_t(),
std::forward<Args>(args)...);
} else if (type_label == "uint16") {
invoke_visit_fn_impl_(type_labels, std::forward<Fn>(fn),
std::uint16_t(), std::forward<Args>(args)...);
} else if (type_label == "uint32") {
invoke_visit_fn_impl_(type_labels, std::forward<Fn>(fn),
std::uint32_t(), std::forward<Args>(args)...);
} else if (type_label == "uint64") {
invoke_visit_fn_impl_(type_labels, std::forward<Fn>(fn),
std::uint64_t(), std::forward<Args>(args)...);
} else if (type_label == "int8") {
invoke_visit_fn_impl_(type_labels, std::forward<Fn>(fn), std::int8_t(),
std::forward<Args>(args)...);
} else if (type_label == "int16") {
invoke_visit_fn_impl_(type_labels, std::forward<Fn>(fn), std::int16_t(),
std::forward<Args>(args)...);
} else if (type_label == "int32") {
invoke_visit_fn_impl_(type_labels, std::forward<Fn>(fn), std::int32_t(),
std::forward<Args>(args)...);
} else if (type_label == "int64") {
invoke_visit_fn_impl_(type_labels, std::forward<Fn>(fn), std::int64_t(),
std::forward<Args>(args)...);
} else {
assert(false);
}
}
}
}
} // namespace __detail
template <typename Fn>
inline void visit_label(const std::vector<std::string>& type_labels, Fn&& fn) {
__detail::invoke_visit_fn_impl_(type_labels, fn);
}
} // namespace binsparse