fe13e6e11c305e6ce8242264ec77c82e19cc021a
[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                 vector<data>            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 vector<data> 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         data newData;
143         newData.name = "Anonymous";
144         newData.value = 1.33;
145         newData.year = 2016;
146         vecData.push_back(newData);
147
148         return vecData;
149 }
150
151
152 vector<EnumC> TestClass::handleEnum(vector<EnumC> vecEn) {
153
154         for (EnumC en : vecEn) {
155                 cout << "Enum: " << en << endl;
156         }
157         
158         return vecEn;
159 }
160
161
162 //int TestClass::callBack() {
163 //      return cb.printInt();
164 //}
165
166 void TestClass::thread1() {
167
168         CallBackInterface* cb = cbvec[0];
169         for(int i = 0; i < 10; i++) {
170                 cb->printInt();
171                 this_thread::sleep_for(chrono::seconds(1));
172         }       
173 }
174
175 void TestClass::thread2() {
176
177         CallBackInterface* cb = cbvec[1];
178         for(int i = 0; i < 10; i++) {
179                 cb->printInt();
180                 this_thread::sleep_for(chrono::seconds(1));
181         }       
182 }
183
184 int TestClass::callBack() {
185
186         int sum = 0;
187         for (CallBackInterface* cb : cbvec) {
188                 sum = sum + cb->printInt();
189         }
190
191         return sum;
192 /*      thread th1 (&TestClass::thread1, this);
193         thread th2 (&TestClass::thread2, this);
194
195         th1.join();
196         th2.join();
197
198         return 1;*/
199 }
200
201 #endif
202