Debian Codegraph @

tests/foomodulegen.py

raw symbols SCIP
1 #! /usr/bin/env python
2 from __future__ import unicode_literals, print_function
3
4 import sys
5 import re
6
7 import pybindgen
8 import pybindgen.utils
9 from pybindgen.typehandlers import base as typehandlers
10 from pybindgen import ReturnValue, Parameter, Module, Function, FileCodeSink
11 from pybindgen import CppMethod, CppConstructor, CppClass, Enum
12 from pybindgen.function import CustomFunctionWrapper
13 from pybindgen.cppmethod import CustomCppMethodWrapper
14 from pybindgen import cppclass
15
16 from pybindgen import param, retval
17
18 import foomodulegen_common
19
20
21
22 def my_module_gen(out_file):
23
24 mod = Module('foo')
25 foomodulegen_common.customize_module_pre(mod)
26
27 mod.add_include ('"foo.h"')
28
29 mod.add_function('TypeNameGet',
30 'std::string',
31 [],
32 custom_name='IntegerTypeNameGet', template_parameters=['int'])
33
34 Foo = mod.add_class('Foo', automatic_type_narrowing=True)
35
36 Foo.add_static_attribute('instance_count', ReturnValue.new('int'))
37 Foo.add_constructor([Parameter.new('std::string', 'datum')])
38 Foo.add_constructor([])
39 Foo.add_constructor([Parameter.new('const Foo&', 'foo')])
40 Foo.add_method('get_datum', ReturnValue.new('const std::string'), [])
41 Foo.add_method('is_initialized', ReturnValue.new('bool'), [], is_const=True)
42 Foo.add_output_stream_operator()
43 Foo.add_method('add_sub', ReturnValue.new('int'), [
44 Parameter.new('int', 'a'),
45 Parameter.new('int', 'b', default_value='3'),
46 Parameter.new('bool', 'subtract', default_value='false')
47 ], is_static=True)
48 Foo.add_custom_instance_attribute("is_unique", "bool", getter="is_unique", is_const=True)
49
50 Zoo = mod.add_class('Zoo', automatic_type_narrowing=True)
51 Zoo.add_constructor([Parameter.new('std::string', 'datum')])
52 Zoo.add_constructor([])
53 Zoo.add_method('get_datum', ReturnValue.new('std::string'), [])
54 Zoo.implicitly_converts_to(Foo)
55
56 Foobar = mod.add_class('Foobar', allow_subclassing=True)
57 Foobar.add_static_attribute('instance_count', ReturnValue.new('int'))
58
59
60 Bar = mod.add_class('Bar', parent=Foo)
61 Bar.inherit_default_constructors()
62 ## a static method..
63 Bar.add_method('Hooray', ReturnValue.new('std::string'), [], is_static=True)
64
65 ## to test RTTI with a hidden subclass
66 mod.add_function('get_hidden_subclass_pointer',
67 ReturnValue.new('Foo*', caller_owns_return=True),
68 [])
69
70
71 ## Zbr is a reference counted class
72 Zbr = mod.add_class('Zbr', memory_policy=cppclass.ReferenceCountingMethodsPolicy(
73 incref_method='Ref', decref_method='Unref', peekref_method="GetReferenceCount"),
74 allow_subclassing=True)
75
76 def helper_class_hook(helper_class):
77 helper_class.add_custom_method(
78 declaration="static int custom_method_added_by_a_hook(int x);",
79 body="""
80 int %s::custom_method_added_by_a_hook(int x)
81 {
82 return x + 1;
83 }
84 """ % helper_class.name)
85 helper_class.add_post_generation_code("// this comment was written by a helper class hook function")
86 Zbr.add_helper_class_hook(helper_class_hook)
87
88 Zbr.add_constructor([])
89 Zbr.add_constructor([Parameter.new('std::string', 'datum')])
90 Zbr.add_method('get_datum', ReturnValue.new('std::string'), [])
91 Zbr.add_method('get_int', ReturnValue.new('int'), [Parameter.new('int', 'x')],
92 is_virtual=True)
93 Zbr.add_static_attribute('instance_count', ReturnValue.new('int'))
94 Zbr.add_method('get_value', ReturnValue.new('int'), [Parameter.new('int*', 'x', direction=Parameter.DIRECTION_OUT)])
95
96 mod.add_function('store_zbr', None,
97 [Parameter.new('Zbr*', 'zbr', transfer_ownership=True)])
98 mod.add_function('invoke_zbr', ReturnValue.new('int'), [Parameter.new('int', 'x')])
99 mod.add_function('delete_stored_zbr', None, [])
100
101
102 mod.add_function('print_something', ReturnValue.new('int'),
103 [Parameter.new('const char*', 'message')],
104 deprecated=True)
105 mod.add_function('print_something_else', ReturnValue.new('int'),
106 [Parameter.new('const char*', 'message2')])
107
108 ## test overloaded functions
109 mod.add_function('get_int_from_string', ReturnValue.new('int'),
110 [Parameter.new('const char*', 'from_string'),
111 Parameter.new('int', 'multiplier', default_value='1')], custom_name="get_int")
112 mod.add_function('get_int_from_float', ReturnValue.new('int'),
113 [Parameter.new('double', 'from_float'),
114 Parameter.new('int', 'multiplier', default_value='1')],
115 custom_name="get_int")
116
117
118
119 SomeObject = mod.add_class('SomeObject', allow_subclassing=True)
120
121 SomeObject.add_instance_attribute('foo', ReturnValue.new('Foo'),
122 getter='get_foo_value',
123 setter='set_foo_value')
124 SomeObject.add_instance_attribute('m_prefix', ReturnValue.new('std::string'))
125 SomeObject.add_static_attribute('staticData', ReturnValue.new('std::string'))
126
127 SomeObject.add_static_attribute('instance_count', ReturnValue.new('int'))
128
129 SomeObject.add_method('add_prefix', ReturnValue.new('int'),
130 [Parameter.new('std::string&', 'message',
131 direction=Parameter.DIRECTION_INOUT)])
132 SomeObject.add_constructor([Parameter.new('std::string', 'prefix')])
133 SomeObject.add_constructor([Parameter.new('int', 'prefix_len')])
134
135 SomeObject.add_method('operator()', ReturnValue.new('int'),
136 [Parameter.new('std::string&', 'message',
137 direction=Parameter.DIRECTION_INOUT)],
138 custom_name='__call__')
139
140
141 # --- some virtual methods ---
142 SomeObject.add_method('get_prefix', ReturnValue.new('std::string'), [],
143 is_virtual=True, is_const=True)
144
145 SomeObject.add_method('get_prefix_with_foo_value', ReturnValue.new('std::string'),
146 [Parameter.new('Foo', 'foo')],
147 is_virtual=True, is_const=True)
148
149 SomeObject.add_method('get_prefix_with_foo_ref',
150 ReturnValue.new('std::string'),
151 [Parameter.new('const Foo&', 'foo',
152 direction=Parameter.DIRECTION_INOUT)],
153 is_virtual=True, is_const=True)
154
155 SomeObject.add_method('get_prefix_with_foo_ptr',
156 ReturnValue.new('std::string'),
157 [Parameter.new('const Foo*', 'foo', transfer_ownership=False)],
158 is_virtual=True, is_const=True)
159
160 ## overloaded virtual methods
161 SomeObject.add_method('get_something',
162 ReturnValue.new('std::string'),
163 [],
164 is_virtual=True, is_const=True)
165 SomeObject.add_method('get_something',
166 ReturnValue.new('std::string'),
167 [Parameter.new('int', 'x')],
168 is_virtual=True, is_const=True)
169
170 SomeObject.add_method('set_pyobject', None,
171 [Parameter.new('PyObject*', 'pyobject', transfer_ownership=False)],
172 is_virtual=True)
173 SomeObject.add_method('get_pyobject',
174 ReturnValue.new('PyObject*', caller_owns_return=True),
175 [],
176 is_virtual=True)
177
178 ## add a function that appears as a method of an object
179 SomeObject.add_function_as_method('some_object_get_something_prefixed',
180 ReturnValue.new('std::string'),
181 [Parameter.new('const SomeObject*', 'obj', transfer_ownership=False),
182 Parameter.new('std::string', 'something')],
183 custom_name='get_something_prefixed')
184
185 ## add a function that appears as a method of an object
186 SomeObject.add_function_as_method('some_object_val_get_something_prefixed',
187 ReturnValue.new('std::string'),
188 [Parameter.new('SomeObject', 'obj'),
189 Parameter.new('std::string', 'something')],
190 custom_name='val_get_something_prefixed')
191
192 ## add a function that appears as a method of an object
193 SomeObject.add_function_as_method('some_object_ref_get_something_prefixed',
194 ReturnValue.new('std::string'),
195 [Parameter.new('const SomeObject&', 'obj'),
196 Parameter.new('std::string', 'something')],
197 custom_name='ref_get_something_prefixed')
198
199 # ---
200 SomeObject.add_method('call_get_prefix', ReturnValue.new('std::string'), [])
201
202 SomeObject.add_method('set_foo_value', None, [Parameter.new('Foo', 'foo')])
203 SomeObject.add_method('get_foo_value', ReturnValue.new('Foo'), [])
204
205 SomeObject.add_method('set_foo_ptr', ReturnValue.new('void'),
206 [Parameter.new('Foo*', 'foo', transfer_ownership=True)])
207 SomeObject.add_method('set_foo_shared_ptr', ReturnValue.new('void'),
208 [Parameter.new('Foo*', 'foo', transfer_ownership=False)])
209
210 SomeObject.add_method('get_foo_shared_ptr', ReturnValue.new('const Foo*', caller_owns_return=False), [])
211 SomeObject.add_method('get_foo_ptr', ReturnValue.new('Foo*', caller_owns_return=True), [])
212
213 SomeObject.add_method('set_foo_by_ref', ReturnValue.new('void'),
214 [Parameter.new('Foo&', 'foo', direction=Parameter.DIRECTION_IN)])
215 SomeObject.add_method('get_foo_by_ref', ReturnValue.new('void'),
216 [Parameter.new('Foo&', 'foo', direction=Parameter.DIRECTION_OUT)])
217
218 ## custodian/ward tests
219 SomeObject.add_method('get_foobar_with_self_as_custodian',
220 ReturnValue.new('Foobar*', custodian=0, reference_existing_object=True), [])
221 SomeObject.add_method('get_foobar_with_other_as_custodian',
222 ReturnValue.new('Foobar*', custodian=1, reference_existing_object=True),
223 [Parameter.new('SomeObject*', 'other', transfer_ownership=False)])
224 SomeObject.add_method('set_foobar_with_self_as_custodian', ReturnValue.new('void'),
225 [Parameter.new('Foobar*', 'foobar',
226 transfer_ownership=True, custodian=0)])
227
228 mod.add_function('get_foobar_with_other_as_custodian',
229 ReturnValue.new('Foobar*', custodian=1, reference_existing_object=True),
230 [Parameter.new('SomeObject*', 'other', transfer_ownership=False)])
231
232 mod.add_function('create_new_foobar', ReturnValue.new('Foobar*', caller_owns_return=True), [])
233
234 mod.add_function('set_foobar_with_other_as_custodian', ReturnValue.new('void'),
235 [Parameter.new('Foobar*', 'foobar', transfer_ownership=True, custodian=2),
236 Parameter.new('SomeObject*', 'other', transfer_ownership=False)])
237
238 mod.add_function('set_foobar_with_return_as_custodian',
239 ReturnValue.new('SomeObject*', caller_owns_return=True),
240 [Parameter.new('Foobar*', 'foobar',
241 transfer_ownership=True, custodian=-1)])
242
243
244 ## get/set recfcounted object Zbr
245 SomeObject.add_method('get_zbr', ReturnValue.new('Zbr*', caller_owns_return=True), [])
246 SomeObject.add_method('get_internal_zbr', ReturnValue.new('Zbr*', caller_owns_return=True), [])
247 SomeObject.add_method('peek_zbr', ReturnValue.new('Zbr*', caller_owns_return=False), [])
248 SomeObject.add_method('set_zbr_transfer', ReturnValue.new('void'),
249 [Parameter.new('Zbr*', 'zbr', transfer_ownership=True)])
250 SomeObject.add_method('set_zbr_shared', ReturnValue.new('void'),
251 [Parameter.new('Zbr*', 'zbr', transfer_ownership=False)])
252
253 ## methods with transformed types
254 SomeObject.add_method('set_zbr_pholder', ReturnValue.new('void'),
255 [Parameter.new('PointerHolder<Zbr>', 'zbr')])
256 SomeObject.add_method('get_zbr_pholder',
257 ReturnValue.new('PointerHolder<Zbr>'), [])
258
259 ## test overloaded methods
260 SomeObject.add_method('get_int', ReturnValue.new('int'),
261 [Parameter.new('const char*', 'from_string')],
262 custom_name="get_int")
263 SomeObject.add_method('get_int', ReturnValue.new('int'),
264 [Parameter.new('double', 'from_float')],
265 custom_name="get_int")
266
267 # Bug #508577
268 SomeObject.add_method('protected_method_that_is_not_virtual',
269 ReturnValue.new('std::string'),
270 [Parameter.new('std::string', 'arg')],
271 is_const=True, visibility='protected')
272
273 SomeObject.add_method('method_returning_cstring', ReturnValue.new('const char *'),
274 [], is_virtual=True, is_const=True)
275
276
277 mod.add_function('store_some_object', ReturnValue.new('void'),
278 [Parameter.new('SomeObject*', 'obj', transfer_ownership=True)])
279 mod.add_function('invoke_some_object_get_prefix', ReturnValue.new('std::string'),
280 [])
281 mod.add_function('take_some_object', ReturnValue.new('SomeObject*', caller_owns_return=True), [])
282 mod.add_function('delete_some_object', ReturnValue.new('void'), [])
283
284 xpto = mod.add_cpp_namespace("xpto")
285 xpto.add_function('some_function', ReturnValue.new('std::string'), [])
286
287 ## enums..
288 xpto.add_enum('FooType', ['FOO_TYPE_AAA', 'FOO_TYPE_BBB', 'FOO_TYPE_CCC'])
289 xpto.add_function('get_foo_type', ReturnValue.new('FooType'), [])
290 xpto.add_function('set_foo_type', ReturnValue.new('void'), [Parameter.new("FooType", 'type')])
291 xpto.add_function('set_foo_type_inout', ReturnValue.new('void'),
292 [Parameter.new("FooType&", 'type', direction=Parameter.DIRECTION_INOUT)])
293 xpto.add_function('set_foo_type_ptr', ReturnValue.new('void'),
294 [Parameter.new("FooType*", 'type', direction=Parameter.DIRECTION_INOUT)])
295
296
297 xpto_SomeClass = xpto.add_class('SomeClass', docstring="This is the docstring for SomeClass")
298 xpto_SomeClass.add_constructor([])
299
300 xpto.add_typedef(Foo, 'FooXpto')
301 xpto.add_function('get_foo_datum', 'std::string', [Parameter.new('const xpto::FooXpto&', 'foo')])
302
303 typehandlers.add_type_alias('uint32_t', 'xpto::FlowId')
304 xpto.add_function('get_flow_id', 'xpto::FlowId', [Parameter.new('xpto::FlowId', 'flowId')])
305
306 # bug #798383
307 XptoClass = xpto.add_struct('XptoClass')
308 XptoClass.add_method("GetSomeClass", retval("xpto::SomeClass*", caller_owns_return=True), [])
309
310
311 ## ---- some implicity conversion APIs
312 mod.add_function('function_that_takes_foo', ReturnValue.new('void'),
313 [Parameter.new('Foo', 'foo')])
314 mod.add_function('function_that_returns_foo', ReturnValue.new('Foo'), [])
315
316 cls = mod.add_class('ClassThatTakesFoo')
317 cls.add_constructor([Parameter.new('Foo', 'foo')])
318 cls.add_method('get_foo', ReturnValue.new('Foo'), [])
319
320 cls = mod.add_class('SingletonClass', is_singleton=True)
321 cls.add_method('GetInstance', ReturnValue.new('SingletonClass*', caller_owns_return=True),
322 [], is_static=True)
323
324
325 ## A class that has no public default constructor...
326 cls = mod.add_class('InterfaceId', is_singleton=True)
327 ## A function that returns such a class...
328 mod.add_function('make_interface_id', ReturnValue.new('InterfaceId'), [])
329
330
331 ## A class the cannot be constructed; this will cause late CodeGenerationError's
332 cls = mod.add_class('CannotBeConstructed')
333 cls.set_cannot_be_constructed("no reason")
334 cls.add_method('get_value', ReturnValue.new('CannotBeConstructed'),
335 [], is_static=True)
336 cls.add_method('get_ptr', ReturnValue.new('CannotBeConstructed*', caller_owns_return=True),
337 [], is_static=True)
338 mod.add_function('get_cannot_be_constructed_value',
339 ReturnValue.new('CannotBeConstructed'),
340 [])
341 mod.add_function('get_cannot_be_constructed_ptr',
342 ReturnValue.new('CannotBeConstructed*', caller_owns_return=True),
343 [])
344
345
346 ## A nested class
347 #NestedClass = mod.add_class('NestedClass', automatic_type_narrowing=True, outer_class=SomeObject)
348 NestedClass = SomeObject.add_class('NestedClass', automatic_type_narrowing=True)
349 NestedClass.add_static_attribute('instance_count', ReturnValue.new('int'))
350 NestedClass.add_constructor([Parameter.new('std::string', 'datum')])
351 NestedClass.add_constructor([])
352 NestedClass.add_method('get_datum', ReturnValue.new('std::string'), [])
353
354 ## A nested enum..
355 #mod.add_enum('NestedEnum', ['FOO_TYPE_AAA', 'FOO_TYPE_BBB', 'FOO_TYPE_CCC'], outer_class=SomeObject)
356 SomeObject.add_enum('NestedEnum', ['FOO_TYPE_AAA', 'FOO_TYPE_BBB', 'FOO_TYPE_CCC'])
357
358 ## anonymous enum
359 SomeObject.add_enum('', ['CONSTANT_A', 'CONSTANT_B', 'CONSTANT_C'])
360
361
362 AbstractBaseClass2 = mod.add_class('AbstractBaseClass2', allow_subclassing=True)
363
364 AbstractBaseClass2.add_method('invoke_private_virtual', ReturnValue.new('int'),
365 [Parameter.new('int', 'x')], is_const=True)
366 AbstractBaseClass2.add_method('invoke_protected_virtual', ReturnValue.new('int'),
367 [Parameter.new('int', 'x')], is_const=True)
368 AbstractBaseClass2.add_method('invoke_protected_pure_virtual', ReturnValue.new('int'),
369 [Parameter.new('int', 'x')], is_const=True)
370 AbstractBaseClass2.add_constructor([], visibility='protected')
371
372 AbstractBaseClass2.add_method('protected_virtual',
373 ReturnValue.new('int'),
374 [Parameter.new('int', 'x')],
375 is_virtual=True, visibility='protected', is_const=True)
376 AbstractBaseClass2.add_method('protected_pure_virtual',
377 ReturnValue.new('int'),
378 [Parameter.new('int', 'x')],
379 is_virtual=True, is_pure_virtual=True, visibility='protected', is_const=True)
380
381 AbstractBaseClass2.add_method('private_virtual',
382 ReturnValue.new('int'), [Parameter.new('int', 'x')],
383 is_virtual=True, is_pure_virtual=True, visibility='private', is_const=True)
384
385
386
387
388 AbstractXpto = mod.add_class('AbstractXpto', allow_subclassing=True)
389 AbstractXpto.add_method('something', ReturnValue.new('void'),
390 [Parameter.new('int', 'x')], is_const=True,
391 is_virtual=True, is_pure_virtual=True)
392 AbstractXpto.add_constructor([])
393
394 AbstractXptoImpl = mod.add_class('AbstractXptoImpl', parent=AbstractXpto)
395 AbstractXptoImpl.add_method('something', ReturnValue.new('void'),
396 [Parameter.new('int', 'x')], is_const=True,
397 is_virtual=True, is_pure_virtual=False)
398 AbstractXptoImpl.add_constructor([])
399
400 Word = mod.add_class('Word')
401 Word.add_instance_attribute('low', 'uint8_t', is_const=False)
402 Word.add_instance_attribute('high', 'uint8_t', is_const=False)
403 Word.add_instance_attribute('word', 'uint16_t', is_const=False)
404 Word.add_constructor([])
405
406
407 mod.add_function('matrix_sum_of_elements',
408 ReturnValue.new('float'),
409 [Parameter.new("float*", 'matrix', direction=Parameter.DIRECTION_IN, array_length=6)])
410
411 mod.add_function('matrix_identity_new',
412 ReturnValue.new('void'),
413 [Parameter.new("float*", 'matrix', direction=Parameter.DIRECTION_OUT, array_length=6)])
414
415 top_ns = mod.add_cpp_namespace('TopNs')
416 outer_base = top_ns.add_class('OuterBase')
417 bottom_ns = top_ns.add_cpp_namespace('PrefixBottomNs')
418 inner = bottom_ns.add_class('PrefixInner',parent=outer_base)
419 inner.add_constructor([])
420 inner.add_method('Do', 'void', [])
421
422
423 Socket = mod.add_class('Socket', allow_subclassing=True)
424 Socket.add_constructor([])
425 Socket.add_method('Bind', ReturnValue.new('int'), [], is_virtual=True)
426 Socket.add_method('Bind', ReturnValue.new('int'), [Parameter.new('int', 'address')], is_virtual=True)
427
428 UdpSocket = mod.add_class('UdpSocket', parent=Socket)
429 UdpSocket.add_constructor([])
430 UdpSocket.add_method('Bind', ReturnValue.new('int'), [], is_virtual=True)
431
432 simple_struct_t = mod.add_struct('simple_struct_t')
433 simple_struct_t.add_instance_attribute('xpto', 'int')
434
435
436 # containers...
437 mod.add_container('SimpleStructList', ReturnValue.new('simple_struct_t'), 'list')
438 mod.add_function('get_simple_list', ReturnValue.new('SimpleStructList'), [])
439 mod.add_function('set_simple_list', 'int', [Parameter.new('SimpleStructList', 'list')])
440
441 mod.add_container('std::set<float>', 'float', 'set')
442
443 TestContainer = mod.add_class('TestContainer', allow_subclassing=True)
444 TestContainer.add_constructor([])
445 TestContainer.add_instance_attribute('m_floatSet', 'std::set<float>')
446 TestContainer.add_method('get_simple_list', ReturnValue.new('SimpleStructList'), [], is_virtual=True)
447 TestContainer.add_method('set_simple_list', 'int', [Parameter.new('SimpleStructList', 'list')], is_virtual=True)
448 TestContainer.add_method('set_simple_list_by_ref', 'int', [Parameter.new('SimpleStructList&', 'inout_list',
449 direction=Parameter.DIRECTION_INOUT)],
450 is_virtual=True)
451
452 mod.add_container('std::vector<simple_struct_t>', ReturnValue.new('simple_struct_t'), 'vector')
453 TestContainer.add_method('get_simple_vec', ReturnValue.new('std::vector<simple_struct_t>'), [], is_virtual=True)
454 TestContainer.add_method('set_simple_vec', 'int', [Parameter.new('std::vector<simple_struct_t>', 'vec')], is_virtual=True)
455
456 mod.add_container('std::vector<std::string>', 'std::string', 'vector')
457 TestContainer.add_method('get_vec', 'void', [Parameter.new('std::vector<std::string> &', 'outVec',
458 direction=Parameter.DIRECTION_OUT)])
459
460 TestContainer.add_method('set_vec_ptr', 'void', [Parameter.new('std::vector<std::string>*', 'inVec',
461 direction=Parameter.DIRECTION_IN, transfer_ownership=True)])
462 TestContainer.add_method('get_vec_ptr', 'void', [Parameter.new('std::vector<std::string>*', 'outVec',
463 direction=Parameter.DIRECTION_OUT)])
464
465
466 mod.add_container('std::map<std::string, simple_struct_t>',
467 (ReturnValue.new('std::string'), ReturnValue.new('simple_struct_t')),
468 'map')
469 TestContainer.add_method('get_simple_map', ReturnValue.new('std::map<std::string, simple_struct_t>'), [], is_virtual=True)
470 TestContainer.add_method('set_simple_map', 'int', [Parameter.new('std::map<std::string, simple_struct_t>', 'map')], is_virtual=True)
471
472
473 Tupl = mod.add_class('Tupl')
474 Tupl.add_binary_comparison_operator('<')
475 Tupl.add_binary_comparison_operator('<=')
476 Tupl.add_binary_comparison_operator('>=')
477 Tupl.add_binary_comparison_operator('>')
478 Tupl.add_binary_comparison_operator('==')
479 Tupl.add_binary_comparison_operator('!=')
480 Tupl.add_binary_numeric_operator('+')
481 Tupl.add_binary_numeric_operator('-')
482 Tupl.add_binary_numeric_operator('*')
483 Tupl.add_binary_numeric_operator('/')
484 Tupl.add_instance_attribute('x', 'int', is_const=False)
485 Tupl.add_instance_attribute('y', 'int', is_const=False)
486 Tupl.add_constructor([Parameter.new('Tupl const &', 'arg0')])
487 Tupl.add_constructor([])
488 Tupl.add_inplace_numeric_operator('+=')
489 Tupl.add_inplace_numeric_operator('-=')
490 Tupl.add_inplace_numeric_operator('*=')
491 Tupl.add_inplace_numeric_operator('/=')
492
493 Tupl.add_unary_numeric_operator('-')
494
495 Tupl.add_inplace_numeric_operator('+=', right='int')
496
497
498 ManipulatedObject = mod.add_class('ManipulatedObject')
499 ManipulatedObject.add_constructor([])
500 ManipulatedObject.add_method('GetValue', 'int', [], is_const=True)
501 ManipulatedObject.add_method('SetValue', 'void', [Parameter.new('int', 'value')])
502
503 ReferenceManipulator = mod.add_class('ReferenceManipulator', allow_subclassing=True)
504 ReferenceManipulator.add_constructor([])
505 ReferenceManipulator.add_method('manipulate_object', 'int', [])
506 ReferenceManipulator.add_method('do_manipulate_object', 'void',
507 [Parameter.new('ManipulatedObject&', 'obj', direction=Parameter.DIRECTION_INOUT)],
508 is_virtual=True, is_pure_virtual=True)
509
510
511 VectorLike = mod.add_class('VectorLike')
512 VectorLike.add_constructor([])
513 VectorLike.add_constructor([Parameter.new("VectorLike&", "obj")])
514 VectorLike.add_method('get_len', 'size_t', [], custom_name='__len__')
515 VectorLike.add_method('add_VectorLike', 'VectorLike', [Parameter.new('VectorLike', 'rhs')], custom_name='__add__')
516 VectorLike.add_method('iadd_VectorLike', 'VectorLike', [Parameter.new('VectorLike', 'rhs')], custom_name='__iadd__')
517 VectorLike.add_method('mul_VectorLike', 'VectorLike', [Parameter.new('unsigned int', 'n')], custom_name='__mul__')
518 VectorLike.add_method('imul_VectorLike', 'VectorLike', [Parameter.new('unsigned int', 'n')], custom_name='__imul__')
519 VectorLike.add_method('set_item', 'int', [Parameter.new('int', 'index'), Parameter.new('double', 'value')],
520 custom_name='__setitem__')
521 VectorLike.add_method('get_item', 'double', [Parameter.new('int', 'index')], custom_name='__getitem__')
522 VectorLike.add_method('set_slice', 'int', [Parameter.new('int', 'index1'),
523 Parameter.new('int', 'index2'),
524 Parameter.new('VectorLike', 'values')], custom_name='__setslice__')
525 VectorLike.add_method('get_slice', 'VectorLike', [Parameter.new('int', 'index1'),
526 Parameter.new('int', 'index2')], custom_name='__getslice__')
527 VectorLike.add_method('contains_value', 'int', [Parameter.new('double', 'value')], custom_name='__contains__')
528 VectorLike.add_method('append', 'void', [Parameter.new('double', 'value')])
529
530 VectorLike2 = mod.add_class('VectorLike2')
531 VectorLike2.add_constructor([])
532 VectorLike2.add_method('append', 'void', [Parameter.new('double', 'value')])
533
534 MapLike = mod.add_class('MapLike')
535 MapLike.add_constructor([])
536 MapLike.add_method('set', 'void', [Parameter.new('int', 'key'), Parameter.new('double', 'value')])
537
538 Error = mod.add_exception('Error')
539 DomainError = mod.add_exception('DomainError', parent=Error)
540
541 mod.add_function('my_inverse_func', 'double', [Parameter.new('double', 'x')],
542 throw=[DomainError])
543
544 ClassThatThrows = mod.add_class('ClassThatThrows', allow_subclassing=True)
545 ClassThatThrows.add_constructor([Parameter.new('double', 'x')], throw=[DomainError])
546 ClassThatThrows.add_method('my_inverse_method', 'double', [Parameter.new('double', 'x')],
547 throw=[DomainError])
548
549 std_exception = mod.add_exception('exception', foreign_cpp_namespace='std', message_rvalue='%(EXC)s.what()')
550 mod.add_function('my_inverse_func2', 'double', [Parameter.new('double', 'x')],
551 throw=[std_exception])
552 ClassThatThrows.add_method('my_inverse_method2', 'double', [Parameter.new('double', 'x')],
553 throw=[std_exception])
554
555 mod.add_function('my_inverse_func3', 'double', [Parameter.new('double', 'x')],
556 throw=[std_exception])
557 ClassThatThrows.add_method('my_inverse_method3', 'double', [Parameter.new('double', 'x')],
558 throw=[std_exception])
559
560 ClassThatThrows.add_method('throw_error', 'int', [], throw=[mod["out_of_range"]], is_const=True, is_virtual=True)
561
562 ClassThatThrows.add_method('throw_out_of_range', 'int', [], throw=[mod["out_of_range"]])
563
564 # https://bugs.launchpad.net/pybindgen/+bug/450255
565 ProtectedConstructor = mod.add_class('ProtectedConstructor')
566 ProtectedConstructor.add_constructor([])
567 ProtectedConstructor.add_constructor([Parameter.new('ProtectedConstructor&', 'c')], visibility='protected')
568
569 # https://bugs.launchpad.net/pybindgen/+bug/455689
570 property_std_string = mod.add_struct('property', template_parameters=['std::string'])
571
572
573
574 Box = mod.add_class('Box')
575 Box.add_constructor([])
576 Box.add_static_attribute('instance_count', ReturnValue.new('int'))
577 Box.add_method('getFoobarInternalPtr', ReturnValue.new('const Foobar*', reference_existing_object=True), [])
578 Box.add_method('getFoobarInternalRef', ReturnValue.new('Foobar&', reference_existing_object=True), [])
579 Box.add_method('getFoobarInternalPtr2', ReturnValue.new('Foobar*', return_internal_reference=True), [])
580 Box.add_method('getFoobarInternalRef2', ReturnValue.new('Foobar&', return_internal_reference=True), [])
581 Box.add_instance_attribute('m_internalFoobar', ReturnValue.new('Foobar*', reference_existing_object=True))
582
583
584 # multiple inheritance
585 MIRoot = mod.add_class('MIRoot')
586 MIRoot.add_constructor([])
587 MIRoot.add_method('root_method', 'int', [], is_const=True)
588
589 MIBase1 = mod.add_class('MIBase1', parent=MIRoot)
590 MIBase1.add_constructor([])
591 MIBase1.add_method('base1_method', 'int', [], is_const=True)
592
593 MIBase2 = mod.add_class('MIBase2', parent=MIRoot)
594 MIBase2.add_constructor([])
595 MIBase2.add_method('base2_method', 'int', [], is_const=True)
596
597 MIMixed = mod.add_class('MIMixed', parent=[MIBase1, MIBase2])
598 MIMixed.add_constructor([])
599 MIMixed.add_method('mixed_method', 'int', [], is_const=True)
600
601
602 mod.add_function('my_throwing_func', 'Tupl', [], throw=[std_exception])
603
604
605 IFoo = mod.add_class("IFoo", destructor_visibility='protected', allow_subclassing=True)
606 IFoo.add_method("DoSomething", None, [], is_pure_virtual=True)
607
608 IFooImpl = mod.add_class("IFooImpl", parent=IFoo, destructor_visibility='public')
609 IFooImpl.add_constructor([])
610 IFooImpl.add_method("DoSomething", None, [], is_virtual=True)
611
612
613 mod.add_function("test_args_kwargs", "int", [param("const char *", "args"), param("const char *", "kwargs")])
614
615
616 #### --- error handler ---
617 class MyErrorHandler(pybindgen.settings.ErrorHandler):
618 def __init__(self):
619 super(MyErrorHandler, self).__init__()
620 self.num_errors = 0
621 def handle_error(self, wrapper, exception, traceback_):
622 print("exception %s in wrapper %s" % (exception, wrapper), file=sys.stderr)
623 self.num_errors += 1
624 if 0: # verbose?
625 import traceback
626 traceback.print_tb(traceback_)
627 return True
628 pybindgen.settings.error_handler = MyErrorHandler()
629
630 foomodulegen_common.customize_module(mod)
631
632 ## ---- finally, generate the whole thing ----
633 mod.generate(FileCodeSink(out_file))
634
635
636 if __name__ == '__main__':
637 import os
638 if "PYBINDGEN_ENABLE_PROFILING" in os.environ:
639 try:
640 import cProfile as profile
641 except ImportError:
642 my_module_gen(sys.stdout)
643 else:
644 print("** running under profiler", file=sys.stderr)
645 profile.run('my_module_gen(sys.stdout)', 'foomodulegen.pstat')
646 else:
647 my_module_gen(sys.stdout)
648