lua-users home
lua-l archive

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


Hi!
I am working on a Lua binding for a library that makes extensive use
of enums to pass on flags, capabilities, and such. Like this:

typedef enum {
     UNKNOWN    = 0x00000000,
     FRONTONLY  = 0x00000001,      
     BACKVIDEO  = 0x00000002,      
     BACKSYSTEM = 0x00000004,
     TRIPLE     = 0x00000008,
     WINDOWS    = 0x00000010
} BufferMode;

So I would like to know the community opinion on this: How to implement this kind of C enums?

So far, my solution is this: translate each symbol of the like 'UNKNOWN'
into a Lua numeric global variable. The global is not mandatory, for I could
pushed it into the module itself. But, in that case, I would have to type:

  module_name.UNKNOWN

each time and I am too lazy for that.

The numeric part comes because I want to add flags and such, like this:

  buffermode = TRIPLE + WINDOWS

Fortunately I don't need to OR, and if needed one could use any bit library.
The cons of this solution is not having any "type checking" or somekind
of "type enforcement" since variables or just variables, I could even mispell
and end up with a nil value. 
The pro of the solution is (at least to me): ease of implementation, ease of usage.

So, what do you think?

Thanks a lot,
Ezequiel.