暴露我試圖揭露使用Boost.Python的這個C++類:延伸的虛擬C++類通過Boost.Python的
class VAlgorithm {
public:
VAlgorithm(const char *name);
virtual ~VAlgorithm();
virtual bool Initialize() = 0;
virtual bool Process() = 0;
virtual bool Finalize() = 0;
virtual const char *GetName() const; // Default implementation in cpp file
}
我的最終目標是在Python殼爲Python類來定義VAlgorithm的孩子。繼this example,我定義了一個回調類:
class VAlgorithm_callback: public VAlgorithm {
public:
VAlgorithm_callback(PyObject *p, const char *name) :
self(p), VAlgorithm(name) {
}
const char * GetName() const {
return call_method<const char *>(self, "GetName");
}
static const char * GetName_default(const VAlgorithm& self_) {
return self_.VAlgorithm::GetName();
}
private:
PyObject *self;
};
現在我只露出類本身和getName()方法。由於它是一個虛擬的類,我把這個代碼內BOOST_PYTHON_MODULE:
class_<VAlgorithm, VAlgorithm_callback, boost::noncopyable>("VAlgorithm", no_init) //
.def("GetName", &VAlgorithm_callback::GetName_default); //
我可以編譯此和在Python殼加載模塊。然後我嘗試定義一個子類,並呼籲在C++代碼中定義的的GetName()默認實現:
>>> class ConcAlg1(VAlgorithm):
... pass
...
>>> c1 = ConcAlg1("c1")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: This class cannot be instantiated from Python
>>> class ConcAlg2(VAlgorithm):
... def __init__(self, name):
... pass
...
>>> c2 = ConcAlg2("c2")
>>> c2.GetName()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
VAlgorithm.GetName(ConcAlg2)
did not match C++ signature:
GetName(VAlgorithm)
>>> class ConcAlg3(VAlgorithm):
... def __init__(self, name):
... super(ConcAlg3, self).__init__(self, name)
...
>>> c3 = ConcAlg3("c3")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __init__
RuntimeError: This class cannot be instantiated from Python
我不是(只是面臨這些問題的第一次)的專家,但在我看來, ConcAlg1和ConcAlg3嘗試實例化一個VAlgorithm對象,並且因暴露VAlgorithm時使用no_init參數而失敗(我不能忽略它或者代碼不會編譯),而ConcAlg2不能調用GetName(),因爲它以某種方式不被認爲是VAlgorithm的一個孩子。 我一定在做一些細小的錯誤,但我無法弄清楚什麼(我是Boost.Python和擴展的新手)。誰能幫幫我嗎?謝謝
事實上,我在找到錯誤後寫了我的答案。我認爲我被一個記錄不完整的例子和我對Boost.Python的糟糕知識誤導了。謝謝你的幫助! –