接上文

example_reader.h

#ifndef EXAMPLE_READER_H
#define EXAMPLE_READER_H

/* Generated by flatcc 0.6.1-dev FlatBuffers schema compiler for C by dvide.com */

#ifndef FLATBUFFERS_COMMON_READER_H
#include "flatbuffers_common_reader.h"
#endif
#include "flatcc/flatcc_flatbuffers.h"
#ifndef __alignas_is_defined
#include <stdalign.h>
#endif
#include "flatcc/flatcc_prologue.h"
#ifndef flatbuffers_identifier
#define flatbuffers_identifier 0
#endif
#ifndef flatbuffers_extension
#define flatbuffers_extension ".bin"
#endif


typedef const struct wii_Packet_table *wii_Packet_table_t;
typedef struct wii_Packet_table *wii_Packet_mutable_table_t;
typedef const flatbuffers_uoffset_t *wii_Packet_vec_t;
typedef flatbuffers_uoffset_t *wii_Packet_mutable_vec_t;
#ifndef wii_Packet_file_identifier
#define wii_Packet_file_identifier flatbuffers_identifier
#endif
/* deprecated, use wii_Packet_file_identifier */
#ifndef wii_Packet_identifier
#define wii_Packet_identifier flatbuffers_identifier
#endif
#define wii_Packet_type_hash ((flatbuffers_thash_t)0x42e00ae)
#define wii_Packet_type_identifier "\xae\x00\x2e\x04"



struct wii_Packet_table { uint8_t unused__; };

static inline size_t wii_Packet_vec_len(wii_Packet_vec_t vec)
__flatbuffers_vec_len(vec)
static inline wii_Packet_table_t wii_Packet_vec_at(wii_Packet_vec_t vec, size_t i)
__flatbuffers_offset_vec_at(wii_Packet_table_t, vec, i, 0)
__flatbuffers_table_as_root(wii_Packet)

__flatbuffers_define_scalar_field(0, wii_Packet, a, flatbuffers_int32, int32_t, INT32_C(0))
__flatbuffers_define_scalar_field(1, wii_Packet, b, flatbuffers_bool, flatbuffers_bool_t, UINT8_C(0))
/**  b1:bool; */
__flatbuffers_define_string_field(2, wii_Packet, c, 0)


#include "flatcc/flatcc_epilogue.h"
#endif /* EXAMPLE_READER_H */

从实际开发上讲,相比于builder和verifier来说reader才是最重要的功能。

example_builder.h中的例子。


#include <stdio.h>
#include <stdlib.h>

#include "hexdump.h"
#include "example_builder.h"
#include "example_verifier.h"

int main(int argc, char* argv[])
{
  int ret = 0;
  char *dbuf = NULL;
  size_t dsize;
  flatcc_builder_t builder;
  flatcc_builder_init(&builder);

  int a = 77;
  flatbuffers_bool_t b = flatbuffers_false;
  flatbuffers_string_ref_t c = flatbuffers_string_create_str(&builder, "hello!!");
  printf("a=0x%x b=%d c=%d\n", a, b, c);
  wii_Packet_create_as_root(&builder, a, b, c);

  dbuf = flatcc_builder_finalize_buffer(&builder, &dsize);
  printf("dbuf=%p dsize=%zd\n", dbuf, dsize);
  hexdump("Packet", dbuf, dsize, stderr);

  wii_Packet_table_t packet = wii_Packet_as_root(dbuf);
  if ( packet)
  {
    printf("a=%d b=%s c=%s\n", wii_Packet_a(packet), wii_Packet_b(packet) ? "true" : "false", wii_Packet_c(packet));
  }

  flatcc_builder_free(dbuf);
  flatcc_builder_clear(&builder);

  return 0;
}

你会发现根本没有用到example_reader.h文件的两个函数,wii_Packet_vec_len和wii_Packet_vec_at。这两个函数实际是数组中才用得到的。

所以我们需要改一下例子,把数组类型在这讲一讲。

数组类型