lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Monday 03 March 2003 11:49 pm, Brian Hook wrote:
> Bit manipulation would seem to be, honestly, to be the complete
> counter-definition of "fluff".  Integers and bits, these are the
> fundamental building blocks of, um, everything.

Don't confuse fundamentals with essentials. Lua doesn't give you the 
opportunity to directly access transistors in the CPU, either.

Here's what I see as how to "do" bitflags in Lua. Let's start with C code:

int myFooFlags = FOO_BLUEPANTS | FOO_3PLANES | FOO_EDIBLE;

here's what I would think of as the Lua Way (well, A Lua Way):

myFooFlags = foo.fooflags { 
    pantscolor = foo.blue,
    planes = 3,
    edible = true
}

Here, foo.fooflags would be a C function which would handle the gruntwork of 
taking the table passed to it and return a userdata that actually stores the 
flag. You get extra verbosity, type checking, and extra scope resolution 
stuff, and all without being needlessly confused by what exactly the 
bitwise-OR is doing.

Here's another way, to complement the first:

myFooFlags = foo.createfooflags()
myFooFlags.pantscolor = foo.blue
myFooFlags.planes = 3,
myFooFlags. edible = true

Here, myFooFlags is being given a metatable that allows it to set bits on the 
fly. You can combine this with the previous approach for maximum flexibility 
during both declaration and modification.

I think of direct representation of bitfields as an implementation detail. 
Scripters shouldn't have to worry about bits or bitfields; all they should 
need to know is that a number represents qualitative data, and this flag data 
you've created represents a grouping of options.

Ben