data mapping, more elegant solution?

Mike vertex at gmx.at
Thu Dec 13 06:46:28 PST 2007


On Thu, 13 Dec 2007 14:43:51 +0100, mandel <oh at no.es> wrote:

> On Thu, 13 Dec 2007 13:35:35 +0000, Regan Heath wrote:
>> Use a union?
>>
>> union thing
>> {
>>    ubyte[8] ub;
>>    uint     ui;
>> }
>>
>> void main()
>> {
>>    thing a;
>>    a.ui = 42;
>> }
>>
> This way I can't insert data at arbitrary places, e.g. array[4..8].
> I also would have to cast thing to ubyte[8] when I pass it to functions.
> It's also hackish. :P

Then pack the hackish things away into something:

struct thing
{
     union
     {
         ubyte[8] ub;
         uint ui;
     }

     void opAssign(uint value)
     {
         ui = value;
     }

     uint opCall()
     {
         return value;
     }

     void opIndexAssign(byte value, uint idx)
     {
         ub[idx] = value;
     }

     byte opIndex(uint idx)
     {
         return ub[idx];
     }
}

Better? You can pass that to functions. For arbitrary places you're either  
stuck with the casting or you maybe can do something with opSlice.

-Mike

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list