[c++] a utility to convert FB data structures to standard data structures?

1,174 views
Skip to first unread message

Shahbaz Chaudhary

unread,
Oct 26, 2015, 11:47:01 AM10/26/15
to FlatBuffers
Is there a utility to convert FB data structures, such as flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> to standard data structures, such as std::vec<std::string>? I've trying to use as much of FB's built-in functionality as possible.

Wouter van Oortmerssen

unread,
Oct 26, 2015, 3:04:28 PM10/26/15
to Shahbaz Chaudhary, FlatBuffers
No there isn't, because a `flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>` lives entirely inside the FlatBuffer and causes no memory allocation, and `std::vector<std::string>` allocates a lot. Though I can see it be convenient for cases where efficiency doesn't matter, I wouldn't want to promote this as a standard way to do things.

On Mon, Oct 26, 2015 at 8:47 AM, Shahbaz Chaudhary <shah...@gmail.com> wrote:
Is there a utility to convert FB data structures, such as flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> to standard data structures, such as std::vec<std::string>? I've trying to use as much of FB's built-in functionality as possible.

--
You received this message because you are subscribed to the Google Groups "FlatBuffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shahbaz Chaudhary

unread,
Oct 27, 2015, 2:02:10 PM10/27/15
to FlatBuffers, shah...@gmail.com
So what is the right way of getting either a standard string or flatbuffers string out of Offset<flatbuffers::String>?

To be more specific, I am getting a chuck on data off the wire:

//Schema
union MyFBData{
  UnionTypeA, UnionTypeB, UnionTypeC...
}

table UnionTypeA{ vector_of_strings: [string]; }
table UnionTypeB{ name: string; id:uint; }
table UnionTypeC{ vector_of_Bs: [UnionTypeB]; }

//Code
struct my_data{ char data[500]; }

callback(my_data &data){
  auto my_obj = GetMyFBData(my_data.data);
  switch(my_obj->unioned_type()){
    case UnionTypeA:
      process(  reinterpret_case<const UnionTypeA*> my_obj->unioned()  );
      break;
    case ...
  }
}

process(UnionTypeA a){
  for(auto &elem : a->vector_of_strings() ){
    //I expect elem will be of type flatbuffers::Offset<flatbuffers::String>
    print_string(elem);//<<== I CAN'T PRINT AN OFFSET
  }
  process_whole_vector_of_strings(???);//<<==NOT EFFECIENT, BUT HOW DO I GET VEC<STR>?
}
...


Perhaps you could start a wiki on github and a page for such snippets? Adding code to the repo is obviously not trivial and non-code description in forums isn't always as helpful to those new to this. With a wiki, others could contribute as well.

Wouter van Oortmerssen

unread,
Oct 28, 2015, 1:12:38 PM10/28/15
to Shahbaz Chaudhary, FlatBuffers
You should never have to deal with Offset directly, if you access an element in a Vector, an Offset<flatbuffers::String> gets dereferenced into a String *, which has c_str() and str() methods that you can use to access the data (c_str() being more efficient).

This is what happens if you call vector->Get(), if it doesn't work equally with for(auto..) that would be a bug, let me know.

As for a wiki, we'd rather just make sure our existing documentation is better. It is all text (markdown), so making a PR for it to contribute is pretty easy.
Reply all
Reply to author
Forward
0 new messages