|
| 1 | +#include <boost/python.hpp> |
| 2 | + |
| 3 | +using namespace boost::python; |
| 4 | + |
| 5 | +struct X |
| 6 | +{ |
| 7 | + X( int value ) : m_value( value ) |
| 8 | + { ++s_count; } |
| 9 | + |
| 10 | + X( const X &other ) : m_value( other.m_value ) |
| 11 | + { ++s_count; } |
| 12 | + |
| 13 | + ~X() |
| 14 | + { --s_count; } |
| 15 | + |
| 16 | + int get_value() const |
| 17 | + { return m_value; } |
| 18 | + |
| 19 | + void set_value(int new_value) |
| 20 | + { m_value = new_value; } |
| 21 | + |
| 22 | + static int get_instance_count() |
| 23 | + { return s_count; } |
| 24 | + |
| 25 | + int m_value; |
| 26 | + |
| 27 | + static int s_count; |
| 28 | +}; |
| 29 | + |
| 30 | +int X::s_count = 0; |
| 31 | + |
| 32 | +int get_X_instance_count() |
| 33 | +{ return X::get_instance_count(); } |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +BOOST_PYTHON_MODULE(properties_ext) |
| 38 | +{ |
| 39 | + typedef return_value_policy<return_by_value> return_by_value_t; |
| 40 | + typedef return_internal_reference<> return_by_internal_reference_t; |
| 41 | + class_<X>("X", init<int>() ) |
| 42 | + //defining read only property |
| 43 | + .add_property( "value_r", &X::get_value ) |
| 44 | + //defining read \ write property |
| 45 | + .add_property( "value_rw", &X::get_value, &X::set_value ) |
| 46 | + //defining read \ write property using make_getter and make_setter |
| 47 | + .add_property( "value_direct", |
| 48 | + make_getter( &X::m_value, return_by_value_t() ), |
| 49 | + make_setter( &X::m_value, return_by_internal_reference_t() ) ) |
| 50 | + //defining read only property for static member |
| 51 | + .add_static_property( "instance_count", &X::get_instance_count ) |
| 52 | + //defining read \ write property for static member using make_getter and make_setter |
| 53 | + .add_static_property( "instance_count_direct", |
| 54 | + make_getter( &X::s_count, return_by_value_t() ), |
| 55 | + make_setter( &X::s_count, return_by_internal_reference_t() ) ) |
| 56 | + //defining class property using a global function |
| 57 | + .add_static_property( "instance_count_injected", &get_X_instance_count ); |
| 58 | +} |
| 59 | + |
| 60 | +#include "module_tail.cpp" |
0 commit comments