forked from PhysicsX/ExampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
103 lines (76 loc) · 2.2 KB
/
example.cpp
File metadata and controls
103 lines (76 loc) · 2.2 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
#include <iostream>
#include "example.pb.h"
#include <fstream> // std::filebuf
#include <chrono>
#include <thread>
int main()
{
/// serialize to bytearray
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;
}