Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/python/test/back_reference.cpp @ 13

Last change on this file since 13 was 12, checked in by landauf, 17 years ago

added boost

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