From d4c3999bc49447ae3d46a8b77dc885d2b8aa654f Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Sun, 2 Mar 2008 18:04:31 +0000 Subject: [PATCH] Add support for c-function-as-constructor (for is_constructor_of annotation). --- pybindgen/cppclass.py | 9 +++++++-- pybindgen/cppmethod.py | 24 ++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index 40f7256..e743b3a 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -10,7 +10,7 @@ from typehandlers.base import ForwardWrapperBase, ReverseWrapperBase, Parameter, from typehandlers.codesink import NullCodeSink, MemoryCodeSink -from cppmethod import CppMethod, CppConstructor, CppNoConstructor, \ +from cppmethod import CppMethod, CppConstructor, CppNoConstructor, CppFunctionAsConstructor, \ CppOverloadedMethod, CppOverloadedConstructor, \ CppVirtualMethodParentCaller, CppVirtualMethodProxy @@ -832,6 +832,7 @@ public: method.is_pure_virtual = False method.self_parameter_pystruct = self.pystruct method.visibility = 'public' + method.force_parse = method.PARSE_TUPLE_AND_KEYWORDS else: raise TypeError @@ -879,7 +880,11 @@ public: wrapper -- a CppConstructor instance """ - assert isinstance(wrapper, CppConstructor) + if isinstance(wrapper, function.Function): + wrapper = CppFunctionAsConstructor(wrapper.parameters, wrapper.function_name) + wrapper.module = self.module + else: + assert isinstance(wrapper, CppConstructor) wrapper.set_class(self) self.constructors.append(wrapper) if not wrapper.parameters: diff --git a/pybindgen/cppmethod.py b/pybindgen/cppmethod.py index ac4ba2f..4a9ebb4 100644 --- a/pybindgen/cppmethod.py +++ b/pybindgen/cppmethod.py @@ -331,6 +331,25 @@ class CppConstructor(ForwardWrapperBase): code_sink.writeln('}') +class CppFunctionAsConstructor(CppConstructor): + """ + Class that generates a wrapper to a C/C++ function that appears as a contructor. + """ + def __init__(self, parameters, c_function_name, unblock_threads=None): + if unblock_threads is None: + unblock_threads = settings.unblock_threads + super(CppFunctionAsConstructor, self).__init__(parameters) + self.c_function_name = c_function_name + + def generate_call(self, class_=None): + "virtual method implementation; do not call" + if class_ is None: + class_ = self._class + #assert isinstance(class_, CppClass) + assert class_.helper_class is None + self.before_call.write_code("self->obj = %s(%s);" % + (self.c_function_name, ", ".join(self.call_params))) + class CppOverloadedConstructor(overloading.OverloadedWrapper): "Support class for overloaded constructors" @@ -481,9 +500,9 @@ class CppVirtualMethodParentCaller(CppMethod): '|'.join(flags), (self.docstring is None and "NULL" or ('"'+self.docstring+'"'))) - def clone(self): - """Creates a semi-deep copy of this method wrapper. The returned + """ + Creates a semi-deep copy of this method wrapper. The returned method wrapper clone contains copies of all parameters, so they can be modified at will. """ @@ -506,6 +525,7 @@ class CppVirtualMethodProxy(ReverseWrapperBase): def __init__(self, method): """ + xxx """ super(CppVirtualMethodProxy, self).__init__(method.return_value, method.parameters) self.method_name = method.method_name