当前位置: 代码迷 >> python >> SWIG包装矢量矢量(C ++到python) - 如何将内部矢量识别为代理对象?
  详细解决方案

SWIG包装矢量矢量(C ++到python) - 如何将内部矢量识别为代理对象?

热度:46   发布时间:2023-07-16 10:10:39.0

我面临着与类似的问题 - 但它不仅仅是简单的C ++解析。 我的C ++代码中有以下内容

namespace ns {
    typedef unsigned long long uint64_t;
    typedef std::vector<uint64_t> Vector;
    typedef std::vector<Vector> VectorOfVectors;

    class MyClass {
        /// ...

        /// Returns a reference to the internal vector allocated in C++ land
        const VectorOfVectors &GetVectors() const;
    };
}

并在SWIG包装器中

%module myswig    
// ...
%template(Uint64V) std::vector<ns::uint64_t>;
%template(VUint64V) std::vector<std::vector<ns::uint64_t> >;

所以包装工作正常,包括类,我可以检索类的向量向量确定:

import myswig
m = myswig.MyClass()
v = m.GetVectors()
print v

这给了我:

<myswig.VUint64V; proxy of <Swig Object of type 'std::vector< std::vector< ns::uint64_t,std::allocator< ns::uint64_t > > > *' at 0x994a050> >

但是,如果我访问向量中的元素,我不会得到一个myswig.Uint64V的代理 - 这是我的问题。

x = v[0]
print x

我希望得到的是:

<myswig.Uint64V; proxy of <Swig Object of type 'std::vector< ns::uint64_t, std::allocator< ns::uint64_t > > *' at 0x994a080> >

相反,我得到:

(<Swig Object of type 'ns::uint64_t *' at 0x994a080>, <Swig Object of type 'ns::uint64_t *' at 0x994a098>) 

也就是说,向量向量的索引给我一个2项元组,而不是我需要的向量类的代理(因此访问内部向量就像访问其他向量一样容易)。

我也收到了警告:

swig/python detected a memory leak of type 'ns::uint64_t *', no destructor found.

因为当然没有为这种类型定义的析构函数。

有任何想法吗?

我与我的一位同事一起工作,我们设法提出了一些解决方案。

首先,在SWIG .i文件中,定义此预处理器变量很重要:

%{
#   define SWIG_PYTHON_EXTRA_NATIVE_CONTAINERS 
%}

然后,为了确保从front(),back(),operator []等方法返回的引用实际上映射到内部向量的正确代理类型,以下类型映射有助于:

// In pop()
%typemap(out) std::vector<std::vector<ns::uint64_t> >::value_type { 
$result = SWIG_NewPointerObj(SWIG_as_voidptr(&$1), $descriptor(std::vector<ns::uint64_t>), 0 |  0 ); 
} 

// In front(), back(), __getitem__()
%typemap(out) std::vector<std::vector<ns::uint64_t> >::value_type & { 
    $result = SWIG_NewPointerObj(SWIG_as_voidptr($1), $descriptor(std::vector<ns::uint64_t>), 0 |  0 ); 
} 

我们还发现,如果你想将ns :: uint64_t视为一个python long变量(相当于一个C unsigned long long),那么还需要一些其他的类型映射来确保使用值和引用的vector方法只需使用64位整数值。

// In __getitem__()
%typemap(out) ns::uint64_t {
    $result = PyLong_FromUnsignedLongLong($1);
}
// Not used (but probably useful to have, just in case)
%typemap(in) ns::uint64_t {
    $1 = PyLong_AsUnsignedLongLong($input);
}
// In pop()
%typemap(out) std::vector<ns::uint64_t>::value_type {
    $result = PyLong_FromUnsignedLongLong($1);
}
// In __getitem__(), front(), back()
%typemap(out) std::vector<ns::uint64_t>::value_type & {
    $result = PyLong_FromUnsignedLongLong(*$1);
}
// In __setitem__(), append(), new Uint64Vector, push_back(), assign(), resize(), insert()
// This allows a python long literal number to be used as a parameter to the above methods. 
// Note the use of a local variable declared at the SWIG wrapper function scope,
// by placing the variable declaration in parentheses () prior to the open brace {
%typemap(in) std::vector<ns::uint64_t>::value_type & (std::vector<ns::uint64_t>::value_type temp) {
    temp = PyLong_AsUnsignedLongLong($input);
    $1 = &temp;
}

我希望这个解决方案能在未来帮助人们!