Greetings,
I'm experimenting with flatbuffers, in C, just getting a feel for it. I'm having a weird issue. Well, a couple more than likely.
I *think* I'm building the flatbuffers and adding all the correct components in the correct manner. However, when I try to read a vector of tables, _vec_len(T) is returning size 0, even though when I hexdump the buffer I can clearly see the said tables being added to/removed from the buffer.
I've used more methods of getting the vector of tables to be readable than I can count. Most have ended with type mismatch compile errors. None of the ones that have actually compiled have resulted in _vec_len(T) returning anything other than 0.
I've also tried using _vec_at(0), but this causes an assertion to fail stating that the given index is out of range.
At this point, I'm either: a) missing something really simple, b) building the flatbuffer completely wrong, or c) making assumptions about how flatbuffers are built that are just plain incorrect.
I have attached my schema and source code, as well as debugging output from the program.
The line that seems to be failing is line 115 in the C code. Either that, or the entire block starting at line 37 is wrong.
By my understanding, using _start(B) and _end(B), everything that gets added to said component of the schema should be added to the buffer and placed into the v-tables for access automatically.
So, I'm not sure where to go. Can anyone point to what I'm doing wrong/misunderstanding? Any help is greatly appreciated.
(I tried attaching the files, but I kept getting an error.)
flatbuffers_test.fbs
------------------------------------------------------------
file_identifier "TEST";
table Ping {
  sent:uint;
}
table Pong {
  sent:uint;
}
union Type {
Ping,
Pong,
}
enum ServerTypes:ubyte {
mach = 0,
cnc,
mill,
press,
drill,
controller,
unknown,
}
table TransitHop {
serverID: uint64;
serverType: ServerTypes;
}
table TransitHeader {
from: TransitHop;
to: TransitHop;
hops: [TransitHop];
}
table TransitSequence {
  transitID: uint64;
begin:Â Â Â uint8;
current:Â Â uint8;
total:Â Â Â uint8;
}
table TestRoot {
type: Type;
sequence: TransitSequence;
routing: TransitHeader;
}
root_type TestRoot;
flatbuffers_test.c
--------------------------------------------------
#include <stdio.h>
#include "flatcc/support/hexdump.h"
#include "flatbuffers_test_builder.h"
// This allows us to verify result in optimized builds.
#define test_assert(x) do { if (!(x)) { assert(0); return -1; }} while(0)
int main(void) {
  flatcc_builder_t builder, *B;
  B = &builder;
  void *buff = NULL;
  size_t size;
  FILE *fh = NULL;
  flatcc_builder_init(B);
  printf("TestRoot - Routing\n\n");
  TestRoot_start_as_root(B);
  {
    // test_root type
    {
      Ping_ref_t testType = Ping_create(B, 0); // the 0 here would be the time the ping is sent
      TestRoot_type_add(B, Type_as_Ping(testType));
    }
    // test_root sequence
    {
      TransitSequence_ref_t sequence = TransitSequence_create(B, 1, 1, 1, 1);
      TestRoot_sequence_add(B, sequence);
    }
    // test_root routing
    {
      TransitHeader_start(B);
      {
        TransitHop_ref_t hopFrom = 0;
        TransitHop_ref_t hopTo = 0;
        TransitHeader_hops_start(B);
        {
          hopFrom = TransitHop_create(B, 1, ServerTypes_mach);
          TransitHop_create(B, 2, ServerTypes_controller);
          TransitHop_create(B, 3, ServerTypes_controller);
          hopTo = TransitHop_create(B, 4, ServerTypes_cnc);
        }
        TransitHop_ref_t hops = TransitHeader_hops_end(B);
        TransitHeader_from_add(B, hopFrom);
        TransitHeader_to_add(B, hopTo);
        TransitHeader_hops_add(B, hops);
      }
      TransitHeader_ref_t routing = TransitHeader_end(B);
      TestRoot_routing_add(B, routing);
    }
  }
  TestRoot_end_as_root(B);
  buff = flatcc_builder_finalize_aligned_buffer(&builder, &size);
  printf("flatbuffer size: %ld\n", size);
  TestRoot_table_t test_root = TestRoot_as_root(buff);
  test_assert(test_root != 0);
  int type_present = TestRoot_type_is_present(test_root);
  int sequence_present = TestRoot_sequence_is_present(test_root);
  int routing_present = TestRoot_routing_is_present(test_root);
  printf("  type present: %d\n", type_present);
  if (type_present == 1) {
    Type_union_type_t type = TestRoot_type_type(test_root);
    printf("\ttype: %s\n", Type_type_name(type));
  }
  printf("sequence present: %d\n", sequence_present);
  if (sequence_present == 1) {
    TransitSequence_table_t sequence = TestRoot_sequence(test_root);
    printf("\ttransitID: %ld\n", TransitSequence_transitID(sequence));
    printf("\t  begin: %d\n", TransitSequence_begin(sequence));
    printf("\t current: %d\n", TransitSequence_current(sequence));
    printf("\t  total: %d\n", TransitSequence_total(sequence));
  }
  printf(" routing present: %d\n", routing_present);
  if (routing_present == 1) {
    TransitHeader_table_t routing = TestRoot_routing(test_root);
    TransitHop_table_t from = TransitHeader_from(routing);
    TransitHop_table_t to = TransitHeader_to(routing);
    TransitHop_vec_t hopsTable = TransitHeader_hops(routing);
    test_assert(hopsTable != 0);
    ServerTypes_enum_t serverTypeFrom = TransitHop_serverType(from);
    ServerTypes_enum_t serverTypeTo = TransitHop_serverType(to);
    printf("\tfrom: %ld\t%s\n", TransitHop_serverID(from), ServerTypes_name(serverTypeFrom));
    printf("\t to: %ld\t%s\n", TransitHop_serverID(to), ServerTypes_name(serverTypeTo));
    int hops_present = TransitHeader_hops_is_present(routing);
    if (hops_present == 1) {
//Â Â Â Â Â Â // this causes [Assertion `flatbuffers_vec_len(vec) > (i) && "index out of range"' failed.] ?????
//Â Â Â Â Â Â TransitHop_table_t hopsTableZero = TransitHop_vec_at(hopsTable, 0);
//
//Â Â Â Â Â Â test_assert(hopsTableZero == 0);
      size_t hops_vec_len = TransitHop_vec_len(hopsTable);
      printf("\thops: %lu\n", hops_vec_len);
      for (size_t i = 0; i < hops_vec_len; i++) {
        printf("\t\t%lu\n", i);
      }
    }
  }
  fh = fopen("/tmp/fbhd-test1", "w");
  if (fh == NULL) {
    printf("Cannot open /tmp/fbhd-test1 for writing\n\n");
    return -1;
  } else {
    hexdump(NULL, buff, size, fh);
  }
  fclose(fh);
  flatcc_builder_aligned_free(buff);
  return 0;
}
debug1.txt
--------------------------------------------------
Program output with the vector of tables being added to the flatbuffer:
TestRoot - Routing
flatbuffer size: 188
  type present: 1
    type: Ping
