-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathexample.cpp
More file actions
85 lines (78 loc) · 2.41 KB
/
Copy pathexample.cpp
File metadata and controls
85 lines (78 loc) · 2.41 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
#include <list>
#include <gtest/gtest.h>
#include "serialize/baseline_serializer.h"
#include "serialize/baseline_deserializer.h"
struct C {
int a;
std::string b;
double c;
};
struct B {
int a = 1;
double b = 0.3f;
std::list<C> c;
};
struct A {
int a = 0;
double b = 0.2f;
std::string c;
B d;
std::vector<int> e;
std::map<std::string, B> f;
};
template <typename T>
std::string SerializeToString(const T& t) {
std::stringstream os;
std::shared_ptr<Serializer> serializer(new BaselineSerializer(os));
serializer->Serialize(t);
return os.str();
}
template <typename T>
std::optional<T> ParseFromString(const std::string& str) {
std::stringstream os;
os << str;
std::shared_ptr<Deserializer> deserializer(new BaselineDeserializer(os));
T ret;
if (deserializer->Deserialize(ret)) {
return ret;
} else {
return {};
}
}
int main(int argc, char **argv) {
A a;
a.c = "base";
a.d.b = 0.7f;
a.e.push_back(2);
a.e.push_back(3);
a.e.push_back(5);
a.f["wangqian"].a = 2;
a.f["joying"].b = 0.5f;
std::string str = SerializeToString(a);
if (auto opb = ParseFromString<A>(str)) {
auto& b = *opb;
std::cout << b.b << std::endl; // should be 0.2f;
std::cout << b.c << std::endl; // should be "base";
std::cout << b.d.b << std::endl; // should be 0.7;
std::cout << b.e.size() << std::endl; // should be 3;
std::cout << b.e[0] << std::endl; // should be 2;
std::cout << b.e[1] << std::endl; // should be 3;
std::cout << b.e[2] << std::endl; // should be 2;
std::cout << b.f.size() << std::endl; // should be 2;
std::cout << b.f["wangqian"].a << std::endl; // should be 2;
std::cout << b.f["joying"].b << std::endl; // should be 0.5;
} else {
std::cout << "Not Parsed Correctly." << std::endl;
}
C tmp = {1, "das", 0.2};
// 和C同构的tuple
using IsomorphismC = std::tuple<int, std::string, double>;
str = SerializeToString(tmp);
if (auto opc = ParseFromString<IsomorphismC>(str)) {
std::cout << std::get<0>(*opc) << std::endl; // should be 1;
std::cout << std::get<1>(*opc) << std::endl; // should be "das";
std::cout << std::get<2>(*opc) << std::endl; // should be 0.2f;
} else {
std::cout << "Not Parsed Correctly." << std::endl;
}
}