Debian Codegraph @

tests/boost/barmodulegen.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.typehandlers.smart_ptr import BoostSharedPtr
11 from pybindgen import ReturnValue, Parameter, Module, Function, FileCodeSink
12 from pybindgen import CppMethod, CppConstructor, CppClass, Enum
13 from pybindgen.function import CustomFunctionWrapper
14 from pybindgen.cppmethod import CustomCppMethodWrapper
15 from pybindgen import cppclass
16
17 from pybindgen import param, retval
18
19 import pybindgen.settings
20 pybindgen.settings.deprecated_virtuals = False
21
22
23 def my_module_gen(out_file):
24
25 mod = Module('bar')
26
27 mod.add_include ('"bar.h"')
28
29 Foo = mod.add_class('Foo', automatic_type_narrowing=True,
30 memory_policy=BoostSharedPtr('::Foo'))
31
32 Foo.add_static_attribute('instance_count', ReturnValue.new('int'))
33 Foo.add_constructor([Parameter.new('std::string', 'datum')])
34 Foo.add_constructor([])
35 Foo.add_method('get_datum', ReturnValue.new('const std::string'), [])
36 Foo.add_method('is_initialized', ReturnValue.new('bool'), [], is_const=True)
37 Foo.add_output_stream_operator()
38
39
40 mod.add_function('function_that_takes_foo', ReturnValue.new('void'),
41 [param('boost::shared_ptr<Foo>', 'foo')])
42 mod.add_function('function_that_returns_foo', retval('boost::shared_ptr<Foo>'), [])
43
44 cls = mod.add_class('ClassThatTakesFoo', allow_subclassing=True)
45 cls.add_constructor([Parameter.new('boost::shared_ptr<Foo>', 'foo')])
46 cls.add_method('get_foo', ReturnValue.new('boost::shared_ptr<Foo>'), [])
47 cls.add_method('get_modified_foo', retval('boost::shared_ptr<Foo>'),
48 [param('boost::shared_ptr<Foo>', 'foo')],
49 is_virtual=True, is_const=True)
50
51
52
53 #### --- error handler ---
54 class MyErrorHandler(pybindgen.settings.ErrorHandler):
55 def __init__(self):
56 super(MyErrorHandler, self).__init__()
57 self.num_errors = 0
58 def handle_error(self, wrapper, exception, traceback_):
59 print("exception %s in wrapper %s" % (exception, wrapper), file=sys.stderr)
60 self.num_errors += 1
61 if 0: # verbose?
62 import traceback
63 traceback.print_tb(traceback_)
64 return True
65 pybindgen.settings.error_handler = MyErrorHandler()
66
67 ## ---- finally, generate the whole thing ----
68 mod.generate(FileCodeSink(out_file))
69
70
71 if __name__ == '__main__':
72 import os
73 if "PYBINDGEN_ENABLE_PROFILING" in os.environ:
74 try:
75 import cProfile as profile
76 except ImportError:
77 my_module_gen(sys.stdout)
78 else:
79 print("** running under profiler", file=sys.stderr)
80 profile.run('my_module_gen(sys.stdout)', 'foomodulegen.pstat')
81 else:
82 my_module_gen(sys.stdout)
83