00. 目录


文章目录


00. 目录01. 限定修饰符介绍02. 字段API分析03. 测试代码04. 编译和测试05. 参考


01. 限定修饰符介绍

repeated 代表可重复,我们可以理解为数组

syntax = "proto3";//指定版本信息,不指定会报错

message Person //message为关键字,作用为定义一种消息类型
{
string name = 1; //姓名
int32 id = 2; //id
string email = 3; //邮件
}

message AddressBook
{
repeated Person people = 1;
}

02. 字段API分析

deng@itcast:/mnt/hgfs/LinuxHome/day03$ protoc addressbook.proto --cpp_out=./
deng@itcast:/mnt/hgfs/LinuxHome/day03$

而对于字段修饰符为repeated的字段生成的函数,则稍微有一些不同,如people字段,则编译器会为其产生如下的代码:

enum : int {
kPeopleFieldNumber = 1,
};
// repeated .Person people = 1;
int people_size() const;
private:
int _internal_people_size() const;
public:
void clear_people();
::Person* mutable_people(int index);
::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::Person >*
mutable_people();
private:
const ::Person& _internal_people(int index) const;
::Person* _internal_add_people();
public:
const ::Person& people(int index) const;
::Person* add_people();
const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::Person >&
people() const;

03. 测试代码

参考代码如下:

#include "addressbook.pb.h"
#include <iostream>
#include <fstream>


using namespace std;

void set_addressbook()
{
AddressBook obj;

Person *p1 = obj.add_people(); //新增加一个Person
p1->set_name("tom");
p1->set_id(1);
p1->set_email("tom@qq.com");

Person *p2 = obj.add_people(); //新增加一个Person
p2->set_name("jim");
p2->set_id(2);
p2->set_email("jim@qq.com");

Person *p3 = obj.add_people(); //新增加一个Person
p3->set_name("abc");
p3->set_id(3);
p3->set_email("abc@qq.com");

fstream output("pb.itcast", ios::out | ios::trunc | ios::binary);

bool flag = obj.SerializeToOstream(&output);//序列化
if (!flag)
{
cerr << "Failed to write file." << endl;
return;
}

output.close();//关闭文件
}

void get_addressbook()
{
AddressBook obj;
fstream input("./pb.itcast", ios::in | ios::binary);
obj.ParseFromIstream(&input); //反序列化
input.close(); //关闭文件

for (int i = 0; i < obj.people_size(); i++)
{
const Person& person = obj.people(i);//取第i个people
cout << "第" << i + 1 << "个信息\n";
cout << "name = " << person.name() << endl;
cout << "id = " << person.id() << endl;
cout << "email = " << person.email() << endl << endl;
}
}

int main()
{
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;

set_addressbook(); //序列化
get_addressbook(); //反序列化

// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();

return 0;
}

04. 编译和测试

编译和测试

deng@itcast:/mnt/hgfs/LinuxHome/day03$ g++  test.cpp addressbook.pb.cc `pkg-config --libs --cflags protobuf`
deng@itcast:/mnt/hgfs/LinuxHome/day03$ ./a.out
第1个信息
name = tom
id = 1
email = tom@qq.com

第2个信息
name = jim
id = 2
email = jim@qq.com

第3个信息
name = abc
id = 3
email = abc@qq.com

deng@itcast:/mnt/hgfs/LinuxHome/day03$

【Protocol Buffer】Protocol Buffer入门教程(五):repeated限定修饰符_protobuf修饰符

05. 参考

官方参考文档:https://developers.google.cn/protocol-buffers/docs/reference/cpp-generated#repeatednumeric

测试代码下载:测试代码下载