TestMap.h
#include <iostream>
#include <map>
#include <memory>
class fooType{
public:
fooType() { };
~fooType() { };
void printFoo() { std::cerr << "FOO!" << std::endl; }
static std::shared_ptr<fooType> make_shared() {
return std::shared_ptr<fooType>(new fooType());
}
};
class testMap : public std::map<int, std::shared_ptr<fooType> > {
public:
void printBar() { std::cerr << "bar." << std::endl; }
};
然后我的SWIG接口文件:
TestMap.i
^{pr2}$最后,我用来测试接口的测试脚本:
测试_接口.py
import TestMap as tm
ft = tm.fooType.make_shared()
myTestMap = tm.testMap()
myTestMap[1] = ft
如前所述,我在尝试使用映射访问器时遇到以下错误:
Traceback (most recent call last):
File "test_interface.py", line 9, in <module>
myTestMap[1] = ft
File "/home/sskutnik/tstSWIG/TestMap.py", line 217, in __setitem__
return _TestMap.fooMap___setitem__(self, *args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'fooMap___setitem__'.
Possible C/C++ prototypes are:
std::map< int,std::shared_ptr< fooType > >::__setitem__(std::map< int,std::shared_ptr< fooType > >::key_type const &)
std::map< int,std::shared_ptr< fooType > >::__setitem__(std::map< int,std::shared_ptr< fooType > >::key_type const &,std::map< int,std::shared_ptr< fooType > >::mapped_type const &
当我检查ft
和{
<TestMap.fooType; proxy of <Swig Object of type 'std::shared_ptr< fooType > *' at 0x7fa812e80a80> >
<TestMap.testMap; proxy of <Swig Object of type 'std::shared_ptr< testMap > *' at 0x7fa812e80c90> >
现在对于奇怪的部分-如果我从SWIG接口文件中省略%shared_ptr(TestMap)
声明并重新编译,则映射访问器(在测试中_接口.py)工作愉快。当我检查myTestMap
的类型时,它是:
<TestMap.testMap; proxy of <Swig Object of type 'testMap *' at 0x7f8eceb50630> >
所以,有两个问题:
testMap*
)时,我的访问器调用能正常工作,而当我有一个shared_ptr
引用(例如,std::shared_ptr< testMap > *
)时,访问器调用就不能正常工作了?在shared_ptr
作为派生映射类型,我该如何解决这个问题?在额外的问题:如果我声明testMap
类型的shared_ptr
的存在,SWIG为什么会自动将testMap*
转换为std::shared_ptr<testMap>
类型(即使它没有被初始化成这样?)在
第一次
myTestMap = tm.testMap()
创建一个透明的共享\ptr。因此myTestMap[1]
是共享的ptr的透明取消引用,随后将值分配给键。第二次
myTestMap = tm.testMap()
创建空的std::map,因此myTestMap[1]
是给映射的key=1
赋值的。在%shared_ptr(testMap)
在语义上与%template(testMap) shared_ptr<testMap>
相似。%template(testMapPtr) shared_ptr<testMap>
将创建一个新的共享的ptr类型testMapPtr
,该类型最初为NULL(参见{a1}),因此{更新:
%shared_ptr(testMap)
创建一个完全透明的共享ptr,该ptr使用testMap默认构造函数初始化。在相关问题 更多 >
编程相关推荐