go的go语言 protobuff这个问题是不是一个bug

在 Golang 中使用 Protobuf - 推酷
在 Golang 中使用 Protobuf
项目为 Golang 提供了
安装 goprotobuf
获取 Protobuf 编译器 protoc(可下载到 Windows 下的二进制版本)
获取 goprotobuf 提供的 Protobuf 编译器插件 protoc-gen-go(被放置于 $GOPATH/bin 下,$GOPATH/bin 应该被加入 PATH 环境变量,以便 protoc 能够找到 protoc-gen-go)
go get /p/goprotobuf/protoc-gen-go
此插件被 protoc 使用,用于编译 .proto 文件为 Golang 源文件,通过此源文件可以使用定义在 .proto 文件中的消息。
获取 goprotobuf 提供的支持库,包含诸如编码(marshaling)、解码(unmarshaling)等功能
go get /p/goprotobuf/proto
使用 goprotobuf
这里通过一个例子来说明用法。先创建一个 .proto 文件 test.proto:
enum FOO { X = 17; };
message Test {
required string label = 1;
optional int32 type = 2 [default=77];
repeated int64 reps = 3;
optional group OptionalGroup = 4 {
required string RequiredField = 5;
编译此 .proto 文件:
protoc --go_out=. *.proto
这里通过 –go_out 来使用 goprotobuf 提供的 Protobuf 编译器插件 protoc-gen-go。这时候我们会生成一个名为 test.pb.go 的源文件。
在使用之前,我们先了解一下每个 Protobuf 消息在 Golang 中有哪一些可用的接口:
每一个 Protobuf 消息对应一个 Golang 结构体
消息中域名字为 camel_case 在对应的 Golang 结构体中为 CamelCase
消息对应的 Golang 结构体中不存在 setter 方法,只需要直接对结构体赋值即可,赋值时可能使用到一些辅助函数,例如:
msg.Foo = proto.String(&hello&)
消息对应的 Golang 结构体中存在 getter 方法,用于返回域的值,如果域未设置值,则返回一个默认值
消息中非 repeated 的域都被实现为一个指针,指针为 nil 时表示域未设置
消息中 repeated 的域被实现为 slice
访问枚举值时,使用“枚举类型名_枚举名”的格式(更多内容可以直接阅读生成的源码)
使用 proto.Marshal 函数进行编码,使用 proto.Unmarshal 函数进行解码
现在我们编写一个小程序:
package main
&/p/goprotobuf/proto&
// test.pb.go 的路径
func main() {
// 创建一个消息 Test
test := &example.Test{
// 使用辅助函数设置域的值
Label: proto.String(&hello&),
proto.Int32(17),
Optionalgroup: &example.Test_OptionalGroup{
RequiredField: proto.String(&good bye&),
// 进行编码
data, err := proto.Marshal(test)
if err != nil {
log.Fatal(&marshaling error: &, err)
// 进行解码
newTest := &example.Test{}
err = proto.Unmarshal(data, newTest)
if err != nil {
log.Fatal(&unmarshaling error: &, err)
// 测试结果
if test.GetLabel() != newTest.GetLabel() {
log.Fatalf(&data mismatch %q != %q&, test.GetLabel(), newTest.GetLabel())
关于 goprotobuf 更为详尽的使用文档见:
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致protobuf网:检测到可能的递归-c#,序列化serialization,递归recursion,protobuf-net-CodeGo.net
protobuf网:检测到可能的递归
我得到一个异常试图序列化对象图(不是很深)。它有意义的部分是这样的:
[错误]致命未处理的异常:ProtoBuf.ProtoException:可能
递归ðetected(偏移:5级(S)):于红
ProtoBuf.ProtoWriter。 (对象)&0x00127&在
ProtoBuf.ProtoWriter.StartSubItem(对象,ProtoBuf.ProtoWriter,布尔)
该图表示的文件/目录结构和我的模型(简化)看起来像这样:
[ProtoContract]
[ProtoInclude(100, typeof(PackageDirectory))]
[ProtoInclude(200, typeof(PackageFile))]
public abstract class PackageMember
[ProtoMember(1)]
public virtual string Name { }
[ProtoMember(2, AsReference=true)]
public PackageDirectory ParentDirectory { }
[ProtoContract]
public class PackageDirectory : PackageMember
[ProtoMember(3)]
private Dictionary&string, PackageMember& _
public PackageDirectory()
_children = new Dictionary&string, PackageMember&();
public PackageDirectory (string name, PackageDirectory parentDirectory)
this.ParentDirectory = parentD
this.Name =
public void Add (PackageMember member)
_children.Add(member.Name, member);
[ProtoContract]
public class PackageFile : PackageMember
private Stream _
private BinaryReader _
private PackageFile()
public PackageFile (string name, int offset, int length, PackageDirectory directory, Stream file)
this.Name =
this.Length =
this.Offset =
this.ParentDirectory =
_reader = new BinaryReader(_file);
[OnDeserialized]
protected virtual void OnDeserialized(SerializationContext context)
var deserializationContext = context.Context as DeserializationC
if (deserializationContext != null)
_file = deserializationContext.FileS
_reader = new BinaryReader(_file);
[ProtoMember(3)]
public int Offset { }
[ProtoMember(4)]
public int Length { }
这棵树的深度是近10-15的水平,这比ProtoBuf.ProtoWriter.RecursionCheckDepth值(25)。 (所以,也许这是一个bug?)
使用的版本是从主干V2(转491)。
其实,我解决了它与代码修改。我改变的价值ProtoBuf.ProtoWriter.RecursionCheckDepth100,一切都似乎是确定。
现在的问题是 CodeGo.net,如果有任何“真正”的方式而不protobuf的修改代码来序列化这类图的?这样的行为,正确的或这是一个错误?
我的平台是给Mono-2.10-8在Windows 7专业版64位
P.S.此外,我发现,如果我deserizlie与THW下面的代码,我应该有PackageDirectory构造函数是公共的。
var value = new PackageDirectory();
RuntimeTypeModel.Default.Deserialize(ms, value, typeof(PackageDirectory), new SerializationContext {
Context = new DeserializationContext {
FileStream = _file,
这是另一个话题,但它很好地呈现代码。我认为,在这种情况下,宣布私有构造函数应该允许现在的行为不同于一个用于Serializer.Deserialize(...)。
本文地址 :CodeGo.net/441196/
-------------------------------------------------------------------------------------------------------------------------
1. 抛出此异常仅当参考被认为是在数据(两次路径),并轨迹才会启用时,深度至少RecursionCheckDepth。其中10-15深度限制引,虽然它不一定是protobuf的处理水平相当的,你指望的情况。它没有任何意义增加这个值100应该使其工作-事实上,生存的本RecursionCheckDepth纯粹是一种优化,以限制参与“典型”图的功夫 CodeGo.net,只有使更多的检查,如果它开始看起来很深。
然而,我,铭记,这也可能意味着在基于继承的处理微妙的错误,或许也与AsReference。广泛的,不断的,我还没有看到这样一个问题。如果你有一个可重复再现我非常希望看到它。
本文标题 :protobuf网:检测到可能的递归
本文地址 :CodeGo.net/441196/
Copyright (C) 2014 CodeGo.netPages: 1/2
主题 : cocos code ide+lua+protobuf编译问题
级别: 侠客
可可豆: 276 CB
威望: 276 点
在线时间: 47(时)
发自: Web Page
cocos code ide+lua+protobuf编译问题&&&
本帖被 honghui 从 Quick-Cocos2d-x讨论区 移动到本区()
在cocos code ide中,LUA语言中调用protobuf,使用protoc-gen-lua转换.proto文件为.lua文件再使用。运行时,提示找不到pb.lua文件。我看了下protobuf文件夹下的文件中的确有文件decoder.lua使用了require &pb&这个语句,但是这个文件夹中的确没有pb.lua文件,只有pb.c文件。不知道各位谁碰到过类似问题,帮忙解决下
UID: 42534
发帖: 52897
可可豆: 524806 CB
威望: 524071 点
在线时间: 1012(时)
发自: Web Page
这个链接应该可以给你帮助。
级别: 侠客
可可豆: 276 CB
威望: 276 点
在线时间: 47(时)
发自: Web Page
引用 引用第1楼yangtao19cs于 17:00发表的&&:这个链接应该可以给你帮助。 非常感谢,不过这个和我的不太一样,他里面用的好像是quick cocos,而我用的是原生的cocos,另外,我是在windows下做的开发,处理pb.c的这个步骤会有问题
级别: 侠客
可可豆: 276 CB
威望: 276 点
在线时间: 47(时)
发自: Web Page
UID: 42534
发帖: 52897
可可豆: 524806 CB
威望: 524071 点
在线时间: 1012(时)
发自: Web Page
回 3楼(releax008) 的帖子
你把pb.c 的引用头替换下面的试试。#include &stdint.h&#include &string.h&#include &lua.h&#include &lualib.h&#include &lauxlib.h&#include &CCPlatformConfig.h&#if WIN32#define __LITTLE_ENDIAN 1234#define __BYTE_ORDER __LITTLE_ENDIAN#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include &endian.h&#else&&&&#include &machine/endian.h&#endif #if __BYTE_ORDER == __LITTLE_ENDIAN#define IS_LITTLE_ENDIAN#endif#define IOSTRING_META &protobuf.IOString&#define checkiostring(L) \&&&&(IOString*) luaL_checkudata(L, 1, IOSTRING_META)#define IOSTRING_BUF_LEN 65535
级别: 侠客
可可豆: 276 CB
威望: 276 点
在线时间: 47(时)
发自: Web Page
替换了,没有效果。我还这样做了,改了pb.c,将pb.c加入到cocos code ide生成的vc工程中,在这个VC工程中打开pb库(调用了一个函数),具体残开这个帖子。在cocos code ide中用命令build runtiem不过到最后还是没有用。今天用build runtime还出来莫名其妙的错误
UID: 42534
发帖: 52897
可可豆: 524806 CB
威望: 524071 点
在线时间: 1012(时)
发自: Web Page
回 5楼(releax008) 的帖子
具体什么错误,我这边windows版本的都可以使用。
级别: 侠客
可可豆: 276 CB
威望: 276 点
在线时间: 47(时)
发自: Web Page
回 6楼(yangtao19cs) 的帖子
[LUA-print] LUA ERROR: [string &E:/test003/Test101/src/protobuf.lua&]:31: module 'pb' not found:&&&&no field package.preload['pb']&&&&no file '.\pb.lua'&&&&no file 'E:\test003\Test101\runtime\win32\lua\pb.lua'&&&&no file 'E:\test003\Test101\runtime\win32\lua\pb\init.lua'&&&&no file 'C:\Program Files (x86)\Lua\5.1\lua\pb.luac'&&&&no file '.\pb.dll'&&&&no file 'E:\test003\Test101\runtime\win32\pb.dll'&&&&no file 'E:\test003\Test101\runtime\win32\loadall.dll'
级别: 侠客
可可豆: 276 CB
威望: 276 点
在线时间: 47(时)
发自: Web Page
回 6楼(yangtao19cs) 的帖子
我用的环境是这样的,win7 64+ vs2013 + cocos 2d 3.2rc()&&+ cocos code ide1.02beta + lua + protoc-gen-lua(最新网上下的),我想在lua中直接调用protobuf。你的呢.protobuf在附件里面
描述:protobuf网上下的版本
(1047 K) 下载次数:23
级别: 侠客
可可豆: 276 CB
威望: 276 点
在线时间: 47(时)
发自: Web Page
回 6楼(yangtao19cs) 的帖子
帮我看下我附件文件夹里面有没有少文件
Pages: 1/2
关注本帖(如果有新回复会站内信通知您)
苹果公司现任CEO是谁?2字 正确答案:库克
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版google protobuf 使用示例
1 定义.proto接口文件
message Person {
required string name = 1;
required int32 id = 2; //unique ID number for this person
optional string email = 3;
enum PhoneType {
MOBILE = 0;
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
repeated PhoneNumber phone = 4;
message AddressBook {
repeated Person person = 1;
service SearchService {
rpc Search (Person) returns (Person);
}自动生成的类:tutorial::Persontutorial::Person::PhoneNumber
全名其实为:typedef Person_PhoneNumber PhoneN枚举见黄色部分
全名其实为:typedef Person_PhoneType PhoneT
tutorial::AddressBook
注意引用时候的命名空间,与接口定义的结构之间的关系,是彼此对应的~~~
// Generated by the protocol buffer compiler.
DO NOT EDIT!
// source: addressbook.proto
#ifndef PROTOBUF_addressbook_2eproto__INCLUDED
#define PROTOBUF_addressbook_2eproto__INCLUDED
#include &string&
#include &google/protobuf/stubs/common.h&
#if GOOGLE_PROTOBUF_VERSION & 2004
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers.
Please update
#error your headers.
#if 2004001 & GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers.
#error regenerate this file with a newer version of protoc.
#include &google/protobuf/generated_message_util.h&
#include &google/protobuf/repeated_field.h&
#include &google/protobuf/extension_set.h&
#include &google/protobuf/generated_message_reflection.h&
// @@protoc_insertion_point(includes)
namespace tutorial {
// Internal implementation detail -- do not call these.
protobuf_AddDesc_addressbook_2eproto();
void protobuf_AssignDesc_addressbook_2eproto();
void protobuf_ShutdownFile_addressbook_2eproto();
class Person_PhoneN
class AddressB
enum Person_PhoneType {
Person_PhoneType_MOBILE = 0,
Person_PhoneType_HOME = 1,
Person_PhoneType_WORK = 2
bool Person_PhoneType_IsValid(int value);
const Person_PhoneType Person_PhoneType_PhoneType_MIN = Person_PhoneType_MOBILE;
const Person_PhoneType Person_PhoneType_PhoneType_MAX = Person_PhoneType_WORK;
const int Person_PhoneType_PhoneType_ARRAYSIZE = Person_PhoneType_PhoneType_MAX + 1;
const ::google::protobuf::EnumDescriptor* Person_PhoneType_descriptor();
inline const ::std::string& Person_PhoneType_Name(Person_PhoneType value) {
return ::google::protobuf::internal::NameOfEnum(
Person_PhoneType_descriptor(), value);
inline bool Person_PhoneType_Parse(
const ::std::string& name, Person_PhoneType* value) {
return ::google::protobuf::internal::ParseNamedEnum&Person_PhoneType&(
Person_PhoneType_descriptor(), name, value);
// ===================================================================
class Person_PhoneNumber : public ::google::protobuf::Message {
Person_PhoneNumber();
virtual ~Person_PhoneNumber();
Person_PhoneNumber(const Person_PhoneNumber& from);
inline Person_PhoneNumber& operator=(const Person_PhoneNumber& from) {
CopyFrom(from);
return *this;
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
return _unknown_fields_;
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
return &_unknown_fields_;
static const ::google::protobuf::Descriptor* descriptor();
static const Person_PhoneNumber& default_instance();
void Swap(Person_PhoneNumber* other);
// implements Message -
Person_PhoneNumber* New() const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const Person_PhoneNumber& from);
void MergeFrom(const Person_PhoneNumber& from);
void Clear();
bool IsInitialized() const;
int ByteSize() const;
bool MergePartialFromCodedStream(
::google::protobuf::io::CodedInputStream* input);
void SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream* output) const;
::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const;
int GetCachedSize() const { return _cached_size_; }
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
::google::protobuf::Metadata GetMetadata() const;
// nested types -
// accessors -
// required string number = 1;
inline bool has_number() const;
inline void clear_number();
static const int kNumberFieldNumber = 1;
inline const ::std::string& number() const;
inline void set_number(const ::std::string& value);
inline void set_number(const char* value);
inline void set_number(const char* value, size_t size);
inline ::std::string* mutable_number();
inline ::std::string* release_number();
// optional .tutorial.Person.PhoneType type = 2 [default = HOME];
inline bool has_type() const;
inline void clear_type();
static const int kTypeFieldNumber = 2;
inline ::tutorial::Person_PhoneType type() const;
inline void set_type(::tutorial::Person_PhoneType value);
// @@protoc_insertion_point(class_scope:tutorial.Person.PhoneNumber)
inline void set_has_number();
inline void clear_has_number();
inline void set_has_type();
inline void clear_has_type();
::google::protobuf::UnknownFieldSet _unknown_fields_;
::std::string* number_;
int type_;
mutable int _cached_size_;
::google::protobuf::uint32 _has_bits_[(2 + 31) / 32];
friend void
protobuf_AddDesc_addressbook_2eproto();
friend void protobuf_AssignDesc_addressbook_2eproto();
friend void protobuf_ShutdownFile_addressbook_2eproto();
void InitAsDefaultInstance();
static Person_PhoneNumber* default_instance_;
class Person : public ::google::protobuf::Message {
virtual ~Person();
Person(const Person& from);
inline Person& operator=(const Person& from) {
CopyFrom(from);
return *this;
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
return _unknown_fields_;
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
return &_unknown_fields_;
static const ::google::protobuf::Descriptor* descriptor();
static const Person& default_instance();
void Swap(Person* other);
// implements Message -
Person* New() const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const Person& from);
void MergeFrom(const Person& from);
void Clear();
bool IsInitialized() const;
int ByteSize() const;
bool MergePartialFromCodedStream(
::google::protobuf::io::CodedInputStream* input);
void SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream* output) const;
::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const;
int GetCachedSize() const { return _cached_size_; }
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
::google::protobuf::Metadata GetMetadata() const;
// nested types -
typedef Person_PhoneNumber PhoneN
typedef Person_PhoneType PhoneT
static const PhoneType MOBILE = Person_PhoneType_MOBILE;
static const PhoneType HOME = Person_PhoneType_HOME;
static const PhoneType WORK = Person_PhoneType_WORK;
static inline bool PhoneType_IsValid(int value) {
return Person_PhoneType_IsValid(value);
static const PhoneType PhoneType_MIN =
Person_PhoneType_PhoneType_MIN;
static const PhoneType PhoneType_MAX =
Person_PhoneType_PhoneType_MAX;
static const int PhoneType_ARRAYSIZE =
Person_PhoneType_PhoneType_ARRAYSIZE;
static inline const ::google::protobuf::EnumDescriptor*
PhoneType_descriptor() {
return Person_PhoneType_descriptor();
static inline const ::std::string& PhoneType_Name(PhoneType value) {
return Person_PhoneType_Name(value);
static inline bool PhoneType_Parse(const ::std::string& name,
PhoneType* value) {
return Person_PhoneType_Parse(name, value);
// accessors -
// required string name = 1;
inline bool has_name() const;
inline void clear_name();
static const int kNameFieldNumber = 1;
inline const ::std::string& name() const;
inline void set_name(const ::std::string& value);
inline void set_name(const char* value);
inline void set_name(const char* value, size_t size);
inline ::std::string* mutable_name();
inline ::std::string* release_name();
// required int32 id = 2;
inline bool has_id() const;
inline void clear_id();
static const int kIdFieldNumber = 2;
inline ::google::protobuf::int32 id() const;
inline void set_id(::google::protobuf::int32 value);
// optional string email = 3;
inline bool has_email() const;
inline void clear_email();
static const int kEmailFieldNumber = 3;
inline const ::std::string& email() const;
inline void set_email(const ::std::string& value);
inline void set_email(const char* value);
inline void set_email(const char* value, size_t size);
inline ::std::string* mutable_email();
inline ::std::string* release_email();
// repeated .tutorial.Person.PhoneNumber phone = 4;
inline int phone_size() const;
inline void clear_phone();
static const int kPhoneFieldNumber = 4;
inline const ::tutorial::Person_PhoneNumber& phone(int index) const;
inline ::tutorial::Person_PhoneNumber* mutable_phone(int index);
inline ::tutorial::Person_PhoneNumber* add_phone();
inline const ::google::protobuf::RepeatedPtrField& ::tutorial::Person_PhoneNumber &&
phone() const;
inline ::google::protobuf::RepeatedPtrField& ::tutorial::Person_PhoneNumber &*
mutable_phone();
// @@protoc_insertion_point(class_scope:tutorial.Person)
inline void set_has_name();
inline void clear_has_name();
inline void set_has_id();
inline void clear_has_id();
inline void set_has_email();
inline void clear_has_email();
::google::protobuf::UnknownFieldSet _unknown_fields_;
::std::string* name_;
::std::string* email_;
::google::protobuf::RepeatedPtrField& ::tutorial::Person_PhoneNumber & phone_;
::google::protobuf::int32 id_;
mutable int _cached_size_;
::google::protobuf::uint32 _has_bits_[(4 + 31) / 32];
friend void
protobuf_AddDesc_addressbook_2eproto();
friend void protobuf_AssignDesc_addressbook_2eproto();
friend void protobuf_ShutdownFile_addressbook_2eproto();
void InitAsDefaultInstance();
static Person* default_instance_;
class AddressBook : public ::google::protobuf::Message {
AddressBook();
virtual ~AddressBook();
AddressBook(const AddressBook& from);
inline AddressBook& operator=(const AddressBook& from) {
CopyFrom(from);
return *this;
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
return _unknown_fields_;
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
return &_unknown_fields_;
static const ::google::protobuf::Descriptor* descriptor();
static const AddressBook& default_instance();
void Swap(AddressBook* other);
// implements Message -
AddressBook* New() const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const AddressBook& from);
void MergeFrom(const AddressBook& from);
void Clear();
bool IsInitialized() const;
int ByteSize() const;
bool MergePartialFromCodedStream(
::google::protobuf::io::CodedInputStream* input);
void SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream* output) const;
::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const;
int GetCachedSize() const { return _cached_size_; }
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
::google::protobuf::Metadata GetMetadata() const;
// nested types -
// accessors -
// repeated .tutorial.Person person = 1;
inline int person_size() const;
inline void clear_person();
static const int kPersonFieldNumber = 1;
inline const ::tutorial::Person& person(int index) const;
inline ::tutorial::Person* mutable_person(int index);
inline ::tutorial::Person* add_person();
inline const ::google::protobuf::RepeatedPtrField& ::tutorial::Person &&
person() const;
inline ::google::protobuf::RepeatedPtrField& ::tutorial::Person &*
mutable_person();
// @@protoc_insertion_point(class_scope:tutorial.AddressBook)
::google::protobuf::UnknownFieldSet _unknown_fields_;
::google::protobuf::RepeatedPtrField& ::tutorial::Person & person_;
mutable int _cached_size_;
::google::protobuf::uint32 _has_bits_[(1 + 31) / 32];
friend void
protobuf_AddDesc_addressbook_2eproto();
friend void protobuf_AssignDesc_addressbook_2eproto();
friend void protobuf_ShutdownFile_addressbook_2eproto();
void InitAsDefaultInstance();
static AddressBook* default_instance_;
// ===================================================================
// ===================================================================
// Person_PhoneNumber
// required string number = 1;
inline bool Person_PhoneNumber::has_number() const {
return (_has_bits_[0] & 0x01u) != 0;
inline void Person_PhoneNumber::set_has_number() {
_has_bits_[0] = 0x01u;
inline void Person_PhoneNumber::clear_has_number() {
_has_bits_[0] &= ~0x01u;
inline void Person_PhoneNumber::clear_number() {
if (number_ != &::google::protobuf::internal::kEmptyString) {
number_-&clear();
clear_has_number();
inline const ::std::string& Person_PhoneNumber::number() const {
return *number_;
inline void Person_PhoneNumber::set_number(const ::std::string& value) {
set_has_number();
if (number_ == &::google::protobuf::internal::kEmptyString) {
number_ = new ::std::string;
number_-&assign(value);
inline void Person_PhoneNumber::set_number(const char* value) {
set_has_number();
if (number_ == &::google::protobuf::internal::kEmptyString) {
number_ = new ::std::string;
number_-&assign(value);
inline void Person_PhoneNumber::set_number(const char* value, size_t size) {
set_has_number();
if (number_ == &::google::protobuf::internal::kEmptyString) {
number_ = new ::std::string;
number_-&assign(reinterpret_cast&const char*&(value), size);
inline ::std::string* Person_PhoneNumber::mutable_number() {
set_has_number();
if (number_ == &::google::protobuf::internal::kEmptyString) {
number_ = new ::std::string;
return number_;
inline ::std::string* Person_PhoneNumber::release_number() {
clear_has_number();
if (number_ == &::google::protobuf::internal::kEmptyString) {
return NULL;
::std::string* temp = number_;
number_ = const_cast& ::std::string*&(&::google::protobuf::internal::kEmptyString);
// optional .tutorial.Person.PhoneType type = 2 [default = HOME];
inline bool Person_PhoneNumber::has_type() const {
return (_has_bits_[0] & 0x02u) != 0;
inline void Person_PhoneNumber::set_has_type() {
_has_bits_[0] = 0x02u;
inline void Person_PhoneNumber::clear_has_type() {
_has_bits_[0] &= ~0x02u;
inline void Person_PhoneNumber::clear_type() {
type_ = 1;
clear_has_type();
inline ::tutorial::Person_PhoneType Person_PhoneNumber::type() const {
return static_cast& ::tutorial::Person_PhoneType &(type_);
inline void Person_PhoneNumber::set_type(::tutorial::Person_PhoneType value) {
GOOGLE_DCHECK(::tutorial::Person_PhoneType_IsValid(value));
set_has_type();
// required string name = 1;
inline bool Person::has_name() const {
return (_has_bits_[0] & 0x01u) != 0;
inline void Person::set_has_name() {
_has_bits_[0] = 0x01u;
inline void Person::clear_has_name() {
_has_bits_[0] &= ~0x01u;
inline void Person::clear_name() {
if (name_ != &::google::protobuf::internal::kEmptyString) {
name_-&clear();
clear_has_name();
inline const ::std::string& Person::name() const {
return *name_;
inline void Person::set_name(const ::std::string& value) {
set_has_name();
if (name_ == &::google::protobuf::internal::kEmptyString) {
name_ = new ::std::string;
name_-&assign(value);
inline void Person::set_name(const char* value) {
set_has_name();
if (name_ == &::google::protobuf::internal::kEmptyString) {
name_ = new ::std::string;
name_-&assign(value);
inline void Person::set_name(const char* value, size_t size) {
set_has_name();
if (name_ == &::google::protobuf::internal::kEmptyString) {
name_ = new ::std::string;
name_-&assign(reinterpret_cast&const char*&(value), size);
inline ::std::string* Person::mutable_name() {
set_has_name();
if (name_ == &::google::protobuf::internal::kEmptyString) {
name_ = new ::std::string;
return name_;
inline ::std::string* Person::release_name() {
clear_has_name();
if (name_ == &::google::protobuf::internal::kEmptyString) {
return NULL;
::std::string* temp = name_;
name_ = const_cast& ::std::string*&(&::google::protobuf::internal::kEmptyString);
// required int32 id = 2;
inline bool Person::has_id() const {
return (_has_bits_[0] & 0x02u) != 0;
inline void Person::set_has_id() {
_has_bits_[0] = 0x02u;
inline void Person::clear_has_id() {
_has_bits_[0] &= ~0x02u;
inline void Person::clear_id() {
clear_has_id();
inline ::google::protobuf::int32 Person::id() const {
return id_;
inline void Person::set_id(::google::protobuf::int32 value) {
set_has_id();
// optional string email = 3;
inline bool Person::has_email() const {
return (_has_bits_[0] & 0x04u) != 0;
inline void Person::set_has_email() {
_has_bits_[0] = 0x04u;
inline void Person::clear_has_email() {
_has_bits_[0] &= ~0x04u;
inline void Person::clear_email() {
if (email_ != &::google::protobuf::internal::kEmptyString) {
email_-&clear();
clear_has_email();
inline const ::std::string& Person::email() const {
return *email_;
inline void Person::set_email(const ::std::string& value) {
set_has_email();
if (email_ == &::google::protobuf::internal::kEmptyString) {
email_ = new ::std::string;
email_-&assign(value);
inline void Person::set_email(const char* value) {
set_has_email();
if (email_ == &::google::protobuf::internal::kEmptyString) {
email_ = new ::std::string;
email_-&assign(value);
inline void Person::set_email(const char* value, size_t size) {
set_has_email();
if (email_ == &::google::protobuf::internal::kEmptyString) {
email_ = new ::std::string;
email_-&assign(reinterpret_cast&const char*&(value), size);
inline ::std::string* Person::mutable_email() {
set_has_email();
if (email_ == &::google::protobuf::internal::kEmptyString) {
email_ = new ::std::string;
return email_;
inline ::std::string* Person::release_email() {
clear_has_email();
if (email_ == &::google::protobuf::internal::kEmptyString) {
return NULL;
::std::string* temp = email_;
email_ = const_cast& ::std::string*&(&::google::protobuf::internal::kEmptyString);
// repeated .tutorial.Person.PhoneNumber phone = 4;
inline int Person::phone_size() const {
return phone_.size();
inline void Person::clear_phone() {
phone_.Clear();
inline const ::tutorial::Person_PhoneNumber& Person::phone(int index) const {
return phone_.Get(index);
inline ::tutorial::Person_PhoneNumber* Person::mutable_phone(int index) {
return phone_.Mutable(index);
inline ::tutorial::Person_PhoneNumber* Person::add_phone() {
return phone_.Add();
inline const ::google::protobuf::RepeatedPtrField& ::tutorial::Person_PhoneNumber &&
Person::phone() const {
return phone_;
inline ::google::protobuf::RepeatedPtrField& ::tutorial::Person_PhoneNumber &*
Person::mutable_phone() {
return &phone_;
// AddressBook
// repeated .tutorial.Person person = 1;
inline int AddressBook::person_size() const {
return person_.size();
inline void AddressBook::clear_person() {
person_.Clear();
inline const ::tutorial::Person& AddressBook::person(int index) const {
return person_.Get(index);
inline ::tutorial::Person* AddressBook::mutable_person(int index) {
return person_.Mutable(index);
inline ::tutorial::Person* AddressBook::add_person() {
return person_.Add();
inline const ::google::protobuf::RepeatedPtrField& ::tutorial::Person &&
AddressBook::person() const {
return person_;
inline ::google::protobuf::RepeatedPtrField& ::tutorial::Person &*
AddressBook::mutable_person() {
return &person_;
// @@protoc_insertion_point(namespace_scope)
// namespace tutorial
#ifndef SWIG
namespace google {
namespace protobuf {
template &&
inline const EnumDescriptor* GetEnumDescriptor& ::tutorial::Person_PhoneType&() {
return ::tutorial::Person_PhoneType_descriptor();
// namespace google
// namespace protobuf
// @@protoc_insertion_point(global_scope)
// PROTOBUF_addressbook_2eproto__INCLUDED
// Generated by the protocol buffer compiler.
DO NOT EDIT!
#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
#include "addressbook.pb.h"
#include &algorithm&
#include &google/protobuf/stubs/once.h&
#include &google/protobuf/io/coded_stream.h&
#include &google/protobuf/wire_format_lite_inl.h&
#include &google/protobuf/descriptor.h&
#include &google/protobuf/reflection_ops.h&
#include &google/protobuf/wire_format.h&
// @@protoc_insertion_point(includes)
namespace tutorial {
namespace {
const ::google::protobuf::Descriptor* Person_descriptor_ = NULL;
const ::google::protobuf::internal::GeneratedMessageReflection*
Person_reflection_ = NULL;
const ::google::protobuf::Descriptor* Person_PhoneNumber_descriptor_ = NULL;
const ::google::protobuf::internal::GeneratedMessageReflection*
Person_PhoneNumber_reflection_ = NULL;
const ::google::protobuf::EnumDescriptor* Person_PhoneType_descriptor_ = NULL;
const ::google::protobuf::Descriptor* AddressBook_descriptor_ = NULL;
const ::google::protobuf::internal::GeneratedMessageReflection*
AddressBook_reflection_ = NULL;
// namespace
void protobuf_AssignDesc_addressbook_2eproto() {
protobuf_AddDesc_addressbook_2eproto();
const ::google::protobuf::FileDescriptor* file =
::google::protobuf::DescriptorPool::generated_pool()-&FindFileByName(
"addressbook.proto");
GOOGLE_CHECK(file != NULL);
Person_descriptor_ = file-&message_type(0);
static const int Person_offsets_[4] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Person, name_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Person, id_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Person, email_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Person, phone_),
Person_reflection_ =
new ::google::protobuf::internal::GeneratedMessageReflection(
Person_descriptor_,
Person::default_instance_,
Person_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Person, _has_bits_[0]),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Person, _unknown_fields_),
::google::protobuf::DescriptorPool::generated_pool(),
::google::protobuf::MessageFactory::generated_factory(),
sizeof(Person));
Person_PhoneNumber_descriptor_ = Person_descriptor_-&nested_type(0);
static const int Person_PhoneNumber_offsets_[2] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Person_PhoneNumber, number_),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Person_PhoneNumber, type_),
Person_PhoneNumber_reflection_ =
new ::google::protobuf::internal::GeneratedMessageReflection(
Person_PhoneNumber_descriptor_,
Person_PhoneNumber::default_instance_,
Person_PhoneNumber_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Person_PhoneNumber, _has_bits_[0]),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Person_PhoneNumber, _unknown_fields_),
::google::protobuf::DescriptorPool::generated_pool(),
::google::protobuf::MessageFactory::generated_factory(),
sizeof(Person_PhoneNumber));
Person_PhoneType_descriptor_ = Person_descriptor_-&enum_type(0);
AddressBook_descriptor_ = file-&message_type(1);
static const int AddressBook_offsets_[1] = {
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(AddressBook, person_),
AddressBook_reflection_ =
new ::google::protobuf::internal::GeneratedMessageReflection(
AddressBook_descriptor_,
AddressBook::default_instance_,
AddressBook_offsets_,
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(AddressBook, _has_bits_[0]),
GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(AddressBook, _unknown_fields_),
::google::protobuf::DescriptorPool::generated_pool(),
::google::protobuf::MessageFactory::generated_factory(),
sizeof(AddressBook));
namespace {
GOOGLE_PROTOBUF_DECLARE_ONCE(protobuf_AssignDescriptors_once_);
inline void protobuf_AssignDescriptorsOnce() {
::google::protobuf::GoogleOnceInit(&protobuf_AssignDescriptors_once_,
&protobuf_AssignDesc_addressbook_2eproto);
void protobuf_RegisterTypes(const ::std::string&) {
protobuf_AssignDescriptorsOnce();
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
Person_descriptor_, &Person::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
Person_PhoneNumber_descriptor_, &Person_PhoneNumber::default_instance());
::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
AddressBook_descriptor_, &AddressBook::default_instance());
// namespace
void protobuf_ShutdownFile_addressbook_2eproto() {
delete Person::default_instance_;
delete Person_reflection_;
delete Person_PhoneNumber::default_instance_;
delete Person_PhoneNumber_reflection_;
delete AddressBook::default_instance_;
delete AddressBook_reflection_;
void protobuf_AddDesc_addressbook_2eproto() {
static bool already_here = false;
if (already_here) return;
already_here = true;
GOOGLE_PROTOBUF_VERIFY_VERSION;
::google::protobuf::DescriptorPool::InternalAddGeneratedFile(
"\n\021addressbook.proto\022\010tutorial\"\332\001\n\006Person"
"\022\014\n\004name\030\001 \002(\t\022\n\n\002id\030\002 \002(\005\022\r\n\005email\030\003 \001("
"\t\022+\n\005phone\030\004 \003(\.tutorial.Person.Phone"
"Number\032M\n\013PhoneNumber\022\016\n\006number\030\001 \002(\t\022.\n"
"\004type\030\002 \001(\.tutorial.Person.PhoneType:"
"\004HOME\"+\n\tPhoneType\022\n\n\006MOBILE\020\\022\010\n\004HOME\020\001"
"\022\010\n\004WORK\020\002\"/\n\013AddressBook\022 \n\006person\030\001 \003("
"\.tutorial.Person2=\n\rSearchService\022,\n\006"
"Search\022\020.tutorial.Person\032\020.tutorial.Pers"
"on", 362);
::google::protobuf::MessageFactory::InternalRegisterGeneratedFile(
"addressbook.proto", &protobuf_RegisterTypes);
Person::default_instance_ = new Person();
Person_PhoneNumber::default_instance_ = new Person_PhoneNumber();
AddressBook::default_instance_ = new AddressBook();
Person::default_instance_-&InitAsDefaultInstance();
Person_PhoneNumber::default_instance_-&InitAsDefaultInstance();
AddressBook::default_instance_-&InitAsDefaultInstance();
::google::protobuf::internal::OnShutdown(&protobuf_ShutdownFile_addressbook_2eproto);
// Force AddDescriptors() to be called at static initialization time.
struct StaticDescriptorInitializer_addressbook_2eproto {
StaticDescriptorInitializer_addressbook_2eproto() {
protobuf_AddDesc_addressbook_2eproto();
} static_descriptor_initializer_addressbook_2eproto_;
// ===================================================================
const ::google::protobuf::EnumDescriptor* Person_PhoneType_descriptor() {
protobuf_AssignDescriptorsOnce();
return Person_PhoneType_descriptor_;
bool Person_PhoneType_IsValid(int value) {
switch(value) {
return true;
return false;
#ifndef _MSC_VER
const Person_PhoneType Person::MOBILE;
const Person_PhoneType Person::HOME;
const Person_PhoneType Person::WORK;
const Person_PhoneType Person::PhoneType_MIN;
const Person_PhoneType Person::PhoneType_MAX;
const int Person::PhoneType_ARRAYSIZE;
// _MSC_VER
#ifndef _MSC_VER
const int Person_PhoneNumber::kNumberFieldN
const int Person_PhoneNumber::kTypeFieldN
// !_MSC_VER
Person_PhoneNumber::Person_PhoneNumber()
: ::google::protobuf::Message() {
SharedCtor();
void Person_PhoneNumber::InitAsDefaultInstance() {
Person_PhoneNumber::Person_PhoneNumber(const Person_PhoneNumber& from)
: ::google::protobuf::Message() {
SharedCtor();
MergeFrom(from);
void Person_PhoneNumber::SharedCtor() {
_cached_size_ = 0;
number_ = const_cast& ::std::string*&(&::google::protobuf::internal::kEmptyString);
type_ = 1;
::memset(_has_bits_, 0, sizeof(_has_bits_));
Person_PhoneNumber::~Person_PhoneNumber() {
SharedDtor();
void Person_PhoneNumber::SharedDtor() {
if (number_ != &::google::protobuf::internal::kEmptyString) {
delete number_;
if (this != default_instance_) {
void Person_PhoneNumber::SetCachedSize(int size) const {
GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
_cached_size_ =
GOOGLE_SAFE_CONCURRENT_WRITES_END();
const ::google::protobuf::Descriptor* Person_PhoneNumber::descriptor() {
protobuf_AssignDescriptorsOnce();
return Person_PhoneNumber_descriptor_;
const Person_PhoneNumber& Person_PhoneNumber::default_instance() {
if (default_instance_ == NULL) protobuf_AddDesc_addressbook_2eproto();
return *default_instance_;
Person_PhoneNumber* Person_PhoneNumber::default_instance_ = NULL;
Person_PhoneNumber* Person_PhoneNumber::New() const {
return new Person_PhoneN
void Person_PhoneNumber::Clear() {
if (_has_bits_[0 / 32] & (0xffu && (0 % 32))) {
if (has_number()) {
if (number_ != &::google::protobuf::internal::kEmptyString) {
number_-&clear();
type_ = 1;
::memset(_has_bits_, 0, sizeof(_has_bits_));
mutable_unknown_fields()-&Clear();
bool Person_PhoneNumber::MergePartialFromCodedStream(
::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!(EXPRESSION)) return false
::google::protobuf::uint32
while ((tag = input-&ReadTag()) != 0) {
switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
// required string number = 1;
if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
DO_(::google::protobuf::internal::WireFormatLite::ReadString(
input, this-&mutable_number()));
::google::protobuf::internal::WireFormat::VerifyUTF8String(
this-&number().data(), this-&number().length(),
::google::protobuf::internal::WireFormat::PARSE);
goto handle_
if (input-&ExpectTag(16)) goto parse_
// optional .tutorial.Person.PhoneType type = 2 [default = HOME];
if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) {
parse_type:
DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive&
int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM&(
input, &value)));
if (::tutorial::Person_PhoneType_IsValid(value)) {
set_type(static_cast& ::tutorial::Person_PhoneType &(value));
mutable_unknown_fields()-&AddVarint(2, value);
goto handle_
if (input-&ExpectAtEnd()) return true;
default: {
handle_uninterpreted:
if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {
return true;
DO_(::google::protobuf::internal::WireFormat::SkipField(
input, tag, mutable_unknown_fields()));
return true;
#undef DO_
void Person_PhoneNumber::SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream* output) const {
// required string number = 1;
if (has_number()) {
::google::protobuf::internal::WireFormat::VerifyUTF8String(
this-&number().data(), this-&number().length(),
::google::protobuf::internal::WireFormat::SERIALIZE);
::google::protobuf::internal::WireFormatLite::WriteString(
1, this-&number(), output);
// optional .tutorial.Person.PhoneType type = 2 [default = HOME];
if (has_type()) {
::google::protobuf::internal::WireFormatLite::WriteEnum(
2, this-&type(), output);
if (!unknown_fields().empty()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
::google::protobuf::uint8* Person_PhoneNumber::SerializeWithCachedSizesToArray(
::google::protobuf::uint8* target) const {
// required string number = 1;
if (has_number()) {
::google::protobuf::internal::WireFormat::VerifyUTF8String(
this-&number().data(), this-&number().length(),
::google::protobuf::internal::WireFormat::SERIALIZE);
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this-&number(), target);
// optional .tutorial.Person.PhoneType type = 2 [default = HOME];
if (has_type()) {
target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(
2, this-&type(), target);
if (!unknown_fields().empty()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
int Person_PhoneNumber::ByteSize() const {
int total_size = 0;
if (_has_bits_[0 / 32] & (0xffu && (0 % 32))) {
// required string number = 1;
if (has_number()) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::StringSize(
this-&number());
// optional .tutorial.Person.PhoneType type = 2 [default = HOME];
if (has_type()) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::EnumSize(this-&type());
if (!unknown_fields().empty()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
_cached_size_ = total_
GOOGLE_SAFE_CONCURRENT_WRITES_END();
return total_
void Person_PhoneNumber::MergeFrom(const ::google::protobuf::Message& from) {
GOOGLE_CHECK_NE(&from, this);
const Person_PhoneNumber* source =
::google::protobuf::internal::dynamic_cast_if_available&const Person_PhoneNumber*&(
if (source == NULL) {
::google::protobuf::internal::ReflectionOps::Merge(from, this);
MergeFrom(*source);
void Person_PhoneNumber::MergeFrom(const Person_PhoneNumber& from) {
GOOGLE_CHECK_NE(&from, this);
if (from._has_bits_[0 / 32] & (0xffu && (0 % 32))) {
if (from.has_number()) {
set_number(from.number());
if (from.has_type()) {
set_type(from.type());
mutable_unknown_fields()-&MergeFrom(from.unknown_fields());
void Person_PhoneNumber::CopyFrom(const ::google::protobuf::Message& from) {
if (&from == this) return;
MergeFrom(from);
void Person_PhoneNumber::CopyFrom(const Person_PhoneNumber& from) {
if (&from == this) return;
MergeFrom(from);
bool Person_PhoneNumber::IsInitialized() const {
if ((_has_bits_[0] & 0x01) != 0x01) return false;
return true;
void Person_PhoneNumber::Swap(Person_PhoneNumber* other) {
if (other != this) {
std::swap(number_, other-&number_);
std::swap(type_, other-&type_);
std::swap(_has_bits_[0], other-&_has_bits_[0]);
_unknown_fields_.Swap(&other-&_unknown_fields_);
std::swap(_cached_size_, other-&_cached_size_);
::google::protobuf::Metadata Person_PhoneNumber::GetMetadata() const {
protobuf_AssignDescriptorsOnce();
::google::protobuf::M
metadata.descriptor = Person_PhoneNumber_descriptor_;
metadata.reflection = Person_PhoneNumber_reflection_;
#ifndef _MSC_VER
const int Person::kNameFieldN
const int Person::kIdFieldN
const int Person::kEmailFieldN
const int Person::kPhoneFieldN
// !_MSC_VER
Person::Person()
: ::google::protobuf::Message() {
SharedCtor();
void Person::InitAsDefaultInstance() {
Person::Person(const Person& from)
: ::google::protobuf::Message() {
SharedCtor();
MergeFrom(from);
void Person::SharedCtor() {
_cached_size_ = 0;
name_ = const_cast& ::std::string*&(&::google::protobuf::internal::kEmptyString);
email_ = const_cast& ::std::string*&(&::google::protobuf::internal::kEmptyString);
::memset(_has_bits_, 0, sizeof(_has_bits_));
Person::~Person() {
SharedDtor();
void Person::SharedDtor() {
if (name_ != &::google::protobuf::internal::kEmptyString) {
delete name_;
if (email_ != &::google::protobuf::internal::kEmptyString) {
delete email_;
if (this != default_instance_) {
void Person::SetCachedSize(int size) const {
GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
_cached_size_ =
GOOGLE_SAFE_CONCURRENT_WRITES_END();
const ::google::protobuf::Descriptor* Person::descriptor() {
protobuf_AssignDescriptorsOnce();
return Person_descriptor_;
const Person& Person::default_instance() {
if (default_instance_ == NULL) protobuf_AddDesc_addressbook_2eproto();
return *default_instance_;
Person* Person::default_instance_ = NULL;
Person* Person::New() const {
return new P
void Person::Clear() {
if (_has_bits_[0 / 32] & (0xffu && (0 % 32))) {
if (has_name()) {
if (name_ != &::google::protobuf::internal::kEmptyString) {
name_-&clear();
if (has_email()) {
if (email_ != &::google::protobuf::internal::kEmptyString) {
email_-&clear();
phone_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
mutable_unknown_fields()-&Clear();
bool Person::MergePartialFromCodedStream(
::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!(EXPRESSION)) return false
::google::protobuf::uint32
while ((tag = input-&ReadTag()) != 0) {
switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
// required string name = 1;
if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
DO_(::google::protobuf::internal::WireFormatLite::ReadString(
input, this-&mutable_name()));
::google::protobuf::internal::WireFormat::VerifyUTF8String(
this-&name().data(), this-&name().length(),
::google::protobuf::internal::WireFormat::PARSE);
goto handle_
if (input-&ExpectTag(16)) goto parse_
// required int32 id = 2;
if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) {
DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive&
::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32&(
input, &id_)));
set_has_id();
goto handle_
if (input-&ExpectTag(26)) goto parse_
// optional string email = 3;
if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
parse_email:
DO_(::google::protobuf::internal::WireFormatLite::ReadString(
input, this-&mutable_email()));
::google::protobuf::internal::WireFormat::VerifyUTF8String(
this-&email().data(), this-&email().length(),
::google::protobuf::internal::WireFormat::PARSE);
goto handle_
if (input-&ExpectTag(34)) goto parse_
// repeated .tutorial.Person.PhoneNumber phone = 4;
if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
parse_phone:
DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(
input, add_phone()));
goto handle_
if (input-&ExpectTag(34)) goto parse_
if (input-&ExpectAtEnd()) return true;
default: {
handle_uninterpreted:
if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {
return true;
DO_(::google::protobuf::internal::WireFormat::SkipField(
input, tag, mutable_unknown_fields()));
return true;
#undef DO_
void Person::SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream* output) const {
// required string name = 1;
if (has_name()) {
::google::protobuf::internal::WireFormat::VerifyUTF8String(
this-&name().data(), this-&name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE);
::google::protobuf::internal::WireFormatLite::WriteString(
1, this-&name(), output);
// required int32 id = 2;
if (has_id()) {
::google::protobuf::internal::WireFormatLite::WriteInt32(2, this-&id(), output);
// optional string email = 3;
if (has_email()) {
::google::protobuf::internal::WireFormat::VerifyUTF8String(
this-&email().data(), this-&email().length(),
::google::protobuf::internal::WireFormat::SERIALIZE);
::google::protobuf::internal::WireFormatLite::WriteString(
3, this-&email(), output);
// repeated .tutorial.Person.PhoneNumber phone = 4;
for (int i = 0; i & this-&phone_size(); i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
4, this-&phone(i), output);
if (!unknown_fields().empty()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
::google::protobuf::uint8* Person::SerializeWithCachedSizesToArray(
::google::protobuf::uint8* target) const {
// required string name = 1;
if (has_name()) {
::google::protobuf::internal::WireFormat::VerifyUTF8String(
this-&name().data(), this-&name().length(),
::google::protobuf::internal::WireFormat::SERIALIZE);
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
1, this-&name(), target);
// required int32 id = 2;
if (has_id()) {
target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(2, this-&id(), target);
// optional string email = 3;
if (has_email()) {
::google::protobuf::internal::WireFormat::VerifyUTF8String(
this-&email().data(), this-&email().length(),
::google::protobuf::internal::WireFormat::SERIALIZE);
::google::protobuf::internal::WireFormatLite::WriteStringToArray(
3, this-&email(), target);
// repeated .tutorial.Person.PhoneNumber phone = 4;
for (int i = 0; i & this-&phone_size(); i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
4, this-&phone(i), target);
if (!unknown_fields().empty()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
int Person::ByteSize() const {
int total_size = 0;
if (_has_bits_[0 / 32] & (0xffu && (0 % 32))) {
// required string name = 1;
if (has_name()) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::StringSize(
this-&name());
// required int32 id = 2;
if (has_id()) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::Int32Size(
this-&id());
// optional string email = 3;
if (has_email()) {
total_size += 1 +
::google::protobuf::internal::WireFormatLite::StringSize(
this-&email());
// repeated .tutorial.Person.PhoneNumber phone = 4;
total_size += 1 * this-&phone_size();
for (int i = 0; i & this-&phone_size(); i++) {
total_size +=
::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual(
this-&phone(i));
if (!unknown_fields().empty()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
_cached_size_ = total_
GOOGLE_SAFE_CONCURRENT_WRITES_END();
return total_
void Person::MergeFrom(const ::google::protobuf::Message& from) {
GOOGLE_CHECK_NE(&from, this);
const Person* source =
::google::protobuf::internal::dynamic_cast_if_available&const Person*&(
if (source == NULL) {
::google::protobuf::internal::ReflectionOps::Merge(from, this);
MergeFrom(*source);
void Person::MergeFrom(const Person& from) {
GOOGLE_CHECK_NE(&from, this);
phone_.MergeFrom(from.phone_);
if (from._has_bits_[0 / 32] & (0xffu && (0 % 32))) {
if (from.has_name()) {
set_name(from.name());
if (from.has_id()) {
set_id(from.id());
if (from.has_email()) {
set_email(from.email());
mutable_unknown_fields()-&MergeFrom(from.unknown_fields());
void Person::CopyFrom(const ::google::protobuf::Message& from) {
if (&from == this) return;
MergeFrom(from);
void Person::CopyFrom(const Person& from) {
if (&from == this) return;
MergeFrom(from);
bool Person::IsInitialized() const {
if ((_has_bits_[0] & 0x03) != 0x03) return false;
for (int i = 0; i & phone_size(); i++) {
if (!this-&phone(i).IsInitialized()) return false;
return true;
void Person::Swap(Person* other) {
if (other != this) {
std::swap(name_, other-&name_);
std::swap(id_, other-&id_);
std::swap(email_, other-&email_);
phone_.Swap(&other-&phone_);
std::swap(_has_bits_[0], other-&_has_bits_[0]);
_unknown_fields_.Swap(&other-&_unknown_fields_);
std::swap(_cached_size_, other-&_cached_size_);
::google::protobuf::Metadata Person::GetMetadata() const {
protobuf_AssignDescriptorsOnce();
::google::protobuf::M
metadata.descriptor = Person_descriptor_;
metadata.reflection = Person_reflection_;
// ===================================================================
#ifndef _MSC_VER
const int AddressBook::kPersonFieldN
// !_MSC_VER
AddressBook::AddressBook()
: ::google::protobuf::Message() {
SharedCtor();
void AddressBook::InitAsDefaultInstance() {
AddressBook::AddressBook(const AddressBook& from)
: ::google::protobuf::Message() {
SharedCtor();
MergeFrom(from);
void AddressBook::SharedCtor() {
_cached_size_ = 0;
::memset(_has_bits_, 0, sizeof(_has_bits_));
AddressBook::~AddressBook() {
SharedDtor();
void AddressBook::SharedDtor() {
if (this != default_instance_) {
void AddressBook::SetCachedSize(int size) const {
GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
_cached_size_ =
GOOGLE_SAFE_CONCURRENT_WRITES_END();
const ::google::protobuf::Descriptor* AddressBook::descriptor() {
protobuf_AssignDescriptorsOnce();
return AddressBook_descriptor_;
const AddressBook& AddressBook::default_instance() {
if (default_instance_ == NULL) protobuf_AddDesc_addressbook_2eproto();
return *default_instance_;
AddressBook* AddressBook::default_instance_ = NULL;
AddressBook* AddressBook::New() const {
return new AddressB
void AddressBook::Clear() {
person_.Clear();
::memset(_has_bits_, 0, sizeof(_has_bits_));
mutable_unknown_fields()-&Clear();
bool AddressBook::MergePartialFromCodedStream(
::google::protobuf::io::CodedInputStream* input) {
#define DO_(EXPRESSION) if (!(EXPRESSION)) return false
::google::protobuf::uint32
while ((tag = input-&ReadTag()) != 0) {
switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
// repeated .tutorial.Person person = 1;
if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
parse_person:
DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(
input, add_person()));
goto handle_
if (input-&ExpectTag(10)) goto parse_
if (input-&ExpectAtEnd()) return true;
default: {
handle_uninterpreted:
if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {
return true;
DO_(::google::protobuf::internal::WireFormat::SkipField(
input, tag, mutable_unknown_fields()));
return true;
#undef DO_
void AddressBook::SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream* output) const {
// repeated .tutorial.Person person = 1;
for (int i = 0; i & this-&person_size(); i++) {
::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
1, this-&person(i), output);
if (!unknown_fields().empty()) {
::google::protobuf::internal::WireFormat::SerializeUnknownFields(
unknown_fields(), output);
::google::protobuf::uint8* AddressBook::SerializeWithCachedSizesToArray(
::google::protobuf::uint8* target) const {
// repeated .tutorial.Person person = 1;
for (int i = 0; i & this-&person_size(); i++) {
target = ::google::protobuf::internal::WireFormatLite::
WriteMessageNoVirtualToArray(
1, this-&person(i), target);
if (!unknown_fields().empty()) {
target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
unknown_fields(), target);
int AddressBook::ByteSize() const {
int total_size = 0;
// repeated .tutorial.Person person = 1;
total_size += 1 * this-&person_size();
for (int i = 0; i & this-&person_size(); i++) {
total_size +=
::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual(
this-&person(i));
if (!unknown_fields().empty()) {
total_size +=
::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
unknown_fields());
GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
_cached_size_ = total_
GOOGLE_SAFE_CONCURRENT_WRITES_END();
return total_
void AddressBook::MergeFrom(const ::google::protobuf::Message& from) {
GOOGLE_CHECK_NE(&from, this);
const AddressBook* source =
::google::protobuf::internal::dynamic_cast_if_available&const AddressBook*&(
if (source == NULL) {
::google::protobuf::internal::ReflectionOps::Merge(from, this);
MergeFrom(*source);
void AddressBook::MergeFrom(const AddressBook& from) {
GOOGLE_CHECK_NE(&from, this);
person_.MergeFrom(from.person_);
mutable_unknown_fields()-&MergeFrom(from.unknown_fields());
void AddressBook::CopyFrom(const ::google::protobuf::Message& from) {
if (&from == this) return;
MergeFrom(from);
void AddressBook::CopyFrom(const AddressBook& from) {
if (&from == this) return;
MergeFrom(from);
bool AddressBook::IsInitialized() const {
for (int i = 0; i & person_size(); i++) {
if (!this-&person(i).IsInitialized()) return false;
return true;
void AddressBook::Swap(AddressBook* other) {
if (other != this) {
person_.Swap(&other-&person_);
std::swap(_has_bits_[0], other-&_has_bits_[0]);
_unknown_fields_.Swap(&other-&_unknown_fields_);
std::swap(_cached_size_, other-&_cached_size_);
::google::protobuf::Metadata AddressBook::GetMetadata() const {
protobuf_AssignDescriptorsOnce();
::google::protobuf::M
metadata.descriptor = AddressBook_descriptor_;
metadata.reflection = AddressBook_reflection_;
// @@protoc_insertion_point(namespace_scope)
// namespace tutorial
// @@protoc_insertion_point(global_scope)
2 编译.proto文件
protoc -I=./ --cpp_out=./ addressbook.proto生成了:addressbook.pb.cc
addressbook.pb.h
要包含的头文件
3 AddPerson.cpp
#include &iostream&
#include &fstream&
#include &string&
#include "addressbook.pb.h"
using namespace
void PromptForAddress(tutorial::Person* person)
cout && "Enter person ID number: ";
person-&set_id(id);
cin.ignore(256, '\n');
cout && "Enter name: ";
string *name = person-&mutable_name();
getline(cin, *name);
cout && "Enter name: ";
getline(cin, *person-&mutable_name());
inline ::std::string* mutable_name();
为啥有*呢?
inline ::std::string*
返回值声明为指针
使用*【person-&mutable_name()】 解引用
cout && "Enter email address (blank for none): ";
getline(cin, email);
if(!email.empty()){
person-&set_email(email);
while(true){
cout && "Enter a phone number (or leave blank to finish):";
getline(cin, number);
if(number.empty()){
tutorial::Person::PhoneNumber* phone_number = person-&add_phone();
phone_number-&set_number(number);
cout && "Is this a mobile, home or work phone ? ";
getline(cin, type);
if(type == "mobile"){
phone_number-&set_type(tutorial::Person::MOBILE);
else if(type == "home"){
phone_number-&set_type(tutorial::Person::HOME);
else if(type == "work"){
phone_number-&set_type(tutorial::Person::WORK);
cout && "Unknown phone type. Using default." &&
int main(int argc, char* argv[]) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2) {
cerr && "Usage: " && argv[0] && " ADDRESS_BOOK_FILE" &&
return -1;
tutorial::AddressBook address_
fstream input(argv[1], ios::in
ios::binary);
if(!input){
cout && argv[1] && ": File not found. Creating a new file." &&
} else if (!address_book.ParseFromIstream(&input)) {
cerr && "Failed to parse address book." &&
return -1;
PromptForAddress(address_book.add_person());
fstream out(argv[1], ios::out
ios::trunc
ios::binary);
if(!address_book.SerializeToOstream(&out)){ //
此方法为 Message 基类中的 class Person_PhoneNumber : public ::google::protobuf::Message
cerr && "Failed to write address book." &&
return -1;
google::protobuf::ShutdownProtobufLibrary();
/* vim: set ts=4 sw=4 sts=4 tw=100 */
4 ListPerson.cpp
#include &iostream&
#include &fstream&
#include "addressbook.pb.h"
using namespace
void ListPeople(const tutorial::AddressBook& address_book) {
for (int i = 0; i & address_book.person_size(); i++) {
const tutorial::Person& person = address_book.person(i);
cout && "Person ID:" && person.id() &&
cout && "Name: " && person.name() &&
if (person.has_email()) {
cout && "E-Mail address: " && person.email();
for ( int j = 0; j & person.phone_size(); j++ ) {
const tutorial::Person::PhoneNumber& phone_number = person.phone(j);
switch(phone_number.type()) {
case tutorial::Person::MOBILE:
"Mobile Phone #: ";
case tutorial::Person::HOME:
cout && " Home Phone #: ";
case tutorial::Person::WORK:
cout && " Work phone #: ";
cout && phone_number.number() &&
int main(int argc, char* argv[]) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
if(argc != 2) {
cerr && "Usage:
" && argv[0] && " ADDRESS_BOOK_FILE" &&
return -1;
tutorial::AddressBook address_
fstream input(argv[1], ios::in
ios::binary);
if (!address_book.ParseFromIstream(&input)) { //
此方法为 Message 基类中的 class Person_PhoneNumber : public ::google::protobuf::Message
cerr && "Failed to parse address book." &&
return -1;
ListPeople(address_book);
google::protobuf::ShutdownProtobufLibrary();
/* vim: set ts=4 sw=4 sts=4 tw=100 */
5 g++ 编译
g++ -g -o writer AddPerson.cpp addressbook.pb.cc -I. -I /home/users/wangquanjun/lab/protobuf/protobuf/include
-L /home/users/wangquanjun/lab/protobuf/protobuf/lib -lprotobuf -pthread
g++ -g -o reader ListPerson.cpp addressbook.pb.cc -I. -I /home/users/wangquanjun/lab/protobuf/protobuf/include
-L /home/users/wangquanjun/lab/protobuf/protobuf/lib -lprotobuf -pthread
touch ADDRESS_BOOK_FILE
./writer ADDRESS_BOOK_FILE
./reader ADDRESS_BOOK_FILE
> 本站内容系网友提交或本网编辑转载,其目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请及时与本网联系,我们将在第一时间删除内容!
下载的是github上的:/google/protobuf
If you get the source from github, you need to generate the configure script first: $ ./autogen.sh This will download gtest sour ...
陈硕 (giantchen_AT_gmail) Blog.csdn.net/Solstice
.cn/giantchen
这篇文章要解决的问题是:在接收到 protobuf 数据之后,如何自动创建具体的 Protobuf Message 对象,再做的反序列化.“自动”的意思是:当程序中新增一个 protobuf Message 类型 ...
最近在读别人代码的时候发现一个的东西,名字叫protobuf, 感觉挺好用的,写在这里,留个记录.那么什么是protobuf 呢?假如您在网上搜索,应该会得到类似这样的文字介绍: Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 ...
RPC(Remote Procedure Call),中文翻译是远程过程调用,其实从原理来说这并不是一个新的概念.我的理解是, 不同的机器之间定义了一些接口, 也有客户端和服务器端,客户端可以通过协商好的接口调用服务器端已经注册好的服务.说白了,还是网络通信的那一套机制.既然还是网络通信,那么为什么需要使用RPC而不是自己去完成这样的一套工作呢?假如是自己做 ...
erlang中使用google protobuf进行通信 /?p=231 初学erlang,花了不少的功夫,想要在erlang中集成google的protobuf用于消息通信.个人觉得,使用类似protobuf这样通用的编解码模块,有一个好处就是这部分完全交给别人,再不用自己关心什么很的大小端,数据长度等琐 ...
原文地址:/liquidx/archive//88366.html译者:gashero目录1
什么是protocol buffer1.2
他们如何工作1.3
为什么不用XML?1.4
听起来像是为我的解决方案,如何开始?1.5
语言指导2.1
根据上一篇博文 Google Protobuf 使用 Java 版 netty 集成 protobuf 的方法非常简单.代码如下: server package protobuf.server. import io.netty.bootstrap.ServerB import io.netty.channel.ChannelFut ...
一 . Protobuf 的入门 Protobuf 是一个灵活,高效,结构化的数据序列化框架, 相比于 XML 等传统的序列化工具,它更小,更快,更灵活,更简单. Protobuf 支持数据结构化一次可以到处使用.甚至跨语言使用.同通过代码生成工具可以自动生成不同语言版本的源代码,甚至可以在使用不同版本的数据结构中进行数据传递,实现数据结构的向前兼容. Go ...}

我要回帖

更多关于 go protobuf 开发文档 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信