diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index 3af23c1..645edcc 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -1652,6 +1652,16 @@ static void return tp_richcompare_function_name + def generate_typedef(self, module, alias): + """ + Generates the appropriate Module code to register the class + with a new name in that module (typedef alias). + """ + module.after_init.write_code( + 'PyModule_AddObject(m, \"%s\", (PyObject *) &%s);' % ( + alias, self.pytypestruct)) + + ### ### ------------ C++ class parameter type handlers ------------ ### diff --git a/pybindgen/module.py b/pybindgen/module.py index 16b65e1..434e92e 100644 --- a/pybindgen/module.py +++ b/pybindgen/module.py @@ -215,6 +215,7 @@ class ModuleBase(dict): self.docstring = docstring self.submodules = [] self.enums = [] + self.typedefs = [] # list of (wrapper, alias) tuples self._forward_declarations_declared = False self.cpp_namespace = cpp_namespace @@ -675,6 +676,12 @@ class ModuleBase(dict): class_.generate(sink, self) sink.writeln() + # typedefs + for (wrapper, alias) in self.typedefs: + if isinstance(wrapper, CppClass): + cls = wrapper + cls.generate_typedef(self, alias) + ## generate the enums if self.enums: main_sink.writeln('/* --- enumerations --- */') @@ -727,7 +734,27 @@ class ModuleBase(dict): def __repr__(self): return "" % self.name - + + def add_typedef(self, wrapper, alias): + """ + Declares an equivalent to a typedef in C:: + typedef Foo Bar; + + @param wrapper: the wrapper object to alias (Foo in the example) + @param alias: name of the typedef alias + + @note: only typedefs for CppClass objects have been + implemented so far; others will be implemented in the future. + """ + assert isinstance(wrapper, CppClass) + assert isinstance(alias, str) + self.typedefs.append((wrapper, alias)) + self.register_type(alias, alias, wrapper) + wrapper.register_alias(alias) + full_name = '::'.join(self.get_namespace_path() + [alias]) + import sys; print >> sys.stderr, "***** full_name", full_name + wrapper.register_alias(full_name) + class Module(ModuleBase): def __init__(self, name, docstring=None, cpp_namespace=None): diff --git a/tests/foo.cc b/tests/foo.cc index 8654d6b..3a89daa 100644 --- a/tests/foo.cc +++ b/tests/foo.cc @@ -92,6 +92,12 @@ namespace xpto { return "hello"; } + + std::string get_foo_datum(FooXpto const &foo) + { + return foo.get_datum(); + } + } diff --git a/tests/foo.h b/tests/foo.h index 2253919..d7311a3 100644 --- a/tests/foo.h +++ b/tests/foo.h @@ -420,6 +420,10 @@ namespace xpto FooType get_foo_type (); void set_foo_type (FooType type); + + typedef Foo FooXpto; + + std::string get_foo_datum(FooXpto const &foo); } // -#- @return(custodian=1) -#- diff --git a/tests/foomodulegen.py b/tests/foomodulegen.py index a5a7c55..ded4eb6 100755 --- a/tests/foomodulegen.py +++ b/tests/foomodulegen.py @@ -243,6 +243,10 @@ int %s::custom_method_added_by_a_hook(int x) xpto_SomeClass = xpto.add_class('SomeClass') xpto_SomeClass.add_constructor([]) + xpto.add_typedef(Foo, 'FooXpto') + xpto.add_function('get_foo_datum', 'std::string', [Parameter.new('xpto::FooXpto&', 'foo', is_const=True)]) + + ## ---- some implicity conversion APIs mod.add_function('function_that_takes_foo', ReturnValue.new('void'), [Parameter.new('Foo', 'foo')]) diff --git a/tests/footest.py b/tests/footest.py index 01086e8..39cff1f 100644 --- a/tests/footest.py +++ b/tests/footest.py @@ -740,6 +740,17 @@ class TestFoo(unittest.TestCase): struct.xpto = 123 self.assertEqual(struct.xpto, 123) + def test_typedef(self): + try: + f = foo.xpto.FooXpto("hello") + except AttributeError: + self.fail() + try: + r = foo.xpto.get_foo_datum(f) + except TypeError: + self.fail() + self.assertEqual(r, "hello") + if __name__ == '__main__': unittest.main()