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 {
YY22     Y(int v) : test_class<1>(v) {}
operator =Y23     Y& operator=(Y const& rhs) { x = rhs.x; return *this; }
24     bool q;
25 };
26 
get_fair_value(X const & x)27 double get_fair_value(X const& x) { return x.value(); }
28 
29 
30 struct VarBase
31 {
VarBaseVarBase32     VarBase(std::string name_) : name(name_) {}
33 
34     std::string const name;
get_name1VarBase35     std::string get_name1() const { return name; }
36 
37 };
38 
39 struct Var : VarBase
40 {
VarVar41     Var(std::string name_) : VarBase(name_), value(), name2(name.c_str()), y(6) {}
get_name2Var42     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 boost_python_test
56 {
57   struct trivial
58   {
trivialboost_python_test::trivial59     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 
compilability_test()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 
BOOST_PYTHON_MODULE(data_members_ext)87 BOOST_PYTHON_MODULE(data_members_ext)
88 {
89     using namespace boost_python_test;
90     class_<X>("X", init<int>())
91         .def("value", &X::value)
92         .def("set", &X::set)
93         .def_readonly("x", &X::x)
94         .add_property("fair_value", get_fair_value)
95         ;
96 
97     class_<Y>("Y", init<int>())
98         .def("value", &Y::value)
99         .def("set", &Y::set)
100         .def_readwrite("x", &Y::x)
101         .def_readwrite("q", &Y::q)
102         ;
103 
104     class_<Var>("Var", init<std::string>())
105         .def_readonly("name", &Var::name)
106         .def_readonly("name2", &Var::name2)
107         .def_readwrite("value", &Var::value)
108         .def_readonly("y", &Var::y)
109 
110         // Test return_by_value for plain values and for
111         // pointers... return_by_value was implemented as a
112         // side-effect of implementing data member support, so it made
113         // sense to add the test here.
114         .def("get_name1", &Var::get_name1, return_value_policy<return_by_value>())
115         .def("get_name2", &Var::get_name2, return_value_policy<return_by_value>())
116 
117         .add_property("name3", &Var::get_name1)
118 
119         // Test static data members
120         .def_readonly("ro1a", &Var::static1)
121         .def_readonly("ro1b", Var::static1)
122         .def_readwrite("rw1a", &Var::static1)
123         .def_readwrite("rw1b", Var::static1)
124 
125         .def_readonly("ro2a", &Var::static2)
126         .def_readonly("ro2b", Var::static2)
127         .def_readwrite("rw2a", &Var::static2)
128         .def_readwrite("rw2b", Var::static2)
129         ;
130 }
131 
132 #include "module_tail.cpp"
133