forked from chronolaw/cpp_study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotobuf.cpp
More file actions
89 lines (65 loc) · 1.55 KB
/
protobuf.cpp
File metadata and controls
89 lines (65 loc) · 1.55 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
// Copyright (c) 2020 by Chrono
//
// sudo apt-get install protobuf-compiler
// sudo apt-get install libprotobuf-dev
// sudo apt-get install libprotoc-dev
//
// protoc --cpp_out=. sample.proto
//
// g++ protobuf.cpp -std=c++11 -lprotobuf -o a.out;./a.out
// g++ protobuf.cpp -std=c++14 -lprotobuf -o a.out;./a.out
#include <cassert>
#include <iostream>
#include <vector>
// just for convient
//#include "sample.pb.h"
#include "sample.pb.cc"
using namespace std;
void case1()
{
using vendor_t = sample::Vendor;
vendor_t v;
assert(!v.IsInitialized());
v.set_id(1);
v.set_name("sony");
v.set_valid(true);
assert(v.IsInitialized());
assert(v.has_id() && v.id() == 1);
assert(v.has_name() && v.name() == "sony");
assert(v.has_valid() && v.valid());
cout << v.DebugString() << endl;
string enc;
v.SerializeToString(&enc);
vendor_t v2;
assert(!v2.IsInitialized());
v2.ParseFromString(enc);
assert(v.id() == v2.id());
}
void case2()
{
auto alloc_vendor = []()
{
return new sample::Vendor;
};
auto v = alloc_vendor();
v->set_id(1);
v->set_name("sony");
v->set_valid(true);
assert(v->IsInitialized());
using product_t = sample::Product;
product_t p;
p.set_id(1);
p.set_name("walkman");
assert(p.tag_size() == 0);
p.add_tag("fashion");
p.add_tag("type_record");
assert(!p.has_vendor());
p.set_allocated_vendor(v);
}
int main()
{
//GOOGLE_PROTOBUF_VERIFY_VERSION;
case1();
case2();
cout << "protobuf demo" << endl;
}