1 | // Copyright David Abrahams 2002. |
---|
2 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
3 | // accompanying file LICENSE_1_0.txt or copy at |
---|
4 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | #include <boost/python/class.hpp> |
---|
6 | #include <boost/python/module.hpp> |
---|
7 | #include <boost/python/return_value_policy.hpp> |
---|
8 | #include <boost/python/return_by_value.hpp> |
---|
9 | #include "test_class.hpp" |
---|
10 | |
---|
11 | #if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245 |
---|
12 | # include <iostream> // works around a KCC intermediate code generation bug |
---|
13 | #endif |
---|
14 | |
---|
15 | |
---|
16 | using namespace boost::python; |
---|
17 | |
---|
18 | typedef test_class<> X; |
---|
19 | |
---|
20 | struct Y : test_class<1> |
---|
21 | { |
---|
22 | Y(int v) : test_class<1>(v) {} |
---|
23 | Y& operator=(Y const& rhs) { x = rhs.x; return *this; } |
---|
24 | bool q; |
---|
25 | }; |
---|
26 | |
---|
27 | double get_fair_value(X const& x) { return x.value(); } |
---|
28 | |
---|
29 | |
---|
30 | struct VarBase |
---|
31 | { |
---|
32 | VarBase(std::string name_) : name(name_) {} |
---|
33 | |
---|
34 | std::string const name; |
---|
35 | std::string get_name1() const { return name; } |
---|
36 | |
---|
37 | }; |
---|
38 | |
---|
39 | struct Var : VarBase |
---|
40 | { |
---|
41 | Var(std::string name_) : VarBase(name_), value(), name2(name.c_str()), y(6) {} |
---|
42 | std::string const& get_name2() const { return name; } |
---|
43 | float value; |
---|
44 | char const* name2; |
---|
45 | Y y; |
---|
46 | |
---|
47 | static int static1; |
---|
48 | static Y static2; |
---|
49 | }; |
---|
50 | |
---|
51 | int Var::static1 = 0; |
---|
52 | Y Var::static2(0); |
---|
53 | |
---|
54 | // Compilability regression tests |
---|
55 | namespace |
---|
56 | { |
---|
57 | struct trivial |
---|
58 | { |
---|
59 | trivial() : value(123) {} |
---|
60 | double value; |
---|
61 | }; |
---|
62 | |
---|
63 | struct Color3 |
---|
64 | { |
---|
65 | static const Color3 black; |
---|
66 | }; |
---|
67 | |
---|
68 | const Color3 Color3::black |
---|
69 | #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) |
---|
70 | = {} |
---|
71 | #endif |
---|
72 | ; |
---|
73 | |
---|
74 | void compilability_test() |
---|
75 | { |
---|
76 | class_<trivial>("trivial") |
---|
77 | .add_property("property", make_getter(&trivial::value, return_value_policy<return_by_value>())) |
---|
78 | .def_readonly("readonly", &trivial::value) |
---|
79 | ; |
---|
80 | |
---|
81 | class_< Color3 >("Color3", init< const Color3 & >()) |
---|
82 | .def_readonly("BLACK", &Color3::black) // line 17 |
---|
83 | ; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | BOOST_PYTHON_MODULE(data_members_ext) |
---|
88 | { |
---|
89 | class_<X>("X", init<int>()) |
---|
90 | .def("value", &X::value) |
---|
91 | .def("set", &X::set) |
---|
92 | .def_readonly("x", &X::x) |
---|
93 | .add_property("fair_value", get_fair_value) |
---|
94 | ; |
---|
95 | |
---|
96 | class_<Y>("Y", init<int>()) |
---|
97 | .def("value", &Y::value) |
---|
98 | .def("set", &Y::set) |
---|
99 | .def_readwrite("x", &Y::x) |
---|
100 | .def_readwrite("q", &Y::q) |
---|
101 | ; |
---|
102 | |
---|
103 | class_<Var>("Var", init<std::string>()) |
---|
104 | .def_readonly("name", &Var::name) |
---|
105 | .def_readonly("name2", &Var::name2) |
---|
106 | .def_readwrite("value", &Var::value) |
---|
107 | .def_readonly("y", &Var::y) |
---|
108 | |
---|
109 | // Test return_by_value for plain values and for |
---|
110 | // pointers... return_by_value was implemented as a |
---|
111 | // side-effect of implementing data member support, so it made |
---|
112 | // sense to add the test here. |
---|
113 | .def("get_name1", &Var::get_name1, return_value_policy<return_by_value>()) |
---|
114 | .def("get_name2", &Var::get_name2, return_value_policy<return_by_value>()) |
---|
115 | |
---|
116 | .add_property("name3", &Var::get_name1) |
---|
117 | |
---|
118 | // Test static data members |
---|
119 | .def_readonly("ro1a", &Var::static1) |
---|
120 | .def_readonly("ro1b", Var::static1) |
---|
121 | .def_readwrite("rw1a", &Var::static1) |
---|
122 | .def_readwrite("rw1b", Var::static1) |
---|
123 | |
---|
124 | .def_readonly("ro2a", &Var::static2) |
---|
125 | .def_readonly("ro2b", Var::static2) |
---|
126 | .def_readwrite("rw2a", &Var::static2) |
---|
127 | .def_readwrite("rw2b", Var::static2) |
---|
128 | ; |
---|
129 | } |
---|
130 | |
---|
131 | #include "module_tail.cpp" |
---|