wrapper_held_type.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include <boost/python/register_ptr_to_python.hpp>
  5. #include <boost/python/def.hpp>
  6. #include <boost/python/class.hpp>
  7. #include <boost/python/wrapper.hpp>
  8. #include <boost/python/module.hpp>
  9. #include <boost/python/implicit.hpp>
  10. #include <memory>
  11. struct data
  12. {
  13. virtual ~data() {}; // silence compiler warnings
  14. virtual int id() const
  15. {
  16. return 42;
  17. }
  18. };
  19. std::auto_ptr<data> create_data()
  20. {
  21. return std::auto_ptr<data>( new data );
  22. }
  23. void do_nothing( std::auto_ptr<data>& ){}
  24. namespace bp = boost::python;
  25. struct data_wrapper : data, bp::wrapper< data >
  26. {
  27. data_wrapper(data const & arg )
  28. : data( arg )
  29. , bp::wrapper< data >()
  30. {}
  31. data_wrapper()
  32. : data()
  33. , bp::wrapper< data >()
  34. {}
  35. virtual int id() const
  36. {
  37. if( bp::override id = this->get_override( "id" ) )
  38. return bp::call<int>(id.ptr()); // id();
  39. else
  40. return data::id( );
  41. }
  42. virtual int default_id( ) const
  43. {
  44. return this->data::id( );
  45. }
  46. };
  47. BOOST_PYTHON_MODULE(wrapper_held_type_ext)
  48. {
  49. bp::class_< data_wrapper, std::auto_ptr< data > >( "data" )
  50. .def( "id", &data::id, &::data_wrapper::default_id );
  51. bp::def( "do_nothing", &do_nothing );
  52. bp::def( "create_data", &create_data );
  53. }
  54. #include "module_tail.cpp"