lua-users home
lua-l archive

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


> I have been looking at the Lua 3.0 manual, and I was wondering how (if
> at all) Lua handles enumerated types. I am curious about this because if
> I want to use Lua as a configuration language, I may want to use Lua to
> map a configuration file to an enumerated type in C or C++.
> 
> Curiously,  Jay Godse (rdg@cyberus.ca)
> 

Lua supports "number" type.
You can export the enumerated constants to Lua and
get them back using the type "number".

Example:

C declaration:

enum
{
 FIRST,
 SECOND
} MyEnum;

C code to export constants to Lua:

lua_pushnumber(FIRST);
lua_storeglobal("FIRST");
lua_pushnumber(SECOND);
lua_storeglobal("SECOND");

C code to get values back to C:

lua_Object lo;
MyEnum value;
...
value = (MyEnum)lua_getnumber(lo);

Then, in Lua, we can write:

...
var = FIRST
...

In fact, we are converting 'enum' to 'float', and then 'float' to 'enum',
but it should work quite well.

I hope this helps.

-- Waldemar