Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use macro in lua

Tags:

c

lua

lua-api

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.

like image 594
Marcos Silva Avatar asked Aug 25 '26 05:08

Marcos Silva


1 Answers

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);
like image 67
user694733 Avatar answered Aug 26 '26 22:08

user694733



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!