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>
6 using namespace boost::python
;
10 // Hmm. return_internal_reference<>() wants to wrap a real class.
14 ret_type() : i(42.5) {}
23 ret_type
& get_i() { return i
; }
30 X( int value
) : m_value( value
)
33 X( const X
&other
) : m_value( other
.m_value
)
42 void set_value(int new_value
)
43 { m_value
= new_value
; }
45 static int get_instance_count()
55 int get_X_instance_count()
56 { return X::get_instance_count(); }
60 BOOST_PYTHON_MODULE(properties_ext
)
62 typedef return_value_policy
<return_by_value
> return_by_value_t
;
63 typedef return_internal_reference
<> return_by_internal_reference_t
;
64 class_
<X
>("X", init
<int>() )
65 //defining read only property
66 .add_property( "value_r", &X::get_value
)
67 .add_property( "value_r_ds", &X::get_value
, "value_r_ds is read-only")
68 //defining read \ write property
69 .add_property( "value_rw", &X::get_value
, &X::set_value
)
70 .add_property( "value_rw_ds", &X::get_value
, &X::set_value
,
71 "value_rw_ds is read-write")
72 //defining read \ write property using make_getter and make_setter
73 .add_property( "value_direct",
74 make_getter( &X::m_value
, return_by_value_t() ),
75 make_setter( &X::m_value
, return_by_internal_reference_t() ) )
76 //defining read only property for static member
77 .add_static_property( "instance_count", &X::get_instance_count
)
78 //defining read \ write property for static member using make_getter and make_setter
79 .add_static_property( "instance_count_direct",
80 make_getter( &X::s_count
, return_by_value_t() ),
81 make_setter( &X::s_count
, return_by_internal_reference_t() ) )
82 //defining class property using a global function
83 .add_static_property( "instance_count_injected", &get_X_instance_count
);
86 class_
< test::ret_type
>( "ret_type")
87 .add_property( "i", &test::ret_type::i
, &test::ret_type::i
)
90 class_
< test::crash_me
> crash_me_wrapper( "crash_me");
93 .def( "get_i", &test::crash_me::get_i
, return_internal_reference
<>())
96 crash_me_wrapper
.add_property( "i", crash_me_wrapper
.attr("get_i"));
100 #include "module_tail.cpp"