def test(): code_out = codesink.FileCodeSink(sys.stdout) pybindgen.write_preamble(code_out) print print "#include <string>" print "#include <stdint.h>" print ## Declare a dummy class sys.stdout.write(''' class Foo { std::string m_datum; public: Foo () : m_datum ("") {} Foo (std::string datum) : m_datum (datum) {} std::string get_datum () const { return m_datum; } Foo (Foo const & other) : m_datum (other.get_datum ()) {} }; ''') module = Module("foo") ## Register type handlers for the class Foo = cppclass.CppClass('Foo') Foo.module = module #Foo.full_name = Foo.name # normally adding the class to a module would take care of this Foo.generate_forward_declarations(code_out, module) wrapper_number = 0 ## test return type handlers of reverse wrappers for return_type, return_handler in typehandlers.base.return_type_matcher.items( ): if os.name == 'nt': if stdint_rx.search(return_type): continue # win32 does not support the u?int\d+_t types (defined in <stdint.h>) if issubclass(return_handler, (cppclass.CppClassPtrReturnValue, typehandlers.pyobjecttype.PyObjectReturnValue)): for caller_owns_return in True, False: retval = return_handler(return_type, caller_owns_return=caller_owns_return) wrapper = MyReverseWrapper(retval, []) wrapper_number += 1 try: wrapper.generate( code_out, '_test_wrapper_number_%i' % (wrapper_number, ), ['static']) except NotImplementedError: print >> sys.stderr, \ ("ReverseWrapper %s(void) (caller_owns_return=%r)" " could not be generated: not implemented" % (retval.ctype, caller_owns_return)) print else: retval = return_handler(return_type) try: wrapper = MyReverseWrapper(retval, []) except NotSupportedError: continue wrapper_number += 1 try: wrapper.generate( code_out, '_test_wrapper_number_%i' % (wrapper_number, ), ['static']) except NotImplementedError: print >> sys.stderr, ( "ReverseWrapper %s(void) could not be generated: not implemented" % (retval.ctype, )) print ## test parameter type handlers of reverse wrappers for param_type, param_handler in typehandlers.base.param_type_matcher.items( ): if os.name == 'nt': if stdint_rx.search(param_type): continue # win32 does not support the u?int\d+_t types (defined in <stdint.h>) for direction in param_handler.DIRECTIONS: if direction == (Parameter.DIRECTION_IN): param_name = 'param' elif direction == (Parameter.DIRECTION_IN | Parameter.DIRECTION_OUT): param_name = 'param_inout' elif direction == (Parameter.DIRECTION_OUT): param_name = 'param_out' def try_wrapper(param, wrapper_number): if 'const' in param.ctype and direction & Parameter.DIRECTION_OUT: return wrapper = MyReverseWrapper(ReturnValue.new('void'), [param]) try: wrapper.generate( code_out, '_test_wrapper_number_%i' % (wrapper_number, ), ['static']) except NotImplementedError: print >> sys.stderr, ( "ReverseWrapper void(%s) could not be generated: not implemented" % (param.ctype)) print if issubclass(param_handler, (cppclass.CppClassPtrParameter, typehandlers.pyobjecttype.PyObjectParam)): for transfer_ownership in True, False: try: param = param_handler( param_type, param_name, transfer_ownership=transfer_ownership) except TypeError: print >> sys.stderr, "ERROR -----> param_handler(param_type=%r, "\ "transfer_ownership=%r, is_const=%r)"\ % (param_type, transfer_ownership, is_const) wrapper_number += 1 try_wrapper(param, wrapper_number) else: param = param_handler(param_type, param_name, direction) wrapper_number += 1 try_wrapper(param, wrapper_number) ## test generic forward wrappers, and module for return_type, return_handler in typehandlers.base.return_type_matcher.items( ): if os.name == 'nt': if stdint_rx.search(return_type): continue # win32 does not support the u?int\d+_t types (defined in <stdint.h>) wrapper_number += 1 function_name = 'foo_function_%i' % (wrapper_number, ) ## declare a fake prototype print "%s %s(void);" % (return_type, function_name) print if issubclass(return_handler, (cppclass.CppClassPtrReturnValue, typehandlers.pyobjecttype.PyObjectReturnValue)): retval = return_handler(return_type, caller_owns_return=True) else: retval = return_handler(return_type) module.add_function(function_name, retval, []) for param_type, param_handler in typehandlers.base.param_type_matcher.items( ): if os.name == 'nt': if stdint_rx.search(param_type): continue # win32 does not support the u?int\d+_t types (defined in <stdint.h>) for is_const in [True, False]: for direction in param_handler.DIRECTIONS: if direction == (Parameter.DIRECTION_IN): param_name = 'param' elif direction == (Parameter.DIRECTION_IN | Parameter.DIRECTION_OUT): param_name = 'param_inout' elif direction == (Parameter.DIRECTION_OUT): param_name = 'param_out' if is_const and direction & Parameter.DIRECTION_OUT: continue # const and output parameter makes no sense if is_const: if '&' in param_type: # const references not allowed continue param_type_with_const = "const %s" % (param_type, ) else: param_type_with_const = param_type if issubclass(param_handler, (cppclass.CppClassPtrParameter, typehandlers.pyobjecttype.PyObjectParam)): for transfer_ownership in True, False: name = param_name + (transfer_ownership and '_transfer' or '_notransfer') try: param = param_handler( param_type, name, transfer_ownership=transfer_ownership) except TypeError: print >> sys.stderr, "ERROR -----> param_handler(param_type=%r, "\ "name=%r, transfer_ownership=%r, is_const=%r)"\ % (param_type, name, transfer_ownership, is_const) wrapper_number += 1 function_name = 'foo_function_%i' % (wrapper_number, ) ## declare a fake prototype print "void %s(%s %s);" % (function_name, param_type_with_const, name) print module.add_function(function_name, ReturnValue.new('void'), [param]) else: param = param_handler(param_type, param_name, direction) wrapper_number += 1 function_name = 'foo_function_%i' % (wrapper_number, ) ## declare a fake prototype print "void %s(%s);" % (function_name, param_type_with_const) print module.add_function(function_name, ReturnValue.new('void'), [param]) module.generate(code_out)
def my_module_gen(out_file): mod = Module('a') mod.add_include('"a.h"') mod.add_function('ADoA', None, []) mod.add_function('ADoB', None, [Parameter.new('uint32_t', 'b')]) mod.add_function('ADoC', ReturnValue.new('uint32_t'), []) mod.generate(FileCodeSink(out_file))
def test(): code_out = codesink.FileCodeSink(sys.stdout) pybindgen.write_preamble(code_out) sys.stdout.write(""" #include <string> #include <stdint.h> """) ## Declare a dummy class sys.stdout.write(''' class Foo { std::string m_datum; public: Foo () : m_datum ("") {} Foo (std::string datum) : m_datum (datum) {} std::string get_datum () const { return m_datum; } Foo (Foo const & other) : m_datum (other.get_datum ()) {} }; ''') module = Module("foo") ## Register type handlers for the class Foo = cppclass.CppClass('Foo') Foo.module = module #Foo.full_name = Foo.name # normally adding the class to a module would take care of this Foo.generate_forward_declarations(code_out, module) wrapper_number = 0 ## test return type handlers of reverse wrappers for return_type, return_handler in list(typehandlers.base.return_type_matcher.items()): if type_blacklisted(return_type): continue if os.name == 'nt': if stdint_rx.search(return_type): continue # win32 does not support the u?int\d+_t types (defined in <stdint.h>) code_out.writeln("/* Test %s (%s) return type */" % (return_type, return_handler)) if issubclass(return_handler, (cppclass.CppClassPtrReturnValue, typehandlers.pyobjecttype.PyObjectReturnValue)): for caller_owns_return in True, False: retval = return_handler(return_type, caller_owns_return=caller_owns_return) wrapper = MyReverseWrapper(retval, []) wrapper_number += 1 try: wrapper.generate(code_out, '_test_wrapper_number_%i' % (wrapper_number,), ['static']) except NotImplementedError: sys.stderr.write("ReverseWrapper %s(void) (caller_owns_return=%r)" " could not be generated: not implemented\n" % (retval.ctype, caller_owns_return)) sys.stdout.write("\n") else: retval = return_handler(return_type) try: wrapper = MyReverseWrapper(retval, []) except NotSupportedError: continue except NotImplementedError: sys.stderr.write("ReverseWrapper %s(void) could not be generated: not implemented\n" % (retval.ctype)) continue wrapper_number += 1 memsink = codesink.MemoryCodeSink() try: wrapper.generate(memsink, '_test_wrapper_number_%i' % (wrapper_number,), ['static']) except NotImplementedError: sys.stderr.write("ReverseWrapper %s xxx (void) could not be generated: not implemented\n" % (retval.ctype,)) continue except NotSupportedError: continue else: memsink.flush_to(code_out) sys.stdout.write("\n") ## test parameter type handlers of reverse wrappers for param_type, param_handler in list(typehandlers.base.param_type_matcher.items()): if type_blacklisted(param_type): continue if os.name == 'nt': if stdint_rx.search(param_type): continue # win32 does not support the u?int\d+_t types (defined in <stdint.h>) for direction in param_handler.DIRECTIONS: if direction == (Parameter.DIRECTION_IN): param_name = 'param' elif direction == (Parameter.DIRECTION_IN|Parameter.DIRECTION_OUT): param_name = 'param_inout' elif direction == (Parameter.DIRECTION_OUT): param_name = 'param_out' def try_wrapper(param, wrapper_number): if 'const' in param.ctype and direction&Parameter.DIRECTION_OUT: return code_out.writeln("/* Test %s (%s) param type */" % (param_type, param_handler)) wrapper = MyReverseWrapper(ReturnValue.new('void'), [param]) try: wrapper.generate(code_out, '_test_wrapper_number_%i' % (wrapper_number,), ['static']) except NotImplementedError: sys.stderr.write("ReverseWrapper void(%s) could not be generated: not implemented" % (param.ctype)) sys.stdout.write("\n") if issubclass(param_handler, (cppclass.CppClassPtrParameter, typehandlers.pyobjecttype.PyObjectParam)): for transfer_ownership in True, False: try: param = param_handler(param_type, param_name, transfer_ownership=transfer_ownership) except TypeError: sys.stderr.write("ERROR -----> param_handler(param_type=%r, " "transfer_ownership=%r, is_const=%r)\n" % (param_type, transfer_ownership, is_const)) wrapper_number += 1 try_wrapper(param, wrapper_number) else: param = param_handler(param_type, param_name, direction) wrapper_number += 1 try_wrapper(param, wrapper_number) ## test generic forward wrappers, and module for return_type, return_handler in list(typehandlers.base.return_type_matcher.items()): if type_blacklisted(return_type): continue if os.name == 'nt': if stdint_rx.search(return_type): continue # win32 does not support the u?int\d+_t types (defined in <stdint.h>) wrapper_number += 1 function_name = 'foo_function_%i' % (wrapper_number,) ## declare a fake prototype sys.stdout.write("%s %s(void);\n\n" % (return_type, function_name)) if issubclass(return_handler, (cppclass.CppClassPtrReturnValue, typehandlers.pyobjecttype.PyObjectReturnValue)): retval = return_handler(return_type, caller_owns_return=True) else: retval = return_handler(return_type) module.add_function(function_name, retval, []) for param_type, param_handler in list(typehandlers.base.param_type_matcher.items()): if type_blacklisted(param_type): continue if os.name == 'nt': if stdint_rx.search(param_type): continue # win32 does not support the u?int\d+_t types (defined in <stdint.h>) for is_const in [True, False]: for direction in param_handler.DIRECTIONS: if direction == (Parameter.DIRECTION_IN): param_name = 'param' elif direction == (Parameter.DIRECTION_IN|Parameter.DIRECTION_OUT): param_name = 'param_inout' elif direction == (Parameter.DIRECTION_OUT): param_name = 'param_out' if is_const and direction & Parameter.DIRECTION_OUT: continue # const and output parameter makes no sense if is_const: if '&' in param_type: # const references not allowed continue param_type_with_const = "const %s" % (param_type,) else: param_type_with_const = param_type if issubclass(param_handler, (cppclass.CppClassPtrParameter, typehandlers.pyobjecttype.PyObjectParam)): for transfer_ownership in True, False: name = param_name + (transfer_ownership and '_transfer' or '_notransfer') try: param = param_handler(param_type, name, transfer_ownership=transfer_ownership) except TypeError: sys.stderr.write("ERROR -----> param_handler(param_type=%r, " "name=%r, transfer_ownership=%r, is_const=%r)\n" % (param_type, name, transfer_ownership, is_const)) wrapper_number += 1 function_name = 'foo_function_%i' % (wrapper_number,) ## declare a fake prototype sys.stdout.write("void %s(%s %s);\n\n" % (function_name, param_type_with_const, name)) module.add_function(function_name, ReturnValue.new('void'), [param]) else: param = param_handler(param_type, param_name, direction) wrapper_number += 1 function_name = 'foo_function_%i' % (wrapper_number,) ## declare a fake prototype sys.stdout.write("void %s(%s);\n\n" % (function_name, param_type_with_const)) module.add_function(function_name, ReturnValue.new('void'), [param]) module.generate(code_out)
def my_module_gen(out_file): mod = Module('a') mod.add_include('"a.h"') mod.add_function('ADoA', None, []) mod.add_function('ADoB', None, [Parameter.new('uint32_t', 'b')]) mod.add_function('ADoC', ReturnValue.new('uint32_t'), []) mod.generate(FileCodeSink(out_file) )
def generate(file_): # Declare modules bamboo = Module('bamboo', cpp_namespace="::bamboo") bits = SubModule('bits', bamboo) module = SubModule('module', bamboo) traits = SubModule('traits', bamboo) dcfile = SubModule('dcfile', bamboo) wire = SubModule('wire', bamboo) # Declare includes bits.add_include('"bits/byteorder.h"') bits.add_include('"bits/sizetag.h"') bits.add_include('"bits/errors.h"') module.add_include('"module/Array.h"') module.add_include('"module/Class.h"') module.add_include('"module/Type.h"') module.add_include('"module/Field.h"') module.add_include('"module/KeywordList.h"') module.add_include('"module/Method.h"') module.add_include('"module/Module.h"') module.add_include('"module/MolecularField.h"') module.add_include('"module/NumericRange.h"') module.add_include('"module/Numeric.h"') module.add_include('"module/Parameter.h"') module.add_include('"module/Struct.h"') module.add_include('"module/Value.h"') traits.add_include('"traits/hashes.h"') dcfile.add_include('"dcfile/format.h"') dcfile.add_include('"dcfile/parse.h"') dcfile.add_include('"dcfile/write.h"') wire.add_include('"wire/Datagram.h"') wire.add_include('"wire/DatagramIterator.h"') # Declare classes indexError = bamboo.add_exception('out_of_range', custom_name = 'IndexError', foreign_cpp_namespace = 'std', message_rvalue = 'exc.what()', is_standard_error = True) nullError = bamboo.add_exception('invalid_argument', custom_name = 'TypeError', foreign_cpp_namespace = 'std', message_rvalue = "'\"NoneType' object\" + exc.reason()", is_standard_error = True) clsKeywordList = module.add_class('KeywordList', docstring = classDocstrings['KeywordList']) clsModule = module.add_class('Module', docstring = classDocstrings['Module']) clsType = module.add_class('Type', docstring = classDocstrings['Type']) clsNumType = module.add_class('Numeric',parent = clsType, docstring = classDocstrings['Numeric']) clsArrType = module.add_class('Array', parent = clsType, docstring = classDocstrings['Array']) clsMethod = module.add_class('Method', parent = clsType, docstring = classDocstrings['Method']) clsStruct = module.add_class('Struct', parent = clsType, docstring = classDocstrings['Struct']) clsClass = module.add_class('Class', parent = clsStruct, docstring = classDocstrings['Class']) clsParam = module.add_class('Parameter', docstring = classDocstrings['Parameter']) clsField = module.add_class('Field', parent = clsKeywordList, docstring = classDocstrings['Field']) clsMolecular = module.add_class('MolecularField', parent = [clsField, clsStruct]) structImport = module.add_struct('Import') structNumber = module.add_struct('Number') structNumericRange = module.add_struct('NumericRange') dgOverflowError = wire.add_exception('DatagramOverflow', message_rvalue = 'exc.what()') dgiEOFError = wire.add_exception('DatagramIteratorEOF', message_rvalue = 'exc.what()') clsDatagram = wire.add_class('Datagram', docstring = classDocstrings['Datagram']) clsDgIter = wire.add_class('DatagramIterator', docstring = classDocstrings['DatagramIterator']) # Declare enums enumSubtype = module.add_enum('Subtype', [ 'kTypeInt8', 'kTypeInt16', 'kTypeInt32', 'kTypeInt64', 'kTypeUint8', 'kTypeUint16', 'kTypeUint32', 'kTypeUint64', 'kTypeChar', 'kTypeFloat32', 'kTypeFloat64', 'kTypeString', 'kTypeVarstring', 'kTypeBlob', 'kTypeVarblob', 'kTypeStruct', 'kTypeMethod', 'kTypeInvalid']) enumNumtype = structNumber.add_enum('Type', ['kNaN', 'kInt', 'kUint', 'kFloat']) # Wrap STL containers bits.add_container('std::vector<std::string>', 'std::string', 'vector', custom_name = 'StringCollection') # Declare member variables structImport.add_copy_constructor() structImport.add_constructor([param('const std::string &', 'moduleName')]) structImport.add_instance_attribute('module', 'std::string') structImport.add_instance_attribute('symbols', 'std::vector<std::string>') structNumber.add_copy_constructor() structNumber.add_instance_attribute('type', 'bamboo::Number::Type') structNumber.add_instance_attribute('integer', 'int64_t') structNumber.add_instance_attribute('uinteger', 'uint64_t') structNumber.add_instance_attribute('floating', 'double') structNumericRange.add_copy_constructor() structNumericRange.add_instance_attribute('type', 'bamboo::Number::Type') structNumericRange.add_instance_attribute('min', 'Number') structNumericRange.add_instance_attribute('max', 'Number') # Declare functions/methods clsKeywordList.add_constructor([]) clsKeywordList.add_copy_constructor() add_method(clsKeywordList, 'has_keyword', retval('bool'), [param('std::string', 'keyword')], is_const = True) add_method(clsKeywordList, 'has_matching_keywords', retval('bool'), [param('const bamboo::KeywordList&', 'other')], is_const = True) add_method(clsKeywordList, 'num_keywords', retval('size_t'), [], is_const = True) add_method(clsKeywordList, 'get_keyword', retval('std::string'), [param('unsigned int', 'n')], is_const = True, throw = [indexError]) add_method(clsKeywordList, 'add_keyword', retval('bool'), [param('std::string', 'keyword')]) add_method(clsKeywordList, 'copy_keywords', None, [param('const bamboo::KeywordList&', 'other')]) clsModule.add_constructor([]) add_method(clsModule, 'num_classes', retval('size_t'), [], is_const = True) add_method(clsModule, 'num_structs', retval('size_t'), [], is_const = True) add_method(clsModule, 'num_types', retval('size_t'), [], is_const = True) add_method(clsModule, 'get_class', retval_ref('bamboo::Class *'), [param('unsigned int', 'n')], throw = [indexError]) add_method(clsModule, 'get_struct', retval_ref('bamboo::Struct *'), [param('unsigned int', 'n')], throw = [indexError]) add_method(clsModule, 'class_by_id', retval_ref('bamboo::Class *'), [param('unsigned int', 'id')]) add_method(clsModule, 'class_by_name', retval_ref('bamboo::Class *'), [param('std::string', 'name')]) add_method(clsModule, 'type_by_id', retval_ref('bamboo::Type *'), [param('unsigned int', 'id')]) add_method(clsModule, 'type_by_name', retval_ref('bamboo::Type *'), [param('std::string', 'name')]) add_method(clsModule, 'field_by_id', retval_ref('bamboo::Field *'), [param('unsigned int', 'id')]) add_method(clsModule, 'num_imports', retval('size_t'), [], is_const = True) add_method(clsModule, 'get_import', retval_ref('bamboo::Import *'), [param('unsigned int', 'n')], throw = [indexError]) add_method(clsModule, 'has_keyword', retval('bool'), [param('std::string', 'keyword')], is_const = True) add_method(clsModule, 'num_keywords', retval('size_t'), [], is_const = True) add_method(clsModule, 'get_keyword', retval('std::string'), [param('unsigned int', 'n')], is_const = True, throw = [indexError]) add_method(clsModule, 'add_class', retval('bool'), [param('std::unique_ptr<Class>', 'dclass')]) add_method(clsModule, 'add_struct', retval('bool'), [param('std::unique_ptr<Struct>', 'dstruct')]) add_method(clsModule, 'add_import', None, [param('std::unique_ptr<Import>', 'import')]) add_method(clsModule, 'add_typedef', retval('bool'), [param('std::string', 'name'), param('Type *', 'type', transfer_ownership = False)]) add_method(clsModule, 'add_keyword', None, [param('std::string', 'keyword')]) clsType.add_constructor([], visibility='protected') add_method(clsType, 'subtype', retval('bamboo::Subtype'), [], is_const = True) add_method(clsType, 'has_fixed_size', retval('bool'), [], is_const = True) add_method(clsType, 'fixed_size', retval('size_t'), [], is_const = True) add_method(clsType, 'has_alias', retval('bool'), [], is_const = True) add_method(clsType, 'alias', retval('std::string'), [], is_const = True) add_method(clsType, 'set_alias', None, [param('std::string', 'alias')]) add_method(clsType, 'as_struct', retval_ref('bamboo::Struct *'), []) add_method(clsType, 'as_method', retval_ref('bamboo::Method *'), []) add_method(clsType, 'as_array', retval_ref('bamboo::Array *'), []) add_method(clsType, 'as_numeric', retval_ref('bamboo::Numeric *'), []) add_method(clsType, 'to_string', retval('std::string'), []) clsNumType.add_constructor([param('bamboo::Subtype', 'subtype')]) add_method(clsNumType, 'divisor', retval('unsigned int'), [], is_const = True) add_method(clsNumType, 'has_modulus', retval('bool'), [], is_const = True) add_method(clsNumType, 'modulus', retval('double'), [], is_const = True) add_method(clsNumType, 'has_range', retval('bool'), [], is_const = True) add_method(clsNumType, 'range', retval('bamboo::NumericRange'), [], is_const = True) add_method(clsNumType, 'set_divisor', retval('bool'), [param('unsigned int', 'divisor')]) add_method(clsNumType, 'set_modulus', retval('bool'), [param('double', 'modulus')]) add_method(clsNumType, 'set_range', retval('bool'), [param('const NumericRange&', 'range')]) clsArrType.add_constructor([ param('bamboo::Type *', 'elementType', transfer_ownership = False), param('bamboo::NumericRange', 'arraySize', default_value = 'bamboo::NumericRange()')]) add_method(clsArrType, 'element_type', retval_ref('const bamboo::Type *'), [], is_const = True) add_method(clsArrType, 'array_size', retval('unsigned int'), [], is_const = True) add_method(clsArrType, 'has_range', retval('bool'), [], is_const = True) add_method(clsArrType, 'range', retval('bamboo::NumericRange'), [], is_const = True) clsMethod.add_constructor([]) add_method(clsMethod, 'num_parameters', retval('size_t'), [], is_const = True) add_method(clsMethod, 'get_parameter', retval_ref('bamboo::Parameter *'), [param('unsigned int', 'n')], throw = [indexError]) add_method(clsMethod, 'parameter_by_name', retval_ref('bamboo::Parameter *'), [param('std::string', 'name')]), add_method(clsMethod, 'add_parameter', retval('bool'), [param('std::unique_ptr<Parameter>', 'param')]) clsStruct.add_constructor([ param('bamboo::Module *', 'module', transfer_ownership = False), param('std::string', 'name')]) add_method(clsStruct, 'id', retval('unsigned int'), [], is_const = True) add_method(clsStruct, 'name', retval('std::string'), [], is_const = True) add_method(clsStruct, 'module', retval_ref('bamboo::Module *'), []) add_method(clsStruct, 'num_fields', retval('size_t'), [], is_const = True) add_method(clsStruct, 'get_field', retval_ref('bamboo::Field *'), [param('unsigned int', 'n')], throw = [indexError]) add_method(clsStruct, 'field_by_id', retval_ref('bamboo::Field *'), [param('unsigned int', 'id')]) add_method(clsStruct, 'field_by_name', retval_ref('bamboo::Field *'), [param('std::string', 'name')]) add_method(clsStruct, 'add_field', retval('bool'), [param('std::unique_ptr<Field>', 'field')]) add_method(clsStruct, 'as_class', retval_ref('bamboo::Class *'), []) clsClass.add_constructor([ param('bamboo::Module *', 'module', transfer_ownership = False), param('std::string', 'name')]) add_method(clsClass, 'num_parents', retval('size_t'), [], is_const = True) add_method(clsClass, 'get_parent', retval_ref('bamboo::Class *'), [param('unsigned int', 'n')], throw = [indexError]) add_method(clsClass, 'num_children', retval('size_t'), [], is_const = True) add_method(clsClass, 'get_child', retval_ref('bamboo::Class *'), [param('unsigned int', 'n')], throw = [indexError]) add_method(clsClass, 'has_constructor', retval('bool'), [], is_const = True) add_method(clsClass, 'constructor', retval_ref('bamboo::Field *'), []) add_method(clsClass, 'num_base_fields', retval('size_t'), [], is_const = True) add_method(clsClass, 'get_base_field', retval_ref('bamboo::Field *'), [param('unsigned int', 'n')], throw = [indexError]) add_method(clsClass, 'add_parent', retval('bool'), [param('bamboo::Class *', 'parent', transfer_ownership = False)]) clsParam.add_constructor([ param('bamboo::Type *', 'type', transfer_ownership = False), param('std::string', 'name', default_value = '""')]) add_method(clsParam, 'name', retval('std::string'), [], is_const = False) add_method(clsParam, 'type', retval_ref('bamboo::Type *'), []) add_method(clsParam, 'get_method', retval_ref('bamboo::Method *'), []) add_method(clsParam, 'has_default_value', retval('bool'), [], is_const = True), #add_method(clsParam, 'default_value', retval('const bamboo::Value'), [], is_const = True) add_method(clsParam, 'set_name', retval('bool'), [param('std::string', 'name')]) add_method(clsParam, 'set_type', retval('bool'), [param('bamboo::Type *', 'type', transfer_ownership = False)]), #add_method(clsParam, 'set_default_value', retval('bool'), [param('const bamboo::Value', 'value')]) #add_method(clsParam, 'set_default_value', retval('bool'), [param('const bamboo::Buffer&', 'value')]) clsField.add_constructor([ param('bamboo::Type *', 'type', transfer_ownership = False), param('std::string', 'name', default_value = '""')]) add_method(clsField, 'id', retval('unsigned int'), [], is_const = True) add_method(clsField, 'name', retval('std::string'), [], is_const = False) add_method(clsField, 'type', retval_ref('bamboo::Type *'), []) add_method(clsField, 'record', retval_ref('bamboo::Struct *'), []) add_method(clsField, 'has_default_value', retval('bool'), [], is_const = True), #add_method(clsField, 'default_value', retval('const bamboo::Value'), [], is_const = True) add_method(clsField, 'set_name', retval('bool'), [param('std::string', 'name')]) add_method(clsField, 'set_type', None, [param('bamboo::Type *', 'type', transfer_ownership = False)]), #add_method(clsField, 'set_default_value', retval('bool'), [param('const bamboo::Value', 'value')]) #add_method(clsField, 'set_default_value', retval('bool'), [param('const bamboo::Buffer&', 'value')]) clsMolecular.add_constructor([ param('bamboo::Class *', 'cls', transfer_ownership = False), param('std::string', 'name')]) clsDatagram.add_constructor([]) clsDatagram.add_copy_constructor() add_method(clsDatagram, 'size', retval('size_t'), [], is_const = True) add_method(clsDatagram, 'cap', retval('size_t'), [], is_const = True) add_method(clsDatagram, 'add_bool', None, [param('bool', 'value')], throw = [dgOverflowError]) add_method(clsDatagram, 'add_char', None, [param('char', 'value')], throw = [dgOverflowError]) add_method(clsDatagram, 'add_int8', None, [param('int8_t', 'value')], throw = [dgOverflowError]) add_method(clsDatagram, 'add_int16', None, [param('int16_t', 'value')], throw = [dgOverflowError]) add_method(clsDatagram, 'add_int32', None, [param('int32_t', 'value')], throw = [dgOverflowError]) add_method(clsDatagram, 'add_int64', None, [param('int64_t', 'value')], throw = [dgOverflowError]) add_method(clsDatagram, 'add_uint8', None, [param('uint8_t', 'value')], throw = [dgOverflowError]) add_method(clsDatagram, 'add_uint16', None, [param('uint16_t', 'value')], throw = [dgOverflowError]) add_method(clsDatagram, 'add_uint32', None, [param('uint32_t', 'value')], throw = [dgOverflowError]) add_method(clsDatagram, 'add_uint64', None, [param('uint64_t', 'value')], throw = [dgOverflowError]) add_method(clsDatagram, 'add_float32', None, [param('float', 'value')], throw = [dgOverflowError]) add_method(clsDatagram, 'add_float64', None, [param('double', 'value')], throw = [dgOverflowError]) add_custom_method(clsDatagram, 'add_data') add_custom_method(clsDatagram, 'add_value') add_custom_method(clsDatagram, 'data', ('METH_NOARGS',)) # add_string also moonlights as add_blob and addBlob add_method(clsDatagram, 'add_string', None, [param('std::string', 'value')], throw = [dgOverflowError]) clsDgIter.add_constructor([ param('const bamboo::Datagram&', 'dg'), param('size_t', 'offset', default_value = '0')]) clsDgIter.add_copy_constructor() add_method(clsDgIter, 'tell', retval('size_t'), [], is_const = True) add_method(clsDgIter, 'seek', None, [param('size_t', 'offset')]) add_method(clsDgIter, 'skip', None, [param('size_t', 'length')], throw = [dgiEOFError]) add_method(clsDgIter, 'skip_type', None, [param('const bamboo::Type *', 'type', transfer_ownership = False)], throw = [dgiEOFError]) add_method(clsDgIter, 'remaining', retval('size_t'), [], is_const = True) add_method(clsDgIter, 'read_bool', retval('bool'), [], throw = [dgiEOFError]) add_method(clsDgIter, 'read_char', retval('char'), [], throw = [dgiEOFError]) add_method(clsDgIter, 'read_int8', retval('int8_t'), [], throw = [dgiEOFError]) add_method(clsDgIter, 'read_int16', retval('int16_t'), [], throw = [dgiEOFError]) add_method(clsDgIter, 'read_int32', retval('int32_t'), [], throw = [dgiEOFError]) add_method(clsDgIter, 'read_int64', retval('int64_t'), [], throw = [dgiEOFError]) add_method(clsDgIter, 'read_uint8', retval('uint8_t'), [], throw = [dgiEOFError]) add_method(clsDgIter, 'read_uint16', retval('uint16_t'), [], throw = [dgiEOFError]) add_method(clsDgIter, 'read_uint32', retval('uint32_t'), [], throw = [dgiEOFError]) add_method(clsDgIter, 'read_uint64', retval('uint64_t'), [], throw = [dgiEOFError]) add_method(clsDgIter, 'read_float32', retval('float'), [], throw = [dgiEOFError]) add_method(clsDgIter, 'read_float64', retval('double'), [], throw = [dgiEOFError]) add_method(clsDgIter, 'read_size', retval('size_t'), [], throw = [dgiEOFError]) # read_string also moonlights as read_blob and readBlob add_method(clsDgIter, 'read_string', retval('std::string'), [], throw = [dgiEOFError]) add_method(clsDgIter, 'read_datagram', retval('bamboo::Datagram'), [], throw = [dgiEOFError]) add_custom_method(clsDgIter, 'read_value') #add_method(clsDgIter, 'read_value', retval('bamboo::Value'), # [param('const bamboo::Type *', 'type', transfer_ownership = False)]) #add_method(clsDgIter, 'read_data', retval('std::string'), [param('size_t', 'length')]) #add_method(clsDgIter, 'read_remainder', retval('std::string'), []) add_function(traits, 'legacy_hash', retval('uint32_t'), [param('const bamboo::Module *', 'module', transfer_ownership = False)]) add_function(dcfile, 'read_dcfile', retval('bamboo::Module *', caller_owns_return = True), [param('std::string', 'filename')]) add_function(dcfile, 'parse_dcfile', retval('bool'), [param('bamboo::Module *', 'module', transfer_ownership = False), param('std::string', 'filename')]) #add_function(dcfile, 'parse_dcvalue', retval('bamboo::Buffer'), # [param('const bamboo::Type *', 'type', transfer_ownership = False), # param('std::string', 'formattedValue'), param('bool&', 'isError')]) bamboo.generate(file_)
def test(): code_out = codesink.FileCodeSink(sys.stdout) pybindgen.write_preamble(code_out) print print "#include <string>" print "#include <stdint.h>" print ## Declare a dummy class sys.stdout.write( """ class Foo { std::string m_datum; public: Foo () : m_datum ("") {} Foo (std::string datum) : m_datum (datum) {} std::string get_datum () const { return m_datum; } Foo (Foo const & other) : m_datum (other.get_datum ()) {} }; """ ) module = Module("foo") ## Register type handlers for the class Foo = cppclass.CppClass("Foo") Foo.module = module # Foo.full_name = Foo.name # normally adding the class to a module would take care of this Foo.generate_forward_declarations(code_out, module) wrapper_number = 0 ## test return type handlers of reverse wrappers for return_type, return_handler in typehandlers.base.return_type_matcher.items(): if os.name == "nt": if stdint_rx.search(return_type): continue # win32 does not support the u?int\d+_t types (defined in <stdint.h>) if issubclass(return_handler, (cppclass.CppClassPtrReturnValue, typehandlers.pyobjecttype.PyObjectReturnValue)): for caller_owns_return in True, False: retval = return_handler(return_type, caller_owns_return=caller_owns_return) wrapper = MyReverseWrapper(retval, []) wrapper_number += 1 try: wrapper.generate(code_out, "_test_wrapper_number_%i" % (wrapper_number,), ["static"]) except NotImplementedError: print >> sys.stderr, ( "ReverseWrapper %s(void) (caller_owns_return=%r)" " could not be generated: not implemented" % (retval.ctype, caller_owns_return) ) print else: retval = return_handler(return_type) try: wrapper = MyReverseWrapper(retval, []) except NotSupportedError: continue wrapper_number += 1 try: wrapper.generate(code_out, "_test_wrapper_number_%i" % (wrapper_number,), ["static"]) except NotImplementedError: print >> sys.stderr, ( "ReverseWrapper %s(void) could not be generated: not implemented" % (retval.ctype,) ) print ## test parameter type handlers of reverse wrappers for param_type, param_handler in typehandlers.base.param_type_matcher.items(): if os.name == "nt": if stdint_rx.search(param_type): continue # win32 does not support the u?int\d+_t types (defined in <stdint.h>) for direction in param_handler.DIRECTIONS: if direction == (Parameter.DIRECTION_IN): param_name = "param" elif direction == (Parameter.DIRECTION_IN | Parameter.DIRECTION_OUT): param_name = "param_inout" elif direction == (Parameter.DIRECTION_OUT): param_name = "param_out" param = param_handler(param_type, param_name, direction) if "const" in param.ctype and direction & Parameter.DIRECTION_OUT: continue wrapper = MyReverseWrapper(ReturnValue.new("void"), [param]) wrapper_number += 1 try: wrapper.generate(code_out, "_test_wrapper_number_%i" % (wrapper_number,), ["static"]) except NotImplementedError: print >> sys.stderr, ("ReverseWrapper void(%s) could not be generated: not implemented" % (param.ctype)) print ## test generic forward wrappers, and module for return_type, return_handler in typehandlers.base.return_type_matcher.items(): if os.name == "nt": if stdint_rx.search(return_type): continue # win32 does not support the u?int\d+_t types (defined in <stdint.h>) wrapper_number += 1 function_name = "foo_function_%i" % (wrapper_number,) ## declare a fake prototype print "%s %s(void);" % (return_type, function_name) print if issubclass(return_handler, (cppclass.CppClassPtrReturnValue, typehandlers.pyobjecttype.PyObjectReturnValue)): retval = return_handler(return_type, caller_owns_return=True) else: retval = return_handler(return_type) module.add_function(function_name, retval, []) for param_type, param_handler in typehandlers.base.param_type_matcher.items(): if os.name == "nt": if stdint_rx.search(param_type): continue # win32 does not support the u?int\d+_t types (defined in <stdint.h>) for is_const in [True, False]: for direction in param_handler.DIRECTIONS: if direction == (Parameter.DIRECTION_IN): param_name = "param" elif direction == (Parameter.DIRECTION_IN | Parameter.DIRECTION_OUT): param_name = "param_inout" elif direction == (Parameter.DIRECTION_OUT): param_name = "param_out" if is_const and direction & Parameter.DIRECTION_OUT: continue # const and output parameter makes no sense if is_const: if "&" in param_type: # const references not allowed continue param_type_with_const = "const %s" % (param_type,) else: param_type_with_const = param_type if issubclass(param_handler, (cppclass.CppClassPtrParameter, typehandlers.pyobjecttype.PyObjectParam)): for transfer_ownership in True, False: name = param_name + (transfer_ownership and "_transfer" or "_notransfer") try: param = param_handler(param_type, name, transfer_ownership=transfer_ownership) except TypeError: print >> sys.stderr, "ERROR -----> param_handler(param_type=%r, " "name=%r, transfer_ownership=%r, is_const=%r)" % ( param_type, name, transfer_ownership, is_const, ) wrapper_number += 1 function_name = "foo_function_%i" % (wrapper_number,) ## declare a fake prototype print "void %s(%s %s);" % (function_name, param_type_with_const, name) print module.add_function(function_name, ReturnValue.new("void"), [param]) else: param = param_handler(param_type, param_name, direction) wrapper_number += 1 function_name = "foo_function_%i" % (wrapper_number,) ## declare a fake prototype print "void %s(%s);" % (function_name, param_type_with_const) print module.add_function(function_name, ReturnValue.new("void"), [param]) module.generate(code_out)