vector of structs

869 views
Skip to first unread message

Kai

unread,
Jan 16, 2015, 5:39:09 AM1/16/15
to flatb...@googlegroups.com
Hi,

I had the following layout:
struct Vec2 {
   x:float;
   y:float;
}

table test {
   points:[Vec2];
}

this generated c++ code to write into a flatbuffer that takes as argument Vec2* (pointers!). I created an array of pointers and inserted this into the right function, but the saved values were not correct. after fiddeling around I had the impression that vector of structs didn't work correctly, changing the struct to table (with some other minor changes to make it compile again) got me the right values....

I've looked at the test-code and this (from monster_test.fbs):
struct Test { a:short; b:byte; }

table Monster {
    ....
    test4:[Test] (id: 9);
}

will generate the following methods:
(call argument to CreateMonster:)
      ....
      flatbuffers::Offset<flatbuffers::Vector<const Test *>> test4 = 0,
      ....
      {
             ....
             builder_.add_test4(test4);
             ....
      }

and add_test4 is this:
      void add_test4(flatbuffers::Offset<flatbuffers::Vector<const Test *>> test4) { fbb_.AddOffset(22, test4); }
(note the Vector of Pointers to Test*)

does the addoffset only add the pointers and not the content of the pointer?

in the sample "test4" is only read, but never written. how would a write-call of "test4" be written?

thanks!
  Kai


Wouter van Oortmerssen

unread,
Jan 16, 2015, 7:13:55 PM1/16/15
to Kai, flatb...@googlegroups.com
To create such vectors, use CreateVectorOfStructs() (of which there is an example in test.cpp). This returns an offset, which you can then pass to CreateMonster or add_test4 etc.

--
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.

Kai

unread,
Jan 19, 2015, 10:57:08 AM1/19/15
to flatb...@googlegroups.com
okay, thanks :)
Reply all
Reply to author
Forward
0 new messages