From 311f18b98d2b553af0e864912f94fb18c6a552f1 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Tue, 19 Aug 2008 14:39:50 +0100 Subject: [PATCH] Fix missing copy constructor that was not being generated into the python script. Add utility method to easily add a copy constructor to a class. --- pybindgen/cppclass.py | 13 +++++++++++++ pybindgen/gccxmlparser.py | 6 +++--- pybindgen/module.py | 13 +++++++++++++ tests/footest.py | 12 ++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index 2cb9660..a465974 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -1185,6 +1185,19 @@ public: self._add_constructor_obj(constructor) return constructor + def add_copy_constructor(self): + """ + Utility method to add a 'copy constructor' method to this class. + """ + try: + constructor = CppConstructor([self.ThisClassRefParameter("%s &" % self.full_name, + 'ctor_arg', is_const=True)]) + except utils.SkipWrapper: + return None + constructor.stack_where_defined = traceback.extract_stack() + 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 diff --git a/pybindgen/gccxmlparser.py b/pybindgen/gccxmlparser.py index 66eb3a7..5e795b7 100644 --- a/pybindgen/gccxmlparser.py +++ b/pybindgen/gccxmlparser.py @@ -1692,9 +1692,9 @@ pybindgen.settings.error_handler = ErrorHandler() except AttributeError: # pygccxml <= 0.9 has_copy_constructor = type_traits.has_trivial_copy(cls) if has_copy_constructor: - class_wrapper.add_constructor([ - class_wrapper.ThisClassRefParameter("%s &" % class_wrapper.full_name, - 'ctor_arg', is_const=True)]) + class_wrapper.add_copy_constructor() + pygen_sink.writeln("cls.add_copy_constructor()") + if has_output_stream_operator: class_wrapper.add_output_stream_operator() pygen_sink.writeln("cls.add_output_stream_operator()") diff --git a/pybindgen/module.py b/pybindgen/module.py index c10ffe2..867877a 100644 --- a/pybindgen/module.py +++ b/pybindgen/module.py @@ -474,6 +474,10 @@ class ModuleBase(dict): have a constructor by default (if omitted, it will be considered to have a trivial constructor). + - no_copy (bool): if True, the structure will not + have a copy constructor by default (if omitted, it will be + considered to have a simple copy constructor). + """ try: @@ -482,12 +486,21 @@ class ModuleBase(dict): no_constructor = False else: del kwargs['no_constructor'] + + try: + no_copy = kwargs['no_copy'] + except KeyError: + no_copy = False + else: + del kwargs['no_copy'] struct = CppClass(*args, **kwargs) struct.stack_where_defined = traceback.extract_stack() self._add_class_obj(struct) if not no_constructor: struct.add_constructor([]) + if not no_copy: + struct.add_copy_constructor() return struct diff --git a/tests/footest.py b/tests/footest.py index aaa966e..41c7609 100644 --- a/tests/footest.py +++ b/tests/footest.py @@ -889,6 +889,18 @@ class TestFoo(unittest.TestCase): rv = test.set_simple_vec(container) self.assertEqual(rv, sum(range(10))) + def test_copy(self): + s1 = foo.simple_struct_t() + s1.xpto = 123 + + # copy via constructor + s2 = foo.simple_struct_t(s1) + self.assertEqual(s2.xpto, 123) + s2.xpto = 321 + self.assertEqual(s2.xpto, 321) + self.assertEqual(s1.xpto, 123) + + # copy via __copy__ TODO if __name__ == '__main__': unittest.main()