我有一个实现发布 - 订阅模式的EventDispatcher
类。它的界面看起来像这样(简化):
class EventDispatcher
{
public:
void publish(const std::string& event_name, std::unique_ptr<Event> event);
std::unique_ptr<Subscription> subscribe(const std::string& event_name, std::unique_ptr<Callback> callback);
private:
std::unordered_map<std::string, std::vector<std::unique_ptr<Callback>>> m_subscriptions;
}
我想将此类公开给Python。最新的SWIG文档指出:
std :: weak_ptr没有可用的特殊智能指针处理 和std :: unique_ptr。
我非常希望能够继续在c ++方面使用unique_ptr。我有什么选择?
我考虑使用SWIG的%扩展功能扩展课程,但我无法使用此方法访问私有成员(m_subscriptions
)。
我能看到的唯一另一个选择是使用SWIG预处理器来定义额外的方法swig_publish
和swig_subscribe
,但这会使我的接口文件变得混乱。
答案 0 :(得分:15)
尽管在C ++ 11笔记中缺乏支持,但在SWIG中使用generic smart pointer support做很多有用的事情是有用的。
简而言之,如果有一个operator->
,那么SWIG已将指针对象的成员合并到指针中,以允许它们在目标语言中长时间互换使用。
我已经使用下面的示例hader文件test.hh汇总了一个完整的示例,说明这可能对您有用:
#include <memory>
#include <iostream>
struct Foobar {
void baz() { std::cout << "This works\n"; }
int wibble;
};
std::unique_ptr<Foobar> make_example() {
return std::unique_ptr<Foobar>(new Foobar);
}
void dump_example(const std::unique_ptr<Foobar>& in) {
std::cout << in->wibble << "\n";
in->baz();
}
为了在Python内部明智地使用unique_ptr,我必须编写以下SWIG文件,std_unique_ptr.i:
namespace std {
%feature("novaluewrapper") unique_ptr;
template <typename Type>
struct unique_ptr {
typedef Type* pointer;
explicit unique_ptr( pointer Ptr );
unique_ptr (unique_ptr&& Right);
template<class Type2, Class Del2> unique_ptr( unique_ptr<Type2, Del2>&& Right );
unique_ptr( const unique_ptr& Right) = delete;
pointer operator-> () const;
pointer release ();
void reset (pointer __p=pointer());
void swap (unique_ptr &__u);
pointer get () const;
operator bool () const;
~unique_ptr();
};
}
%define wrap_unique_ptr(Name, Type)
%template(Name) std::unique_ptr<Type>;
%newobject std::unique_ptr<Type>::release;
%typemap(out) std::unique_ptr<Type> %{
$result = SWIG_NewPointerObj(new $1_ltype(std::move($1)), $&1_descriptor, SWIG_POINTER_OWN);
%}
%enddef
其中包含足够的std::unique_ptr
定义的子集。 (您可以添加或删除构造函数,具体取决于您在Python中确切的语义,我忽略了自定义删除器)。
它还添加了一个宏wrap_unique_ptr
来设置支持。类型映射只是强制SWIG生成的代码在按值返回时使用移动构造函数而不是复制构造函数。
我们可以通过以下方式使用它:
%module test
%{
#include "test.hh"
%}
%include "std_unique_ptr.i"
wrap_unique_ptr(FooUniquePtr, Foobar);
%include "test.hh"
我建立了这个:
swig3.0 -py3 -c++ -python -Wall test.i
g++ -Wall -Wextra -Wno-missing-field-initializers test_wrap.cxx -std=c++11 -I/usr/include/python3.4/ -lpython3.4m -shared -o _test.so
这允许我们使用以下Python:
from test import *
a = make_example()
print(a)
a.wibble = 1234567
a.baz()
dump_example(a)
a.baz()
print(bool(a))
print(bool(FooUniquePtr(None)))
b=a.release()
print(b)
请注意,尽管是unique_ptr<Foobar>
,我们仍然可以说a.baz()
和a.wibble
。 release()
方法还返回一个可用的'raw'指针,该指针现在归Python所有(否则它不会拥有所有者)。 <{1}}会像你期望的那样在Python中返回一个借来的指针。
根据您计划如何使用指针,这可能是您自己的打印方式的一个良好开端,而且比get()
和%extend
所有您拥有unique_ptrs更清晰。
与release()
相比,这不会修改in类型映射,也不会像shared_ptr支持那样更改构造函数。当你的原始指针在Python中成为unique_ptrs时,你有责任选择。
前一段时间我为using std::weak_ptr
with SWIG写了类似的答案。
答案 1 :(得分:2)
奇怪的是,似乎有%ignore
函数,%extend
类可以定义被忽略函数的替代实现,最后在替代实现中调用最初被忽略的函数。功能。例如:
%ignore EventDispatcher::subscribe(const std::string&, std::unique_ptr<Callback>);
%include "EventDispatcher.hpp"
%extend suborbital::EventDispatcher
{
EventSubscription* EventDispatcher::subscribe(const std::string& event_name, PyObject* callback)
{
std::unique_ptr<Callback> callback_ptr(new Callback(callback));
return $self->subscribe(event_name, std::move(callback_ptr)).release();
}
}
答案 2 :(得分:-2)
还没有对unique_ptr的支持。 http://www.swig.org/Doc3.0/CPlusPlus11.html
您需要使用智能指针,如下所示: http://www.swig.org/Doc3.0/Library.html#Library_std_shared_ptr