[C++-sig] boost/python: how to: call memberfunctions of an existing c++ object from python?

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Wed Mar 9 18:08:43 CET 2005


If your C++ function takes as parameter a pointer or a reference on your 
object, then Boost won't copy it !

Then, if you want to use your own smart pointer to manage memory 
yourself (using shared_ptr for example), you will need to define the 
pointee template class partially specialized for your smart pointer (if 
your compiler allows this) and the get_pointer template function also.

Pierre

Marco Borm a écrit :
> (I also posted this message to gmane.comp.lib.boost.devel)
> 
> Hi Boost/Python developers,
> 
> at first I have to say that I am no boost nor a python expert.
> I've searched for hours now on the web for my problem but was unable to find
> some solution for my problem:
> 
> Currently I am working on the possibility that an python-script is called by
> an existing c++ object. This was "easy" to do with and without the boost
> python-classes.
> 
> To make this more complicate, the script must be able to call some member
> functions of this object.
> So to make this possible I created an BOOST_PYTHON_MODULE() containing the
> class and the to be called member function-definitions.
> Now it is possible to create new instances of the class and call them from
> python. It is also possible to do the following in my c++ object-code: "ret
> = boost::python::call<int>(PyFunction.ptr(), this );"
> 
> But now my problem: boost copies the object and passes only that copy to the
> function.
> Is it and how is it possible to make the object accessible to the function
> within the python-script?
> I have found some hints in the boost-source for using smartptr but I was
> unable to use them as an parameter for call(): "TypeError: No to_python
> (by-value) converter found for C++ type: class boost::shared_ptr<TYPE>".
> I am also don't known if this is the right way to do.
> 
> 
> Thanks for help!
> Marco Borm
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
> 

-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68



More information about the Cplusplus-sig mailing list