[C++-sig] [Boost.Python] shared_ptr<const T>
Roman Yakovenko
roman.yakovenko at gmail.com
Mon Nov 20 12:29:42 CET 2006
On 11/20/06, Charles Flèche <charlesf at 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-spec
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
More information about the Cplusplus-sig
mailing list