-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.h
386 lines (308 loc) · 12.8 KB
/
context.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#ifndef JWUTIL_CONTEXT_H
#define JWUTIL_CONTEXT_H
#define JWUTIL_CONTEXT_ENABLE_STRUCT_GENERATION 0
#if JWUTIL_CONTEXT_ENABLE_STRUCT_GENERATION
#include <fstream>
#endif
#include <assert.h>
#include <typeindex>
#include <vector>
#include <string>
#include <unordered_map>
#include <cxxabi.h>
namespace jw_util {
template <typename DerivedType>
class Context {
public:
enum class LogLevel {
Trace,
Info,
Warning,
Error,
};
Context(float loadFactor = 0.1f) {
// Trade memory for speed
classMap.max_load_factor(loadFactor);
}
Context(const Context& other) = delete;
Context &operator=(const Context &other) = delete;
Context(Context&&) = delete;
Context& operator=(Context&&) = delete;
template <typename InterfaceType>
void provideInstance(InterfaceType *instance) {
assert(instance);
emitLog(LogLevel::Info, getTypeName<DerivedType>() + "::provideInstance<" + getTypeName<InterfaceType>() + ">()");
ClassEntry entry;
entry.template setBorrowedInstance<InterfaceType>(instance);
auto inserted = classMap.emplace(std::type_index(typeid(InterfaceType)), entry);
assert(inserted.second);
}
template <typename InterfaceType>
InterfaceType *swapInstance(InterfaceType *newInstance) {
assert(newInstance);
emitLog(LogLevel::Trace, getTypeName<DerivedType>() + "::swapInstance<" + getTypeName<InterfaceType>() + ">()");
auto found = classMap.find(std::type_index(typeid(InterfaceType)));
assert(found != classMap.end());
return found->second.template swapBorrowedInstance<InterfaceType>(newInstance);
}
template <typename InterfaceType>
InterfaceType *removeInstance() {
emitLog(LogLevel::Info, getTypeName<DerivedType>() + "::removeInstance<" + getTypeName<InterfaceType>() + ">()");
auto found = classMap.find(std::type_index(typeid(InterfaceType)));
assert(found != classMap.end());
InterfaceType *res = found->second.template swapBorrowedInstance<InterfaceType>(0);
classMap.erase(found);
return res;
}
template <typename InterfaceType>
bool has() {
// Not even trace level
// emitLog(LogLevel::Trace, getTypeName<DerivedType>() + "::has<" + getTypeName<InterfaceType>() + ">()");
return classMap.find(std::type_index(typeid(InterfaceType))) != classMap.end();
}
template <typename InterfaceType, typename ImplementationType = InterfaceType, typename... ArgTypes>
InterfaceType &construct(ArgTypes... args) {
emitLog(LogLevel::Trace, getTypeName<DerivedType>() + "::construct<" + getTypeName<InterfaceType>() + ", " + getTypeName<ImplementationType>() + ">()");
auto inserted = classMap.emplace(std::type_index(typeid(InterfaceType)), ClassEntry());
assert(inserted.second);
inserted.first->second.template createManagedInstance<InterfaceType, ImplementationType, ArgTypes...>(this, std::forward<ArgTypes>(args)...);
classOrder.push_back(&inserted.first->second);
return *inserted.first->second.template getInstance<InterfaceType>();
}
template <typename InterfaceType, typename ImplementationType = InterfaceType, typename... ArgTypes>
InterfaceType &get(ArgTypes... args) {
// Not even trace level
// emitLog(LogLevel::Trace, getTypeName<DerivedType>() + "::get<" + getTypeName<InterfaceType>() + ">()");
return internalGet<InterfaceType, ImplementationType, ArgTypes...>(0, std::forward<ArgTypes>(args)...);
}
unsigned int getManagedTypeCount() const {
return classOrder.size();
}
unsigned int getTotalTypeCount() const {
return classMap.size();
}
std::vector<std::string> getTypeNames() const {
std::vector<std::string> res;
res.reserve(classMap.size());
typename std::unordered_map<std::type_index, ClassEntry>::const_iterator i = classMap.cbegin();
while (i != classMap.cend()) {
res.push_back(makeTypeName(i->first));
i++;
}
return res;
}
/*
void createSomething() {
// This method is kind of hacky
assert(false);
assert(!classMap.empty());
// Any iteration over a std::unordered_map will be in an implementation-dependent order,
// So we explicitly randomize it here so we don't get weird edge cases.
static std::random_device randomDevice;
static std::mt19937 randomEngine(randomDevice());
unsigned int index = std::uniform_int_distribution<unsigned int>(0, classMap.size() - 1)(randomEngine);
if (DEBUG_CONTEXT) {
// std::cout << getTypeName<DerivedType>() + "::createSomething: rand(" << classMap.size() << ") -> " << index << std::endl;
}
auto offset = std::next(classMap.begin(), index);
auto i = offset;
do {
if (!i->second.hasInstance()) {
i->second.createManagedInstance(*this);
classOrder.push_back(&i->second);
return;
}
i++;
if (i == classMap.end()) {
i = classMap.begin();
}
} while (i != offset);
// If we get here, there is a circular dependency
assert(false);
}
*/
void reset() {
runDestructors();
classOrder.clear();
classMap.clear();
}
~Context() {
runDestructors();
}
private:
class ClassEntry {
public:
bool hasInstance() const {
return returnInstance;
}
template <typename ClassType>
ClassType *getInstance() const {
assert(returnInstance && "If this fails, you probably have a circular dependency");
assert(std::is_const<ClassType>::value || !isConst);
#if JWUTIL_CONTEXT_ENABLE_STRUCT_GENERATION
getCount++;
#endif
return const_cast<ClassType *>(static_cast<const ClassType *>(returnInstance));
}
template <typename ClassType>
void setBorrowedInstance(ClassType *instance) {
assert(!returnInstance);
assert(!managedInstance);
assert(!destroyPtr);
#if JWUTIL_CONTEXT_ENABLE_STRUCT_GENERATION
typeName = getTypeName<ClassType>();
setBorrowedCount++;
#endif
returnInstance = static_cast<const void *>(instance);
managedInstance = 0;
isConst = std::is_const<ClassType>::value;
destroyPtr = &ClassEntry::noop;
}
template <typename ClassType>
ClassType *swapBorrowedInstance(ClassType *newInstance) {
assert(returnInstance);
assert(!managedInstance);
assert(isConst == std::is_const<ClassType>::value);
assert(destroyPtr == &ClassEntry::noop);
#if JWUTIL_CONTEXT_ENABLE_STRUCT_GENERATION
swapBorrowedCount++;
#endif
ClassType *res = getInstance<ClassType>();
returnInstance = static_cast<const void *>(newInstance);
return res;
}
template <typename ReturnType, typename ManagedType, typename... ArgTypes>
void createManagedInstance(Context *context, ArgTypes... args) {
assert(!returnInstance);
assert(!managedInstance);
assert(!destroyPtr);
#if JWUTIL_CONTEXT_ENABLE_STRUCT_GENERATION
createManagedCount++;
#endif
destroyPtr = &ClassEntry::destroyStub<ReturnType, ManagedType>;
ManagedType *instance = new ManagedType(*static_cast<DerivedType *>(context), std::forward<ArgTypes>(args)...);
returnInstance = static_cast<const ReturnType *>(instance);
managedInstance = static_cast<const void *>(instance);
isConst = std::is_const<ManagedType>::value;
}
void release(Context *context) {
(this->*destroyPtr)(context);
destroyPtr = 0;
returnInstance = 0;
}
#if JWUTIL_CONTEXT_ENABLE_STRUCT_GENERATION
std::string typeName;
mutable unsigned int getCount = 0;
mutable unsigned int setBorrowedCount = 0;
mutable unsigned int swapBorrowedCount = 0;
mutable unsigned int prepareManagedCount = 0;
mutable unsigned int createManagedCount = 0;
#endif
private:
void (ClassEntry::*destroyPtr)(Context *context) = 0;
const void *returnInstance = 0;
const void *managedInstance = 0;
bool isConst;
template <typename ReturnType, typename ManagedType>
void destroyStub(Context *context) {
context->emitLog(LogLevel::Info, getTypeName<DerivedType>() + "::destroyStub<" + getTypeName<ReturnType>() + ", " + getTypeName<ManagedType>() + ">()");
delete static_cast<const ManagedType *>(managedInstance);
returnInstance = 0;
managedInstance = 0;
}
void noop(Context *context) {
(void) context;
}
};
template <typename InterfaceType, typename ImplementationType, typename... ArgTypes, typename = decltype(ImplementationType(std::declval<DerivedType &>(), std::declval<ArgTypes>()...))>
InterfaceType &internalGet(int, ArgTypes... args) {
auto inserted = classMap.emplace(std::type_index(typeid(InterfaceType)), ClassEntry());
if (inserted.second) {
inserted.first->second.template createManagedInstance<InterfaceType, ImplementationType, ArgTypes...>(this, std::forward<ArgTypes>(args)...);
classOrder.push_back(&inserted.first->second);
}
return *inserted.first->second.template getInstance<InterfaceType>();
}
template <typename InterfaceType, typename ImplementationType>
InterfaceType &internalGet(...) {
static_assert(std::is_same<InterfaceType, ImplementationType>::value, "Supplied an ImplementationType to Context::get, but it is not constructible with those arguments");
auto found = classMap.find(std::type_index(typeid(InterfaceType)));
if (found == classMap.cend()) {
emitLog(LogLevel::Error, getTypeName<DerivedType>() + "::get: Cannot find non-constructible type " + getTypeName<InterfaceType>());
assert(false);
}
return *found->second.template getInstance<InterfaceType>();
}
void runDestructors() {
#if JWUTIL_CONTEXT_ENABLE_STRUCT_GENERATION
std::ofstream file;
file.open(getStructFilename());
file << generateStruct();
file.close();
#endif
typename std::vector<ClassEntry *>::reverse_iterator i = classOrder.rbegin();
while (i != classOrder.rend()) {
ClassEntry &entry = **i;
entry.release(this);
i++;
}
}
#if JWUTIL_CONTEXT_ENABLE_STRUCT_GENERATION
std::string generateStruct() const {
std::string className = getTypeName<DerivedType>();
std::string res;
res += "class " + className + " {\n";
res += "public:\n";
unsigned int varIndex = 0;
typename std::vector<ClassEntry *>::const_iterator i = classOrder.cbegin();
while (i != classOrder.cend()) {
const ClassEntry &entry = **i;
std::string indent = " ";
res += indent + entry.typeName + " *_" + std::to_string(varIndex) + ";\n";
res += indent + "template <>\n";
res += indent + entry.typeName + " *get<" + entry.typeName + ">() { return _" + std::to_string(varIndex) + "; }\n";
varIndex++;
i++;
}
res += "};\n";
return res;
}
std::string getStructFilename() const {
std::string className = getTypeName<DerivedType>();
std::string::iterator i = className.begin();
while (i != className.end()) {
bool isLetterLc = *i >= 'a' && *i <= 'z';
bool isLetterUc = *i >= 'A' && *i <= 'Z';
bool isNumber = *i >= '0' && *i <= '9';
bool isSymbol = *i == '_';
if (!isLetterLc && !isLetterUc && !isNumber && !isSymbol) {
*i = '_';
}
i++;
}
className += ".gen.h";
return className;
}
#endif
void emitLog(LogLevel level, const std::string &msg) {
static_cast<DerivedType *>(this)->log(level, msg);
}
static std::string makeTypeName(std::type_index typeId) {
int status = 0;
std::size_t length;
char *realname = abi::__cxa_demangle(typeId.name(), 0, &length, &status);
assert(status == 0);
std::string str(realname, length - 1);
std::free(realname);
return str;
}
template <typename Type>
static std::string getTypeName() {
static thread_local std::string str = makeTypeName(typeid(Type));
return str;
}
std::vector<ClassEntry *> classOrder;
std::unordered_map<std::type_index, ClassEntry> classMap;
};
}
#endif // JWUTIL_CONTEXT_H