sequence present: 1
    transitID: 1
      begin: 1
     current: 1
      total: 1
 routing present: 1
    from: 1 mach
     to: 4 cnc
    hops: 0
00000000 0c 00 00 00 54 45 53 54 00 00 00 00 5c ff ff ff |....TEST....\...|
00000010Â 01 00 00 00 70 00 00 00Â 5c 00 00 00 04 00 00 00Â |....p...\.......|
00000020Â 7a ff ff ff 0c 00 00 00Â 3c 00 00 00 08 00 00 00Â |z.......<.......|
00000030 00 00 00 00 96 ff ff ff 04 00 00 00 00 00 00 00 |................|
00000040 01 00 00 00 a6 ff ff ff 03 00 00 00 00 00 00 00 |................|
00000050 05 00 00 00 b6 ff ff ff 02 00 00 00 00 00 00 00 |................|
00000060 05 00 00 00 cc ff ff ff 01 00 00 00 00 00 00 00 |................|
00000070 00 00 00 00 e8 ff ff ff 01 00 00 00 00 00 00 00 |................|
00000080 01 01 01 00 fc ff ff ff 04 00 04 00 0c 00 0f 00 |................|
00000090Â 04 00 0c 00 0d 00 0e 00Â 06 00 0c 00 04 00 08 00Â |................|
000000a0Â 0d 00 04 00 0c 00 0a 00Â 10 00 08 00 0c 00 04 00Â |................|
000000b0Â 0c 00 14 00 04 00 08 00Â 0c 00 10 00Â Â Â Â Â Â Â |............|
Program output without the vector of tables being added to the flatbuffer:
TestRoot - Routing
flatbuffer size: 74
  type present: 1
    type: Ping
sequence present: 1
    transitID: 1
      begin: 1
     current: 1
      total: 1
 routing present: 0
00000000 0c 00 00 00 54 45 53 54 00 00 00 00 cc ff ff ff |....TEST........|
00000010 01 00 00 00 18 00 00 00 04 00 00 00 e8 ff ff ff |................|
00000020 01 00 00 00 00 00 00 00 01 01 01 00 fc ff ff ff |................|
00000030Â 04 00 04 00 0c 00 0f 00Â 04 00 0c 00 0d 00 0e 00Â |................|
00000040Â 0a 00 10 00 04 00 08 00Â 0c 00Â Â Â Â Â Â Â Â Â Â |..........|
Program output when trying to use _vec_at(0):
TestRoot - Routing
flatbuffer size: 188
  type present: 1
    type: Ping
sequence present: 1
    transitID: 1
      begin: 1
     current: 1
      total: 1
 routing present: 1
    from: 1 mach
     to: 4 cnc
flatbuffers_test: /home/vadtec/schema/generated/flatbuffers_test_reader.h:192: TransitHop_vec_at: Assertion `flatbuffers_vec_len(vec) > (i) && "index out of range"' failed.