如何在SWIG界面文件中检查Lua版本? [英] How to check Lua version in SWIG interface file?
问题描述
我具有以下功能,仅在Lua版本等于或小于5.1
时才想使用.
I have the below function which I would like to use only when the Lua version is equal to, or smaller than 5.1
.
所以我在myBindings.i
文件中将其写为以下内容:
So I wrote it like the following in myBindings.i
file:
/* used to backport lua_len to Lua 5.1 */
#if LUA_VERSION_NUM <= 501
%{
static void lua_len(lua_State *L, int i)
{
switch (lua_type(L, i))
{
case LUA_TSTRING:
lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
break;
case LUA_TTABLE:
if (!luaL_callmeta(L, i, "__len"))
lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
break;
case LUA_TUSERDATA:
if (luaL_callmeta(L, i, "__len"))
break;
default:
luaL_error(L, "attempt to get length of a %s value",
lua_typename(L, lua_type(L, i)));
}
}
%}
#endif
但是,当我编译代码时,编译器不会跳过Lua 5.3
中的lua_len
函数.
However when I compile the code, the compiler doesn't skip the lua_len
function in Lua 5.3
.
如何根据版本信息向编译器公开lua_len
函数?
how can I expose lua_len
function to the compiler depending on the version info?
推荐答案
TL; DR::您必须将预处理程序宏移动到文字块%{ ... %}
中.
TL;DR: You have to move the preprocessor macros inside the literal block %{ ... %}
.
在这种情况下
#if CONDITION
%{
...
%}
#endif
CONDITION
由SWIG评估. SWIG不了解宏LUA_VERSION_NUM
,因为它与先验接口无关(即,您还可以生成LUA_VERSION_NUM
没有任何意义的Python接口).
the CONDITION
is evaluated by SWIG. SWIG does not know about the macro LUA_VERSION_NUM
because it is a priori interface agnostic (i.e. you could also generate a Python interface where LUA_VERSION_NUM
has no meaning).
在变体中
%{
#if CONDITION
...
#endif
%}
SWIG将把文字块内的所有内容转发到接口文件.字面上会发生这种情况,而无需SWIG进行任何进一步检查,因此预处理器宏将保持不变. C ++编译器将包含<lua.hpp>
并在其中找到LUA_VERSION_NUM
的定义,因此该宏将具有预期的作用.
SWIG will forward everything inside the literal block to the interface file. This will happen literally without any further inspection by SWIG, so preprocessor macros will be untouched. The C++ compiler will include <lua.hpp>
and finds a definition of LUA_VERSION_NUM
there, so the macro will have its intended effect.
这篇关于如何在SWIG界面文件中检查Lua版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!