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/def.hpp> |
---|
8 | #include <boost/python/has_back_reference.hpp> |
---|
9 | #include <boost/python/back_reference.hpp> |
---|
10 | #include <boost/ref.hpp> |
---|
11 | #include <boost/utility.hpp> |
---|
12 | #include <memory> |
---|
13 | #define BOOST_ENABLE_ASSERT_HANDLER |
---|
14 | #include <boost/assert.hpp> |
---|
15 | #include <boost/python/copy_const_reference.hpp> |
---|
16 | #include <boost/python/return_value_policy.hpp> |
---|
17 | #include <boost/mpl/bool.hpp> |
---|
18 | |
---|
19 | // This test shows that a class can be wrapped "as itself" but also |
---|
20 | // acquire a back-reference iff has_back_reference<> is appropriately |
---|
21 | // specialized. |
---|
22 | using namespace boost::python; |
---|
23 | |
---|
24 | struct X |
---|
25 | { |
---|
26 | explicit X(int x) : x(x), magic(7654321) { ++counter; } |
---|
27 | X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; } |
---|
28 | virtual ~X() { BOOST_ASSERT(magic == 7654321); magic = 6666666; x = 9999; --counter; } |
---|
29 | |
---|
30 | void set(int x) { BOOST_ASSERT(magic == 7654321); this->x = x; } |
---|
31 | int value() const { BOOST_ASSERT(magic == 7654321); return x; } |
---|
32 | static int count() { return counter; } |
---|
33 | private: |
---|
34 | void operator=(X const&); |
---|
35 | private: |
---|
36 | int x; |
---|
37 | long magic; |
---|
38 | static int counter; |
---|
39 | }; |
---|
40 | |
---|
41 | int X::counter; |
---|
42 | |
---|
43 | struct Y : X |
---|
44 | { |
---|
45 | Y(PyObject* self, int x) : X(x), self(self) {} |
---|
46 | Y(PyObject* self, Y const& rhs) : X(rhs), self(self) {} |
---|
47 | private: |
---|
48 | Y(Y const&); |
---|
49 | PyObject* self; |
---|
50 | }; |
---|
51 | |
---|
52 | struct Z : X |
---|
53 | { |
---|
54 | Z(PyObject* self, int x) : X(x), self(self) {} |
---|
55 | Z(PyObject* self, Z const& rhs) : X(rhs), self(self) {} |
---|
56 | private: |
---|
57 | Z(Z const&); |
---|
58 | PyObject* self; |
---|
59 | }; |
---|
60 | |
---|
61 | Y const& copy_Y(Y const& y) { return y; } |
---|
62 | Z const& copy_Z(Z const& z) { return z; } |
---|
63 | |
---|
64 | namespace boost { namespace python |
---|
65 | { |
---|
66 | template <> |
---|
67 | struct has_back_reference<Y> |
---|
68 | : mpl::true_ |
---|
69 | { |
---|
70 | }; |
---|
71 | |
---|
72 | template <> |
---|
73 | struct has_back_reference<Z> |
---|
74 | : mpl::true_ |
---|
75 | { |
---|
76 | }; |
---|
77 | }} |
---|
78 | |
---|
79 | // prove that back_references get initialized with the right PyObject* |
---|
80 | object y_identity(back_reference<Y const&> y) |
---|
81 | { |
---|
82 | return y.source(); |
---|
83 | } |
---|
84 | |
---|
85 | // prove that back_references contain the right value |
---|
86 | bool y_equality(back_reference<Y const&> y1, Y const& y2) |
---|
87 | { |
---|
88 | return &y1.get() == &y2; |
---|
89 | } |
---|
90 | |
---|
91 | BOOST_PYTHON_MODULE(back_reference_ext) |
---|
92 | { |
---|
93 | def("copy_Y", copy_Y, return_value_policy<copy_const_reference>()); |
---|
94 | def("copy_Z", copy_Z, return_value_policy<copy_const_reference>()); |
---|
95 | def("x_instances", &X::count); |
---|
96 | |
---|
97 | class_<Y>("Y", init<int>()) |
---|
98 | .def("value", &Y::value) |
---|
99 | .def("set", &Y::set) |
---|
100 | ; |
---|
101 | |
---|
102 | class_<Z,std::auto_ptr<Z> >("Z", init<int>()) |
---|
103 | .def("value", &Z::value) |
---|
104 | .def("set", &Z::set) |
---|
105 | ; |
---|
106 | |
---|
107 | def("y_identity", y_identity); |
---|
108 | def("y_equality", y_equality); |
---|
109 | |
---|
110 | } |
---|
111 | |
---|
112 | #include "module_tail.cpp" |
---|