2014-12-25 79 views
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

+0

你有一些代碼來演示什麼可行,什麼被忽略? –

+0

我提供了一些更多的細節。 – Homar

回答

1

所以經過一些實驗,似乎typemaps必須之前指定的SWIG模板聲明。換句話說,我需要聲明:

%template(EntityWatchPtr) watch_ptr<Entity>; 

以前類型映射:

%typemap(out) std::string Entity::value 
{ 
    $result = PyString_FromString("Typemapped value"); 
} 

在我痛飲接口文件。我的程序的輸出是:

<module.Entity; proxy of <Swig Object of type 'Entity *' at 0x10b7b3ba0> > 
Typemapped value 
<module.EntityWatchPtr; proxy of <Swig Object of type 'watch_ptr<Entity> *' at 0x10b7ff3c0> > 
Typemapped value