I am new to both protocol buffers and C ++, so this might be the main question, but I was not lucky to find the answers. Basically, I want the functionality of a dictionary defined in my .proto
file, like enum
. I use the protocol buffer to send data, and I want to determine the units and their corresponding names. enum
would allow me to define units, but I don't know how to match human-readable strings.
As an example of what I mean, the .proto
file might look something like this:
message DataPack { // obviously not valid, but something like this dict UnitType { KmPerHour = "km/h"; MiPerHour = "mph"; } required int id = 1; repeated DataPoint pt = 2; message DataPoint { required int id = 1; required int value = 2; optional UnitType theunit = 3; } }
and then there is something like creating / processing messages:
// construct DataPack pack; pack->set_id(123); DataPack::DataPoint pt = pack.add_point(); pt->set_id(456); pt->set_value(789); pt->set_unit(DataPack::UnitType::KmPerHour); // read values DataPack::UnitType theunit = pt.unit(); cout << theunit.name << endl; // print "km/h"
I could just define an enum
with unit names and write a function to map them to strings on the receiving side, but it would be wiser to have them defined in the same place, and this solution seems too complicated (at least for those who have recently been spoiled by the amenities of Python). Is there an easier way to do this?
source share