Replies: 8 comments 9 replies
-
The first thing to realize is that structs are self-contained blocks of memory, including nested structs, while tables are not. So you can't really first create the inner structs and then add a reference. Have a look at builder.md You can also look at the generated code because constructors are generated in an easily readable function as I recall. I think the choice was to have all arguments flattened when using the constructor method because - why would you construct some fields but then not the children? You cannot first use child constructors because as mentioned, everything is created at once. So you can use the constructor if you provide all the child fields also in the constructor call as you suggest, but want to avoid. The other option is to manually create the structs in native C memory and fill in the structs there, then add the outermost struct by pointer. In this case the struct will be efficiently copied to the output buffer on little endian systems, and on big endian systems the content will be endian converted. Be sure to null the struct with memset first to avoid garbage in padding areas. Failing to do so is not catastrophic, but bad style. A third option is to get a temp pointer to a struct from the builder, for example via a push or add call - see builder.md. Here it is not necessary to do memset the struct. Let me know if this works out for you and please ask again if you need more help. BTW: it might be possible to use a constructor on a struct on the C stack before being added the builder. I'm not quite sure. But if it is, you should be able to use constructors also for child structs. You can look at the generated code. Mikkel |
Beta Was this translation helpful? Give feedback.
-
Also, if you want to add the struct to a table, the call you are using will not work because it creates a separate memory block for the struct similar to child tables. But structs are embedded inside the block of memory that represents a table, i.e. structs are usually inline. The call you are using can be used when structs are top level objects in a buffer (only supported by FlatCC), or when structs are used in unions. Otherwise you should a table field specific add or create call. In your case add if you want to add fields manually. |
Beta Was this translation helpful? Give feedback.
-
This only works for tables. If S1S2 were a table with a member field1 of type S1, then something along those lines should work. You need to figure out what kind of buffer you will be creating. Do you want to create a FlatBuffer with a struct as root? There should be something like S1S2_start_as_root(); If you do this, you must be aware that the buffer will not be readable by other languages because flatc chose not to support structs as root objects at some point. |
Beta Was this translation helpful? Give feedback.
-
I earlier wrote that there might be a struct constructor for structs in C memory. This method is called assign: From builder.md:
You can use that on child structs after you have a pointer to the struct. Assign takes care of zero padding. |
Beta Was this translation helpful? Give feedback.
-
I took ns_S1S2_field1_start() from the https://github.com/dvidelabs/flatcc/blob/master/doc/builder.md#structs
Anyway, my root is a table but I have many nested structs and I'm looking for a way to build the bin without breaking the all structure of the data. |
Beta Was this translation helpful? Give feedback.
-
I created a new branch It does what you expected with the original create call, except you need to get the pointer first. Se also builder.md under structs. |
Beta Was this translation helpful? Give feedback.
-
Tests added: flatcc/test/monster_test/monster_test.c Line 2050 in a0af9c1 |
Beta Was this translation helpful? Give feedback.
-
Thanks for your help.
|
Beta Was this translation helpful? Give feedback.
-
Hello,
Please consider the following schema:
I tried the following to build:
I got the following error:
If I defined tables instead of structs it works.
It seems ns_S1S2_create expects all the nested structs fields.
How can I create nested structs by creating each structs and nested structs separately ?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions