-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule.h
91 lines (79 loc) · 1.59 KB
/
module.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
#ifndef MODULE_H
#define MODULE_H
#include <string>
#include <cstdio>
namespace felis {
enum ModuleType {
CoreModule,
WorkloadModule,
ExportModule,
};
template <int Type>
class Module {
static Module<Type> *&head() {
static Module<Type> *head = nullptr;
return head;
}
Module<Type> *next;
bool initialized = false;
protected:
bool required = false;
struct {
std::string name;
std::string description;
} info;
Module();
virtual void Init() = 0;
public:
void Load() {
if (initialized)
return;
printf("Loading %s module...\n", info.name.c_str());
fflush(stdout);
Init();
initialized = true;
}
static void InitRequiredModules();
static void InitModule(std::string name);
static void ShowAllModules();
};
template <int Type>
Module<Type>::Module()
{
Module<Type> **p = &head();
while (*p) p = &((*p)->next);
*p = this;
next = nullptr;
}
template <int Type>
void Module<Type>::InitRequiredModules()
{
for (auto p = head(); p; p = p->next) {
if (p->required)
p->Load();
}
}
template <int Type>
void Module<Type>::ShowAllModules()
{
for (auto p = head(); p; p = p->next) {
printf("Found module %s%s: %s\n",
p->info.name.c_str(),
p->required ? "(required)" : "",
p->info.description.c_str());
}
}
template <int Type>
void Module<Type>::InitModule(std::string name)
{
for (auto p = head(); p; p = p->next) {
if (p->info.name == name) {
p->Load();
return;
}
}
printf("Cannot find workload module %s\n", name.c_str());
std::abort();
}
}
#endif /* MODULE_H */