properties.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright David Abrahams 2004. 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.hpp>
  5. using namespace boost::python;
  6. namespace test {
  7. // Hmm. return_internal_reference<>() wants to wrap a real class.
  8. class ret_type
  9. {
  10. public:
  11. ret_type() : i(42.5) {}
  12. double i;
  13. };
  14. class crash_me
  15. {
  16. private:
  17. ret_type i;
  18. public:
  19. ret_type& get_i() { return i; }
  20. };
  21. }
  22. struct X
  23. {
  24. X( int value ) : m_value( value )
  25. { ++s_count; }
  26. X( const X &other ) : m_value( other.m_value )
  27. { ++s_count; }
  28. ~X()
  29. { --s_count; }
  30. int get_value() const
  31. { return m_value; }
  32. void set_value(int new_value)
  33. { m_value = new_value; }
  34. static int get_instance_count()
  35. { return s_count; }
  36. int m_value;
  37. static int s_count;
  38. };
  39. int X::s_count = 0;
  40. int get_X_instance_count()
  41. { return X::get_instance_count(); }
  42. BOOST_PYTHON_MODULE(properties_ext)
  43. {
  44. typedef return_value_policy<return_by_value> return_by_value_t;
  45. typedef return_internal_reference<> return_by_internal_reference_t;
  46. class_<X>("X", init<int>() )
  47. //defining read only property
  48. .add_property( "value_r", &X::get_value )
  49. .add_property( "value_r_ds", &X::get_value, "value_r_ds is read-only")
  50. //defining read \ write property
  51. .add_property( "value_rw", &X::get_value, &X::set_value )
  52. .add_property( "value_rw_ds", &X::get_value, &X::set_value,
  53. "value_rw_ds is read-write")
  54. //defining read \ write property using make_getter and make_setter
  55. .add_property( "value_direct",
  56. make_getter( &X::m_value, return_by_value_t() ),
  57. make_setter( &X::m_value, return_by_internal_reference_t() ) )
  58. //defining read only property for static member
  59. .add_static_property( "instance_count", &X::get_instance_count )
  60. //defining read \ write property for static member using make_getter and make_setter
  61. .add_static_property( "instance_count_direct",
  62. make_getter( &X::s_count, return_by_value_t() ),
  63. make_setter( &X::s_count, return_by_internal_reference_t() ) )
  64. //defining class property using a global function
  65. .add_static_property( "instance_count_injected", &get_X_instance_count );
  66. class_< test::ret_type>( "ret_type")
  67. .add_property( "i", &test::ret_type::i, &test::ret_type::i)
  68. ;
  69. class_< test::crash_me> crash_me_wrapper( "crash_me");
  70. crash_me_wrapper
  71. .def( "get_i", &test::crash_me::get_i , return_internal_reference<>())
  72. ;
  73. crash_me_wrapper.add_property( "i", crash_me_wrapper.attr("get_i"));
  74. }
  75. #include "module_tail.cpp"