Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Bitfield accessors

Moderators: kris

Posted: 11/20/07 19:46:54

Would be nice to have bitfield accessors in tango.
This is a basic thing for all stuff that handles with machine data and C libs.
So libs don't need to have their own implementations.

To show the idea.... (i don't know if this can work)

template BitfieldAccess( T, uint memberOffset, uint startBit, int width ){
   static if( width == 1 ){
      bool opCall();                // bool getter
      void opCall( bool newValue ); // bool setter
   }
   else{
      T opCall();                // getter
      void opCall( T newValue ); // setter
   }
}

// example struct with bitfield access
struct MyStruct {
    int flags;
    mixin BitfieldAccess( typeof(flags), flags.offsetof, 2, 3 ) mode;
    mixin BitfieldAccess( typeof(flags), flags.offsetof, 5, 1 ) active;
}

// used like this
MyStruct s;
s.mode( 3 );
s.active = true;
bool res = s.active;
Author Message

Posted: 02/17/08 07:00:18

we're kinda hoping bitfields will be supported by the compiler instead. There's apparently an existing BitField? template kicking around, but I understand the efficiency of it is not so attractive