google / emboss

Emboss is a tool for generating code that reads and writes binary data structures.
Apache License 2.0
68 stars 21 forks source link

Provide a way to read the offset and size of a field. #99

Open reventlov opened 9 months ago

reventlov commented 9 months ago

Given a structure like:

struct Foo:
  0 [+4]  UInt  a
  some_complex_offset [+some_complex_size]  Structure b

it would be useful to be able to get the values of some_complex_offset and some_complex_size, both in Emboss definitions and in C++.

The tricky part of this is that the offset and size of a field are properties of the structure that contains the field, not properties of the field itself, so a function like $offset_of(b) would need some new machinery to ensure that the argument was a physical field and that it was called in a context where it makes sense.

One thought I had was to make implicit virtual fields $offset_of_b and $size_of_b, similar to the existing implicit virtual field $size_in_bytes. (These might be spelled $offset_of b or $offset_of(b) in an .emb. The C++ names would need to be regular C++ identifiers, or there would need to be contortions in the C++ code generator to make something like view.OffsetOf().b() work.)