简体   繁体   English

将 SWIG 用于 C++ 结构中的 std::vector 成员

[英]Using SWIG for std::vector members in C++ structs

I am trying to use SWIG to wrap a C++ struct containing vector members, and provide an interface with Python.我正在尝试使用 SWIG 包装包含向量成员的 C++ 结构,并提供与 Python 的接口。 I have a small test case using the following three files (Ubuntu-16.04):我有一个使用以下三个文件(Ubuntu-16.04)的小测试用例:

A Makefile that looks like this:一个看起来像这样的 Makefile:

FLAGS = -fPIC

PYTHONI = -I /usr/include/python3.5/
PYTHONL = -Xlinker -export-dynamic

all:
    swig -Wall -c++ -python -o test.cxx test.i
    g++ $(FLAGS) $(PYTHONI) -c test.cxx -o test_wrap.o
    g++ $(PYTHONL) $(LIBFLAGS) -shared -o _test.so test_wrap.o -lstdc++

A test C++ header defining a struct here (test.h):一个测试 C++ header 在这里定义一个结构(test.h):

#ifndef TEST_H
#define    TEST_H

#include <vector>

using std :: vector;

   struct myStruct {
      
      float var1; // m
      vector<float> arr1; // m
      vector<float> arr2; // C
      int var2;
   };

#endif

And a SWIG interface file here (test.i):这里有一个 SWIG 接口文件(test.i):

%module test
%include "std_vector.i"

typedef std::vector<float> FloatVector;

namespace std {
   %template(FloatVector) vector<float>;
};

%naturalvar myStruct::arr1;

%{
#include <vector>
#include "test.h"
%}

%include "test.h"

Running make all creates a test.py file, which can be imported and creates the myStruct class:运行 make all 创建一个 test.py 文件,可以导入并创建 myStruct class:

import test
foo = test.myStruct()

I can set the var1 and var2 members with calls like so:我可以使用如下调用设置 var1 和 var2 成员:

foo.var1 = 1.23
foo.var2 = 4

However, I cannot make edits to the arr1 or arr2 members.但是,我无法对 arr1 或 arr2 成员进行编辑。 These are both of type 'SwigPyObject', even though I have tried to use the %naturalvar on arr1.这些都是“SwigPyObject”类型,即使我尝试在 arr1 上使用%naturalvar What I want to do is create a structure, populate it with data from my Python session, and then use that structure in other calls to C++ functions.我想做的是创建一个结构,用我的 Python session 中的数据填充它,然后在对 C++ 函数的其他调用中使用该结构。

Using using in a header file is a bad idea.在 header 文件中使用using是个坏主意。 SwigPyObject type indicates type typemaps weren't matched properly. SwigPyObject类型表示类型类型映射没有正确匹配。 For example, this works:例如,这有效:

test.i :测试.i

%module test

%include <std_vector.i>
%template(FloatVector) std::vector<float>;
%naturalvar myStruct::arr1;
%naturalvar myStruct::arr2;

%inline %{
#include <vector>
struct myStruct {
    float var1;
    std::vector<float> arr1;
    std::vector<float> arr2;
    int var2;
};
%}

Demo:演示:

>>> import test
>>> foo = test.myStruct()
>>> foo.arr1
()
>>> foo.var1=1.23
>>> foo.var2=5
>>> foo.arr1=1,2,3
>>> foo.arr2=1.1,2.2
>>> foo
<test.myStruct; proxy of <Swig Object of type 'myStruct *' at 0x000001F94E7487E0> >
>>> foo.var1
1.2300000190734863
>>> foo.var2
5
>>> foo.arr1
(1.0, 2.0, 3.0)
>>> foo.arr2
(1.100000023841858, 2.200000047683716)

Without %naturalvar the values can still be assigned, but need:没有%naturalvar仍然可以分配值,但需要:

>>> foo.arr1 = test.FloatVector([1,2,3])

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

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