lua-users home
lua-l archive

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

Thomas Lauer wrote:
> * trying to go down the string route. That won't work well as many of
> the constants are to be or'ed together or will be used in (bitwise)
> and's  
> [...]
> I have a gut feeling that the third option is the best of the lot but
> I would like to know what others have done when faced with long lists
> of constants. (A quick search in the archives has brought no
> revelation.)   
> Things I consider are first and foremost ease of use (for the clients
> of the library, not for me;-) and efficiency, space- and time-wise. 

For my OpenGL and Win32 API bindings, I went down the string route.
Bitwise operators and bit mask constants are used most of the time as a
hack to emulate a boolean set, I don't see any good reason (beyond
speed) to expose such cumbersome interface to a Lua programmer. Instead
I use regular Lua sets (that is tables like {onflag=true,
offflag=false}), and I have a converting functions that I reuse in all
bindings needing bitfields.

It works very well for OpenGL, because GL_ constants don't overlap much
and share a common prefix and few types (GLuint and GLbitfield). It's a
little more complicated for Win32, since there are many overlapping
constant sets with each a different prefix and a different C type, and
some API functions use sometimes a combination of several of these sets.
But in both case the ease of use at final completely justifies the extra
work involved in writing the binding.