diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index 7404a1d..5764d6d 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -1020,22 +1020,52 @@ public: assert isinstance(reason, basestring) self.cannot_be_constructed = reason - def add_constructor(self, wrapper): + def _add_constructor_obj(self, wrapper): """ Add a constructor to the class. @param wrapper: a CppConstructor instance """ - if isinstance(wrapper, function.Function): - wrapper = CppFunctionAsConstructor(wrapper.parameters, wrapper.function_name) - wrapper.module = self.module - else: - assert isinstance(wrapper, CppConstructor) + assert isinstance(wrapper, CppConstructor) wrapper.set_class(self) self.constructors.append(wrapper) if not wrapper.parameters: self.has_trivial_constructor = True + def add_constructor(self, *args, **kwargs): + """ + Add a constructor to the class. See the documentation for + L{CppConstructor.__init__} for information on accepted parameters. + """ + + ## + if len(args) == 1 and isinstance(args[0], CppConstructor): + warnings.warn("add_constructor has changed API; see the API documentation", + DeprecationWarning, stacklevel=2) + #raise TypeError("FIXME: remove this exception") + constructor = args[0] + elif len(args) == 1 and isinstance(args[0], function.Function): + warnings.warn("add_constructor has changed API; see the API documentation", + DeprecationWarning, stacklevel=2) + #raise TypeError("FIXME: remove this exception") + func = args[0] + constructor = CppFunctionAsConstructor(func.function_name, func.parameters) + constructor.module = self.module + ## + else: + constructor = CppConstructor(*args, **kwargs) + self._add_constructor_obj(constructor) + return constructor + + def add_function_as_constructor(self, *args, **kwargs): + """ + Wrap a function that behaves as a constructor to the class. See the documentation for + L{CppFunctionAsConstructor.__init__} for information on accepted parameters. + """ + constructor = CppFunctionAsConstructor(*args, **kwargs) + self._add_constructor_obj(constructor) + return constructor + def add_static_attribute(self, value_type, name, is_const=False): """ @param value_type: a ReturnValue object diff --git a/pybindgen/cppmethod.py b/pybindgen/cppmethod.py index 6513dc7..8d67899 100644 --- a/pybindgen/cppmethod.py +++ b/pybindgen/cppmethod.py @@ -383,7 +383,16 @@ 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): + def __init__(self, c_function_name, parameters, unblock_threads=None): + """ + @param c_function_name: name of the C/C++ function; it is + implied that this function returns a pointer to the a class + instance with caller_owns_return semantics. + + @param parameters: the function/constructor parameters + @type parameters: list of L{Parameter} + + """ if unblock_threads is None: unblock_threads = settings.unblock_threads super(CppFunctionAsConstructor, self).__init__(parameters)