returns 各种类型的函数的 SWIG 类型映射

SWIG typemap for a function that returns various types

已编辑:

我正在尝试根据 Lua 中使用的变量名创建一个简单的 getter 函数。

例如Lua.

中可以这样使用
num = 123
str = "hello"
print(my.getValue("num")); --> result: 123
print(my.getValue("str")); --> result: hello

这是myBindings.h文件

static void getValue(const char *name, lua_State *L)
{
    lua_getglobal(L, name);
    switch (lua_type(L, -1))
    {
        case LUA_TBOOLEAN:
            //return boolean
            break;
        case LUA_TNUMBER:
            //return number
            break;
        case LUA_TSTRING:
            //return string
            break;
        case LUA_TTABLE:
            //return table
            break;
        default:
            //return nil
            break;
    }
    lua_pop(L, 1);
}

这是 myBindings.i 文件。

%module my
%{
    #include "myBindings.h"
%}

%include <stl.i>
%include <std_except.i>
%include <exception.i>
%include <typemaps.i>

%typemap(default) (lua_State *L) 
{
     = L;
}

%include "myBindings.h"

如何创建 SWIG 类型映射,以便 getValue 函数 returns Lua 中的各种类型?

getValue 函数有一处错误。当你在 switch 语句之后弹出时,你将弹出你在 switch 语句中压入的任何内容。我猜想的目的是将您查询其类型的全局变量从堆栈中弹出。因此,我只是将类型保存在局部变量中,然后立即弹出全局变量。为了这个例子,我推了一些虚拟值。

static void getValue(const char *name, lua_State *L)
{
    lua_getglobal(L, name);
    switch (lua_type(L, -1))
    {
        case LUA_TBOOLEAN:
            lua_pushboolean(L, true);
            break;
        case LUA_TNUMBER:
            lua_pushnumber(L, 3.14);
            break;
        case LUA_TSTRING:
            lua_pushstring(L, "Hello world!");
            break;
        case LUA_TTABLE:
            lua_newtable(L);
            lua_pushstring(L, "value");
            lua_setfield(L, -2, "key");
            break;
        default:
            lua_pushnil(L);
            break;
    }
    // Before we can return we have to clean up the stack.  There is
    // currently the global "name" and the return value on the stack in this order
    //
    // 1. Return value
    // 2. "name"
    //
    // We cannot just lua_pop(L, 1) because that would remove the
    // return value which we of course want to keep.  To pop an
    // element at a specific position we have to use lua_remove.
    lua_remove(L, -2);
}

接口文件看起来不错,但您必须通知 SWIG 您已在 C++ 函数中推送到堆栈并且想要 return 您推送的内容。因此,您必须在 argout 类型映射中增加 SWIG_arg

%module my
%{
#include "myBindings.h"
%}

%typemap(default) (lua_State *L) {
     = L;
}

%typemap(argout) (const char *name, lua_State *L) {
    ++SWIG_arg;
}

%include "myBindings.h"

现在我们可以查看测试脚本了。在第一种情况下,我们使用 "num" 调用 getValue,其中 num 具有类型编号。因此我们期望获得 3.14 因为这是 C++ 函数推送的内容。在第二种情况下,我们用 "str" 调用,其中 str 是字符串类型。因此我们应该得到 Hello world!.

local my = require("my")

num = 123
str = "hello"
print(my.getValue("num"))
print(my.getValue("str"))

我们来试试吧!

$ swig -lua -c++ test.i
$ clang++ -Wall -Wextra -Wpedantic -I /usr/include/lua5.2/ -fPIC -shared test_wrap.cxx -o my.so -llua5.2
$ lua5.2 test.lua
3.14
Hello world!

编辑问题前的旧答案

in 类型映射错误。 numinputs = 0 告诉 SWIG 具有此签名的函数需要零输入。这不是真的,因为 name 作为参数传递。但是,增加 numinputs = 1 会强制您自己检查并转换第一个参数。最好让 SWIG 处理并从类型映射中完全删除此参数。

%typemap(in, numinputs = 0) (void **p) {
     = nullptr;
}

argout 类型映射没有任何意义,甚至不是有效的 C++。即使您纠正了无效的转换,它仍然是一个巨大的类型不安全内存泄漏。