I have a library which creates objects (instances of class A) and pass them to a python program which should be able to call their methods.
Basically I have C++ class instances and I want to use them from python. Occasionally that object should be passed back to C++ for some manipulations.
I created the following wrapper file (let’s assume that the New function is called somewhere in the C++ code):
#include <boost/python.hpp>
#include <iostream>
#include <boost/smart_ptr.hpp>
using namespace boost;
using namespace boost::python;
int calls = 0;
struct A
{
int f() { return calls++; }
~A() { std::cout << "destroyed\n"; }
};
shared_ptr<A> existing_instance;
void New() { existing_instance = shared_ptr<A>( new A() ); }
int Count( shared_ptr<A> a ) { return a.use_count(); }
BOOST_PYTHON_MODULE(libp)
{
class_<A>("A")
.def("f", &A::f)
;
def("Count", &Count);
register_ptr_to_python< shared_ptr<A> >();
}
The code lacks the part where the python gets the existing_instance. I didn’t paste that, but let’s just say I use a callback mechanism for that purpose.
This code works but I have a few questions:
-
In the Count function (and in all other C++ manipulation functions) is it fine to pass
alike that or it’s better to do something likeconst shared_ptr<A>&? In the code snippets I found in the python boost documentation the reference is often used but I don’t understand the difference (apart from having a higher reference counter, of course). -
Is this code “safe”? When I pass the existing_instance to python, its counter will be incremented (just once, even if in python I make more copies of the object, of course) so there is no way that the C++ code could destroy the object as far as python holds at least a “copy”. Am I correct? I tried to play with pointers and it seems I’m correct, I’m asking just to be sure.
-
I’d like to prevent python from creating instances of A. They should only be passed from C++ code. How could I achieve that? EDIT: found, I just need to use no_init and noncopyable:
class_<A, boost::noncopyable>("A", no_init)
boost::pythonknows all aboutboost::shared_ptr, but you need to tell it thatboost::shared_ptr<A>holds an instance ofA, you do this by addingboost::shared_ptr<A>in the template argument list toclass_, more information on this ‘Held Type’ is here in the boost documentation.To prevent instances being created from python, you add
boost::python::no_initto the class_ constructor, so you end up with:In general you should not pass around shared pointers by reference, since if the reference to the shared pointer is invalidated, then the reference to which the shared pointer is pointing to is also invalidated (since taking a reference of the shared pointer didn’t increment the reference counter to the pointed to object).
It is perfectly safe to pass
boost::shared_ptrobjects around to and from python, reference counts (python and shared_ptr) will be correctly managed provided you don’t change thereturn_value_policy. If you change the policy of a method exposed in python so that it returns a reference to a shared pointer then you can cause problems, just as passing shared pointers around by c++ references can cause problems.(Also, you should use
make_shared<A>(...)in preference toshared_ptr<A>(new A(...)).)