fb9c05406561d329c9b16987d226c34b4c2897aa
[iot2.git] / iotjava / iotrmi / C++ / sample / 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 "StructC.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                 //~TestClass();
17
18                 void                            setA(int _int);
19                 void                            setB(float _float);
20                 void                            setC(string _string);
21                 string                          sumArray(vector<string> newA);
22                 //int64_t                               sumArray(vector<int> newA);
23                 int                                     setAndGetA(int newA);
24                 int                                     setACAndGetA(string newC, int newA);
25                 void                            registerCallback(CallBackInterface* _cb);
26                 void                            registerCallback(vector<CallBackInterface*> _cb);
27                 int                                     callBack();
28                 void                            handleStruct(vector<data> vecData);
29                 vector<EnumC>           handleEnum(vector<EnumC> vecEn);
30
31                 void                            thread1();
32                 void                            thread2();
33
34         private:                
35                 int                                                     intA;
36                 float                                           floatB;
37                 string                                          stringC;
38                 CallBackInterface                       *cb;
39                 vector<CallBackInterface*>      cbvec;
40
41 };
42
43
44 TestClass::TestClass() {
45
46         intA = 1;
47         floatB = 2;
48         stringC = "345";
49         cb = NULL;
50         // cbvec doesn't need to be initialized again
51 }
52
53
54 TestClass::TestClass(int _int, float _float, string _string) {
55
56         intA = _int;
57         floatB = _float;
58         stringC = _string;
59         cb = NULL;
60         // cbvec doesn't need to be initialized again
61 }
62
63
64 void TestClass::setA(int _int) {
65
66         intA = _int;
67 }
68
69
70 void TestClass::setB(float _float) {
71
72         floatB = _float;
73 }
74
75
76 void TestClass::setC(string _string) {
77
78         stringC = _string;
79 }
80
81
82 string TestClass::sumArray(vector<string> newA) {
83
84         string sum = "";
85         int len = newA.size();
86         for(int c = 0; c < len; c++) {
87                 sum = sum + newA[c];
88         }
89         return sum;
90 }
91
92
93 /*int64_t TestClass::sumArray(vector<int> newA) {
94
95         int64_t sum = 0;
96         int len = newA.size();
97         for(int c = 0; c < len; c++) {
98                 sum = sum + newA[c];
99         }
100         return sum;
101 }*/
102
103
104 int TestClass::setAndGetA(int newA) {
105
106         intA = newA;
107         return intA;
108 }
109
110
111 int TestClass::setACAndGetA(string newC, int newA) {
112
113         stringC = newC;
114         intA = newA;
115         return intA;
116 }
117
118
119 void TestClass::registerCallback(CallBackInterface* _cb) {
120
121         cb = _cb;
122 }
123
124
125 void TestClass::registerCallback(vector<CallBackInterface*> _cb) {
126
127         for (CallBackInterface* cb : _cb) {
128                 cbvec.push_back(cb);
129                 cout << "Registering callback object!" << endl;
130         }
131 }
132
133
134 void TestClass::handleStruct(vector<data> vecData) {
135
136         for (data dat : vecData) {
137
138                 cout << "Name: " << dat.name << endl;
139                 cout << "Value: " << dat.value << endl;
140                 cout << "Year: " << dat.year << endl;
141         }
142 }
143
144
145 vector<EnumC> TestClass::handleEnum(vector<EnumC> vecEn) {
146
147         for (EnumC en : vecEn) {
148                 cout << "Enum: " << en << endl;
149         }
150         
151         return vecEn;
152 }
153
154
155 //int TestClass::callBack() {
156 //      return cb.printInt();
157 //}
158
159 void TestClass::thread1() {
160
161         CallBackInterface* cb = cbvec[0];
162         for(int i = 0; i < 10; i++) {
163                 cb->printInt();
164                 this_thread::sleep_for(chrono::seconds(1));
165         }       
166 }
167
168 void TestClass::thread2() {
169
170         CallBackInterface* cb = cbvec[1];
171         for(int i = 0; i < 10; i++) {
172                 cb->printInt();
173                 this_thread::sleep_for(chrono::seconds(1));
174         }       
175 }
176
177 int TestClass::callBack() {
178
179         /*int sum = 0;
180         for (CallBackInterface* cb : cbvec) {
181                 sum = sum + cb->printInt();
182         }
183
184         return sum;*/
185         thread th1 (&TestClass::thread1, this);
186         thread th2 (&TestClass::thread2, this);
187
188         th1.join();
189         th2.join();
190
191         return 1;
192 }
193
194 #endif
195