Hello everybody ! I've googled a lot and I wasn't able to find an answer for this, I think, easy beginner question. Let's say I have a that class : /////////////////// // Classe.h /////////////////// class Classe { public: Classe(); }; /////////////////// // Classe.cpp /////////////////// Classe::Classe() { } //////////////////// I'm wrapping it inside my Boost.Python section : class_< Classe >( "Classe" ) ; register_ptr_to_python< boost::shared_ptr<Classe> >(); I'd like to register a shared_ptr<const Classe>, but I am not able to do it. When I do a register_ptr_to_python< boost::shared_ptr<const Classe>
(), I have that error in my logs :
[...] .../pointer_holder.hpp:138: error: invalid conversion from `const void*' to `void*' [...] How can I register a shared_ptr<const Classe> ? Thank you a lot !! -- Charles Fleche The Mill, London
That kind of question has already been posted a few month ago, and had no answer. I'd like to know if it's because it's a 100% newbie question and you are tired of answer that (if so, please point me to the FAQ so I could RTFM), or if there is a real technical problem behind that. Thank you a lot ! -- Charles Fleche The Mill, London
Not sure if that is the right solution, but I am tackling this problem in a following way: namespace boost{ namespace python{ template <class T> inline T* get_pointer( boost::shared_ptr<const T> const& p){ return const_cast<T*>(p.get()); } template <class T> struct pintee< boost::shared_ptr<const T> >{ typedef T type; }; }} and later on boost::python::register_ptr_to_python< boost::shared_ptr<const my_class>
();
Looks more like workaround to me, so if anyone can propose better solution, I am all ears. Charles Flèche wrote:
That kind of question has already been posted a few month ago, and had no answer. I'd like to know if it's because it's a 100% newbie question and you are tired of answer that (if so, please point me to the FAQ so I could RTFM), or if there is a real technical problem behind that.
Thank you a lot !
Ok for the workaround, but I'm going to give it a try !
I've tried it, but if now it compiles well, I have a python error at runtime : Traceback (most recent call last): File "<stdin>", line 1, in ? Boost.Python.ArgumentError: Python argument types in Classd.f_const_shared_ptr(Classd, Classd) did not match C++ signature: f_const_shared_ptr(Classd {lvalue}, boost::shared_ptr<Classd const>) These are the files : // Classd.h #ifndef CLASSD_H #define CLASSD_H #include <string> #include <boost/shared_ptr.hpp> using namespace boost; class Classd { public: Classd(); std::string type(); shared_ptr<Classd> factory(); shared_ptr<const Classd> factory_const(); void f_object( Classd ); void f_const_object( const Classd ); void f_shared_ptr( shared_ptr<Classd> ); void f_const_shared_ptr( shared_ptr<const Classd> ); protected: void log( std::string ); }; #endif // Classd.cpp #include "Classd.h" #include <iostream> using namespace std; using namespace boost; Classd::Classd() { } string Classd::type() { return string( "I'm a Classd" ); } shared_ptr<Classd> Classd::factory() { shared_ptr<Classd> ret( new Classd ); return ret; } shared_ptr<const Classd> Classd::factory_const() { shared_ptr<Classd> ret( new Classd ); return ret; } void Classd::f_object( Classd ) { log( "f_object" ); } void Classd::f_const_object( const Classd ) { log( "f_const_object" ); } void Classd::f_shared_ptr( shared_ptr<Classd> ) { log( "f_shared_ptr" ); } void Classd::f_const_shared_ptr( shared_ptr<const Classd> ) { log( "f_const_shared_ptr" ); } void Classd::log( string l ) { cout << "Classd : " << l << endl; } template class shared_ptr<Classd>; template class shared_ptr<const Classd>; // Wrap.cpp -- trimmed down version with just Classd #include <boost/python.hpp> using namespace boost::python; #include "Classd.h" namespace boost { namespace python { template <class T> inline T* get_pointer( boost::shared_ptr<const T> const& p) { return const_cast<T*>(p.get()); } template <class T> struct pointee< boost::shared_ptr<const T> > { typedef T type; }; } } BOOST_PYTHON_MODULE(OwnTest) { class_< Classd >( "Classd" ) .def( "type", &Classd::type ) .def( "factory", &Classd::factory ) .def( "factory_const", &Classd::factory_const ) .def( "f_object", &Classd::f_object ) .def( "f_const_object", &Classd::f_const_object ) .def( "f_shared_ptr", &Classd::f_shared_ptr ) .def( "f_const_shared_ptr", &Classd::f_const_shared_ptr ) ; register_ptr_to_python< boost::shared_ptr< Classd > >(); register_ptr_to_python< boost::shared_ptr< const Classd > >(); } // Python log
a.factory() <OwnTest.Classd object at 0xb7f20994> a.factory_const() <OwnTest.Classd object at 0xb7f209cc> a.f_object( a ) Classd : f_object a.f_const_object( a ) Classd : f_const_object a.f_shared_ptr( a ) Classd : f_shared_ptr a.f_const_shared_ptr( a ) Traceback (most recent call last): File "<stdin>", line 1, in ? Boost.Python.ArgumentError: Python argument types in Classd.f_const_shared_ptr(Classd, Classd) did not match C++ signature: f_const_shared_ptr(Classd {lvalue}, boost::shared_ptr<Classd const>)
-- Charles Fleche
On 11/20/06, Charles Flèche <charlesf@the-mill.com> wrote:
Ok for the workaround, but I'm going to give it a try !
I've tried it, but if now it compiles well, I have a python error at runtime :
Traceback (most recent call last): File "<stdin>", line 1, in ? Boost.Python.ArgumentError: Python argument types in Classd.f_const_shared_ptr(Classd, Classd) did not match C++ signature: f_const_shared_ptr(Classd {lvalue}, boost::shared_ptr<Classd const>)
These are the files :
// Classd.h
#ifndef CLASSD_H #define CLASSD_H
#include <string>
#include <boost/shared_ptr.hpp> using namespace boost;
class Classd { public: Classd(); std::string type(); shared_ptr<Classd> factory(); shared_ptr<const Classd> factory_const();
void f_object( Classd ); void f_const_object( const Classd ); void f_shared_ptr( shared_ptr<Classd> ); void f_const_shared_ptr( shared_ptr<const Classd> );
protected: void log( std::string );
};
#endif
// Classd.cpp
#include "Classd.h"
#include <iostream> using namespace std; using namespace boost;
Classd::Classd() { }
string Classd::type() { return string( "I'm a Classd" ); }
shared_ptr<Classd> Classd::factory() { shared_ptr<Classd> ret( new Classd ); return ret; }
shared_ptr<const Classd> Classd::factory_const() { shared_ptr<Classd> ret( new Classd ); return ret; }
void Classd::f_object( Classd ) { log( "f_object" ); }
void Classd::f_const_object( const Classd ) { log( "f_const_object" ); }
void Classd::f_shared_ptr( shared_ptr<Classd> ) { log( "f_shared_ptr" ); }
void Classd::f_const_shared_ptr( shared_ptr<const Classd> ) { log( "f_const_shared_ptr" ); }
void Classd::log( string l ) { cout << "Classd : " << l << endl; }
template class shared_ptr<Classd>; template class shared_ptr<const Classd>;
// Wrap.cpp -- trimmed down version with just Classd
#include <boost/python.hpp> using namespace boost::python;
#include "Classd.h"
namespace boost { namespace python {
template <class T> inline T* get_pointer( boost::shared_ptr<const T> const& p) { return const_cast<T*>(p.get()); }
template <class T> struct pointee< boost::shared_ptr<const T> > { typedef T type; }; } }
BOOST_PYTHON_MODULE(OwnTest) { class_< Classd >( "Classd" ) .def( "type", &Classd::type ) .def( "factory", &Classd::factory ) .def( "factory_const", &Classd::factory_const ) .def( "f_object", &Classd::f_object ) .def( "f_const_object", &Classd::f_const_object ) .def( "f_shared_ptr", &Classd::f_shared_ptr ) .def( "f_const_shared_ptr", &Classd::f_const_shared_ptr ) ;
register_ptr_to_python< boost::shared_ptr< Classd > >(); register_ptr_to_python< boost::shared_ptr< const Classd > >();
}
// Python log
a.factory() <OwnTest.Classd object at 0xb7f20994> a.factory_const() <OwnTest.Classd object at 0xb7f209cc> a.f_object( a ) Classd : f_object a.f_const_object( a ) Classd : f_const_object a.f_shared_ptr( a ) Classd : f_shared_ptr a.f_const_shared_ptr( a ) Traceback (most recent call last): File "<stdin>", line 1, in ? Boost.Python.ArgumentError: Python argument types in Classd.f_const_shared_ptr(Classd, Classd) did not match C++ signature: f_const_shared_ptr(Classd {lvalue}, boost::shared_ptr<Classd const>)
I think you should also register implicit conversion between smart pointers http://boost.org/libs/python/doc/v2/implicit.html#implicitly_convertible-spe... -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/
I think you should also register implicit conversion between smart pointers http://boost.org/libs/python/doc/v2/implicit.html#implicitly_convertible-spe...
Thank you a lot Kirill and Roman, it's working well now ! -- Charles Fleche
On 11/20/06, Charles Flèche <charlesf@the-mill.com> wrote:
I think you should also register implicit conversion between smart pointers http://boost.org/libs/python/doc/v2/implicit.html#implicitly_convertible-spe...
Thank you a lot Kirill and Roman, it's working well now !
I just created and tested a complete solution to the problem: http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/docs/bpl_... http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/docs/bpl_... Hope this will be helpful for other too. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/
I just created and tested a complete solution to the problem: http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/docs/bpl_... http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/docs/bpl_...
Excellent Roman ! I'm sure it will be really helpful ! -- Charles Fleche
"Roman Yakovenko" <roman.yakovenko@gmail.com> writes:
On 11/20/06, Charles Flèche <charlesf@the-mill.com> wrote:
I think you should also register implicit conversion between smart pointers http://boost.org/libs/python/doc/v2/implicit.html#implicitly_convertible-spe...
Thank you a lot Kirill and Roman, it's working well now !
I just created and tested a complete solution to the problem: http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/docs/bpl_... http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/docs/bpl_...
Hope this will be helpful for other too.
Hi Roman, Thanks for giving people a stopgap solution. Your overload of get_pointer is clever but unfortunately evil. How about submitting a patch to Boost.Python that actually fixes the problem? -- Dave Abrahams Boost Consulting www.boost-consulting.com
On 11/23/06, David Abrahams <dave@boost-consulting.com> wrote:
Your overload of get_pointer is clever but unfortunately evil.
Yes I know.
How about submitting a patch to Boost.Python that actually fixes the problem?
I will try, but I am not sure I understand the scope of the problem/solution. It is pretty easy to fix the pointer_holder::holds and pointer_holder_back_reference::holds member functions. I did that and the patch is attached. Is this is all that should be done? -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/
participants (4)
-
Charles Flèche -
David Abrahams -
Kirill Lapshin -
Roman Yakovenko