我尝试为C++库创建一个Spy-Python接口,为一些函数添加Python包装,我将非常感激来自Sigg的一些人的帮助。你知道吗
目前我有这样的消息来源:
namespace Test {
class CodeResponseEvent {
public:
CodeResponseEvent(std::string activation_code);
std::string getActivationCode() const;
private:
const std::string activation_code_;
};
class CodeRequestEvent {
public:
CodeRequestEvent(std::string user_id);
std::shared_ptr<CodeResponseEvent> execute();
private:
const std::string user_id_;
};
}
%module test
%include std_string.i
%include <std_shared_ptr.i>
%{#include "test.h"%}
%include "test.h"
%shared_ptr(Test::CodeResponseEvent);
Python代码如下所示:
codeResponse = test.CodeRequestEvent("user").execute()
结果我得到了价值
<Swig Object of type 'std::shared_ptr< Test::CodeResponseEvent> *'>
所以问题是如何打开这个SwigPyobject来调用getActivationCode()方法?你知道吗
您可以只调用对象上的方法,但请注意,您需要在%之前声明%shared\u ptr,包括标头。下面是一个独立的工作示例。我刚刚%内联了一个文件解决方案的头文件:
演示如下。请注意,如果在使用前声明了共享指针,
r
是一个代理,而不是一个通用Swig对象:相关问题 更多 >
编程相关推荐