Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/test/shared_ptr.cpp @ 45

Last change on this file since 45 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 4.3 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
6#include <boost/python/module.hpp>
7#include <boost/python/class.hpp>
8#include <boost/python/call_method.hpp>
9#include <boost/python/extract.hpp>
10#include <boost/python/def.hpp>
11#include <boost/shared_ptr.hpp>
12#include "test_class.hpp"
13
14#include <memory>
15
16using namespace boost::python;
17using boost::shared_ptr;
18
19typedef test_class<> X;
20typedef test_class<1> Y;
21
22template <class T>
23struct functions
24{
25    static int look(shared_ptr<T> const& x)
26    {
27        return (x.get()) ? x->value() : -1;
28    }
29
30    static void store(shared_ptr<T> x)
31    {
32        storage = x;
33    }
34
35    static void release_store()
36    {
37        store(shared_ptr<T>());
38    }
39   
40    static void modify(shared_ptr<T>& x)
41    {
42        x.reset();
43    }
44
45    static shared_ptr<T> get() { return storage; }
46       
47    static int look_store()
48    {
49        return look(get());
50    }
51
52    template <class C>
53    static void expose(C const& c)
54    {
55        def("look", &look);
56        def("store", &store);
57        def("modify", &modify);
58        def("identity", &identity);
59        def("null", &null);
60           
61        const_cast<C&>(c)
62            .def("look", &look)
63            .staticmethod("look")
64            .def("store", &store)
65            .staticmethod("store")
66            .def("modify", &modify)
67            .staticmethod("modify")
68            .def("look_store", &look_store)
69            .staticmethod("look_store")
70            .def("identity", &identity)
71            .staticmethod("identity")
72            .def("null", &null)
73            .staticmethod("null")
74            .def("get", &get)
75            .staticmethod("get")
76            .def("count", &T::count)
77            .staticmethod("count")
78            .def("release", &release_store)
79            .staticmethod("release")
80            ;
81    }
82
83    static shared_ptr<T> identity(shared_ptr<T> x) { return x; }
84    static shared_ptr<T> null(T const&) { return shared_ptr<T>(); }
85   
86
87    static shared_ptr<T> storage;
88};
89
90template <class T> shared_ptr<T> functions<T>::storage;
91
92struct Z : test_class<2>
93{
94    Z(int x) : test_class<2>(x) {}
95    virtual int v() { return this->value(); }
96};
97
98struct ZWrap : Z
99{
100    ZWrap(PyObject* self, int x)
101        : Z(x), m_self(self) {}
102
103   
104    virtual int v() { return call_method<int>(m_self, "v"); }
105    int default_v() { return Z::v(); }
106   
107
108    PyObject* m_self;
109};
110
111struct YY : Y
112{
113    YY(int n) : Y(n) {}
114};
115
116struct YYY : Y
117{
118    YYY(int n) : Y(n) {}
119};
120
121shared_ptr<Y> factory(int n)
122{
123    return shared_ptr<Y>(n < 42 ? new Y(n) : new YY(n));
124}
125
126// regressions from Nicodemus
127    struct A
128    {
129        virtual int f() = 0;
130        static int call_f(shared_ptr<A>& a) { return a->f(); }
131    };
132
133    struct B: A
134    {
135        int f() { return 1; }
136    };
137
138    boost::shared_ptr<A> New(bool make)
139    {
140        return boost::shared_ptr<A>( make ? new B() : 0 );
141    }
142
143    struct A_Wrapper: A
144    {
145        A_Wrapper(PyObject* self_):
146            A(), self(self_) {}
147
148        int f() {
149            return call_method< int >(self, "f");
150        }
151
152        PyObject* self;
153    };
154
155// ------
156
157BOOST_PYTHON_MODULE(shared_ptr_ext)
158{
159    class_<A, boost::shared_ptr<A_Wrapper>, boost::noncopyable>("A")
160        .def("call_f", &A::call_f)
161        .staticmethod("call_f")
162        ;
163
164    // This is the ugliness required to register a to-python converter
165    // for shared_ptr<A>.
166    objects::class_value_wrapper<
167        shared_ptr<A>
168      , objects::make_ptr_instance<A, objects::pointer_holder<shared_ptr<A>,A> >
169    >();
170       
171    def("New", &New);
172
173    def("factory", factory);
174   
175    functions<X>::expose(
176        class_<X, boost::noncopyable>("X", init<int>())
177             .def("value", &X::value)
178        );
179   
180    functions<Y>::expose(
181        class_<Y, boost::shared_ptr<Y> >("Y", init<int>())
182            .def("value", &Y::value)
183        );
184   
185    class_<YY, bases<Y>, boost::noncopyable>("YY", init<int>())
186        ;
187
188    class_<YYY, shared_ptr<YYY>, bases<Y> >("YYY", init<int>())
189        ;
190
191    functions<Z>::expose(
192        class_<Z, ZWrap>("Z", init<int>())
193            .def("value", &Z::value)
194            .def("v", &Z::v, &ZWrap::default_v)
195        );
196}
197
198#include "module_tail.cpp"
199
Note: See TracBrowser for help on using the repository browser.