-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexample.cpp
More file actions
137 lines (106 loc) · 3.11 KB
/
example.cpp
File metadata and controls
137 lines (106 loc) · 3.11 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
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include "test.pb.h"
#include <fstream> // std::filebuf
#include <chrono>
#include <thread>
int main()
{
/// serialize to bytearray
Test test;
test.set_number(1);
test.set_name("something");
endl(std::cout);
size_t size = test.ByteSizeLong();
std::cout<<"size "<<size<<std::endl;
// serialize to array
void *buffer = malloc(size);
bool flag = test.SerializeToArray(buffer, size);
if(flag == false)
std::cout<<"problem"<<std::endl;
Test testRead;
testRead.ParseFromArray(buffer, size);
std::cout<<"number "<<testRead.number()<<std::endl;
std::cout<<"name "<<testRead.name()<<std::endl;
// serialize to string
Test testString;
testString.set_number(23);
testString.add_id(24);
testString.add_id(25);
testString.add_id(26);
std::string str = testString.SerializeAsString();
Test testStringRead;
testStringRead.ParseFromString(str);
std::cout<<"number string "<<testStringRead.number()<<std::endl;
std::cout<<testStringRead.id_size()<<std::endl;
std::cout<<testStringRead.id(0)<<std::endl;
std::cout<<testStringRead.id(1)<<std::endl;
/*
Person per;
per.set_id(5000);
per.set_number(5000);
per.set_anotherid(5000);
per.set_aid(5000);
per.set_bid(5000);
per.set_cid(5000);
per.set_did(5000);
per.set_eid(5000);
per.set_fid(5000);
per.set_gid(5000);
//per.set_name("MadamCurie");
//gper.clear_name();
//std::cout<<per.name();
endl(std::cout);
size_t size = per.ByteSizeLong();
std::cout<<"size bytes "<<size<<std::endl;
void *buffer = malloc(size);
bool flag = per.SerializeToArray(buffer, size);
if(flag == false)
std::cout<<"serialize problem"<<std::endl;
Person per3;
per3.ParseFromArray(buffer,size);
endl(std::cout);
std::cout<<"id "<<per3.id();
endl(std::cout);
std::cout<<"id "<<per3.number();
endl(std::cout);
std::cout<<"id "<<per3.anotherid();
//serialize to string
std::string str = per.SerializeAsString();
uint32_t var = 5000;
std::cout<<"var size " <<sizeof(var)*10;
endl(std::cout);
std::cout<<"serialized string size " <<str.size();
endl(std::cout);
std::cout<<"serialized string " <<str;
Person per2;
per2.ParseFromString(str);
endl(std::cout);
std::cout<<"id "<<per2.id();
endl(std::cout);
std::cout<<"id "<<per2.number();
endl(std::cout);
std::cout<<"id "<<per2.anotherid();
// file example
Person file;
file.set_number(321);
std::string filename = "test.txt";
std::fstream output(filename, std::ios::out | std::ios::trunc | std::ios::binary);
if(!file.SerializeToOstream(&output))
{
std::cout<<"failed to write a file"<<std::endl;
}
output.close();
endl(std::cout);
Person file2;
std::fstream input(filename, std::ios::in | std::ios::binary);
if (!input) {
std::cout << ": File not found." << std::endl;
}
if(!file2.ParseFromIstream(&input))
{
std::cout<<"failed to read a file"<<std::endl;
}
std::cout<<"number from file "<<file2.number()<<std::endl;
*/
return 0;
}