1 // Copyright David Abrahams 2005. Distributed under the Boost 2 // Software License, Version 1.0. (See accompanying 3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 4 5 #include <boost/python/register_ptr_to_python.hpp> 6 #include <boost/python/def.hpp> 7 #include <boost/python/class.hpp> 8 #include <boost/python/wrapper.hpp> 9 #include <boost/python/module.hpp> 10 #include <boost/python/implicit.hpp> 11 12 #include <memory> 13 14 struct data 15 { ~datadata16 virtual ~data() {}; // silence compiler warnings iddata17 virtual int id() const 18 { 19 return 42; 20 } 21 }; 22 create_data()23std::auto_ptr<data> create_data() 24 { 25 return std::auto_ptr<data>( new data ); 26 } 27 do_nothing(std::auto_ptr<data> &)28void do_nothing( std::auto_ptr<data>& ){} 29 30 31 namespace bp = boost::python; 32 33 struct data_wrapper : data, bp::wrapper< data > 34 { data_wrapperdata_wrapper35 data_wrapper(data const & arg ) 36 : data( arg ) 37 , bp::wrapper< data >() 38 {} 39 data_wrapperdata_wrapper40 data_wrapper() 41 : data() 42 , bp::wrapper< data >() 43 {} 44 iddata_wrapper45 virtual int id() const 46 { 47 if( bp::override id = this->get_override( "id" ) ) 48 return bp::call<int>(id.ptr()); // id(); 49 else 50 return data::id( ); 51 } 52 default_iddata_wrapper53 virtual int default_id( ) const 54 { 55 return this->data::id( ); 56 } 57 58 }; 59 BOOST_PYTHON_MODULE(wrapper_held_type_ext)60BOOST_PYTHON_MODULE(wrapper_held_type_ext) 61 { 62 bp::class_< data_wrapper, std::auto_ptr< data > >( "data" ) 63 .def( "id", &data::id, &::data_wrapper::default_id ); 64 65 bp::def( "do_nothing", &do_nothing ); 66 bp::def( "create_data", &create_data ); 67 } 68 69 #include "module_tail.cpp" 70