It's easy to register a C + + enumeration in LUA code, just like a global variable. We use the enumeration name as a namespace to avoid conflicting enumeration of the brochures. The enumeration of the brochures is stored in the Global environment (the thread environment).
When you access enumerations in LUA code, you access the corresponding values by name.
Sample_9.cpp C + + code such as the following:
Enum in Lua code, in order to avoid conflicts, use names as enumtable to store enum Week{monday,tuesday,wednesday,thursday,friday,saturday,sunday, };//defines a macro used to make it easy to use the Set key value # Lua_enum (L, Val) lua_pushliteral (L, #val); Lua_pushnumber (L, Val); Lua_settable (L,-3) void Register_enum (lua_state* l) { //creates a enumtable that stores all enumerations of an enum. By enumeration name = enumeration value lua_newtable (l);//Set enumtable["Monday"]=mondaylua_enum (l, Monday); Lua_enum (L, Tuesday); Lua_enum (L, Wednesday); Lua_enum (L, Thursday); Lua_enum (L, Friday); Lua_enum (L, Saturday); Lua_enum (L, Sunday);//The enumtable exists in the _G Global Environment (thread environment), with the enumeration name as the key Lua_setglobal (L, "Week");} int main (int argc, char *argv[]) {lua_state* L = Lua_open (); Lual_openlibs (L); Register_enum (L); Lual_dofile (L, "sample_9. Lua "); Lua_close (L); return 0;}
Sample_9.lua LUA code codes such as the following:
Local week=_g["Week"]print (Week) print (week.monday) print (week.tuesday) print (week.wednesday) print (week.thursday) Print (week.friday) print (week.saturday) print (week.sunday)
Output Result:
Lua vs. C + + Interaction series: Register enum enum into LUA code