Hi,
> We're trying to get rid of that max_count option, and I came across
> Nanopb_Cpp as a potential way to nicely turn the repeated message into a
> std::vector. The problem I'm running into is that the documentation for
> Nanopb_Cpp seems to say that I need to do a full conversion of the entire
> protobuf. I would love to just get a std::vector of our existing structs.
> Am I missing a simple way to use Nanopb_Cpp to just convert a repeating
> message into a vector?
I'm not very familiar with nanopb_cpp, but in main nanopb you could do
something like this (untested):
.proto:
option (nanopb_fileopt).include = "<vector>";
message MyMessage
{
repeated SubMessage myfield = 1 [(nanopb).callback_datatype="std::vector<SubMessage>"];
}
.cxx:
bool MyMessage_myfield_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_iter_t *field)
{
std::vector<SubMessage> *vec = (std::vector<SubMessage>*)field->pData;
for (SubMessage &item: vec)
{
pb_encode_tag_for_field(ostream, field);
pb_encode_submessage(ostream, SubMessage_fields, &item);
}
}
I'm not sure if there will be some complexity about putting a C++ object inside
the C struct, because it needs some logic to call the constructors and destructors
correctly. I think it should work, because the struct construction will be in your
own C++ code, and the nanopb C side only sees offsets to fields.
A pointer to a vector would be an alternative.
--
Petteri