31 void _set_field_value(
const google::protobuf::Reflection* refl,
32 const google::protobuf::FieldDescriptor* field_desc,
33 std::unique_ptr<google::protobuf::Message>& msg,
double val)
35 refl->SetDouble(msg.get(), field_desc, val);
38 void _set_field_value(
const google::protobuf::Reflection* refl,
39 const google::protobuf::FieldDescriptor* field_desc,
40 std::unique_ptr<google::protobuf::Message>& msg,
bool val)
42 refl->SetBool(msg.get(), field_desc, val);
45 void _set_field_value(
const google::protobuf::Reflection* refl,
46 const google::protobuf::FieldDescriptor* field_desc,
47 std::unique_ptr<google::protobuf::Message>& msg,
int val)
49 if (field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_ENUM)
51 auto enum_desc = field_desc->enum_type();
52 auto enum_val_desc = enum_desc->FindValueByNumber(val);
54 refl->SetEnum(msg.get(), field_desc, enum_val_desc);
59 template <
typename FieldType>
60 void set_field(std::unique_ptr<google::protobuf::Message>& msg,
const char* field_name,
64 const google::protobuf::Reflection* refl = msg->GetReflection();
65 const google::protobuf::Descriptor* desc = msg->GetDescriptor();
66 const google::protobuf::FieldDescriptor* field_desc = desc->FindFieldByName(field_name);
68 _set_field_value(refl, field_desc, msg, val);
70 std::cerr <<
"Invalid field name: " << field_name << std::endl;
82 "/home/toby/share/examples/quickstart_navreport");
85 const google::protobuf::FileDescriptor* nav_report_file_desc =
88 if (!nav_report_file_desc)
90 std::cerr <<
"Failed to read in navreport.proto" << std::endl;
94 std::string encoded_bytes;
99 const google::protobuf::Descriptor* nav_report_desc = nav_report_file_desc->message_type(0);
100 codec.load(nav_report_desc);
105 std::unique_ptr<google::protobuf::Message>>(nav_report_desc);
107 set_field(r_out,
"x", 450.0);
108 set_field(r_out,
"y", 550.0);
109 set_field(r_out,
"z", -100.0);
111 set_field(r_out,
"veh_class", AUV);
112 set_field(r_out,
"battery_ok",
true);
114 codec.encode(&encoded_bytes, *r_out);
120 auto r_in = codec.decode<std::unique_ptr<google::protobuf::Message>>(encoded_bytes);
121 std::cout << r_in->ShortDebugString() << std::endl;