One simple approach you could use is to have a parent message containing each potential message type inside a oneof <https://developers.google.com/protocol-buffers/docs/proto#oneof> field. This way you only have to parse the message once, and it's easy to tell which kind of message is inside the oneof.
On Tue, May 1, 2018 at 5:14 AM Andrew Bouchard <[email protected]> wrote: > Thank you - that is actually a really clear example. I thought that more > information than that was going with the protobuf, or that more > intelligence was built into the parsing than is. > > What I'm trying at the moment is just adding a first field to all of the > messages in this set that is a string of the type of the message, in which > case my example above becomes: > > void CModule::ProcessMessages( CMOOSMsg &msg ) > { > // Declare message types > MyMessages::TypeA type_a; > MyMessages::TypeB type_b; > > // Parse a message of type A > if( type_a.ParseFromString( msg.GetString() ) && ( type_a.type() == > "TypeA" ) ) > { > printf( "Received a message of type A\n" ); > } > // Parse a message of type B > else if( type_b.ParseFromString( msg.GetString() ) && ( type_b.type() > == "TypeB" ) ) > { > printf( "Received a message of type B\n" ); > } > else > { > printf( "ERROR: Unrecognized message type" ); > } > } > > Perhaps not the most elegant solution, but if I understand how things work > now I think that it should solve my issue. > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/protobuf. > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/protobuf. For more options, visit https://groups.google.com/d/optout.
