c++ vector 序列化_QT序列化和反序列化

c++ vector 序列化_QT序列化和反序列化c++使用protobuf中的repeated与map类型_protorepeated

c++

Protobuf中的repeated V、map<K,V>序列化使用。

一、简单类型repeated

//定义message
message M_TEST
{
	repeated double dparams		=	1;	//double参数集
	repeated int32	iparams		=	2;	//int参数集
	         string desc		=	99;	//描述
}

简单类型repeated,可以通过add_<参数名>(参数值)的方式设置值,再进行序列化;

M_TEST testpacket;
testpacket.set_desc("Demo");

int i = 0;
do{
	double di = i + 0.001;
	testpacket.add_iparams(i);
	testpacket.add_dparams(di);

	i++;
} while (testpacket.ByteSize() < 1024);//填充测试数据

//序列化
string strpb;
testpacket.SerializeToString(&strpb);

二、对象类型repeated

//定义协议1
message M_Person
{
	string 	    name 	= 1;    //姓名
	int32 	    age		= 2;    //年龄
}

//定义协议2
message M_Team
{
    repeated    M_Person    member      =    1;    //成员信息
                string      desc        =    99;   //描述
}

对象类型repeated,可以通过add_参数名()的方式设置值,再进行序列化;

M_Team teampacket;
M_Person *person;

teampacket.set_desc("Demo");
person = teampacket.add_member();
person->set_name("张三");
person->set_age(33);

string strpb;
teampacket.SerializeToString(&strpb);

三、多层嵌套与map<K,V>使用示例

1)协议主体示例如下:

//围棋竞赛详情
message M_Battle
{
    repeated    M_GroupInfo    junior    =    1;    //初中队
    repeated    M_GroupInfo    senior    =    2;    //高中队
    map<string,M_GroupInfo>    teacher   =    3;    //教师队
                string         desc      =    99;   //描述
}

//队伍成员信息
message M_GroupInfo
{
    string        nickname    =    1;    //昵称
    E_Gender      gender      =    2;    //性别
    M_Project     project     =    3;    //参赛项目
    string        desc        =    99;   //描述
}

//竞赛项目信息
message M_Project
{
    int32         id          =    1;    //项目ID
    string        name        =    2;    //项目名称
    string        desc        =    99;   //项目描述
}

//性别
enum E_Gender
{
    GENDER_UNKNOWN            =    0;    //未知
    GENDER_MALE               =    1;    //男性
    GENDER_FEMALE             =    2;    //女性
}

2)map与repeated使用示例如下:

M_Battle battlepacket;

//M_Battle变量teacher:处理map类型
auto* wMap = battlepacket.mutable_teacher();
for (int i = 1; i <= 3; i++){
	M_GroupInfo group_teacher;
	//M_GroupInfo变量nickname
	group_teacher.set_nickname("fuzi" + to_string(i));
	//M_GroupInfo变量gender
	group_teacher.set_gender(GENDER_UNKNOWN);
	//M_GroupInfo变量project
	M_Project *project_teacher = new M_Project();
	project_teacher->set_id(i);
	project_teacher->set_name("teacher project" + to_string(i));
	project_teacher->set_desc("This is teacher project " + to_string(i));
	group_teacher.set_allocated_project(project_teacher);
	//M_GroupInfo变量desc
	group_teacher.set_desc("This is group_teacher member " + to_string(i));
	//添加到map键值
	string key = "teacher" + to_string(i);
	(*wMap)[key] = group_teacher;
}

//M_Battle变量desc:基础类型
battlepacket.set_desc("Demo");

//M_Battle变量junior, senior
int i = 1;
do{
	M_GroupInfo* group_junior;
	M_GroupInfo* group_senior;
	M_Project *project_junior = new M_Project();
	M_Project *project_senior = new M_Project();

	project_junior->set_id(i);
	project_junior->set_name("junior project " + to_string(i));
	project_junior->set_desc("This is junior project " + to_string(i));

	project_senior->set_id(i*2);
	project_senior->set_name("senior project " + to_string(i));
	project_senior->set_desc("This is senior project " + to_string(i));

	group_junior = battlepacket.add_junior();
	group_junior->set_nickname("zhangsan" + to_string(i));
	group_junior->set_gender(GENDER_MALE);
	group_junior->set_allocated_project(project_junior);
	group_junior->set_desc("This is group_junior member " + to_string(i));

	group_senior = battlepacket.add_senior();
	group_senior->set_nickname("lisi" + to_string(i));
	group_senior->set_gender(GENDER_FEMALE);
	group_senior->set_allocated_project(project_senior);
	group_senior->set_desc("This is group_senior member " + to_string(i));

	i++;

} while (battlepacket.ByteSize() < 1024);//填充数据

//序列化
int size = battlepacket.ByteSize();
void *p = malloc(size);
battlepacket.SerializeToArray(p, size);

//或序列化
//string strpb;
//battlepacket.SerializeToString(&strpb);

今天的文章c++ vector 序列化_QT序列化和反序列化分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/64848.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注