-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scalar Aliases? #5597
Comments
FlatBuffers has no way to do C/C++ style typedefs itself. Wrapping in a struct is indeed the only way to do something similar, and is a "zero cost" abstraction, other than the slightly clumsier syntax. @mikkelfj who maintains the C implementation. |
I have tried to read this question several times, and I simply do not understand it. Can I please have a concrete example with a C type, a flatbuffer schema, and exactly which names should match? You can do a lot with define but that is manual work. If you are willing to put in some effort, you can generate a binary schema (bfbs) and read that information and use it to generate defines or inline functions or similar adaptors, but since I do not understand the fundamental problem I'm not sure if it will help. Another point: I'd actually like to have typedef's in the schema language itself because you say that this 'string' is actually a date format, and you can associate attributes with that which can be used, for example when reading binary schema. But there is no such thing for the time being. |
Let's say I have a legacy C header file with the following declaration:
It would be great if you could define an alias called blSessionHandle that reference ulong32. Or, as mentioned above, a typedef mechanism. Currently, the only option is to declare blSessionHandle as a struct which containing a single ulong32 member; but that member is named and adds an unnecessary level of indirection when calling the accessor and mutator. This could work with just declaring an alias to ulong32 or create a typedef-like construct. |
I still don't understand. If you have a table field
Would you then like a) the generated code to return a value of type blSessionHandle, or would you like the schema syntax to use the name b) blSessionHandle?
? In case b) this does not relate to the actual "legacy" C code, because it is just within the schema, but it does help maintain context between the two systems. case b) is actually close to what I would like to add, if not identical to, but there it is work, and it requires coordination. Let's call b) for schema typedef. My thoughts on handling the schema typedef is something along always using structs internally because it simplifies many things including unions and alignment. But the C code generator can handle single value typedefs specially and automatically lift the indirection you mention. This is something I have pondered in the past and something I might do even if only supported for flatcc because it annoys me sufficiently not to have it. The typedef from the earlier example will internally, but not user visibly be translated into
The code generator then picks up on the typedef attribute and unwraps it because it is a primitive. The struct style can be written in todays schema format as is and the typedef attribute could be declared. A user would also be able to use the struct form directly in schema. This has the benefit of allowing new attributes to be introduced:
You might also want Another much more pragmatic option is to use the C preprocessor or the sed tool to expand predefined types. |
Note that you can also use enums if it is strictly for scalar types.
|
This issue is stale because it has been open 6 months with no activity. Please comment or this will be closed in 14 days. |
I would like to reopen this topic again. I am trying to convert an internal binary format heavily reliant on mmap towards flatbuffers. While the concepts used are very alike, not having the ability to have 'type abstractions' (without structs) is clumsy. Maybe it is zero cost as @aardappel suggested, if this struct-shadow-type has to be added to a vector, it just looks awefull. In relationship to for example the Java generated code is plain overhead. The use case is the following. At serialisation stage I would like to define the 'size' of my fields so they are recognizable. For example, for a list of stops never exceeding uint16_t, I rather use spidx_t in my code, so that a reference to that stop list also uses spidx_t. Sure I can do like below...
...but the code for the description below is far more elegant.
I can see a situation where preprocessing a fbs file is an option, but so is aliasing. |
Simple typedefs wouldn't be hard if its a schema-only feature.
Then a simple extra lookup wherever it parses a type. |
Exactly that. Could we reopen this issue? |
I support that.
I'd also like to associate attributes with the type definition. For example, you might want to add attributes for validation or for JSON print format specific to the type, or specify metadata such as crypto cipher. Or just to clarify what the semantics of the type is.
|
Also, BTW: I think attributes should accept list form. And there is formatting error on the schema syntax for grammar: |
Yes, I think aliases can work for any type, even more complex ones, e.g.
not sure how useful that is, but no reason to limit it, I think. We can certainly allow attributes, though we currently have no specific use for them:
PRs welcome :) |
It is not exactly an alias due to metadata, and
FlatBuffers spec isn't clear about attribute semantics for repeated values. I suggest treating them as ordered lists and let backends decide further interpretations. That means that This becomes significant when types are based on types that already have metadata. In that case attributes are appended rather than replaced, but a backend to choose the first or last value if it so desires. We could define a new default value for the type, for some base types, but this probably stretching it a bit too far. |
Corner case:
This type can be used in structs, but not in table fields.
cannot be used in a struct. All of this doesn't quite solve the missing scalars in Unions because even if structs can be typedef'ed, the access mechanism won't change. I think this is orthogonal and at some point we should consider adding scalar types to unions. |
This issue is stale because it has been open 6 months with no activity. Please comment or this will be closed in 14 days. |
Please keep it open. |
This issue is stale because it has been open 6 months with no activity. Please comment or this will be closed in 14 days. |
Please keep open. |
I'm writing a schema where I have fields that are indices into an array elsewhere in the schema. I'd like to document that these fields are indices into a specific array. In C++, I'd use a Is there an already existing way to do this? If not, consider it another use case for type aliases. |
So far the best scalar type alias should be #5597 (comment) ?
But this doesn't fully solve the issue if you want to convert from own custom type to flatbuffers generated type... |
I'm trying to wrap an existing legacy C API in Flatbuffers to decouple logic.
Maybe I missed this, but is there anyway that I apply an alias (or typedef) to a internally-defined scalar to keep the naming of the objects consistent with the specification?
i.e. My existing C header files have a number of typedef's which define a contextually-named type e.g. "slotId" to a native size construct "unsigned long". I can't find a way to do that in FB without wrapping it in a struct, but now I have this pesky internal field I need to deal with.
The text was updated successfully, but these errors were encountered: