forked from boostorg/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm1.cpp
More file actions
336 lines (290 loc) · 8.16 KB
/
m1.cpp
File metadata and controls
336 lines (290 loc) · 8.16 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright David Abrahams 2001. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#include "simple_type.hpp"
#include "complicated.hpp"
#include <boost/python/converter/wrapper.hpp>
#include <boost/python/converter/unwrapper.hpp>
#include <boost/python/detail/config.hpp>
#include <boost/python/convert.hpp>
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/object/value_holder.hpp>
#include <boost/python/object/class.hpp>
#include <boost/python/object/inheritance.hpp>
#include <boost/python/converter/class.hpp>
#include <boost/python/make_function.hpp>
#include <boost/python/errors.hpp>
#include <boost/mpl/type_list.hpp>
#include <string.h>
// Declare some straightforward extension types
extern "C" void
dealloc(PyObject* self)
{
PyObject_Del(self);
}
// Noddy is a type we got from one of the Python sample files
struct NoddyObject : PyObject
{
int x;
};
PyTypeObject NoddyType = {
PyObject_HEAD_INIT(NULL)
0,
"Noddy",
sizeof(NoddyObject),
0,
dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
};
// Create a Noddy containing 42
PyObject* new_noddy()
{
NoddyObject* noddy = PyObject_New(NoddyObject, &NoddyType);
noddy->x = 42;
return (PyObject*)noddy;
}
// Simple is a wrapper around a struct simple, which just contains a char*
struct SimpleObject : PyObject
{
simple x;
};
PyTypeObject SimpleType = {
PyObject_HEAD_INIT(NULL)
0,
"Simple",
sizeof(SimpleObject),
0,
dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
};
// Create a Simple containing "hello, world"
PyObject* new_simple()
{
SimpleObject* simple = PyObject_New(SimpleObject, &SimpleType);
simple->x.s = "hello, world";
return (PyObject*)simple;
}
//
// Declare some wrappers/unwrappers to test the low-level conversion
// mechanism. See boost/python/converter/source.hpp,target.hpp for a
// description of how the type parameters to wrapper<> and unwrapper<>
// are selected.
//
// Wrap an int by converting it to a Python Int
struct int_wrapper
: boost::python::converter::wrapper<int const&>
{
PyObject* convert(int const& x) const
{
return PyInt_FromLong(x);
}
};
// Wrap a simple by converting it to a Simple
struct simple_wrapper
: boost::python::converter::wrapper<simple const&>
{
PyObject* convert(simple const& x) const
{
SimpleObject* p = PyObject_New(SimpleObject, &SimpleType);
p->x = x;
return p;
}
};
// wrap a mutable reference to a simple by converting it to a
// Simple. Normally we wouldn't do it this way, since modifications to
// the result clearly don't change the original object, but here we're
// just proving that the mechanism works.
struct simple_ref_wrapper
: boost::python::converter::wrapper<simple&>
{
PyObject* convert(simple& x) const
{
SimpleObject* p = PyObject_New(SimpleObject, &SimpleType);
p->x = x;
return p;
}
};
// extract an int from a Python Int by converting it to an int. Since
// int is a scalar type, we convert by-value. Since Python Ints are
// immutable, there's no non-const reference converter.
struct native_int_unwrapper
: boost::python::converter::unwrapper<int>
{
void* can_convert(PyObject* p) const
{
return PyInt_Check(p) ? non_null : 0;
}
int convert(PyObject* p, void*, boost::type<int>) const
{
return PyInt_AsLong(p);
}
};
// Extract an int from a Noddy
struct noddy_int_unwrapper
: boost::python::converter::unwrapper<int>
{
void* can_convert(PyObject* p) const
{
return p->ob_type == &NoddyType ? non_null : 0;
}
int convert(PyObject* p, void*, boost::type<int>) const
{
return static_cast<NoddyObject*>(p)->x;
}
};
// Extract a mutable reference to an int from a Noddy.
struct noddy_int_ref_unwrapper
: boost::python::converter::unwrapper<int&>
{
void* can_convert(PyObject* p) const
{
return p->ob_type == &NoddyType ? non_null : 0;
}
int& convert(PyObject* p, void*, boost::type<int&>) const
{
return static_cast<NoddyObject*>(p)->x;
}
};
// Extract a mutable reference to a simple from a Simple
struct simple_ref_unwrapper
: boost::python::converter::unwrapper<simple&>
{
void* can_convert(PyObject* p) const
{
return p->ob_type == &SimpleType ? non_null : 0;
}
simple& convert(PyObject* p, void*, boost::type<simple&>) const
{
return static_cast<SimpleObject*>(p)->x;
}
};
// Extract a const reference to a simple from a Simple
struct simple_const_ref_unwrapper
: boost::python::converter::unwrapper<simple const&>
{
void* can_convert(PyObject* p) const
{
return p->ob_type == &SimpleType ? non_null : 0;
}
simple const& convert(PyObject* p, void*, boost::type<simple const&>) const
{
return static_cast<SimpleObject*>(p)->x;
}
};
//
// Some C++ functions to expose to Python
//
// Returns the length of s's held string
int f(simple const& s)
{
return strlen(s.s);
}
// A trivial passthru function for simple objects
simple const& g(simple const& x)
{
return x;
}
struct A
{
A() : x(0) {}
char const* name() { return "A"; }
int x;
};
struct B : A
{
B() : x(1) {}
char const* name() { return "B"; }
int x;
};
struct C : A
{
C() : x(2) {}
char const* name() { return "C"; }
virtual ~C() {}
int x;
};
struct D : B, C
{
D() : x(3) {}
char const* name() { return "D"; }
int x;
};
int take_a(A const& a) { return a.x; }
int take_b(B const& b) { return b.x; }
int take_c(C const& c) { return c.x; }
int take_d(D const& d) { return d.x; }
BOOST_PYTHON_MODULE_INIT(m1)
{
using boost::python::module;
using boost::python::class_;
module m1("m1");
// Create the converters; they are self-registering/unregistering.
static int_wrapper wrap_int;
static simple_wrapper wrap_simple;
static native_int_unwrapper unwrap_int1;
static noddy_int_unwrapper unwrap_int2;
static noddy_int_ref_unwrapper unwrap_int3;
static simple_ref_unwrapper unwrap_simple;
static simple_const_ref_unwrapper unwrap_simple_const_ref;
static simple_ref_wrapper wrap_simple_ref;
// This unwrapper extracts pointers and references to the "complicated" class.
// static boost::python::converter::class_unwrapper<complicated> unwrap_complicated;
// Insert the extension metaclass object
m1.add(
boost::python::objects::class_metatype()
, "xclass");
// Insert the base class for all extension classes
m1.add(boost::python::objects::class_type()
, "xinst");
m1.def(new_noddy, "new_noddy");
m1.def(new_simple, "new_simple");
// Expose f()
m1.def(f, "f");
// Expose g()
m1.def(g, "g");
m1.def(take_a, "take_a");
m1.def(take_b, "take_b");
m1.def(take_c, "take_c");
m1.def(take_d, "take_d");
class_<A>(m1, "A")
.def_init()
.def(&A::name, "name")
;
class_<B,bases<A> >(m1, "B")
.def_init()
.def(&B::name, "name")
;
class_<C,bases<A> >(m1, "C")
.def_init()
.def(&C::name, "name")
;
class_<D,bases<B,C> >(m1, "D")
.def_init()
.def(&D::name, "name")
;
class_<complicated>(m1, "complicated")
.def_init(args<simple const&,int>())
.def_init(args<simple const&>())
.def(&complicated::get_n, "get_n")
;
}
#include "module_tail.cpp"