簡體   English   中英

將 boost::shared_ptrs 的向量的 std::vector 從 C++ 傳遞到 Python 時出現問題

[英]Problems passing a std::vector of vector of boost::shared_ptrs from C++ to Python

我在傳回 <std::vector <std::vector <boost::shared_ptr Detection>>> 時遇到問題,我希望有人能幫助我。 I am able to pass the vector of vector but when I try and access the Detection , I get a TypeError that there is no python class registered for C++ class boost::shared_ptr which is in the Module definition.

我的 C++ 代碼如下:

class Detection {
public: 
    Detection () {};
    ~Detection() {};
    
    double getR () {return r_;};
    double getA () {return a_;};

    double r_;
    double a_;
}

class Detector {
public
    std::vector <std::vector <boost::shared_ptr <Detection> > > getDetections();
}

std::vector <std::vector <boost::shared_ptr <Detection> > > 
Detector::getDetections () {

    std::vector <std::vector <boost::shared_ptr <Detection> > > allVecDetections;

    // Build 4 vectors containing 2 detections each
    for (unsigned int i=0; i < 4; i++) {
        std::vector<boost::shared_ptr<Detection> > vecDetections;
        for (unsigned int j = 0; j < 2; j++ ) {
            boost::shared_ptr<Detection> dt (new Detection (j+i, ts));
            dt->r_ = (j+i) + (j*i);
            dt->a_ = (j+i) * (j*i);
            vecDetections.push_back (dt);
        }
        allVecDetections.push_back (vecDetections);
    }
    return allVecDetections;
}
// Module.cpp
BOOST_PYTHON_MODULE (myModule) {
  
   boost::python::class_<Detector>("Detector")
      .def("getDetections",  &getDetections)
    ;

    boost::python::class_<Detection>("Detection")
      .def("getA",  &getA)
      .def("getR",   &getR)
     ;
  
    // Map list of Detections 
    boost::python::class_<std::vector <boost::shared_ptr <Detection> > > ("DetectionVector")
        .def(vector_indexing_suite<std::vector <boost::shared_ptr <Detection> > > ());

    // Map list of lists of Detections 
    boost::python::class_<std::vector <std::vector <boost::shared_ptr <Detection> > > > ("DetectionVectorVector")
        .def(vector_indexing_suite<std::vector<std::vector <boost::shared_ptr <Detection> > > > ());

    // Register our shared pointers with Python
    register_ptr_to_python<boost::shared_ptr<Detection> >(); 
  
}

我可以從 Python 調用 getDetections 並返回 4 個向量,每個向量包含 2 個檢測。 這是 Python 代碼:

import myModule
...
plots = det.getDetections()
numvectors = len(plots)
print (f'Received {numvectors} vectors ')
jdx = 0
pdx = 0
while jdx < numvectors:
    detections = plots[jdx]
    pdx = 0
    numPlots = len(detections)
    print (f'Received {numPlots} extractions')
    while pdx < numPlots:
        print ("Extraction " + str (pdx) + ":")
        detect = detections[jdx]

這運行但我得到以下 output:

Received 4 vectors  
Received 2 extractions  
Extraction 0:  
Traceback (most recent call last):  
  File "scripts/test.py", line 87, in <module>
    foo = detections[jdx].getA()  
TypeError: No Python class registered for C++ class boost::shared_ptr<Detection>  

那么,為什么我會收到有關 Python 沒有為 boost::shared_ptr 注冊 class 的投訴?

感謝你給與我的幫助。 (上面的代碼已被修剪,因此在編寫問題的過程中可能引入了拼寫錯誤)。

接受 Valeca 的建議,我重新完成了傳回 vector<vector> 的工作(消除了 boost::shared_ptr)。 現在該模塊有些不滿意,因為在 Pyhton 腳本中導入模塊時出現了段錯誤。

以下是新的變化:

class Detection {
public: 
    Detection () {};
    ~Detection() {};

    Detection (const Detection &D) {
      : r_ (D.r_),
        a_ (D.a_)
    {};

    void operator = (const Detection &D) {
        r_ = D.r_;
        a_ = D.a_;
    };

    double getR () {return r_;};
    double getA () {return a_;};

    double r_;
    double a_;

    friend bool operator== (const Detection &d1, const Detection &d2);
    friend bool operator!= (const Detection &d1, const Detection &d2);
};

bool operator== (const Detection &d1, const Detection &d2) {
    return (d1.r_ == d2.r_ && d1.a_ == d2.a_);
}

bool operator!= (const Detection &d1, const Detection &d2) {
    return !(d1.r_ == d2.r_ && d1.a_ == d2.a_ );
}

class Detector {
public
    std::vector <std::vector <Detection> > getDetections();
}

std::vector <std::vector <Detection> > 
Detector::getDetections () {

    std::vector <std::vector <Detection> > allVecDetections;

    // Build 4 vectors containing 2 detections each
    for (unsigned int i=0; i < 4; i++) {
        std::vector<Detection> vecDetections;
        for (unsigned int j = 0; j < 2; j++ ) {
            Detection dt (j+i, ts);
            dt.r_ = (j+i) + (j*i);
            dt.a_ = (j+i) * (j*i);
            vecDetections.push_back (dt);
        }
        allVecDetections.push_back (vecDetections);
    }
    return allVecDetections;
}

// Module.cpp
BOOST_PYTHON_MODULE (myModule) {

   boost::python::class_<Detector>("Detector")
      .def("getDetections",  &getDetections)
    ;

    boost::python::class_<Detection>("Detection")
      .def("getA",  &getA)
      .def("getR",   &getR)
     ;

    // Map list of Detections 
    boost::python::class_<std::vector <Detection> > ("DetectionVector")
        .def(vector_indexing_suite<std::vector <Detection> > ());

    // Map list of lists of Detections 
    boost::python::class_<std::vector <std::vector<Detection> > > ("DetectionVectorVector")
        .def(vector_indexing_suite<std::vector<std::vector <Detection> > > ());

    // Register our shared pointers with Python
    // register_ptr_to_python<boost::shared_ptr<Detection> >(); 

}

現在啟動時 Python 邊段故障。 我可能添加了比需要更多的重載運算符,但沒有它們,我會看到:

* /usr/include/c++/7/bits/predefined_ops.h:241:17: 錯誤:'operator==' 不匹配(操作數類型為'Detection' 和'const Detection'){ return __it == _M_value; }

編譯我的模塊時。

感謝您對此有所了解。

我讓它與vector< vector <boost::shared_ptr > >一起工作,結果證明我在注冊我的向量時錯過了 NoProxy 覆蓋。 請參閱 Module.cpp 中的固定代碼。

    boost::python::class_<std::vector <boost::shared_ptr <Detection> > > ("DetectionVector")
        .def(vector_indexing_suite<std::vector <boost::shared_ptr <Detection> >,true > ());

    // Map list of lists of Detections 
    boost::python::class_<std::vector <std::vector <boost::shared_ptr <Detection> > > > ("DetectionVectorVector")
        .def(vector_indexing_suite<std::vector<std::vector <boost::shared_ptr <Detection> > >, true > ());


我記得看到有關此問題的 Stack Overflow 消息,其中作者說真實情況非常重要。 確實如此。 感謝所有幫助提出建議的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM