Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在swig接口中取消boost::shared_ptr引用的好方法_Python_C++_Boost_Swig - Fatal编程技术网

Python 在swig接口中取消boost::shared_ptr引用的好方法

Python 在swig接口中取消boost::shared_ptr引用的好方法,python,c++,boost,swig,Python,C++,Boost,Swig,有没有一种好的、自动化的方法允许我将共享的\u ptr传递到希望在SWIG接口中引用的函数中 我有一个提供以下功能的库: // Module message typedef boost::shared_ptr< Item > ItemPtr; class Message{ public: ItemPtr getItem( int index) ... ... }; 这些是在单独的模块中 显然,我想做一些类似的事情: >>> item=mes

有没有一种好的、自动化的方法允许我将
共享的\u ptr
传递到希望在SWIG接口中引用的函数中

我有一个提供以下功能的库:

// Module message
typedef boost::shared_ptr< Item > ItemPtr;
class Message{
  public:
     ItemPtr getItem( int index) ...
     ...
};
这些是在单独的模块中

显然,我想做一些类似的事情:

>>> item=message.getItem(4)
>>> client.processItem( item )
在python中, 但是类型没有对齐,
是一个
共享的\u ptr
,而函数需要一个引用

到目前为止,我的解决方案是覆盖接口文件中的函数

%extend client {
    void processItem( ItemPtr ptr){ 
        $self->processItem( *ptr);
    }
}

是否有一种方法可以避免重复所有需要引用的接口函数,但我将使用
共享的\u ptr

只需从
getItem(int index)
函数返回一个
Item
实例。Ohterwise您必须重载函数才能接收两种参数类型(
shared_ptr
Item
references)

该%extend解决方案有什么问题?我也会这么做。它简洁易懂。它不容易扩展到10个(或数百个)这样的函数。我明白了——在源代码处重写getter函数,而不是在所有客户端目标函数处重写getter函数。
%extend client {
    void processItem( ItemPtr ptr){ 
        $self->processItem( *ptr);
    }
}