-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
252 lines (197 loc) · 4.83 KB
/
test.cpp
File metadata and controls
252 lines (197 loc) · 4.83 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
#include <iostream>
// base_profile.hpp
struct BaseApi;
struct BaseProfile
{
virtual BaseApi* getApi() = 0;
protected:
virtual ~BaseProfile() {}
};
// end base_profile.hpp
// profile.hpp
struct Profile : public virtual BaseProfile
{
int getInstId() const { return inst_id; }
int getServId() const { return serv_id; }
int getError() const { return error; }
bool isEnabled() const { return enabled; }
protected:
virtual void onEnabledChange(bool new_state) = 0;
void setError(int err) { error = err; }
Profile(int inst_id, int serv_id) :
inst_id(inst_id),
serv_id(serv_id),
error(0),
enabled(false)
{}
private:
void setEnabled(bool state)
{
if (state != enabled)
{
enabled = state;
onEnabledChange(enabled);
}
}
int inst_id;
int serv_id;
int error;
bool enabled;
friend struct PM;
};
// end of profile.hpp
// base_api.hpp
struct BaseApi : public virtual BaseProfile
{
protected:
virtual ~BaseApi() {}
};
// specific_api.hpp
struct SpecificApi : public BaseApi
{
virtual void specificMethod() = 0;
virtual void specMethVoidRes(int) = 0;
virtual int specMethIntRes(double) = 0;
protected:
virtual ~SpecificApi() {}
};
// end of specific_api.hpp
// end of base_api.hpp
// profile_lib.hpp
struct ProfileLibApi
{
Profile* (*createProfile)(int, int);
char const* (*getProfileName)();
int (*getProfileVersion)();
};
#define LIBRARY_BOILERPLATE(PROF, NAME, VERSION) \
Profile* createProfile(int instance_id, int service_id) { return new PROF(instance_id, service_id); } \
char const* getProfileName() { return NAME; } \
int getProfileVersion() { return VERSION; } \
static ProfileLibApi PROFILE_LIB_API_TABLE = { createProfile, getProfileName, getProfileVersion };
#define PROFILE_BOILERPLATE(PROF) \
PROF(int inst_id, int serv_id) : Profile(inst_id, serv_id) {} \
virtual BaseApi* getApi() { return this; }
// end of profile_lib.hpp
// specific_profile.hpp
namespace LibraryCode
{
struct SpecificProfile : public SpecificApi, public Profile
{
PROFILE_BOILERPLATE(SpecificProfile)
virtual void onEnabledChange(bool new_state)
{
std::cout << __PRETTY_FUNCTION__ << " new state = " << new_state << std::endl;
// open channel, whatever
}
virtual void specificMethod()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
virtual void specMethVoidRes(int a)
{
std::cout << __PRETTY_FUNCTION__ << " a = " << a << std::endl;
}
virtual int specMethIntRes(double b)
{
std::cout << __PRETTY_FUNCTION__ << " b = " << b << std::endl;
return (int)b;
}
};
LIBRARY_BOILERPLATE(SpecificProfile, "SpecificProfile", 9000)
}
// end of specific_profile.hpp
// profile manager
void* pseudo_dlsym()
{
return &LibraryCode::PROFILE_LIB_API_TABLE;
}
struct PM
{
Profile* profile;
PM()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
ProfileLibApi* plapi = reinterpret_cast<ProfileLibApi*>(pseudo_dlsym());
std::cout << "loading profile" << std::endl;
std::cout << "name: " << plapi->getProfileName() << std::endl;
std::cout << "version: " << plapi->getProfileVersion() << std::endl;
profile = plapi->createProfile(28, 42);
profile->setEnabled(true);
}
~PM()
{
killProfile();
}
BaseApi* getProfile()
{
if (profile)
return profile->getApi();
else
return 0;
}
void releaseProfile()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
void killProfile()
{
delete profile;
profile = 0;
}
};
// end of profile manager
// handler
#define HANDLER_BOILERPLATE_CALL(API, NAME, RESULT, ...) \
do { \
BaseApi* ba = pm.getProfile(); \
if (ba) { \
RESULT = static_cast<API*>(ba)->NAME(__VA_ARGS__); \
pm.releaseProfile(); \
return true; \
} \
return false; \
} while (0)
#define HANDLER_BOILERPLATE_VOID_CALL(API, NAME, ...) \
do { \
BaseApi* ba = pm.getProfile(); \
if (ba) { \
static_cast<API*>(ba)->NAME(__VA_ARGS__); \
pm.releaseProfile(); \
return true; \
} \
return false; \
} while (0)
struct SpecificHandler
{
PM& pm;
SpecificHandler(PM& pm) : pm(pm) {}
bool specificMethod()
{
BaseApi* ba = pm.getProfile();
if (ba) {
static_cast<SpecificApi*>(ba)->specificMethod();
pm.releaseProfile();
return true;
}
return false;
}
bool specMethVoidRes(int const a)
{
HANDLER_BOILERPLATE_VOID_CALL(SpecificApi, specMethVoidRes, a);
}
bool specMethIntRes(double const b, int & res)
{
HANDLER_BOILERPLATE_CALL(SpecificApi, specMethIntRes, res, b);
}
};
int main()
{
PM pm;
SpecificHandler h(pm);
std::cout << h.specificMethod() << std::endl;
int res = 0;
std::cout << h.specMethIntRes(100.345, res) << " res = " << res << std::endl;
pm.killProfile();
std::cout << h.specMethVoidRes(100) << std::endl;
}