Protobuf c ++扩展使用 [英] Protobuf c++ extensions use
问题描述
我使用google protobuf库版本2.61并想使用扩展程序
I'm using google protobuf library version 2.61 and want to use extensions,
我有以下proto文件:
I have the following proto files:
package communication;
message BaseMessage {
required uint64 server_id = 1;
required string uuid = 2;
required uint64 message_id = 3;
extensions 100 to max;
}
message GetIdentify {
extend BaseMessage {
optional GetIdentify message = 100;
}
required string hostname = 1;
}
我可以使用以下代码构建邮件:
I am able to build the message using the following code:
communication::BaseMessage base_message;
base_message.set_message_id(123456);
base_message.set_server_id(112313123);
base_message.set_uuid("asdaskdjasd213123123asd");
base_message.MutableExtension(communication::GetIdentify::message)->set_hostname("http://test123123123ing");
但是,我想做相反的操作,并获取未知扩展的消息并解析它,扩展它是,并根据它解析。
However I would like to do the opposite action and get message with unknown extension and parse it and find which extension it is and parse according to it.
我使用nanopb为我的c项目和python版本。但我发现很难在c ++中编写protobuf代码,因为我找不到足够好的文档和代码示例。
I've used nanopb for my c project and python version. but I find it really hard to write protobuf code in c++ because I can't find good enough documentation and code examples.
有没有办法做到这一点,变量类型的扩展?
Is there a way to do this without adding additional variable of type of extension?
也是最优雅的方式,在c ++中这样做
Also what is the most elegant way to do so in c++
推荐答案
库解析消息和所有扩展。您可以使用HasExtension方法检查扩展的存在。
The library parse message and all extensions. You can check presence of the extension using HasExtension method.
Java实现需要在解析之前在解析器中注册扩展。但在C ++中,一切都自动完成。请参阅以下代码。 (我用protobuf 2.5.0测试它)
Java implementation needs to register extensions in parser, before parsing. But in C++ everything done automatically. Please see following code. (I tested it with protobuf 2.5.0)
创建并写入消息:
#include <iostream>
#include <fstream>
#include <string>
#include <communication/p.pb.h>
using namespace std;
int
main(int, char **)
{
communication::BaseMessage base_message;
base_message.set_message_id(123456);
base_message.set_server_id(112313123);
base_message.set_uuid("asdaskdjasd213123123asd");
base_message.MutableExtension(communication::GetIdentify::message)->set_hostname("http://test123123123ing");
base_message.SerializeToOstream(&cout);
return 0;
}
读取邮件,测试扩充功能并列印:
Read message, test extension and print it:
#include <iostream>
#include <fstream>
#include <string>
#include <communication/p.pb.h>
#include <google/protobuf/text_format.h>
using namespace google::protobuf;
using namespace std;
int
main(int, char **)
{
communication::BaseMessage base_message;
base_message.ParseFromIstream(&cin);
if (base_message.HasExtension(communication::GetIdentify::message)) {
const communication::GetIdentify &ii = base_message.GetExtension(communication::GetIdentify::message);
cout << "yes, msg has extension: " << ii.hostname() << endl << endl;
} else {
cout << "no, msg has no extension" << endl << endl;
}
string res;
TextFormat::PrintToString(base_message, &res);
cout << res << endl;
return 0;
}
这篇关于Protobuf c ++扩展使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!