-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_architecture_api.cpp
169 lines (147 loc) · 4.3 KB
/
plugin_architecture_api.cpp
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
#include "plugin_architecture_api.h"
#include "exceptions.h"
#include <stdexcept>
bool VSFPluginArchitecture::architectureExists = false;
VSFPluginArchitecture::VSFPluginArchitecture() {
if (!architectureExists)
{
architectureExists = true;
}
else
{
throw TooManyPluginArchitectures();
}
memset(pluginRequestHandlers,0, sizeof(VSFPlugin*)*(Request_Number_MaxNumber - Request_Identifier));
}
void VSFPluginArchitecture::registerEventHandler(VSFPlugin & handler, Event_Number_Enum eventNumber)
{
if (eventNumber >= Event_Number_MaxNumber || eventNumber <= Event_Identifier)
{
throw std::out_of_range("eventNumber not in enum bounds");
}
else
{
eventNumber = (Event_Number_Enum)(eventNumber - Event_Number_Identifier);
}
pluginEventHandlers[eventNumber].push_back(&handler);
}
void VSFPluginArchitecture::registerRequestHandler(VSFPlugin & handler, Request_Number_Enum requestNumber)
{
if (requestNumber >= Request_Number_MaxNumber || requestNumber < Request_Identifier)
{
throw std::out_of_range("eventNumber not in enum bounds");
}
else
{
requestNumber = (Request_Number_Enum)(requestNumber - Request_Number_Identifier);
}
if (pluginRequestHandlers[requestNumber])
{
throw TooManyRequestHandlers();
}
else
{
pluginRequestHandlers[requestNumber] = &handler;
}
}
void VSFPluginArchitecture::unregisterEventHandler(VSFPlugin & handler, Event_Number_Enum eventNumber)
{
if (eventNumber >= Event_Number_MaxNumber || eventNumber <= Event_Identifier)
{
throw std::out_of_range("eventNumber not in enum bounds");
}
else
{
eventNumber = (Event_Number_Enum)(eventNumber - Event_Number_Identifier);
}
bool found = false;
for (std::vector<VSFPlugin*>::iterator i = pluginEventHandlers[eventNumber].begin();
i != pluginEventHandlers[eventNumber].end(); ++i)
{
if (*i == &handler)
{
pluginEventHandlers[eventNumber].erase(i);
found = true;
break;
}
}
if (!found)
{
throw UnregisteringNotRegisterd();
}
}
void VSFPluginArchitecture::unregisterRequestHandler(VSFPlugin & handler, Request_Number_Enum requestNumber)
{
if (requestNumber >= Request_Number_MaxNumber || requestNumber < Request_Identifier)
{
throw std::out_of_range("requestNumber not in enum bounds");
}
else
{
requestNumber = (Request_Number_Enum)(requestNumber - Request_Number_Identifier);
}
if (pluginRequestHandlers[requestNumber] == &handler)
{
pluginRequestHandlers[requestNumber] = 0;
}
else
{
throw UnregisteringNotRegisterd();
}
}
void VSFPluginArchitecture::unregisterRequestHandler(Request_Number_Enum requestNumber)
{
if (requestNumber >= Request_Number_MaxNumber || requestNumber < Request_Identifier)
{
throw std::out_of_range("requestNumber not in enum bounds");
}
else
{
requestNumber = (Request_Number_Enum)(requestNumber - Request_Number_Identifier);
}
pluginRequestHandlers[requestNumber] = 0;
}
void VSFPluginArchitecture::handleEvent(Event & e) {
int eventNumber = e.getNumber();
if (eventNumber >= Event_Number_MaxNumber || eventNumber <= Event_Identifier)
{
throw std::out_of_range("eventNumber not in enum bounds");
}
else
{
eventNumber = (Event_Number_Enum)(eventNumber - Event_Number_Identifier);
}
if (pluginEventHandlers[eventNumber].empty())
{
throw NoEventHandler();
}
for (std::vector<VSFPlugin*>::iterator i = pluginEventHandlers[eventNumber].begin();
i != pluginEventHandlers[eventNumber].end(); ++i)
{
e.getHandled(*(*i));
}
}
UnknownObject VSFPluginArchitecture::handleRequest(Request & r) {
int requestNumber = r.getNumber();
if (requestNumber >= Request_Number_MaxNumber || requestNumber < Request_Identifier)
{
throw std::out_of_range("requestNumber not in enum bounds");
}
else
{
requestNumber = (Request_Number_Enum)(requestNumber - Request_Number_Identifier);
}
if (pluginRequestHandlers[requestNumber] == 0)
{
throw NoRequestHandler();
}
return r.getHandled(*pluginRequestHandlers[requestNumber]);
}
VSFPluginArchitecture * VSFPluginArchitectureSingleton::architecture = 0;
VSFPluginArchitecture & VSFPluginArchitectureSingleton::getVSFPluginArchitecture() {
if (architecture == 0)
{
architecture = new VSFPluginArchitecture();
}
return *architecture;
}