1
我有以下的C++類:痛飲typemaps與智能指針
class Entity : public Watchable
{
public:
[...]
std::string value() const
{
return "Entity::value()";
}
};
Entity* create_entity_pointer()
{
return new Entity();
}
watch_ptr<Entity> create_entity_watch_pointer()
{
return watch_ptr<Entity>(new Entity());
}
...和下面的呷類型映射聲明:
%typemap(out) std::string Entity::value
{
$result = PyString_FromString("Typemapped value");
}
的watch_ptr class暴露於Python和我宣佈所有的可能包裝的可能類型:
%template(EntityWatchPtr) watch_ptr<Entity>;
這可以按預期的方式調用來自Python的Entity*
上的屬性函數。但是,SWIG在watch_ptr<Entity>
上調用時會忽略該類型映射。我的python腳本是如下:
from module import *
player1 = create_entity_pointer()
print(player1)
print(player1.value())
player2 = create_entity_watch_pointer()
print(player2)
print(player2.value())
這將產生以下的輸出:
<module.Entity; proxy of <Swig Object of type 'Entity *' at 0x100b15ba0> >
Typemapped value
<module.EntityWatchPtr; proxy of <Swig Object of type 'watch_ptr<Entity> *' at 0x100b613c0> >
Entity::value()
我怎樣才能獲得與類型映射智能指針的工作? 我已經把完整的源代碼在網上:https://github.com/kermado/SwigSmartPtrs
你有一些代碼來演示什麼可行,什麼被忽略? –
我提供了一些更多的細節。 – Homar