为返回对向量的函数创建SWIG类型映射

问题描述 投票:2回答:1

我正在尝试创建一个getter函数,它返回Lua中的对向量。

我在C ++中有以下矢量数据:

{{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>

/* convert the output std::vector<pair<float, std::string>> to 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);
}

The Result I Want :

Value : 2
Name : b

The Result I Get :

向量:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x10)

我该如何更改代码以获得我想要的结果?

c++ lua swig typemaps
1个回答
1
投票

基本上解决方案是不使用输出参数(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
© www.soinside.com 2019 - 2024. All rights reserved.