/
simple_demo_dll.cpp
530 lines (423 loc) · 13.5 KB
/
simple_demo_dll.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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#include "simple_demo.h"
#include <map>
#include <algorithm>
// Extern C
struct KVStore{
std::map<std::string,std::string> m_;
};
extern "C"{
HKVStore CALLING_CONVENTION Create_KVStore(){
try{
return new KVStore;
}
catch(std::exception&){
return nullptr;
}
}
void CALLING_CONVENTION Destroy_KVStore(HKVStore h){
delete h;
}
error_code CALLING_CONVENTION Put (HKVStore h,const char* key, int32_t key_count,const char* value, int32_t value_count){
try{
std::string key(key,key_count);
std::string value(value,value_count);
h->m_[key] = value;
return 0;
}
catch(std::exception&){
return -1;
}
}
error_code CALLING_CONVENTION Get(HKVStore h, const char* key, int32_t key_count,const char** pvalue,int32_t* pvalue_count,char* breturn){
try{
std::string key(key,key_count);
auto iter = h->m_.find(key);
if(iter == h->m_.end()){
*breturn = 0;
return 0;
}
else{
std::string value = iter->second;
auto pc = new char[value.size()];
std::copy(value.begin(),value.end(),pc);
*pvalue_count = value.size();
*pvalue = pc;
return 0;
}
}
catch(std::exception&){
return -1;
}
}
error_code CALLING_CONVENTION Delete(HKVStore h, const char* key, int32_t key_count,char* breturn){
try{
std::string key(key,key_count);
auto iter = h->m_.find(key);
if(iter == h->m_.end()){
*breturn = 0;
return 0;
}
else{
h->m_.erase(iter);
return 0;
}
}
catch(std::exception&){
return -1;
}
}
}
// Compiler Vtable
struct KVStoreImplementation:public IKVStore{
std::map<std::string,std::string> m_;
virtual error_code CALLING_CONVENTION Put (const char* key, int32_t key_count,const char* value, int32_t value_count) override{
try{
std::string key(key,key_count);
std::string value(value,value_count);
m_[key] = value;
return 0;
}
catch(std::exception&){
return -1;
}
}
virtual error_code CALLING_CONVENTION Get(const char* key, int32_t key_count,const char** pvalue,int32_t* pvalue_count,char* breturn) override{
try{
std::string key(key,key_count);
auto iter = m_.find(key);
if(iter == m_.end()){
*breturn = 0;
return 0;
}
else{
std::string value = iter->second;
auto pc = new char[value.size()];
std::copy(value.begin(),value.end(),pc);
*pvalue_count = value.size();
*pvalue = pc;
return 0;
}
}
catch(std::exception&){
return -1;
}
}
virtual error_code CALLING_CONVENTION Delete(const char* key, int32_t key_count,char* breturn)override{
try{
std::string key(key,key_count);
auto iter = m_.find(key);
if(iter == m_.end()){
*breturn = 0;
return 0;
}
else{
m_.erase(iter);
return 0;
}
}
catch(std::exception&){
return -1;
}
}
virtual void CALLING_CONVENTION Destroy() override {
delete this;
}
};
extern "C"{
IKVStore* CALLING_CONVENTION Create_KVStoreImplementation(){
try{
return new KVStoreImplementation;
}
catch(std::exception&){
return nullptr;
}
}
}
// Programmer generated vtable
struct KVStore2Implementation:public IKVStore2{
std::map<std::string,std::string> m_;
IKVStoreVtable vt;
KVStore2Implementation(){
vtable = &vt;
vtable->Put = &Put_;
vtable->Get = &Get_;
vtable->Delete = &Delete_;
vtable->Destroy = &Destroy_;
}
static void CALLING_CONVENTION Destroy_(IKVStore2* ikv ){
delete static_cast<KVStore2Implementation*>(ikv);
}
static error_code CALLING_CONVENTION Put_ (IKVStore2* ikv, const char* key, int32_t key_count,const char* value, int32_t value_count){
try{
std::string key(key,key_count);
std::string value(value,value_count);
static_cast<KVStore2Implementation*>(ikv)->m_[key] = value;
return 0;
}
catch(std::exception&){
return -1;
}
}
static error_code CALLING_CONVENTION Get_(IKVStore2* ikv, const char* key, int32_t key_count,const char** pvalue,int32_t* pvalue_count,char* breturn){
try{
std::string key(key,key_count);
auto iter = static_cast<KVStore2Implementation*>(ikv)->m_.find(key);
if(iter == static_cast<KVStore2Implementation*>(ikv)->m_.end()){
*breturn = 0;
return 0;
}
else{
std::string value = iter->second;
auto pc = new char[value.size()];
std::copy(value.begin(),value.end(),pc);
*pvalue_count = value.size();
*pvalue = pc;
return 0;
}
}
catch(std::exception&){
return -1;
}
}
static error_code CALLING_CONVENTION Delete_(IKVStore2* ikv, const char* key, int32_t key_count,char* breturn){
try{
std::string key(key,key_count);
auto iter = static_cast<KVStore2Implementation*>(ikv)->m_.find(key);
if(iter == static_cast<KVStore2Implementation*>(ikv)->m_.end()){
*breturn = 0;
return 0;
}
else{
static_cast<KVStore2Implementation*>(ikv)->m_.erase(iter);
return 0;
}
}
catch(std::exception&){
return -1;
}
}
};
extern "C"{
IKVStore2* CALLING_CONVENTION Create_KVStore2Implementation(){
try{
return new KVStore2Implementation;
}
catch(std::exception&){
return nullptr;
}
}
}
struct KVStore2Implementation2{
std::map<std::string,std::string> m_;
IKVStore2DerivedImplementation imp_;
KVStore2Implementation2(){
imp_.Put = [this](std::string key, std::string value){
m_[key] = value;
};
}
};
extern "C"{
IKVStore2* CALLING_CONVENTION Create_KVStore2Implementation2(){
try{
auto p = new KVStore2Implementation2;
return &p->imp_;
}
catch(std::exception&){
return nullptr;
}
}
}
struct KVStore2Implementation3{
std::map<std::string,std::string> m_;
IKVStore2DerivedImplementation2 imp_;
KVStore2Implementation3(){
imp_.Put = [this](std::string key, std::string value){
m_[key] = value;
};
}
};
extern "C"{
cross_compiler_interface::portable_base* CALLING_CONVENTION Create_KVStore2Implementation3(){
try{
auto p = new KVStore2Implementation3;
return &p->imp_;
}
catch(std::exception&){
return nullptr;
}
}
}
struct IKV_simple_cross_function3_implementation{
std::map<std::string,std::string> m_;
implement_interface<IKV_simple_cross_function3> imp_;
IKV_simple_cross_function3_implementation(){
imp_.Put = [this](std::string key, std::string value){
m_[key] = value;
};
}
};
extern "C"{
cross_compiler_interface::portable_base* CALLING_CONVENTION Create_IKV_simple_cross_function3_implementation(){
try{
auto p = new IKV_simple_cross_function3_implementation;
return p->imp_.get_portable_base();
}
catch(std::exception&){
return nullptr;
}
}
}
struct IKV_simple_cross_function4_implementation{
std::map<std::string,std::string> m_;
implement_interface<IKV_simple_cross_function4> imp_;
IKV_simple_cross_function4_implementation(){
imp_.Put = [this](std::string key, std::string value){
m_[key] = value;
};
}
};
extern "C"{
cross_compiler_interface::portable_base* CALLING_CONVENTION Create_IKV_simple_cross_function4_implementation(){
try{
auto p = new IKV_simple_cross_function4_implementation;
return p->imp_.get_portable_base();
}
catch(std::exception&){
return nullptr;
}
}
}
struct ImplementKVStore{
cross_compiler_interface::implement_interface<InterfaceKVStore> imp_;
std::map<std::string,std::string> m_;
ImplementKVStore(){
imp_.Put = [this](std::string key, std::string value){
m_[key] = value;
};
imp_.Get = [this](std::string key, cross_compiler_interface::out<std::string> value)->bool{
auto iter = m_.find(key);
if(iter==m_.end()) return false;
value.set(iter->second);
return true;
};
imp_.Delete = [this](std::string key)->bool{
auto iter = m_.find(key);
if(iter==m_.end())return false;
m_.erase(iter);
return true;
};
imp_.Destroy = [this](){
delete this;
};
}
};
extern "C"{
cross_compiler_interface::portable_base* CALLING_CONVENTION Create_ImplementKVStore(){
try{
auto p = new ImplementKVStore;
return p->imp_.get_portable_base();
}
catch(std::exception&){
return nullptr;
}
}
}
struct ImplementKVStore2
:public cross_compiler_interface::implement_unknown_interfaces<ImplementKVStore2,InterfaceKVStore2>
{
std::map<std::string,std::string> m_;
ImplementKVStore2(){
using cross_compiler_interface::cr_string;
auto imp = get_implementation<InterfaceKVStore2>();
imp->Put = [this](cr_string key, cr_string value){
m_[key.to_string()] = value.to_string();
};
imp->Get = [this](cr_string key, cross_compiler_interface::out<std::string> value)
->bool{
auto iter = m_.find(key.to_string());
if(iter==m_.end()) return false;
value.set(iter->second);
return true;
};
imp->Delete = [this](cr_string key)->bool{
auto iter = m_.find(key.to_string());
if(iter==m_.end())return false;
m_.erase(iter);
return true;
};
}
};
extern "C"{
cross_compiler_interface::portable_base* CALLING_CONVENTION Create_ImplementKVStore2(){
try{
auto p = ImplementKVStore2::create();
return p.get_portable_base_addref();
}
catch(std::exception&){
return nullptr;
}
}
}
struct PropertyInterfaceImplementationHelper{
std::map<std::string, std::string> m_;
PropertyInterfaceImplementationHelper(cross_compiler_interface::implement_interface<PropertyInterface>& imp){
imp.SetProperty = [this](std::string key, std::string value){
m_[key] = value;
};
imp.GetProperty = [this](std::string key, std::string default_value){
auto iter = m_.find(key);
if(iter==m_.end()) return default_value;
return iter->second;
};
};
};
struct ImplementPropertyInterface{
cross_compiler_interface::implement_interface<PropertyInterface> imp_;
PropertyInterfaceImplementationHelper helper_;
ImplementPropertyInterface():helper_(imp_){}
};
struct ImplementPropertyInterfaceBinary{
cross_compiler_interface::module m_;
cross_compiler_interface::use_interface<PropertyInterface> other_;
cross_compiler_interface::implement_interface<PropertyInterface> imp_;
ImplementPropertyInterfaceBinary()
:m_("AwesomeDll")
{
other_ = cross_compiler_interface::create<PropertyInterface>(m_,"CreatePropertyManager");
imp_.set_runtime_parent(other_);
}
};
struct ImplementKVStoreFinal
:public cross_compiler_interface::implement_unknown_interfaces<ImplementKVStoreFinal,KVStoreFinal::Interface>{
typedef cross_compiler_interface::cr_string cr_string;
std::map<std::string,std::string> m_;
void Put(cr_string key, cr_string value){
m_[key.to_string()] = value.to_string();
}
bool Get(cr_string key, cross_compiler_interface::out<std::string> pvalue){
auto iter = m_.find(key.to_string());
if(iter==m_.end()) return false;
pvalue.set(iter->second);
return true;
}
bool Delete(cr_string key){
auto iter = m_.find(key.to_string());
if(iter==m_.end())return false;
m_.erase(iter);
return true;
}
ImplementKVStoreFinal(){
get_implementation<KVStoreFinal::Interface>()->map_to_member_functions_no_prefix(this);
}
};
extern "C"{
cross_compiler_interface::portable_base* CALLING_CONVENTION Create_ImplementKVStoreFinal(){
try{
auto p = ImplementKVStoreFinal::create();
return p.get_portable_base_addref();
}
catch(std::exception&){
return nullptr;
}
}
}