简体   繁体   English

如何在lua中使用宏

[英]How to use macro in lua

Was looking now few threads on lua and found this post very interesting: 正在寻找现在很少的线程在lua,发现这篇文章非常有趣:

Alert messages for lua functions lua函数的警报消息

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' 编译error c2059 syntax error 'string'error c2059 syntax error 'string'

I did a test and replaces the function p->##fld by p->wooxy_value and it worked. 我做了一个测试并用p->wooxy_value替换了函数p->##fld并且它有效。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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