/* * Normally enums are required to be ascending in the schema and * therefore there is no need to sort enums. If not, we export them in * the order defined anyway becuase there is no well-defined ordering * and blindly sorting the content would just loose more information. * * In conclusion: find by enum value is only support when enums are * defined in consequtive order. * * refers to: `opts->ascending_enum` * * `size` must hold the maximum buffer size. * Returns intput buffer if successful and updates size argument. */ void *fb_codegen_bfbs_to_buffer(fb_options_t *opts, fb_schema_t *S, void *buffer, size_t *size) { flatcc_builder_t builder, *B; B = &builder; flatcc_builder_init(B); export_schema(B, opts, S); if (!flatcc_builder_copy_buffer(B, buffer, *size)) { goto done; } sort_fields(buffer); done: *size = flatcc_builder_get_buffer_size(B); flatcc_builder_clear(B); return buffer; }
int encode(flatcc_builder_t *B, void *buffer, size_t *size) { int i, veclen = 3; void *buffer_ok; flatcc_builder_reset(B); C(start_as_root(B)); C(list_start(B, 0)); for (i = 0; i < veclen; ++i) { /* * By using push_start instead of push_create we can construct * the sibling field (of Bar type) in-place on the stack, * otherwise we would need to create a temporary Bar struct. */ C(list_push_start(B)); FooBar(sibling_create(B, 0xABADCAFEABADCAFE + i, 10000 + i, '@' + i, 1000000 + i, 123456 + i, 3.14159f + i, 10000 + i)); FooBar(name_create_str(B, "Hello, World!")); FooBar(rating_add(B, 3.1415432432445543543 + i)); FooBar(postfix_add(B, '!' + i)); C(list_push_end(B)); } C(list_end(B)); C(location_create_str(B, "https://www.example.com/myurl/")); C(fruit_add(B, Enum(Bananas))); C(initialized_add(B, True)); C(end_as_root(B)); /* * This only works with the default emitter and only if the buffer * is larger enough. Otherwise use whatever custom operation the * emitter provides. */ buffer_ok = flatcc_builder_copy_buffer(B, buffer, *size); *size = flatcc_builder_get_buffer_size(B); return !buffer_ok; }