Templates, D way

Void-995 via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 5 15:51:45 PDT 2017


On Tuesday, 5 September 2017 at 21:18:11 UTC, crimaniak wrote:
> 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
> }

The whole thing is that I don't know either padding nor elements 
count. Those values are read from file. First you need to read 
whole file data and cast those bytes as pointer to file header 
structure. Offset of each list is bigger then size of the parent 
structure. Length of each list vary from 0 to whatever.


More information about the Digitalmars-d mailing list