Conversion from protobuf-c object to Protobuf C++ message

187 views
Skip to first unread message

Kiran Kumar

unread,
Jul 12, 2019, 2:18:42 AM7/12/19
to Protocol Buffers

I have written a C++ class for comparing two Google Protocol Buffer messages. It takes two GPB objects and returns whether the two GPB messages are exactly same or not. This class also has another API which returns even the attributes that got modified between the two messages. The C++ API (simple case of just return true/false) looks like below. It takes Google Protobuf base class "Message" objects as input. So this API can be called to compare any type of protobuf messages as long as firstMsg and secondMsg are of same type. i.e. can be used to compare message of type Student, or message of type Teacher.


bool get_diff(google::protobuf::Message* firstMsg,
                         google::protobuf::Message* secondMsg)


This works fine for me to use from a C++ code. However, now I have a new use case where I need to call this API from a C code. My C code also has protobuf messages that are generated with protoc-c compiler. I am having difficulty in generating google::protobuf::Message from a protobuf-c message to call this API.

Any suggestions on how to move forward ?

Adam Cozzette

unread,
Jul 15, 2019, 12:51:43 PM7/15/19
to Kiran Kumar, Protocol Buffers
I suspect the only reliable way to convert between C++ generated messages and protobuf-c messages is to serialize the message and then parse it back into the other kind of message. You might also want to check out MessageDifferencer since that already does equality comparisons and diffing that sound similar to what you are doing.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/ae2866da-ed59-4a23-a3fa-e68035e4359d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages