Testing complex callbacks invocation; fixing subtle bugs
[iot2.git] / iotjava / iotrmi / C++ / basics / TestClass.hpp
1 #ifndef _TESTCLASS_HPP__
2 #define _TESTCLASS_HPP__
3
4 #include <iostream>
5 #include <thread>
6 #include <chrono>
7 #include "TestClassInterface.hpp"
8 #include "CallBackInterfaceWithCallBack.hpp"
9
10 using namespace std;
11
12 class TestClass : public TestClassInterface {
13         public:
14                 TestClass();
15                 TestClass(int _int, float _float, string _string);
16
17                 char                            getByte(char in);
18                 short                           getShort(short in);
19                 int64_t                         getLong(int64_t in);
20                 float                           getFloat(float in);
21                 double                          getDouble(double in);
22                 bool                            getBoolean(bool in);
23                 char                            getChar(char in);
24
25                 vector<char>            getByteArray(vector<char> in);
26                 vector<short>           getShortArray(vector<short> in);
27                 vector<int64_t>         getLongArray(vector<int64_t> in);
28                 vector<float>           getFloatArray(vector<float> in);
29                 vector<double>          getDoubleArray(vector<double> in);
30                 vector<bool>            getBooleanArray(vector<bool> in);
31                 vector<char>            getCharArray(vector<char> in);
32
33                 vector<char>            getByteList(vector<char> in);
34                 vector<short>           getShortList(vector<short> in);
35                 vector<int64_t>         getLongList(vector<int64_t> in);
36                 vector<float>           getFloatList(vector<float> in);
37                 vector<double>          getDoubleList(vector<double> in);
38                 vector<bool>            getBooleanList(vector<bool> in);
39                 vector<char>            getCharList(vector<char> in);
40
41                 // Callbacks
42                 void                            registerCallback(CallBackInterfaceWithCallBack* _cb);
43                 void                            registerCallbackArray(vector<CallBackInterfaceWithCallBack*> _cb);
44                 void                            registerCallbackList(vector<CallBackInterfaceWithCallBack*> _cb);
45                 void                            registerCallbackComplex(int in, vector<CallBackInterfaceWithCallBack*> _cb, double db);
46                 int                                     callBack();
47
48                 // Enum
49                 Enum                            handleEnum(Enum en);
50                 vector<Enum>            handleEnumArray(vector<Enum> vecEn);
51                 vector<Enum>            handleEnumList(vector<Enum> vecEn);
52                 Enum                            handleEnumComplex(Enum en, int i, char c);
53                 vector<Enum>            handleEnumComplex2(vector<Enum> en, int i, char c);
54
55                 // Struct
56                 Struct                          handleStruct(Struct str);
57                 vector<Struct>          handleStructArray(vector<Struct> vecStr);
58                 vector<Struct>          handleStructList(vector<Struct> vecStr);
59                 Struct                          handleStructComplex(int in, char c, Struct str);
60                 vector<Struct>          handleStructComplex2(int in, char c, vector<Struct> vecStr);
61
62                 int                                     getA();
63                 void                            setA(int _int);
64                 void                            setB(float _float);
65                 void                            setC(string _string);
66                 string                          sumArray(vector<string> newA);
67                 int                                     setAndGetA(int newA);
68                 int                                     setACAndGetA(string newC, int newA);
69
70         private:                
71                 int                                                                             intA;
72                 float                                                                   floatB;
73                 string                                                                  stringC;
74                 vector<CallBackInterfaceWithCallBack*>  cbvec;
75 };
76
77
78 TestClass::TestClass() {
79
80         intA = 1;
81         floatB = 2;
82         stringC = "345";
83         // cbvec doesn't need to be initialized again
84 }
85
86
87 TestClass::TestClass(int _int, float _float, string _string) {
88
89         intA = _int;
90         floatB = _float;
91         stringC = _string;
92         // cbvec doesn't need to be initialized again
93 }
94
95
96 void TestClass::registerCallback(CallBackInterfaceWithCallBack* _cb) {
97
98         cbvec.push_back(_cb);
99         cout << "Registering callback object!" << endl;
100 }
101
102
103 void TestClass::registerCallbackArray(vector<CallBackInterfaceWithCallBack*> _cb) {
104
105         for (CallBackInterfaceWithCallBack* cb : _cb) {
106                 cbvec.push_back(cb);
107                 cout << "Registering callback object in array!" << endl;
108         }
109 }
110
111
112 void TestClass::registerCallbackList(vector<CallBackInterfaceWithCallBack*> _cb) {
113
114         for (CallBackInterfaceWithCallBack* cb : _cb) {
115                 cbvec.push_back(cb);
116                 cout << "Registering callback object in list!" << endl;
117         }
118 }
119
120
121 void TestClass::registerCallbackComplex(int in, vector<CallBackInterfaceWithCallBack*> _cb, double db) {
122
123         for (CallBackInterfaceWithCallBack* cb : _cb) {
124                 cbvec.push_back(cb);
125                 cout << "Registering callback object in list!" << endl;
126         }
127
128         cout << "Integer: " << in << endl;
129         cout << "Double: " << db << endl;
130 }
131
132
133 int TestClass::callBack() {
134
135         int sum = 0;
136         for (CallBackInterfaceWithCallBack* cb : cbvec) {
137                 sum = sum + cb->printInt();
138         }
139
140         return sum;
141 }
142
143
144 // Single variables
145 char TestClass::getByte(char in) {
146
147         return in;
148 }
149
150
151 short TestClass::getShort(short in) {
152
153         return in;
154 }
155
156
157 int64_t TestClass::getLong(int64_t in) {
158
159         return in;
160 }
161
162
163 float TestClass::getFloat(float in) {
164
165         return in;
166 }
167
168
169 double TestClass::getDouble(double in) {
170
171         return in;
172 }
173
174
175 bool TestClass::getBoolean(bool in) {
176
177         return in;
178 }
179
180
181 char TestClass::getChar(char in) {
182
183         return in;
184 }
185
186
187 // Arrays
188 vector<char> TestClass::getByteArray(vector<char> in) {
189
190         return in;
191 }
192
193
194 vector<short> TestClass::getShortArray(vector<short> in) {
195
196         return in;
197 }
198
199
200 vector<int64_t> TestClass::getLongArray(vector<int64_t> in) {
201
202         return in;
203 }
204
205
206 vector<float> TestClass::getFloatArray(vector<float> in) {
207
208         return in;
209 }
210
211
212 vector<double> TestClass::getDoubleArray(vector<double> in) {
213
214         return in;
215 }
216
217
218 vector<bool> TestClass::getBooleanArray(vector<bool> in) {
219
220         return in;
221 }
222
223
224 vector<char> TestClass::getCharArray(vector<char> in) {
225
226         return in;
227 }
228
229 // List
230 vector<char> TestClass::getByteList(vector<char> in) {
231
232         return in;
233 }
234
235
236 vector<short> TestClass::getShortList(vector<short> in) {
237
238         return in;
239 }
240
241
242 vector<int64_t> TestClass::getLongList(vector<int64_t> in) {
243
244         return in;
245 }
246
247
248 vector<float> TestClass::getFloatList(vector<float> in) {
249
250         return in;
251 }
252
253
254 vector<double> TestClass::getDoubleList(vector<double> in) {
255
256         return in;
257 }
258
259
260 vector<bool> TestClass::getBooleanList(vector<bool> in) {
261
262         return in;
263 }
264
265
266 vector<char> TestClass::getCharList(vector<char> in) {
267
268         return in;
269 }
270
271
272 int TestClass::getA() {
273
274         return intA;
275 }
276
277
278 void TestClass::setA(int _int) {
279
280         intA = _int;
281 }
282
283
284 void TestClass::setB(float _float) {
285
286         floatB = _float;
287 }
288
289
290 void TestClass::setC(string _string) {
291
292         stringC = _string;
293 }
294
295
296 // Enum
297 Enum TestClass::handleEnum(Enum en) {
298
299         cout << "Enum: " << en << endl;
300         
301         return en;
302 }
303
304
305 vector<Enum> TestClass::handleEnumArray(vector<Enum> vecEn) {
306
307         for (Enum en : vecEn) {
308                 cout << "Enum: " << en << endl;
309         }
310         
311         return vecEn;
312 }
313
314
315 vector<Enum> TestClass::handleEnumList(vector<Enum> vecEn) {
316
317         for (Enum en : vecEn) {
318                 cout << "Enum: " << en << endl;
319         }
320         
321         return vecEn;
322 }
323
324
325 Enum TestClass::handleEnumComplex(Enum en, int i, char c) {
326
327         cout << "Enum: " << en << endl;
328         cout << "Integer: " << i << endl;
329         cout << "Char: " << c << endl;
330         
331         return en;
332 }
333
334
335 vector<Enum> TestClass::handleEnumComplex2(vector<Enum> vecEn, int in, char c) {
336
337         for (Enum en : vecEn) {
338                 cout << "Enum: " << en << endl;
339         }
340         cout << "Integer: " << in << endl;
341         cout << "Char: " << c << endl;
342         
343         return vecEn;
344 }
345
346
347
348 // Struct
349 Struct TestClass::handleStruct(Struct str) {
350
351         cout << "Name: " << str.name << endl;
352         cout << "Value: " << str.value << endl;
353         cout << "Year: " << str.year << endl;
354
355         Struct test;
356         test.name = "Anonymous";
357         test.value = 1.33;
358         test.year = 2016;
359         str = test;
360
361         return str;
362 }
363
364
365 vector<Struct> TestClass::handleStructArray(vector<Struct> vecStr) {
366
367         for (Struct str : vecStr) {
368
369                 cout << "Name: " << str.name << endl;
370                 cout << "Value: " << str.value << endl;
371                 cout << "Year: " << str.year << endl;
372         }
373         Struct test;
374         test.name = "Anonymous";
375         test.value = 1.33;
376         test.year = 2016;
377         vecStr.push_back(test);
378
379         return vecStr;
380 }
381
382
383 vector<Struct> TestClass::handleStructList(vector<Struct> vecStr) {
384
385         for (Struct str : vecStr) {
386
387                 cout << "Name: " << str.name << endl;
388                 cout << "Value: " << str.value << endl;
389                 cout << "Year: " << str.year << endl;
390         }
391         Struct test;
392         test.name = "Trimananda";
393         test.value = 1.33;
394         test.year = 2016;
395         vecStr.push_back(test);
396
397         return vecStr;
398 }
399
400
401 Struct TestClass::handleStructComplex(int in, char c, Struct str) {
402
403         cout << "Name: " << str.name << endl;
404         cout << "Value: " << str.value << endl;
405         cout << "Year: " << str.year << endl;
406
407         cout << "Integer: " << in << endl;
408         cout << "Char: " << c << endl;
409
410         Struct test;
411         test.name = "Anonymous";
412         test.value = 1.33;
413         test.year = 2016;
414         str = test;
415
416         return str;
417 }
418
419
420 vector<Struct> TestClass::handleStructComplex2(int in, char c, vector<Struct> vecStr) {
421
422         for (Struct str : vecStr) {
423                 cout << "Name: " << str.name << endl;
424                 cout << "Value: " << str.value << endl;
425                 cout << "Year: " << str.year << endl;
426         }
427
428         cout << "Integer: " << in << endl;
429         cout << "Char: " << c << endl;
430
431         return vecStr;
432 }
433
434
435 string TestClass::sumArray(vector<string> newA) {
436
437         string sum = "";
438         int len = newA.size();
439         for(int c = 0; c < len; c++) {
440                 sum = sum + newA[c];
441         }
442         return sum;
443 }
444
445
446 int TestClass::setAndGetA(int newA) {
447
448         intA = newA;
449         return intA;
450 }
451
452
453 int TestClass::setACAndGetA(string newC, int newA) {
454
455         stringC = newC;
456         intA = newA;
457         return intA;
458 }
459
460 #endif
461