-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathEnumVariantMap.h
More file actions
339 lines (288 loc) · 10.9 KB
/
EnumVariantMap.h
File metadata and controls
339 lines (288 loc) · 10.9 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
EnumVariantMap.h
Abstract:
Template for enum-based variant maps.
--*/
#pragma once
#include <map>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
// This template set is used for Arg storage and Context Data storage by enum type.
// The backing storage is a std::multimap of the enum to a variant of types.
// This enables strongly typed storage and retrieval of values based on an enum key.
namespace wsl::windows::wslc {
// Enum based variant helper.
// Enum must be an enum whose first member has the value 0, each subsequent member increases by 1, and the final member is named Max.
// Mapping is a template type that takes one template parameter of type Enum, and whose members define value_t as the type for that enum value.
template <typename Enum, template <Enum> typename Mapping>
struct EnumBasedVariant
{
private:
// Used to deduce the variant type; making a variant that includes std::monostate and all Mapping types.
template <size_t... I>
static inline auto Deduce(std::index_sequence<I...>)
{
return std::variant<std::monostate, typename Mapping<static_cast<Enum>(I)>::value_t...>{};
}
public:
// Holds data of any type listed in Mapping.
using variant_t = decltype(Deduce(std::make_index_sequence<static_cast<size_t>(Enum::Max)>()));
// Gets the index into the variant for the given Data.
static constexpr inline size_t Index(Enum e)
{
return static_cast<size_t>(e) + 1;
}
};
// An action that can be taken on an EnumBasedVariantMap.
enum class EnumBasedVariantMapAction
{
Add,
Contains,
Get,
GetAll,
Count,
Remove,
};
// A callback function that can be used for logging map actions.
template <typename Enum>
using EnumBasedVariantMapActionCallback = void (*)(const void* map, Enum value, EnumBasedVariantMapAction action);
// Forward declaration for EnumBasedVariantMapEmplacer
template <typename Enum, template <Enum> typename Mapping, typename V>
struct EnumBasedVariantMapEmplacer;
// Provides a multimap of the Enum to the mapped types (allows multiple values per key).
template <typename Enum, template <Enum> typename Mapping, EnumBasedVariantMapActionCallback<Enum> Callback = nullptr>
struct EnumBasedVariantMap
{
using Variant = EnumBasedVariant<Enum, Mapping>;
template <Enum E>
using mapping_t = typename Mapping<E>::value_t;
// Adds a value to the map. With multimap, this always adds a new entry (doesn't overwrite).
template <Enum E>
void Add(mapping_t<E>&& v)
{
if constexpr (Callback)
{
Callback(this, E, EnumBasedVariantMapAction::Add);
}
// Compile-time type checking - this should always pass since mapping_t<E> is the correct type
using CleanV = std::remove_cvref_t<mapping_t<E>>;
static_assert(
std::is_same_v<CleanV, mapping_t<E>>,
"Type mismatch in Add: provided type does not match the expected type for this enum value");
typename Variant::variant_t variant;
variant.template emplace<Variant::Index(E)>(std::move(v));
m_data.emplace(E, std::move(variant));
}
template <Enum E>
void Add(const mapping_t<E>& v)
{
if constexpr (Callback)
{
Callback(this, E, EnumBasedVariantMapAction::Add);
}
// Compile-time type checking - this should always pass since mapping_t<E> is the correct type
using CleanV = std::remove_cvref_t<mapping_t<E>>;
static_assert(
std::is_same_v<CleanV, mapping_t<E>>,
"Type mismatch in Add: provided type does not match the expected type for this enum value");
typename Variant::variant_t variant;
variant.template emplace<Variant::Index(E)>(v);
m_data.emplace(E, std::move(variant));
}
// Runtime version of Add that takes the enum as a parameter.
template <typename V>
void Add(Enum e, V&& v)
{
if constexpr (Callback)
{
Callback(this, e, EnumBasedVariantMapAction::Add);
}
// Check if the type matches the SPECIFIC enum value at compile time if possible
using CleanV = std::remove_cvref_t<V>;
// Pre-check if this type matches the specific enum value being added to
if (!IsMatchingType<CleanV>(e))
{
THROW_HR_MSG(E_INVALIDARG, "Type mismatch: provided type does not match the expected type for enum value %d", static_cast<int>(e));
}
typename Variant::variant_t variant;
EmplaceAtRuntimeIndex(variant, e, std::forward<V>(v), std::make_index_sequence<static_cast<size_t>(Enum::Max)>());
m_data.emplace(e, std::move(variant));
}
// Runtime method to check if value V matches the mapped type for an enum value.
template <typename V>
bool IsMatchingType(Enum e) const
{
return IsMatchingTypeImpl<V>(e, std::make_index_sequence<static_cast<size_t>(Enum::Max)>());
}
// Return a value indicating whether the given enum has at least one entry.
bool Contains(Enum e) const
{
if constexpr (Callback)
{
Callback(this, e, EnumBasedVariantMapAction::Contains);
}
return (m_data.find(e) != m_data.end());
}
// Gets the count of values for a specific enum key.
size_t Count(Enum e) const
{
if constexpr (Callback)
{
Callback(this, e, EnumBasedVariantMapAction::Count);
}
return m_data.count(e);
}
// Gets the FIRST value for the enum key (for backward compatibility).
// Non-const version returns a reference that can be modified.
template <Enum E>
mapping_t<E>& Get()
{
if constexpr (Callback)
{
Callback(this, E, EnumBasedVariantMapAction::Get);
}
auto itr = m_data.find(E);
THROW_HR_IF_MSG(E_NOT_SET, itr == m_data.end(), "Get(%d): key not found", static_cast<int>(E));
// Validate that the variant holds the expected type at the expected index
constexpr size_t expectedIndex = Variant::Index(E);
if (itr->second.index() != expectedIndex)
{
THROW_HR_MSG(
E_UNEXPECTED,
"Get(%d): variant type mismatch - expected index %zu, got %zu",
static_cast<int>(E),
expectedIndex,
itr->second.index());
}
return std::get<expectedIndex>(itr->second);
}
// Const overload of Get, cannot be modified.
template <Enum E>
const mapping_t<E>& Get() const
{
if constexpr (Callback)
{
Callback(this, E, EnumBasedVariantMapAction::Get);
}
auto itr = m_data.find(E);
THROW_HR_IF_MSG(E_NOT_SET, itr == m_data.cend(), "Get(%d): key not found", static_cast<int>(E));
// Validate that the variant holds the expected type at the expected index
constexpr size_t expectedIndex = Variant::Index(E);
if (itr->second.index() != expectedIndex)
{
THROW_HR_MSG(
E_UNEXPECTED,
"Get(%d): variant type mismatch - expected index %zu, got %zu",
static_cast<int>(E),
expectedIndex,
itr->second.index());
}
return std::get<expectedIndex>(itr->second);
}
// Gets ALL values for a specific enum key as a vector.
template <Enum E>
std::vector<mapping_t<E>> GetAll() const
{
if constexpr (Callback)
{
Callback(this, E, EnumBasedVariantMapAction::GetAll);
}
std::vector<mapping_t<E>> results;
auto range = m_data.equal_range(E);
for (auto it = range.first; it != range.second; ++it)
{
results.push_back(std::get<Variant::Index(E)>(it->second));
}
return results;
}
// Removes ALL entries for a specific enum key.
void Remove(Enum e)
{
if constexpr (Callback)
{
Callback(this, e, EnumBasedVariantMapAction::Remove);
}
m_data.erase(e);
}
// Gets the total number of items stored (across all keys).
size_t GetCount() const
{
return m_data.size();
}
// Gets a vector of all UNIQUE enum keys stored in the map.
std::vector<Enum> GetKeys() const
{
std::vector<Enum> keys;
Enum lastKey = static_cast<Enum>(-1);
bool first = true;
for (const auto& pair : m_data)
{
if (first || pair.first != lastKey)
{
keys.push_back(pair.first);
lastKey = pair.first;
first = false;
}
}
return keys;
}
private:
// Helper to implement runtime type checking.
template <typename V, size_t... I>
bool IsMatchingTypeImpl(Enum e, std::index_sequence<I...>) const
{
bool result = false;
((static_cast<size_t>(e) == I ? (result = std::is_same_v<std::remove_cvref_t<V>, mapping_t<static_cast<Enum>(I)>>, true) : false) || ...);
return result;
}
// Helper to emplace at runtime-determined index
template <typename V, size_t... I>
void EmplaceAtRuntimeIndex(typename Variant::variant_t& variant, Enum e, V&& v, std::index_sequence<I...>)
{
size_t index = static_cast<size_t>(e) + 1;
bool handled = false;
(
[&] {
if (index == I + 1 && !handled)
{
using Emplacer = wsl::windows::wslc::EnumBasedVariantMapEmplacer<Enum, Mapping, V>;
Emplacer::template Emplace<I + 1>(variant, std::forward<V>(v));
handled = true;
}
}(),
...);
if (!handled)
{
using CleanV = std::remove_cvref_t<V>;
THROW_HR_MSG(E_INVALIDARG, "Invalid enum value: %d", static_cast<int>(e));
}
}
std::multimap<Enum, typename Variant::variant_t> m_data;
};
// Helper for runtime emplacement into std::variant for EnumBasedVariantMap
template <typename Enum, template <Enum> typename Mapping, typename V>
struct EnumBasedVariantMapEmplacer
{
template <size_t Index>
static void Emplace(typename EnumBasedVariant<Enum, Mapping>::variant_t& variant, V&& value)
{
using TargetType = typename Mapping<static_cast<Enum>(Index - 1)>::value_t;
using CleanV = std::remove_cvref_t<V>;
constexpr bool is_same_type = std::is_same_v<CleanV, TargetType>;
constexpr bool is_convertible = std::is_convertible_v<CleanV, TargetType>;
constexpr bool is_constructible = std::is_constructible_v<TargetType, CleanV>;
if constexpr (is_same_type || is_convertible || is_constructible)
{
variant.template emplace<Index>(std::forward<V>(value));
}
else
{
throw std::runtime_error("Runtime type mismatch: cannot convert value to target type for this enum value");
}
}
};
} // namespace wsl::windows::wslc