Protobuf, understand the hood
proto文件定义
package lm;
message Foo{
required int32 id = 1;
}
message Bar{
required int32 id = 1 [default = 12];
required string str= 2 [default = "bar::str"];
required Foo foo= 3;
}
下面分析protoc
编译生成的:ph.b和pb.cc文件。
初始化
protoc
会定义一个全局对象:
struct StaticDescriptorInitializer_lm_2ehelloworld_2eproto {
StaticDescriptorInitializer_lm_2ehelloworld_2eproto() {
protobuf_AddDesc_lm_2ehelloworld_2eproto();
};
} static_descriptor_initializer_lm_2ehelloworld_2eproto_;
其中protobuf_AddDesc_lm_2ehelloworld_2eproto()
函数完成了默认值的初始化:
void protobuf_AddDesc_lm_2ehelloworld_2eproto() {
static bool already_here = false;
...
Foo::default_instance_ = new Foo();
Bar::_default_str_ =
new ::std::string("bar::str", 8); // string默认值存在这里
Bar::default_instance_ = new Bar();
Foo::default_instance_->InitAsDefaultInstance();
Bar::default_instance_->InitAsDefaultInstance();
::google::protobuf::internal::OnShutdown(&protobuf_ShutdownFile_lm_2ehelloworld_2eproto);
}
同时注册了清理函数protobuf_ShutdownFile_lm_2ehelloworld_2eproto
:
void protobuf_ShutdownFile_lm_2ehelloworld_2eproto() {
delete Foo::default_instance_;
delete Foo_reflection_;
delete Bar::default_instance_;
delete Bar_reflection_;
delete Bar::_default_str_;
}
构造
默认值的赋值在构造函数里:
Bar::Bar()
: ::google::protobuf::Message() {
SharedCtor();
// @@protoc_insertion_point(constructor:lm.Bar)
}
void Bar::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
id_ = 12; // id默认值
str_ = const_cast< ::std::string*>(_default_str_); // str默认值
foo_ = NULL;
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
整数标示符的名称为kXxxFieldNumber
:
class Bar : public ::google::protobuf::Message {
public:
static const int kIdFieldNumber = 1;
static const int kStrFieldNumber = 2;
static const int kFooFieldNumber = 3;
};
析构
新建的对象在析构函数里删除:
Bar::~Bar() {
// @@protoc_insertion_point(destructor:lm.Bar)
SharedDtor();
}
void Bar::SharedDtor() {
if (str_ != _default_str_) {
delete str_;
}
if (this != default_instance_) {
delete foo_;
}
};
Mutable Pointer
使用mutable_xxx()
时,如果没有set过,则new一个新对象出来返回:
inline ::std::string* Bar::mutable_str() {
set_has_str();
if (str_ == _default_str_) {
str_ = new ::std::string(*_default_str_);
}
return str_;
}
inline ::lm::Foo* Bar::mutable_foo() {
set_has_foo();
if (foo_ == NULL) foo_ = new ::lm::Foo;
return foo_;
}
Allocate Value
使用set_allocated_xxx()
时,清理掉原来的对象,用新对象赋值:
inline void Bar::set_allocated_str(::std::string* str) {
if (str_ != _default_str_) {
delete str_;
}
if (str) {
set_has_str();
str_ = str;
} else {
clear_has_str();
str_ = const_cast< ::std::string*>(_default_str_);
}
}
inline void Bar::set_allocated_foo(::lm::Foo* foo) {
delete foo_;
foo_ = foo;
if (foo) {
set_has_foo();
} else {
clear_has_foo();
}
}
Clear
Clear()
时重设为默认值:
void Bar::Clear() {
if (_has_bits_[0 / 32] & 7) {
id_ = 12;
if (has_str()) {
if (str_ != _default_str_) {
str_->assign(*_default_str_);
}
}
if (has_foo()) {
if (foo_ != NULL) foo_->::lm::Foo::Clear();
}
}
::memset(_has_bits_, 0, sizeof(_has_bits_));
mutable_unknown_fields()->Clear();
}
从这里面看出,只是重新赋值,如果是allocated的值,不会清理,所以需要allocate的人自己管理内存。
Protobuf, understand the hood的更多相关文章
- UNDER THE HOOD OF THE NEW AZURE PORTAL
http://jbeckwith.com/2014/09/20/how-the-azure-portal-works/ So - I haven’t been doing much blogging ...
- [under the hood]Reduce EXE and DLL Size with LIBCTINY.LIB
Matt Pietrek Download the code for this article: Hood0101.exe (45KB) W ay back in my October 1996 co ...
- C++ Under the Hood
The original article is taken from http://msdn.microsoft.com/archive/en-us/dnarvc/html/jangrayhood.a ...
- under the hood
under the hood adjective a metaphorical area that contains the underlying implementation of somethin ...
- C++操作Kafka使用Protobuf进行跨语言数据交互
C++操作Kafka使用Protobuf进行跨语言数据交互 Kafka 是一种分布式的,基于发布 / 订阅的消息系统.主要设计目标如下: 以时间复杂度为 O(1) 的方式提供消息持久化能力,即使对 T ...
- python通过protobuf实现rpc
由于项目组现在用的rpc是基于google protobuf rpc协议实现的,所以花了点时间了解下protobuf rpc.rpc对于做分布式系统的人来说肯定不陌生,对于rpc不了解的童鞋可以自行g ...
- Protobuf使用规范分享
一.Protobuf 的优点 Protobuf 有如 XML,不过它更小.更快.也更简单.它以高效的二进制方式存储,比 XML 小 3 到 10 倍,快 20 到 100 倍.你可以定义自己的数据结构 ...
- java netty socket库和自定义C#socket库利用protobuf进行通信完整实例
之前的文章讲述了socket通信的一些基本知识,已经本人自定义的C#版本的socket.和java netty 库的二次封装,但是没有真正的发表测试用例. 本文只是为了讲解利用protobuf 进行C ...
- 在Wcf中应用ProtoBuf替代默认的序列化器
Google的ProtoBuf序列化器性能的牛逼已经有目共睹了,可以把它应用到Socket通讯,队列,Wcf中,身为dotnet程序员一边期待着不久后Grpc对dotnet core的支持更期待着Wc ...
随机推荐
- uva 10222 - Decode the Mad man
#include <iostream> #include <string> #include <cctype> using namespace std; int m ...
- C 语言 联合union初见
1.什么是联合? “联合”是一种构造类型的数据结构.在一个“联合”内可以定义多种不同的数据类型, 一个被说明为该“联合”类型的变量中,允许装入该“联合”所定义的任何一种数据,这些数据共享同一段内存,已 ...
- 创建区域Areas,添加TagHelper
创建区域Areas,添加TagHelper 目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 在Asp.net Core VS2015中,我们发现还有很多不太简便的地方,比如右 ...
- centos6.6安装mysql5.7.6(采用MySQL Yum Repository)—(先看最后一行)
在centos6.6系统上采用MySQL Yum Repository安装mysql5.7.6: 帮助文档:http://dev.mysql.com/doc/refman/5.7/en/linux-i ...
- java事件响应方法汇总(容器类监听、监听器类、AbstractAction、反射)
Java图形用户界面中,处理事件时所必须的步骤是: 1.创建接受响应的组件(控件)2.实现相关事件监听接口3.注册事件源的动作监听器4.事件触发时的事件处理 相应的可以通过以下的集中方式来作出事件响应 ...
- C++中new和delete的背后( call edx 调用虚表内的第二个函数(析构函数))
关于 C++中new背后的行为, 以前已经写过一篇了 理解C++中new背后的行为, 但是里面也只是泛泛而谈,没有真凭实据, 下面我们从汇编的角度看C++编译器究竟在背后干了什么? 我们的代码很简 ...
- wireshark保存部分报文的方法
抓包时采用下列两种命令: tcpdump –s 0 –i eth0 host IP1 and IP2 and port 5060 and 5080 –v –w file1.pcap 与 tcpdump ...
- Win32/MFC的基本概念
一.MFC的基本概念 单文档.多文档和对话框框架的区别 MFC中的类继承图的基本框架 CView类与CDocument的关系 Onpaint()和Ondraw()的关系 hdc-cdc区别联系 RUN ...
- 知方可补不足~CSS中的几个伪元素
对于一个很好的编辑器VS来说,它对于编程语句的自动提示功能是很强大的,有时,我们根本不需要看相关API,而直接看VS给我们的提示就可以完成一个新技术的学习了. 今天我们来说几个CSS中的伪元素,它们在 ...
- 关于SetCapture() 和 ReleaseCapture()的使用方法
查MSND,对SetCapture()函数的说明为:“该函数在属于当前线程的指定窗体里设置鼠标捕获.一旦窗体捕获了鼠标,全部鼠标输入都针对该窗体,不管光标是否在窗体的边界内.同一时刻仅仅能有一个窗体捕 ...