简体   繁体   中英

How to use macro in lua

Was looking now few threads on lua and found this post very interesting:

Alert messages for lua functions

I am trying to use the same macro to my code with some changes to operation:

#define GET_INTEGER_WARN(ind, fld) do { \
lua_getfield(L, ind, #fld); \
p->##fld = lua_tointeger(L, -1); \
\
if (!lua_isinteger(L, -1)) \
    printf(#fld" allows only numbers;"); \
} while (0)

my code:

lua_getfield(L, -1, "wooxy_value"); 
p->wooxy_value = lua_tointeger(L, -1);

lua_getfield(L, -2, "wooxy_type"); 
p->wooxy_type = lua_tointeger(L, -1);

I changed my code as the author explain, thus:

GET_INTEGER_WARN(-1, "wooxy_value");  
GET_INTEGER_WARN(-2, "wooxy_type"); 

More macro error occurs at the following location:

p->##fld = lua_tointeger(L, -1); \

compilation erro: error c2059 syntax error 'string'

I did a test and replaces the function p->##fld by p->wooxy_value and it worked.

More in this way works only for a function, can anybody tell me what is wrong with the macro? The message is also appearing even using integer value.

Using string literal makes no sense:

GET_INTEGER_WARN(-1, "wooxy_value"); 

results in

p->"wooxy_value" = lua_tointeger(L, -1);

Drop the quotes:

GET_INTEGER_WARN(-1, wooxy_value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM