Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/python/test/m1.cpp @ 12

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

added boost

File size: 5.9 KB
Line 
1// Copyright David Abrahams 2001.
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
7#include <boost/python/def.hpp>
8#include <boost/python/module.hpp>
9#include <boost/python/class.hpp>
10#include <boost/python/lvalue_from_pytype.hpp>
11#include <boost/python/copy_const_reference.hpp>
12#include <boost/python/return_value_policy.hpp>
13#include <boost/python/to_python_converter.hpp>
14#include <boost/python/errors.hpp>
15#include <boost/python/manage_new_object.hpp>
16#include <string.h>
17#include "simple_type.hpp"
18#include "complicated.hpp"
19
20// Declare some straightforward extension types
21extern "C" void
22dealloc(PyObject* self)
23{
24    PyObject_Del(self);
25}
26
27// Noddy is a type we got from one of the Python sample files
28struct NoddyObject : PyObject
29{
30    int x;
31};
32
33PyTypeObject NoddyType = {
34    PyObject_HEAD_INIT(NULL)
35    0,
36    "Noddy",
37    sizeof(NoddyObject),
38    0,
39    dealloc, /*tp_dealloc*/
40    0,          /*tp_print*/
41    0,          /*tp_getattr*/
42    0,          /*tp_setattr*/
43    0,          /*tp_compare*/
44    0,          /*tp_repr*/
45    0,          /*tp_as_number*/
46    0,          /*tp_as_sequence*/
47    0,          /*tp_as_mapping*/
48    0,          /*tp_hash */
49};
50
51// Create a Noddy containing 42
52PyObject* new_noddy()
53{
54    NoddyObject* noddy = PyObject_New(NoddyObject, &NoddyType);
55    noddy->x = 42;
56    return (PyObject*)noddy;
57}
58
59// Simple is a wrapper around a struct simple, which just contains a char*
60struct SimpleObject
61{
62    PyObject_HEAD
63    simple x;
64};
65
66struct extract_simple_object
67{
68    static simple& execute(SimpleObject& o) { return o.x; }
69};
70
71PyTypeObject SimpleType = {
72    PyObject_HEAD_INIT(NULL)
73    0,
74    "Simple",
75    sizeof(SimpleObject),
76    0,
77    dealloc,    /*tp_dealloc*/
78    0,          /*tp_print*/
79    0,          /*tp_getattr*/
80    0,          /*tp_setattr*/
81    0,          /*tp_compare*/
82    0,          /*tp_repr*/
83    0,          /*tp_as_number*/
84    0,          /*tp_as_sequence*/
85    0,          /*tp_as_mapping*/
86    0,          /*tp_hash */
87};
88
89// Create a Simple containing "hello, world"
90PyObject* new_simple()
91{
92    SimpleObject* simple = PyObject_New(SimpleObject, &SimpleType);
93    simple->x.s = "hello, world";
94    return (PyObject*)simple;
95}
96
97//
98// Declare some wrappers/unwrappers to test the low-level conversion
99// mechanism.
100//
101using boost::python::to_python_converter;
102
103// Wrap a simple by copying it into a Simple
104struct simple_to_python
105    : to_python_converter<simple, simple_to_python>
106{
107    static PyObject* convert(simple const& x)
108    {
109        SimpleObject* p = PyObject_New(SimpleObject, &SimpleType);
110        p->x = x;
111        return (PyObject*)p;
112    }
113};
114
115struct int_from_noddy
116{
117    static int& execute(NoddyObject& p)
118    {
119        return p.x;
120    }
121};
122
123//
124// Some C++ functions to expose to Python
125//
126
127// Returns the length of s's held string
128int f(simple const& s)
129{
130    return strlen(s.s);
131}
132
133int f_mutable_ref(simple& s)
134{
135    return strlen(s.s);
136}
137
138int f_mutable_ptr(simple* s)
139{
140    return strlen(s->s);
141}
142
143int f_const_ptr(simple const* s)
144{
145    return strlen(s->s);
146}
147
148int f2(SimpleObject const& s)
149{
150    return strlen(s.x.s);
151}
152
153// A trivial passthru function for simple objects
154simple const& g(simple const& x)
155{
156    return x;
157}
158
159struct A
160{
161    A() : x(0) {}
162    virtual ~A() {}
163    char const* name() { return "A"; }
164    int x;
165};
166
167struct B : A
168{
169    B() : x(1) {}
170    static char const* name(B*) { return "B"; }
171    int x;
172};
173
174struct C : A
175{
176    C() : x(2) {}
177    char const* name() { return "C"; }
178    virtual ~C() {}
179    int x;
180};
181
182struct D : B, C
183{
184    D() : x(3) {}
185    char const* name() { return "D"; }
186    int x;
187};
188
189A take_a(A const& a) { return a; }
190B take_b(B& b) { return b; }
191C take_c(C* c) { return *c; }
192D take_d(D* const& d) { return *d; }
193
194D take_d_shared_ptr(boost::shared_ptr<D> d) { return *d; }
195
196boost::shared_ptr<A> d_factory() { return boost::shared_ptr<B>(new D); }
197
198struct Unregistered {};
199Unregistered make_unregistered(int) { return Unregistered(); }
200
201Unregistered* make_unregistered2(int) { return new Unregistered; }
202
203BOOST_PYTHON_MODULE(m1)
204{
205    using namespace boost::python;
206    using boost::shared_ptr;
207   
208    simple_to_python();
209
210    lvalue_from_pytype<int_from_noddy,&NoddyType>();
211
212    lvalue_from_pytype<
213#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300 // doesn't support non-type member pointer parameters
214        extract_member<SimpleObject, simple, &SimpleObject::x>
215#else
216        extract_simple_object
217#endif
218        , &SimpleType
219        >();
220
221    lvalue_from_pytype<extract_identity<SimpleObject>,&SimpleType>();
222   
223    def("new_noddy", new_noddy);
224    def("new_simple", new_simple);
225
226    def("make_unregistered", make_unregistered);
227    def("make_unregistered2", make_unregistered2, return_value_policy<manage_new_object>());
228
229      // Expose f() in all its variations
230    def("f", f);
231    def("f_mutable_ref", f_mutable_ref);
232    def("f_mutable_ptr", f_mutable_ptr);
233    def("f_const_ptr", f_const_ptr);
234
235    def("f2", f2);
236       
237      // Expose g()
238    def("g", g , return_value_policy<copy_const_reference>()
239        );
240
241    def("take_a", take_a);
242    def("take_b", take_b);
243    def("take_c", take_c);
244    def("take_d", take_d);
245
246
247    def("take_d_shared_ptr", take_d_shared_ptr);
248    def("d_factory", d_factory);
249
250    class_<A, shared_ptr<A> >("A")
251        .def("name", &A::name)
252        ;
253
254    // sequence points don't ensure that "A" is constructed before "B"
255    // or "C" below if we make them part of the same chain
256    class_<B,bases<A> >("B")
257        .def("name", &B::name)
258        ;
259       
260    class_<C,bases<A> >("C")
261        .def("name", &C::name)
262        ;
263
264    class_<D, bases<B,C> >("D")
265        .def("name", &D::name)
266        ;
267
268    class_<complicated>("complicated",
269                        init<simple const&,int>())
270        .def(init<simple const&>())
271        .def("get_n", &complicated::get_n)
272        ;
273}
274
275#include "module_tail.cpp"
Note: See TracBrowser for help on using the repository browser.