samedi 22 janvier 2022

[Protobuf3-C++]How to unpack a field of Any with reflction?

My proto file looks like this:

message HqMessage {
    uint32 busi_no          = 1;
    google.protobuf.Any detail      = 2;
}

message B0T4F1 {
    uint32 MarketType               = 501;
}

//---------------------------------------------------------------------

STEP1, use protobuf's reflection, I had set the message of BOT4F1 to the 'detail':


const google::protobuf::Descriptor* _detail_des = _des_pool->FindMessageTypeByName("B0T4F1");
const google::protobuf::Message* _detail_prototype_message = m_dynamic_factory.GetPrototype(_detail_des);
google::protobuf::Message* _detail_msg = _detail_prototype_message->New();
const google::protobuf::Reflection* _detail_msg_ref = _detail_prototype_message->GetReflection();
const google::protobuf::FieldDescriptor* _detail_fd = _detail_des->FindFieldByNumber(501);
uint32 mkt_type =100;
_fd = _des->FindFieldByNumber(2);
_msg_ref ->**SetAllocatedMessage**(_msg, _detail_msg, _fd);

STEP2,serialize the message to a buffer, then parse the HqMessage from the buffer.

STEP3, first, I get the message of Any, then serialize the any message to buffer again. Finally,get the message object of B0T4F1 through the new buffer.

How can i parse the buffer to the message of B0T4F1 without serialize ’detail' message again. Could I get the ‘B0T4F1' message from 'HqMessage ' directly? How can i do this?





Aucun commentaire:

Enregistrer un commentaire