Templates, D way
crimaniak via Digitalmars-d
digitalmars-d at puremagic.com
Tue Sep 5 14:18:11 PDT 2017
On Tuesday, 5 September 2017 at 14:55:21 UTC, Void-995 wrote:
> Using unions? <Type>Count and <Type>Offset are different
> depending on input data, so the address where they are is
> varying depending on which file I've loaded. Or I didn't get
> what you meant.
Yes, so you need separate union type for every type of input
data. But these values are known in compile time, so you can do
mixin, creating the union for each type like this:
union MyBinaryStructA
{
ubyte[...] asBytes;
struct asStruct
{
align(1):
ubyte[...] dummy1; // padding before firstList
MyBinarySubStructAForA[...] firstList;
ubyte[...] dummy2; // padding between lists
MyBinarySubStructBForA[...] secondList;
...
}
}
unittest
{
MyBinaryStructA a;
a.asBytes = someBinarySourceForA; // fill from unstructured
source
auto item = a.asStruct.firstList[1]; // access to structure
element
}
More information about the Digitalmars-d
mailing list