-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
127 lines (94 loc) · 2.23 KB
/
main.cpp
File metadata and controls
127 lines (94 loc) · 2.23 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
#include <iostream>
#include <iomanip>
#include "serializer/Bundle.h"
#include "serializer/Serializable.h"
using namespace std;
class A: public Serializable {
public:
A(int a = 0, bool b = false, double c = 0) : a(a), b(b), c(c)
{
}
void printValues() {
cout << "a = " << a << endl
<< "b = " << b << endl
<< "c = " << c << endl
<< "d =";
for (auto s : d)
cout << " " << s;
cout << endl;
}
int a;
bool b;
double c;
vector<string> d;
private:
virtual void writeToBundle(Bundle & bundle) const {
bundle << a << b << c << d;
}
virtual void readFromBundle(Bundle & bundle) {
bundle >> a >> b >> c;
bundle.getArray(d);
}
};
int main(int argc, char **argv)
{
/////////////////// test with variables
cout << "variable serializations..." << endl << endl;
int a = 13, aa;
bool b = true, bb;
double c = 4232252.32329, cc;
string d = "abcdefghijklmnopqrstuvwxyz 1234567890 !'^+%&/()=?_ >£#$½¾{[]}\\|", dd;
Bundle bundle;
cout << "serialized values:" << endl
<< "a = " << a << endl
<< "b = " << b << endl
<< "c = " << std::fixed << c << endl
<< "d = " << d << endl
<< endl
<< "deserialized values:" << endl;
// serialize
bundle << a << b << c << d;
// int len = bundle.byteCount();
// bundle.exportData(u8buf, size);
// bundle.importData(u8buf, len)
while (bundle.count())
{
switch (bundle.getNextType())
{
case Bundle::TYPE_INT:
aa = bundle.getInt();
cout << "int => " << aa << endl;
break;
case Bundle::TYPE_BOOL:
bb = bundle.getBool();
cout << "bool => " << bb << endl;
break;
case Bundle::TYPE_DOUBLE:
bundle >> cc;
cout << "double => " << cc << endl;
break;
case Bundle::TYPE_STRING:
bundle >> dd;
cout << "string => " << dd << endl;
break;
default:
cerr << "ERROR: undef type" << endl;
exit(1);
}
}
/////////////// test with objs
bundle.clear();
cout << endl << endl << "Object serialization..." << endl << endl;
A obj(523, false, 42523.23242);
obj.d.push_back("qwerty");
obj.d.push_back("asdfgh");
obj.d.push_back("zxcvbn");
cout << "original obj:" << endl;
obj.printValues();
bundle << obj;
A obj2;
bundle >> obj2;
cout << endl << "serialized obj:" << endl;
obj2.printValues();
return 0;
}