I have code which has multiple complex C++ data structures that I want to save to disk to later deserialize to be used again. Let's put an example to easily understand what I'm trying to do. I have the following C++ struct:
struct myCstruct
{
int id;
OtherStructType ot;
std::vector<std::string> my_array;
std::vector<StructType_2> my_other_array;
};
I wrote the equivalent .proto
files to represent myCstruct
:
syntax = "proto3";
import "OtherStructType.proto"; // OtherStructType proto code is here
import "3drStructType.proto"; // 3drStructType proto code is here
package my_ns;
message myCstruct{
int32 id = 1;
OtherStructType ot = 2;
repeated string my_array = 3;
repeated StructType_2 my_other_array = 4;
}
Somewhere in my code I have a variable foo
referencing an instance of myCstruct
. How can I now cast var to a type my_ns::myCstruct
given that I've compiled the .proto
files and included the generated C++ files in my project?:
struct myCstruct var = some_func();
my_ns::myCstruct proto_struct;
// How can I dump the content of var into proto_struct???
I know protobuf
creates methods to set attributes but I want to dump the entire content of the C++ struct in the protobuf class instance. I don't want to:
struct myCstruct var = some_func();
my_ns::myCstruct proto_struct;
proto_struct_set_id(var.id);
proto_struct_set_ot(var.ot);
...
Any help is appreciated.