Cython和c ++类构造函数

vla*_*gre 5 c++ python cython word-wrap

有人建议使用Cython操作c ++对象的方法,当一个类的c ++实例需要提供另一个包装类的构造函数时,如下所述?

请查看PySession类的pyx文件中的注释,该类将python PyConfigParams对象作为参数,然后需要从中提取值以构造c ++ ConfigParams对象.然后使用ConfigParams对象来提供Session的构造函数.

理想的做法是允许我将PyConfigParams对象包装的ConfigParams c ++对象直接"注入"Session的构造函数,而不必先拆除它然后构建一个新的c ++对象来提供构造函数.当然,这是有效的.然而,实施这种解决方案是一种繁琐,残酷的方式,更不用说不可靠了.

我知道PyCapsule,但它可能需要触摸c ++标头,这是我不能做的事情.

与此相关,但另一个问题是:如果我需要一个包装类(让我们说这里是PySession)来通过返回一个ConfigParams实例来模拟C++ api的行为怎么办?我是否需要反过来并拆除c ++对象来构建Python PyConfigParams,然后将其返回给Python世界中的Python用户?任何建议都非常欢迎!谢谢!

假设我有两个名为ConfigParams和Session的c ++类.ConfigParams的一个实例用于提供Session类的构造函数:

C++类

ConfigParams类

// ConfigParams.h
#include <iostream> 
using namespace std; 
class ConfigParams 
{ 
  int parameter1; 
 public: 
  ConfigParams(int par1) { this->parameter1 = par1;} 
  int getPar1() { return this->parameter1; } 
};
Run Code Online (Sandbox Code Playgroud)

会话类

// Session.h 
#include <iostream> 
using namespace std; 
#include "configparams.h" 
class Session 
{ 
  int sessionX; 
 public: 
  Session(ConfigParams parameters) { this->sessionX = parameters.getPar1(); } 
  void doSomething(); 
}; 

void Session::doSomething() 
{ 
  cout << "Session parameters set as: " << endl; 
  cout << "X = " << this->sessionX << endl; 
} 
Run Code Online (Sandbox Code Playgroud)

上述类的Cython pyx和pxd文件:

PyConfigParams

# configparams.pxd 
cdef extern from "configparams.h": 
    cppclass ConfigParams: 
        ConfigParams(int par1) 
        int getPar1() 

# configparams.pyx 
cdef class PyConfigParams: 
    cdef ConfigParams* thisptr 
    def __cinit__(self, i): 
        self.thisptr = new ConfigParams(<int> i) 
    def getPar1(self): 
        return self.thisptr.getPar1() 
Run Code Online (Sandbox Code Playgroud)

PySession类

# session.pxd 
from configparams cimport * 
cdef extern from "session.h": 
    cdef cppclass Session: 
        Session(ConfigParams parameters) 
        void doSomething() 

# session.pyx
cdef class PySession: 
    cdef Session* thisptr 
    def __cinit__(self, pars): 
        # Note that here I have to extract the values 
        # from the pars (python PyConfigParams object) 
        # in order to build a c++ ConfigParams object 
        # which feeds the c ++ constructor of Session. 
        cdef ConfigParams* cpppargsptr = new ConfigParams(<int> pars.getPar1()) 
        self.thisptr = new Session(cpppargsptr[0]) 
    def doSomething(self): 
        self.thisptr.doSomething() 
Run Code Online (Sandbox Code Playgroud)

vla*_*gre 5

解:

在configparams.pxd模块中转发声明PyConfigParams(因此可以从session.pyx模块调用它)

# configparams.pxd                                                                                                                                                                                                                                            
cdef extern from "configparams.h":
    cppclass ConfigParams:
        ConfigParams(int par1)
        int getPar1()

cdef class PyConfigParams:
    cdef ConfigParams* thisptr
Run Code Online (Sandbox Code Playgroud)

在session.pyx模块中导入PyConfigParams,并为constuctor转换参数,这将授予对c ++对象的PyConfigParams指针的访问权限,该指针需要被解除引用.

# session.pyx                                                                                                                                                                                                                                                 
from configparams cimport PyConfigParams
from cython.operator cimport dereference as deref

cdef class PySession:
    cdef Session* thisptr
    def __cinit__(self, PyConfigParams pars):
        self.thisptr = new Session(deref(pars.thisptr))
    def doSomething(self):
        self.thisptr.doSomething()
Run Code Online (Sandbox Code Playgroud)