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

Support MakeBitsViewOfInt() #78

Open BenjaminLawson opened 1 year ago

BenjaminLawson commented 1 year ago

It would be nice if the C++ generated code supported MakeBitsView() types with uint backing storage. We often need to save bitmasks from packet views as integers/enums for passing around in our code. We then need to do bitmasking later without Emboss, which often requires creating new C++ enums. One thing to consider is that we may want the backing storage to have host endianness.

uint64_t feature_mask;
auto feature_view = emboss::MakeFeatureView(feature_mask);
bool foo_supported = feature_view.foo_supported().Read();
uint16_t bar = feature_view.bar().Read();
reventlov commented 1 year ago

This should actually be pretty easy. bits and struct views are actually generated almost identically, with a templated backing storage. MakeStructView() just constructs and passes in a ContiguousBuffer.

MakeBitsViewOfInt() could construct and pass BitBlock<IntBuffer>, where IntBuffer is a new class with the same interface as BigEndianByteOrderer, LittleEndianByteOrderer, and NullByteOrderer -- just a few trivial methods.

Technically you could do this right now in user code, but that use is only quasi-supported -- I always intended to allow user-defined storage, but I haven't documented or stabilized the interfaces yet.