Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/test/m1.cpp @ 29

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

updated boost from 1_33_1 to 1_34_1

File size: 8.0 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    0,          /* tp_call */
50    0,          /* tp_str */
51    0,          /* tp_getattro */
52    0,          /* tp_setattro */
53    0,          /* tp_as_buffer */
54    0,          /* tp_flags */
55    0,          /* tp_doc */
56    0,          /* tp_traverse */
57    0,          /* tp_clear */
58    0,          /* tp_richcompare */
59    0,          /* tp_weaklistoffset */
60    0,          /* tp_iter */
61    0,          /* tp_iternext */
62    0,          /* tp_methods */
63    0,          /* tp_members */
64    0,          /* tp_getset */
65    0,          /* tp_base */
66    0,          /* tp_dict */
67    0,          /* tp_descr_get */
68    0,          /* tp_descr_set */
69    0,          /* tp_dictoffset */
70    0,          /* tp_init */
71    0,          /* tp_alloc */
72    0,          /* tp_new */
73    0,          /* tp_free */
74    0,          /* tp_is_gc */
75    0,          /* tp_bases */
76    0,          /* tp_mro */
77    0,          /* tp_cache */
78    0,          /* tp_subclasses */
79    0,          /* tp_weaklist */
80#if PYTHON_API_VERSION >= 1012
81    0           /* tp_del */
82#endif
83};
84
85// Create a Noddy containing 42
86PyObject* new_noddy()
87{
88    NoddyObject* noddy = PyObject_New(NoddyObject, &NoddyType);
89    noddy->x = 42;
90    return (PyObject*)noddy;
91}
92
93// Simple is a wrapper around a struct simple, which just contains a char*
94struct SimpleObject
95{
96    PyObject_HEAD
97    simple x;
98};
99
100struct extract_simple_object
101{
102    static simple& execute(SimpleObject& o) { return o.x; }
103};
104
105PyTypeObject SimpleType = {
106    PyObject_HEAD_INIT(NULL)
107    0,
108    "Simple",
109    sizeof(SimpleObject),
110    0,
111    dealloc,    /* tp_dealloc */
112    0,          /* tp_print */
113    0,          /* tp_getattr */
114    0,          /* tp_setattr */
115    0,          /* tp_compare */
116    0,          /* tp_repr */
117    0,          /* tp_as_number */
118    0,          /* tp_as_sequence */
119    0,          /* tp_as_mapping */
120    0,          /* tp_hash */
121    0,          /* tp_call */
122    0,          /* tp_str */
123    0,          /* tp_getattro */
124    0,          /* tp_setattro */
125    0,          /* tp_as_buffer */
126    0,          /* tp_flags */
127    0,          /* tp_doc */
128    0,          /* tp_traverse */
129    0,          /* tp_clear */
130    0,          /* tp_richcompare */
131    0,          /* tp_weaklistoffset */
132    0,          /* tp_iter */
133    0,          /* tp_iternext */
134    0,          /* tp_methods */
135    0,          /* tp_members */
136    0,          /* tp_getset */
137    0,          /* tp_base */
138    0,          /* tp_dict */
139    0,          /* tp_descr_get */
140    0,          /* tp_descr_set */
141    0,          /* tp_dictoffset */
142    0,          /* tp_init */
143    0,          /* tp_alloc */
144    0,          /* tp_new */
145    0,          /* tp_free */
146    0,          /* tp_is_gc */
147    0,          /* tp_bases */
148    0,          /* tp_mro */
149    0,          /* tp_cache */
150    0,          /* tp_subclasses */
151    0,          /* tp_weaklist */
152#if PYTHON_API_VERSION >= 1012
153    0           /* tp_del */
154#endif
155};
156
157// Create a Simple containing "hello, world"
158PyObject* new_simple()
159{
160    SimpleObject* simple = PyObject_New(SimpleObject, &SimpleType);
161    simple->x.s = "hello, world";
162    return (PyObject*)simple;
163}
164
165//
166// Declare some wrappers/unwrappers to test the low-level conversion
167// mechanism.
168//
169using boost::python::to_python_converter;
170
171// Wrap a simple by copying it into a Simple
172struct simple_to_python
173    : to_python_converter<simple, simple_to_python>
174{
175    static PyObject* convert(simple const& x)
176    {
177        SimpleObject* p = PyObject_New(SimpleObject, &SimpleType);
178        p->x = x;
179        return (PyObject*)p;
180    }
181};
182
183struct int_from_noddy
184{
185    static int& execute(NoddyObject& p)
186    {
187        return p.x;
188    }
189};
190
191//
192// Some C++ functions to expose to Python
193//
194
195// Returns the length of s's held string
196int f(simple const& s)
197{
198    return strlen(s.s);
199}
200
201int f_mutable_ref(simple& s)
202{
203    return strlen(s.s);
204}
205
206int f_mutable_ptr(simple* s)
207{
208    return strlen(s->s);
209}
210
211int f_const_ptr(simple const* s)
212{
213    return strlen(s->s);
214}
215
216int f2(SimpleObject const& s)
217{
218    return strlen(s.x.s);
219}
220
221// A trivial passthru function for simple objects
222simple const& g(simple const& x)
223{
224    return x;
225}
226
227struct A
228{
229    A() : x(0) {}
230    virtual ~A() {}
231    char const* name() { return "A"; }
232    int x;
233};
234
235struct B : A
236{
237    B() : x(1) {}
238    static char const* name(B*) { return "B"; }
239    int x;
240};
241
242struct C : A
243{
244    C() : x(2) {}
245    char const* name() { return "C"; }
246    virtual ~C() {}
247    int x;
248};
249
250struct D : B, C
251{
252    D() : x(3) {}
253    char const* name() { return "D"; }
254    int x;
255};
256
257A take_a(A const& a) { return a; }
258B take_b(B& b) { return b; }
259C take_c(C* c) { return *c; }
260D take_d(D* const& d) { return *d; }
261
262D take_d_shared_ptr(boost::shared_ptr<D> d) { return *d; }
263
264boost::shared_ptr<A> d_factory() { return boost::shared_ptr<B>(new D); }
265
266struct Unregistered {};
267Unregistered make_unregistered(int) { return Unregistered(); }
268
269Unregistered* make_unregistered2(int) { return new Unregistered; }
270
271BOOST_PYTHON_MODULE(m1)
272{
273    using namespace boost::python;
274    using boost::shared_ptr;
275   
276    simple_to_python();
277
278    lvalue_from_pytype<int_from_noddy,&NoddyType>();
279
280    lvalue_from_pytype<
281#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300 // doesn't support non-type member pointer parameters
282        extract_member<SimpleObject, simple, &SimpleObject::x>
283#else
284        extract_simple_object
285#endif
286        , &SimpleType
287        >();
288
289    lvalue_from_pytype<extract_identity<SimpleObject>,&SimpleType>();
290   
291    def("new_noddy", new_noddy);
292    def("new_simple", new_simple);
293
294    def("make_unregistered", make_unregistered);
295    def("make_unregistered2", make_unregistered2, return_value_policy<manage_new_object>());
296
297      // Expose f() in all its variations
298    def("f", f);
299    def("f_mutable_ref", f_mutable_ref);
300    def("f_mutable_ptr", f_mutable_ptr);
301    def("f_const_ptr", f_const_ptr);
302
303    def("f2", f2);
304       
305      // Expose g()
306    def("g", g , return_value_policy<copy_const_reference>()
307        );
308
309    def("take_a", take_a);
310    def("take_b", take_b);
311    def("take_c", take_c);
312    def("take_d", take_d);
313
314
315    def("take_d_shared_ptr", take_d_shared_ptr);
316    def("d_factory", d_factory);
317
318    class_<A, shared_ptr<A> >("A")
319        .def("name", &A::name)
320        ;
321
322    // sequence points don't ensure that "A" is constructed before "B"
323    // or "C" below if we make them part of the same chain
324    class_<B,bases<A> >("B")
325        .def("name", &B::name)
326        ;
327       
328    class_<C,bases<A> >("C")
329        .def("name", &C::name)
330        ;
331
332    class_<D, bases<B,C> >("D")
333        .def("name", &D::name)
334        ;
335
336    class_<complicated>("complicated",
337                        init<simple const&,int>())
338        .def(init<simple const&>())
339        .def("get_n", &complicated::get_n)
340        ;
341}
342
343#include "module_tail.cpp"
Note: See TracBrowser for help on using the repository browser.