lua-users home
lua-l archive

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


I like to use tables for bitfield arguments, like so:

  set_buffer_mode { 'TRIPLE', 'WINDOWS' }
  -- or --
  set_buffer_mode { TRIPLE = true, WINDOWS = true }

My C function for set_buffer_mode then uses a helper function to decode
the table into a integer.

-Rob

On Thu, 15 Dec 2011 06:34:27 -0800 (PST)
Ezequiel García <elezegarcia@yahoo.com.ar> wrote:

> 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.
> 
> 

Attachment: signature.asc
Description: PGP signature