DCCL v3
quick-runtime.cpp
1 #include "dccl.h"
2 #include <iostream>
3 #include <memory>
4 
5 // Advanced version of quick.cpp that loads the .proto at runtime, instead of compile time
6 // compile with g++ dccl-runtime.cpp -std=c++11 -lprotobuf -ldccl -o dccl-runtime && ./dccl-runtime
7 
8 // sets Reflection fields based on type of value
9 void _set_field_value(const google::protobuf::Reflection* refl, const google::protobuf::FieldDescriptor* field_desc, std::unique_ptr<google::protobuf::Message>& msg,
10  double val)
11 { refl->SetDouble(msg.get(), field_desc, val); }
12 
13 void _set_field_value(const google::protobuf::Reflection* refl, const google::protobuf::FieldDescriptor* field_desc, std::unique_ptr<google::protobuf::Message>& msg,
14  bool val)
15 { refl->SetBool(msg.get(), field_desc, val); }
16 
17 
18 void _set_field_value(const google::protobuf::Reflection* refl, const google::protobuf::FieldDescriptor* field_desc, std::unique_ptr<google::protobuf::Message>& msg,
19  int val)
20 {
21  if(field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_ENUM)
22  {
23  auto enum_desc = field_desc->enum_type();
24  auto enum_val_desc = enum_desc->FindValueByNumber(val);
25  if(enum_val_desc)
26  refl->SetEnum(msg.get(), field_desc, enum_val_desc);
27  }
28 }
29 
30 // Look up a field by name and set its value
31 template<typename FieldType>
32 void set_field(std::unique_ptr<google::protobuf::Message>& msg, const char* field_name, FieldType val)
33 {
34  // use protobuf Reflection to fill in message
35  const google::protobuf::Reflection* refl = msg->GetReflection();
36  const google::protobuf::Descriptor* desc = msg->GetDescriptor();
37  const google::protobuf::FieldDescriptor* field_desc = desc->FindFieldByName(field_name);
38  if(field_desc)
39  _set_field_value(refl, field_desc, msg, val);
40  else
41  std::cerr << "Invalid field name: " << field_name << std::endl;
42 }
43 
44 int main()
45 {
46  // Allows runtime compilation of .proto files
48  // uncomment for debugging
49  // dccl::dlog.connect(dccl::logger::DEBUG1_PLUS, &std::cerr);
50 
51  // Add any -I for protoc
52  dccl::DynamicProtobufManager::add_include_path("/home/toby/dccl/share/examples/quickstart_navreport");
53 
54  // FileDescriptor = type introspection on .proto
55  const google::protobuf::FileDescriptor* nav_report_file_desc =
57 
58  if(!nav_report_file_desc)
59  {
60  std::cerr << "Failed to read in navreport.proto" << std::endl;
61  exit(EXIT_FAILURE);
62  }
63 
64  std::string encoded_bytes;
65  dccl::Codec codec;
66 
67  // Descriptor = type introspection on the Message
68  // we know there's only one Message in this .proto (index 0)
69  const google::protobuf::Descriptor* nav_report_desc = nav_report_file_desc->message_type(0);
70  codec.load(nav_report_desc);
71 
72 
73  // SENDER
74  {
75  auto r_out = dccl::DynamicProtobufManager::new_protobuf_message<std::unique_ptr<google::protobuf::Message>>(nav_report_desc);
76 
77  set_field(r_out, "x", 450.0);
78  set_field(r_out, "y", 550.0);
79  set_field(r_out, "z", -100.0);
80  const int AUV = 1;
81  set_field(r_out, "veh_class", AUV);
82  set_field(r_out, "battery_ok", true);
83 
84 
85 
86  codec.encode(&encoded_bytes, *r_out);
87  }
88  // send encoded_bytes across your link
89 
90  // RECEIVER
91  {
92  auto r_in = codec.decode<std::unique_ptr<google::protobuf::Message>>(encoded_bytes);
93  std::cout << r_in->ShortDebugString() << std::endl;
94  }
95 }
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:56
static void enable_compilation()
Enable on the fly compilation of .proto files on the local disk. Must be called before load_from_prot...
void load()
All messages must be explicited loaded and validated (size checks, option extensions checks...
Definition: codec.h:96
void encode(std::string *bytes, const google::protobuf::Message &msg, bool header_only=false, int user_id=-1)
Encodes a DCCL message.
Definition: codec.cpp:273
static void add_include_path(const std::string &path)
Add a path for searching for import messages when loading .proto files using load_from_proto_file() ...
static const google::protobuf::FileDescriptor * load_from_proto_file(const std::string &protofile_absolute_path)
Load a message from a .proto file on the disk. enable_compilation() must be called first...
CharIterator decode(CharIterator begin, CharIterator end, google::protobuf::Message *msg, bool header_only=false)
Decode a DCCL message when the type is known at compile time.
Definition: codec.h:439