简体   繁体   中英

Protobuf c++ extensions use

I'm using google protobuf library version 2.61 and want to use extensions,

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.

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?

Also what is the most elegant way to do so in c++

The library parse message and all extensions. You can check presence of the extension using HasExtension method.

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)

Create and write message:

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM