protobuf使用系列详解三:补充四种API的使用:mutable_*、ParseFrom*、set_allocated_*

一、补充API说明

1)protobuf mutable_* 函数

  • 作用:

若该对象存在,则直接返回该对象,若不存在则新new 一个。

  • 使用:
//proto
message Answer 
{
    optional uint32 choice = 1;
    optional uint32 id = 2;
}
message Info
{
    required Answer answer = 1
}
message Rsp
{
    repeated Info info = 1;
}


//C++
Rsp rsp;
for(int index = 0;index<info.answer_size();index++)
{
    Info * info = rsp.add_info();
    Answer* ans = info->mutable_answer();
    ans->set_choice(index);
    ans->set_id(index+1);
}

2)bool ParseFromString(const string& data)和bool SerializeToString(string* output) const

  • ParseFromString() 作用
函数是protobuf 提供的一个C++ API,它的功能就是将string 里面的数据反序列化(解析)到指定的消息结构类中。
它其实是一个消息结构类的成员函数,反序列化之后,该消息结构类中的数据成员将会被填充,且有效。
  • SerializeToString() 作用

protobuf 提供的一个C++ API,它的功能与parseFromString() 函数相对应,是将消息结构的数据序列化(填充)到output 所指向的string 对象中。在string 对象中以二进制的形式存储。

  • 注意点

1)若要ParseFromString() 函数成功执行并返回true 。还需要注意一点就是输入参数的数据构造要正确,不能多一个也不是少一个字节。将data 的长度resize() 太大,导致了该函数返回false

3)ParseFromArray

  • ParseFromArray作用:
将proto结构体反序列成可以解读的结构体
//proto
message TEAM_MEMBER_MARK_INFO
{
	required int32 w = 1;
	required int32 x = 1;
	required int32 y = 1;
	required int32 z = 1;
}
message C2G_TEAM_CHANGE_MARK
{
	required TEAM_MEMBER_MARK_INFO mark = 1;
	optional bool share_mark;
}


//C++
TEAM_MEMBER_MARK stMark;
C2G_TEAM_CHANGE_MARK msg;
int32_t nRetCode = 0;

nRetCode = msg.ParseFromArray(pBuffer,nSize);
LOG_PROCESS_ERROR(nRetCode);

st.Mark.nw = msg.mark().w();
st.Mark.nw = msg.mark().x();
st.Mark.nw = msg.mark().y();
st.Mark.nw = msg.mark().z();

4)set_allocated_*的使用

//proto
message vector3D
{
	required float x = 1;
	required float y = 2;
	required float z = 3;
};
message PlayerPos 
{
	required uint32 playerID = 1;
	required vector3D  pos = 2;
}

//C++  
//set_allocated_pos的做法
PlayerPos player;vector3D  *tmp = new Vector3D;
tmp->x = 1;
tmp->y = 2;
tmp->z = 3;
player.set_allocated_pos(tmp)


//mutable_pos的方法
inline ::vector3D* PlayerPos::mutable_pos() 
{  
	set_has_pos();  
	if (pos_ == NULL) 
		pos_ = new ::vector3D;  
	return pos_;
}
PlayerPos player;
vector3D  *tmp = player.mutable_pos();
tmp->x = 1;
tmp->y = 2;
tmp->z = 3;
  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
`set_allocated_xxx()` 是 Protocol Buffers 中的一个动态分配内存的方法。它可以在运行时为一个已经存在的对象分配新的内存,并将指针赋值给对应的属性。 具体来说,如果一个消息中包含一个 `xxx` 属性,那么这个属性对应的 setter 方法就会有一个对应的 `set_allocated_xxx()` 方法。这个方法接受一个指向新对象的指针,并将这个指针赋值给属性。同时,如果原来的对象不为空,`set_allocated_xxx()` 会自动释放这个对象的内存。 例如,假设我们有如下的 Protobuf 消息定义: ``` message MyMessage { int32 id = 1; MyOtherMessage other_message = 2; } ``` 其中 `MyOtherMessage` 是另一个消息类型。那么,`MyMessage` 类型会自动生成如下的 setter 方法: ``` void set_other_message(const MyOtherMessage& value); MyOtherMessage* mutable_other_message(); MyOtherMessage* release_other_message(); void set_allocated_other_message(MyOtherMessage* other_message); ``` 其中,`set_allocated_other_message()` 就是我们要讲的 `set_allocated_xxx()` 方法。我们可以使用它来为 `other_message` 属性分配新的内存。例如: ``` MyMessage message; MyOtherMessage* new_other_message = new MyOtherMessage(); message.set_allocated_other_message(new_other_message); ``` 这段代码会为 `message` 对象的 `other_message` 属性分配新的内存,并将 `new_other_message` 的指针赋值给属性。如果原来的 `other_message` 不为空,它的内存会被自动释放。 需要注意的是,由于 `set_allocated_xxx()` 会负责释放原来的对象内存,所以我们不能在之后再次使用原来的对象。如果我们需要在之后继续使用对象,我们可以使用 `release_xxx()` 方法来释放属性的所有权,而不是使用 `set_allocated_xxx()`。例如: ``` MyOtherMessage* old_other_message = message.release_other_message(); // 在这里使用 old_other_message delete old_other_message; ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值