简体   繁体   English

将C ++抽象类作为参数传递给Cython

[英]Passing C++ abstract class as argument to Cython

If I have an abstract base class and a derived class that inherits from it but I have another class that takes the abstract base class object as an argument, how should I go about wrapping it? 如果我有一个抽象基类和一个继承自它的派生类,但是我还有另一个类将抽象基类对象作为参数,那么我应该如何包装它呢?

class A {
 public:
  A(int x, int y);
  virtual int FooA(int x, int y) = 0;
  virtual void FooB() = 0;

}

class B : public A{
  B(int x, int y, int z, int w);
  int FooA(int x, int y);
  void FooB();
  void FooC(int z, int w);
}

class C {
 public:
  C(A* ptr, int p);

}

How should I go about wrapping this as a whole? 我应该如何将其整体包装? I know that I am not supposed to wrap the abstract class A but I am having difficulties wrapping C since I do not have the python object for A 我知道我不应该包装抽象类A,但是包装C时遇到了困难,因为我没有A的python对象

EDIT: Actually I managed to wrap the class but I am still getting errors which I will illustrate further. 编辑:实际上我设法包装了类,但是我仍然收到错误,我将进一步说明。

source.pyx source.pyx

cdef extern from "A.h"
   cdef cppclass A:
      A(int x, int y)
      int FooA(int x, int y)
      void FooB()  

cdef extern from "B.h"
   cdef cppclass B(A):  
      B(int x, int y, int z, int w)
      int FooA(int x, int y)
      void FooB()
      void FooC(int z, int w)  

cdef extern from "C.h"
   cdef cppclass C:
      C(A* ptr, int p)

cdef class pyA:
   cdef A* baseptr
   def __cinit__(self, int x, int y):
      if type(self) is A:
          self.baseptr = new A(int x, int y)

   def __dealloc__(self):
      if type(self) is A:
          del self.baseptr

   def FooA(self, int x, int y):
      pass

   def FooB(self):
      pass

cdef class pyB(pyA):
   def __cinit__(self, int x, int y, int z, int w):
      if type(self) is pyB:
         self.derivedptr = self.baseptr = new B(int x, int y, int z, int w) 
   def __dealloc__(self):
      del self.derivedptr 

   def FooC(self, int z, int w):
      self.derivedptr.FooC(int z, int w)

cdef class pyC:
   cdef C *thisptr
   def __cinit__(self, pyA ptr, int p):
      self.thisptr(<A *> ptr.thisptr, int p)
   def __dealloc__(self):
      del self.thisptr

This compiled by when I was testing pyC and passing pyB as the first argument, I got: 这是在我测试pyC并将pyB作为第一个参数传递时编译的,我得到了:

TypeError: "Expected pyA, got pyB". 

Shouldn't pyB be able to be passed as the first argument as well since it is a subclass of pyA? 由于pyB是pyA的子类,因此不应该也可以将其作为第一个参数传递吗? Or am I way off? 还是我要离开?

My answer is late. 我的答案来晚了。 But for those who might be interested, here is the working code. 但是对于那些可能感兴趣的人,这里是有效的代码。

Ah

#ifndef A_H_
#define A_H_

class A {
public:
  virtual int FooA(int x, int y) = 0;
  virtual void FooB() = 0;
};

#endif /* A_H_ */

Bh BH

#ifndef B_H_
#define B_H_

#include "A.h"

class B : public A{
public:
  B(int x, int y, int z, int w);
  int FooA(int x, int y);
  void FooB();
  void FooC(int z, int w);
};

#endif /* B_H_ */

Ch

#ifndef C_H_
#define C_H_

#include "A.h"

class C {
 public:
  C(A* ptr, int p);
};

#endif /* C_H_ */

B.cpp B.cpp

#include "B.h"
#include <iostream>

using namespace std;

B::B(int x, int y, int z, int w)
{
    cout << "B::B" << endl;
    cout << x << " " << y << " "
         << z << " " << w << endl;
}

int B::FooA(int x, int y)
{
    cout << "B::FooA" << endl;
    cout << x << " " << y << endl;
    return 0;
}

void B::FooB()
{
    cout << "B::FooB" << endl;
}

void B::FooC(int z, int w)
{
    cout << "B::FooC" << endl;
    cout << z << " " << w << endl;
}

C.cpp C.cpp

#include "C.h"
#include <iostream>

using namespace std;

C::C(A* ptr, int p)
{
    cout << "C::C" << endl;
    cout << ptr->FooA(0,1) << endl;
    ptr->FooB();
}

source.pyx source.pyx

# distutils: language = c++
# distutils: sources = [B.cpp, C.cpp]

cdef extern from "A.h":
   cdef cppclass A:
      A(int x, int y) except +
      int FooA(int x, int y)
      void FooB()  

cdef extern from "B.h":
   cdef cppclass B(A):  
      B(int x, int y, int z, int w) except +
      int FooA(int x, int y)
      void FooB()
      void FooC(int z, int w)  

cdef extern from "C.h":
   cdef cppclass C:
      C(A *ptr, int p) except +

cdef class pyA:
   cdef A *baseptr
   def __cinit__(self):
      pass

   def FooA(self, int x, int y):
      pass

   def FooB(self):
      pass

cdef class pyB(pyA):
   cdef B *derivedptr
   def __cinit__(self, int x, int y, int z, int w):
     self.derivedptr = new B(x, y, z, w)
     self.baseptr = self.derivedptr

   def __dealloc__(self):
      del self.derivedptr 

   def FooC(self, int z, int w):
      self.derivedptr.FooC(z, w)

cdef class pyC:
   cdef C *thisptr
   def __cinit__(self, pyA ptr, int p):
      self.thisptr = new C(ptr.baseptr, p)

   def __dealloc__(self):
      del self.thisptr

setup.py setup.py

from distutils.core import setup
from Cython.Build import cythonize

setup(
    name = 'testapp',
    ext_modules = cythonize('*.pyx'),
)

Finally, compile 最后编译

$python setup.py build_ext --inplace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM