Lua vs. C + + Interaction series: registering enum enum into LUA code

Source: Internet
Author: User
Tags lua


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.