How do I ensure that the same Python instance is always returned for a particular C++ instance?
I'm using Boost.Python to wrap a C++ library.
How do I ensure that the same Python instance (by object identity) is always returned for a particular C++ instance (by pointer identity)? I can't extend the C++ classes, but I can add a member variable (such as a PyObject * or a boost::python::handle<>) if that helps. I'm t开发者_Go百科hinking that I should be able to cache the Python instance in the C++ instance, and return the cached instance instead of creating a new one. However, I can't figure out what wrapping code is required.
Example class to be wrapped:
class C {
public:
boost::python::handle<> wrapper_;
private:
C();
C(const C &);
~C();
};
Can anyone offer advice?
After investing some time into this very problem I came to the conclusion that it's more trouble than it's worth. I have resigned myself that id() will identify the (potentially short-lived) wrapper object and not the actual C++ object.
Instead I identify my C++ objects in some other way, e.g. by looking at the contents.
精彩评论