обертывание класса, который является std :: vector сложного класса, с помощью cython - PullRequest
0 голосов
/ 11 июля 2020

Я работаю в python с cython, чтобы обернуть код C ++ . Я обертываю C ++ класс cplfunction, который содержит карту и двойник и не может быть обернут, как здесь , как описано здесь и в конце раздела здесь . Короче говоря, мне нужно иметь cdef cplfunction*thisptr и не может cdef cplfunction Mycplfunction (см. Минимальный пример ниже)

До этого момента у меня все нормально. Моя проблема возникает, когда я пытаюсь обернуть свой второй класс cplfunctionvec, который является вектором первого. См. Мой минимальный пример ниже с обычной последовательностью .hpp, .pxd, .pyx.

Моя проблема в том, что мне не удается обернуть метод Maxf

#ifndef CPLFUNCTION_H
#define CPLFUNCTION_H
namespace cplfunction {
    class cplfunction {
        public:
            std::map<double,double> MyMap_ ;
            double x0;
            cplfunction();
            cplfunction(cplfunction const & x);
            cplfunction(std::map<double,double> MyMap_, double x0);
            ~cplfunction();
    };
}
#endif


namespace cplfunctionvec {
    class cplfunctionvec {
        public:
            std::vector<cplfunction> Myvec_ ;
            cplfunctionvec();
            cplfunctionvec(cplfunctionvec const & x);
            cplfunctionvec(std::vector<double> S1, std::vector<double> B1, std::vector<double> f0);
            ~cplfunctionvec();
            cplfunctionvec Maxf(cplfunctionvec const & func1);
    };
}
#endif




cdef extern from "cplfunction.hpp" :
    cdef cppclass cplfunction:
        cplfunction() except +
        cplfunction(cplfunction &)
        cplfunction(map[double,double],double) except +

cdef extern from "cplfunction.cpp" :
    cdef cppclass cplfunctionvec:
        cplfunctionvec() except +
        cplfunctionvec(cplfunctionvec &) except +
        cplfunctionvec(vector[double],vector[double],vector[double])  except +.   
        cplfunctionvec Maxf(cplfunctionvec &)

cdef class Pycplfunction:
    cdef cplfunction *thisptr ## * is required here, see https://stackoverflow.com/questions/33573038/how-to-expose-a-function-returning-a-c-object-to-python-using-cython
    # typiquement le genre de bazar que l'on n'aurait pas si l'on pouvait utiliser boost-python ;)
    def __cinit__(self,map[double,double] MyMap, double x0):
        self.thisptr = new cplfunction(MyMap,x0)

cdef copy(self, cplfunction &other):
        self.thisptr = new cplfunction(other)

cdef class Pycplfunctionvec:
    cdef cplfunctionvec *thisptr

    def __cinit__(self, vector[double] S1, vector[double] B1, vector[double] f0):
        self.thisptr = new cplfunctionvec(S1,B1,f0)

    # def Maxf(self, Pycplfunctionvec x):
    #     self.thisptr.Maxf(x.thisptr)
...