summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/python/test/virtual_functions.cpp
blob: 774b11b121111f4f974212ef3aeceed7f3d69539 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright David Abrahams 2002.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <boost/python/class.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/return_internal_reference.hpp>
#include <boost/python/call_method.hpp>
#include <boost/ref.hpp>
#include <boost/utility.hpp>

#define BOOST_ENABLE_ASSERT_HANDLER
#include <boost/assert.hpp>

using namespace boost::python;

struct X
{
    explicit X(int x) : x(x), magic(7654321) { ++counter; }
    X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; }
    virtual ~X() { BOOST_ASSERT(magic == 7654321); magic = 6666666; x = 9999; --counter; }

    void set(int _x) { BOOST_ASSERT(magic == 7654321); this->x = _x; }
    int value() const { BOOST_ASSERT(magic == 7654321); return x; }
    static int count() { return counter; }
 private:
    void operator=(X const&);
 private:
    int x;
    long magic;
    static int counter;
};

struct Y : X
{
    Y(int x) : X(x) {};
};

struct abstract : X
{
    abstract(int x) : X(x) {};
    int call_f(Y const& y) { return f(y); }
    virtual int f(Y const& y) = 0;
    abstract& call_g(Y const& y) { return g(y); }
    virtual abstract& g(Y const& y) = 0;
};

struct concrete : X
{
    concrete(int x) : X(x) {};
    int call_f(Y const& y) { return f(y); }
    virtual int f(Y const& y) { set(y.value()); return y.value(); }
};

struct abstract_callback : abstract
{
    abstract_callback(PyObject* p, int x)
        : abstract(x), self(p)
    {}

    int f(Y const& y)
    {
        return call_method<int>(self, "f", boost::ref(y));
    }

    abstract& g(Y const& y)
    {
        return call_method<abstract&>(self, "g", boost::ref(y));
    }

    PyObject* self;
};

struct concrete_callback : concrete
{
    concrete_callback(PyObject* p, int x)
        : concrete(x), self(p)
    {}

    concrete_callback(PyObject* p, concrete const& x)
        : concrete(x), self(p)
    {}

    int f(Y const& y)
    {
        return call_method<int>(self, "f", boost::ref(y));
    }

    int f_impl(Y const& y)
    {
        return this->concrete::f(y);
    }
    
    PyObject* self;
};

int X::counter;

BOOST_PYTHON_MODULE(virtual_functions_ext)
{
    class_<concrete, concrete_callback>("concrete", init<int>())
        .def("value", &concrete::value)
        .def("set", &concrete::set)
        .def("call_f", &concrete::call_f)
        .def("f", &concrete_callback::f_impl)
        ;
        
    class_<abstract, boost::noncopyable, abstract_callback
        >("abstract", init<int>())
            
        .def("value", &abstract::value)
        .def("call_f", &abstract::call_f)
        .def("call_g", &abstract::call_g, return_internal_reference<>())
        .def("set", &abstract::set)
        ;
        
    class_<Y>("Y", init<int>())
        .def("value", &Y::value)
        .def("set", &Y::set)
        ;
}

#include "module_tail.cpp"