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