Enumerated types

classic Classic list List threaded Threaded
2 messages Options
rdg
Reply | Threaded
Open this post in threaded view
|

Enumerated types

rdg
Hi.

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 ([hidden email])


Reply | Threaded
Open this post in threaded view
|

Re: Enumerated types

Waldemar Celes-2
> 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 ([hidden email])
> 

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