It is easy to register a C + + enumeration in LUA code, just like registering a global variable. We use the enumeration name as a namespace to avoid conflicting enumeration registrations. The registered enumeration is stored in the Global environment (the thread environment).
When an enumeration is accessed in Lua code, the corresponding value is accessed by name.
Sample_9.cpp C + + code is as follows:
Enum registered in LUA code, in order to avoid conflicts, to store enum Week{monday,tuesday,wednesday,thursday,friday,saturday,sunday as a name as Enumtable, };//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;}
The Sample_9.lua LUA code codes are as follows:
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:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Lua vs. C + + Interaction series: registering enum enum into LUA code