interfacing C bitfield structs
Regan Heath
regan at netwin.co.nz
Sat Mar 18 02:50:29 PST 2006
On Fri, 17 Mar 2006 23:50:39 +0100, Wolfgang Draxinger
<wdraxinger at darkstargames.de> wrote:
> I'm currently writing D includes for several game and graphics
> related libraries (OpenGL, OpenAL, DevIL, SDL, cairo,
> Freetype2).
>
> Now with SDL I've encountered a problem: SDL makes use of serval
> bitfields and I've no idea, how to interface them to D.
>
> Has somebody an idea how I best interface with it elegantly?
I don't know if you'd call this elegant but here is what I did. The
important thing is using a type which is the correct size for the
bitfields and then creating some way to interface the bits you need.
struct DCB {
DWORD DCBlength; /* sizeof(DCB) */
DWORD BaudRate; /* Baudrate at which running */
uint BITS;
//bit fBinary; /* Binary Mode (skip EOF check) */
//bit fParity; /* Enable parity checking */
//bit fOutxCtsFlow; /* CTS handshaking on output */
//bit fOutxDsrFlow; /* DSR handshaking on output */
//bit[2] fDtrControl; /* DTR Flow control */
//bit fDsrSensitivity; /* DSR Sensitivity */
//bit fTXContinueOnXoff; /* Continue TX when Xoff sent */
//bit fOutX; /* Enable output X-ON/X-OFF */
//bit fInX; /* Enable input X-ON/X-OFF */
//bit fErrorChar; /* Enable Err Replacement */
//bit fNull; /* Enable Null stripping */
//bit[2] fRtsControl; /* Rts Flow control */
//bit fAbortOnError; /* Abort all reads and writes on Error */
//bit[17] fDummy2; /* Reserved */
WORD wReserved; /* Not currently used */
WORD XonLim; /* Transmit X-ON threshold */
WORD XoffLim; /* Transmit X-OFF threshold */
BYTE ByteSize; /* Number of bits/byte, 4-8 */
BYTE Parity; /* 0-4=None,Odd,Even,Mark,Space */
BYTE StopBits; /* 0,1,2 = 1, 1.5, 2 */
char XonChar; /* Tx and Rx X-ON character */
char XoffChar; /* Tx and Rx X-OFF character */
char ErrorChar; /* Error replacement char */
char EofChar; /* End of Input character */
char EvtChar; /* Received Event character */
WORD wReserved1; /* Fill for now. */
}
alias DCB* LPDCB;
private import std.intrinsic;
void fBinary(DCB* dcb, uint b) { (b) ? bts(&dcb.BITS,0) :
btr(&dcb.BITS,0); }
void fParity(DCB* dcb, uint b) { (b) ? bts(&dcb.BITS,1) :
btr(&dcb.BITS,1); }
void fOutxCtsFlow(DCB* dcb, uint b) { (b) ? bts(&dcb.BITS,2) :
btr(&dcb.BITS,2); }
void fOutxDsrFlow(DCB* dcb, uint b) { (b) ? bts(&dcb.BITS,3) :
btr(&dcb.BITS,3); }
void fDtrControl(DCB* dcb, uint b) { (bt(&b,0)) ? bts(&dcb.BITS,4) :
btr(&dcb.BITS,4); (bt(&b,1)) ? bts(&dcb.BITS,5) : btr(&dcb.BITS,5); }
void fDsrSensitivity(DCB* dcb, uint b) { (b) ? bts(&dcb.BITS,6) :
btr(&dcb.BITS,6); }
void fTXContinueOnXoff(DCB* dcb, uint b) { (b) ? bts(&dcb.BITS,7) :
btr(&dcb.BITS,7); }
void fOutX(DCB* dcb, uint b) { (b) ? bts(&dcb.BITS,8) :
btr(&dcb.BITS,8); }
void fInX(DCB* dcb, uint b) { (b) ? bts(&dcb.BITS,9) :
btr(&dcb.BITS,9); }
void fErrorChar(DCB* dcb, uint b) { (b) ? bts(&dcb.BITS,10) :
btr(&dcb.BITS,10); }
void fNull(DCB* dcb, uint b) { (b) ? bts(&dcb.BITS,11) :
btr(&dcb.BITS,11); }
void fRtsControl(DCB* dcb, uint b) { (bt(&b,0)) ? bts(&dcb.BITS,12)
: btr(&dcb.BITS,12); (bt(&b,1)) ? bts(&dcb.BITS,13) : btr(&dcb.BITS,13); }
void fAbortOnError(DCB* dcb, uint b) { (b) ? bts(&dcb.BITS,14) :
btr(&dcb.BITS,14); }
Note: The commented types shown in the struct were an attempt to use D's
bit type. They were originally C bitfields.
Regan
More information about the Digitalmars-d
mailing list