创建一个 SWIG typemap 以返回一个 vector of pairs 的函数

我正在尝试创建一个 getter 函数,该函数在 Lua 中返回一组双持续值的向量。

我有以下 C++ 中的 vector 数据:

{{1, "a"}, {2, "b"}, {3, "c"}}

我想将该向量作为表格在 Lua 中返回,这样可以与以下 Lua 表格 t 相同:

local t = {};
t[1].value = 1
t[1].name = "a"
t[2].value = 2
t[2].name = "b"
t[3].value = 3
t[3].name = "c"

以下是我的代码:

" MyBindings.h"

#include "main.h"

class MyClass
{
public:
    MyClass()
    :MyData({{1, "a"}, {2, "b"}, {3, "c"}}){}

    void getMyData(std::vector<pair<float, std::string>> *datap)
    {
        *datap = MyData;
    }
    std::vector<pair<float, std::string>> MyData;
};

" MyBindings.i"

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

%include <stl.i>
%include <typemaps.i>
%include <std_string.i>
%include <std_vector.i>

/* 将输出的 std::vector<pair<float, std::string>> 转换为 lua_Table */
%typemap(in, numinputs = 0) (std::vector<pair<float, std::string>> *datap)
(std::vector<pair<float, std::string>> *tdatap = nullptr)
%{
%}
%typemap(argout) (std::vector<pair<float, std::string>> *datap)
{
    lua_newtable(L);
    for (size_t i = 0; i < $1->size(); ++i)
    {
        lua_newtable(L);
        lua_pushinteger(L, static_cast<lua_Number>($1->at(i).first));
        lua_setfield(L, -2, "value");
        lua_pushstring(L, $1->at(i).second.c_str());
        lua_setfield(L, -2, "name");
        lua_rawseti(L, -2, i + 1);
    }
    SWIG_arg++;
}

%include "MyBindings.h"

" main.cpp"

#include "main.h"
#include "lua.hpp"

extern "C"
{
    int luaopen_my(lua_State *L);
}

int main()
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    luaopen_my(L);
    lua_settop(L, 0);
    luaL_dostring(L, "local c = my.MyClass()\n"
                     "local t = c:getMyData()\n"
                     "print('Value : ' .. t[2].value)\n"
                     "print('Name : ' .. t[2].name)\n");
    lua_close(L);
}

我想要的结果:

Value : 2
Name : b

我得到的结果:

vector: Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)

我应该如何更改我的代码以获得所需的结果?

原文链接 https://stackoverflow.com/questions/52110159

点赞
stackoverflow用户1944004
stackoverflow用户1944004

基本的解决方案是不使用输出参数(argout)。实际上,这是 C++ 编程的通用建议。在 C 时代,无法轻松地通过值从函数返回数组,因此需要输出参数。

稍微重构一下之后,我得到了下面的代码:

MyBindings.h

#include <string>
#include <utility>
#include <vector>

class MyClass {
    std::vector<std::pair<float, std::string>> m_data;
public:
    MyClass() : m_data({{1, "a"}, {2, "b"}, {3, "c"}}) {}

    std::vector<std::pair<float, std::string>> data() { return m_data; }
};

MyBindings.i

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

%typemap(out) std::vector<std::pair<float, std::string>>
{
    lua_newtable(L);
    for (size_t i = 0; i < $1.size(); ++i)
    {
        lua_newtable(L);
        lua_pushinteger(L, static_cast<lua_Number>($1.at(i).first));
        lua_setfield(L, -2, "value");
        lua_pushstring(L, $1.at(i).second.c_str());
        lua_setfield(L, -2, "name");
        lua_rawseti(L, -2, i + 1);
    }
    SWIG_arg++;
}

%include "MyBindings.h"

main.cpp

#include "lua.hpp"

extern "C"
{
    int luaopen_my(lua_State *L);
}

int main()
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    luaopen_my(L);
    lua_settop(L, 0);
    luaL_dostring(L, "local c = my.MyClass()\n"
                     "local t = c:data()\n"
                     "print('Value : ' .. t[2].value)\n"
                     "print('Name : ' .. t[2].name)\n");
    lua_close(L);
}

以下是一个示例调用:

$ swig -c++ -lua MyBindings.i
$ clang++ -Wall -Wextra -Wpedantic -std=c++11 -I/usr/include/lua5.3 MyBindings_wrap.cxx main.cpp -llua5.3
$ ./a.out
Value : 2
Name : b
2018-08-31 10:45:47