jeudi 30 août 2018

How to best expose a C++ class to Python?

Suppose, I have a C++ class:

class Scenario {
public:
    Scenario() {};
    void setOne(double _one) { one = _one; };
    ...

protected:
    double one;
    int    two;
    float  three;
}

which I'd like to expose to Python scripts so as to automatically gain the direct access to the fields, serialization, and other conveniences.

According to the tutorial, any class/structure so exposed must begin with PyObject_HEAD.

Should I inherit my PyScenario from Scenario:

struct PyScenario : public Scenario {
    PyObject_HEAD
}

or enclose one in the other:

struct PyScenario {
    PyObject_HEAD
    Scenario scenario;
}

I think, the former would be easier to manipulate, but I'm worried, it will not work (or will not be portable), because the "head" part may not end up at the beginning of the structure.

Or is there a completely different "pattern" to follow in such cases?

Aucun commentaire:

Enregistrer un commentaire