]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
6 | using namespace boost::python; | |
7 | ||
8 | namespace test { | |
9 | ||
10 | // Hmm. return_internal_reference<>() wants to wrap a real class. | |
11 | class ret_type | |
12 | { | |
13 | public: | |
14 | ret_type() : i(42.5) {} | |
15 | double i; | |
16 | }; | |
17 | ||
18 | class crash_me | |
19 | { | |
20 | private: | |
21 | ret_type i; | |
22 | public: | |
23 | ret_type& get_i() { return i; } | |
24 | }; | |
25 | ||
26 | } | |
27 | ||
28 | struct X | |
29 | { | |
30 | X( int value ) : m_value( value ) | |
31 | { ++s_count; } | |
32 | ||
33 | X( const X &other ) : m_value( other.m_value ) | |
34 | { ++s_count; } | |
35 | ||
36 | ~X() | |
37 | { --s_count; } | |
38 | ||
39 | int get_value() const | |
40 | { return m_value; } | |
41 | ||
42 | void set_value(int new_value) | |
43 | { m_value = new_value; } | |
44 | ||
45 | static int get_instance_count() | |
46 | { return s_count; } | |
47 | ||
48 | int m_value; | |
49 | ||
50 | static int s_count; | |
51 | }; | |
52 | ||
53 | int X::s_count = 0; | |
54 | ||
55 | int get_X_instance_count() | |
56 | { return X::get_instance_count(); } | |
57 | ||
58 | ||
59 | ||
60 | BOOST_PYTHON_MODULE(properties_ext) | |
61 | { | |
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 ); | |
84 | ||
85 | ||
86 | class_< test::ret_type>( "ret_type") | |
87 | .add_property( "i", &test::ret_type::i, &test::ret_type::i) | |
88 | ; | |
89 | ||
90 | class_< test::crash_me> crash_me_wrapper( "crash_me"); | |
91 | ||
92 | crash_me_wrapper | |
93 | .def( "get_i", &test::crash_me::get_i , return_internal_reference<>()) | |
94 | ; | |
95 | ||
96 | crash_me_wrapper.add_property( "i", crash_me_wrapper.attr("get_i")); | |
97 | ||
98 | } | |
99 | ||
100 | #include "module_tail.cpp" |