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
a
like 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)