C struct -> D struct (alignment hacks)
Don
nospam at nospam.com
Thu Feb 5 00:34:21 PST 2009
Mike wrote:
> On Wed, 04 Feb 2009 21:04:16 +0100, Jarrett Billingsley
> <jarrett.billingsley at gmail.com> wrote:
>
>> Nope. The C struct is defining a bitfield; a and b will actually be
>> contained within a single 4-byte field. Your D version defines three
>> integers.
>>
>> Unfortunately the C specification does not specify any required
>> ordering for bitfields, padding, ordering etc. Fortunately, most
>> compilers just put them in order, starting from the lowest bits.
>>
>> What you'll have to do, then, is put a single int field that
>> corresponds to the C struct's bitfields. Then, have methods which
>> shift and mask the bits to get and set the individual bitfields. htod
>> will do this for you, if you're on windows and just want to run an .h
>> file containing that struct through it.
>
> Thanks. I tried htod with Wine, and although it runs it can't open
> include files, so I thought I'd translate the headers manually. However
> - I found more of those bitfields; I'll better find a machine with
> Windows then :)
>
If you end up doing it manually, look in Phobos2, typecons.d has
functions for generating bitfields. It'll work on D1 with trivial
modifications.
More information about the Digitalmars-d-learn
mailing list