diff --git a/examples/b/modulegen.py b/examples/b/modulegen.py index 2aec57a..c780828 100644 --- a/examples/b/modulegen.py +++ b/examples/b/modulegen.py @@ -15,8 +15,8 @@ def my_module_gen(out_file): B = mod.add_class('B') B.add_constructor([]) - B.add_instance_attribute(ReturnValue.new('uint32_t'), 'b_a') - B.add_instance_attribute(ReturnValue.new('uint32_t'), 'b_b') + B.add_instance_attribute('b_a', ReturnValue.new('uint32_t')) + B.add_instance_attribute('b_b', ReturnValue.new('uint32_t')) mod.add_function('BDoA', None, [Parameter.new('B', 'b')]) mod.add_function('BDoB', ReturnValue.new('B'), []) diff --git a/examples/d/modulegen.py b/examples/d/modulegen.py index 29f34a8..363eac7 100644 --- a/examples/d/modulegen.py +++ b/examples/d/modulegen.py @@ -14,7 +14,7 @@ def my_module_gen(out_file): mod.add_include('"d.h"') D = mod.add_class('D', free_function='DDestroy') - D.add_instance_attribute(ReturnValue.new('bool'), 'd') + D.add_instance_attribute('d', ReturnValue.new('bool')) D.add_function_as_constructor("DCreate", ReturnValue.new("D*", caller_owns_return=True), []) mod.add_function('DDoA', None, [Parameter.new('D*', 'd', transfer_ownership=False)]) mod.add_function('DDoB', None, [Parameter.new('D&', 'd', direction=Parameter.DIRECTION_IN)]) diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index ee3e222..a1aab54 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -1066,12 +1066,19 @@ public: self._add_constructor_obj(constructor) return constructor - def add_static_attribute(self, value_type, name, is_const=False): + def add_static_attribute(self, name, value_type, is_const=False): """ @param value_type: a ReturnValue object @param name: attribute name (i.e. the name of the class member variable) @param is_const: True if the attribute is const, i.e. cannot be modified """ + + ## backward compatibility check + if isinstance(value_type, str) and isinstance(name, ReturnValue): + warnings.warn("add_static_attribute has changed API; see the API documentation (but trying to correct...)", + DeprecationWarning, stacklevel=2) + value_type, name = name, value_type + assert isinstance(value_type, ReturnValue) getter = CppStaticAttributeGetter(value_type, self, name) if is_const: @@ -1080,7 +1087,7 @@ public: setter = CppStaticAttributeSetter(value_type, self, name) self.static_attributes.add_attribute(name, getter, setter) - def add_instance_attribute(self, value_type, name, is_const=False, + def add_instance_attribute(self, name, value_type, is_const=False, getter=None, setter=None): """ @param value_type: a ReturnValue object @@ -1089,6 +1096,13 @@ public: @param getter: None, or name of a method of this class used to get the value @param setter: None, or name of a method of this class used to set the value """ + + ## backward compatibility check + if isinstance(value_type, str) and isinstance(name, ReturnValue): + warnings.warn("add_static_attribute has changed API; see the API documentation (but trying to correct...)", + DeprecationWarning, stacklevel=2) + value_type, name = name, value_type + assert isinstance(value_type, ReturnValue) getter_wrapper = CppInstanceAttributeGetter(value_type, self, name, getter=getter) if is_const: diff --git a/pybindgen/gccxmlparser.py b/pybindgen/gccxmlparser.py index 90d3002..d2f549d 100644 --- a/pybindgen/gccxmlparser.py +++ b/pybindgen/gccxmlparser.py @@ -1179,15 +1179,15 @@ class ModuleParser(object): Warning, member.location.file_name, member.location.line) continue if member.type_qualifiers.has_static: - class_wrapper.add_static_attribute(return_type, member.name, + class_wrapper.add_static_attribute(member.name, return_type, is_const=type_traits.is_const(member.type)) - self.pygen_sink.writeln("cls.add_static_attribute(%s, %r, is_const=%r)" % - (return_type._pygen_repr, member.name, type_traits.is_const(member.type))) + self.pygen_sink.writeln("cls.add_static_attribute(%r, %s, is_const=%r)" % + (member.name, return_type._pygen_repr, type_traits.is_const(member.type))) else: - class_wrapper.add_instance_attribute(return_type, member.name, + class_wrapper.add_instance_attribute(member.name, return_type, is_const=type_traits.is_const(member.type)) - self.pygen_sink.writeln("cls.add_instance_attribute(%s, %r, is_const=%r)" % - (return_type._pygen_repr, member.name, type_traits.is_const(member.type))) + self.pygen_sink.writeln("cls.add_instance_attribute(%r, %s, is_const=%r)" % + (member.name, return_type._pygen_repr, type_traits.is_const(member.type))) ## TODO: invoke post_scan_hooks elif isinstance(member, calldef.destructor_t): pass diff --git a/tests/foomodulegen.py b/tests/foomodulegen.py index a3e561a..c351e3f 100755 --- a/tests/foomodulegen.py +++ b/tests/foomodulegen.py @@ -24,7 +24,7 @@ def my_module_gen(out_file): Foo = mod.add_class('Foo', automatic_type_narrowing=True) - Foo.add_static_attribute(ReturnValue.new('int'), 'instance_count') + Foo.add_static_attribute('instance_count', ReturnValue.new('int')) Foo.add_constructor([Parameter.new('std::string', 'datum')]) Foo.add_constructor([]) Foo.add_method('get_datum', ReturnValue.new('std::string'), []) @@ -37,7 +37,7 @@ def my_module_gen(out_file): Zoo.implicitly_converts_to(Foo) Foobar = mod.add_class('Foobar') - Foobar.add_static_attribute(ReturnValue.new('int'), 'instance_count') + Foobar.add_static_attribute('instance_count', ReturnValue.new('int')) Bar = mod.add_class('Bar', parent=Foo) @@ -71,7 +71,7 @@ int %s::custom_method_added_by_a_hook(int x) Zbr.add_method('get_datum', ReturnValue.new('std::string'), []) Zbr.add_method('get_int', ReturnValue.new('int'), [Parameter.new('int', 'x')], is_virtual=True) - Zbr.add_static_attribute(ReturnValue.new('int'), 'instance_count') + Zbr.add_static_attribute('instance_count', ReturnValue.new('int')) mod.add_function('store_zbr', None, [Parameter.new('Zbr*', 'zbr', transfer_ownership=True)]) @@ -95,13 +95,13 @@ int %s::custom_method_added_by_a_hook(int x) SomeObject = mod.add_class('SomeObject', allow_subclassing=True) - SomeObject.add_instance_attribute(ReturnValue.new('Foo'), 'foo', + SomeObject.add_instance_attribute('foo', ReturnValue.new('Foo'), getter='get_foo_value', setter='set_foo_value') - SomeObject.add_instance_attribute(ReturnValue.new('std::string'), 'm_prefix') - SomeObject.add_static_attribute(ReturnValue.new('std::string'), 'staticData') + SomeObject.add_instance_attribute('m_prefix', ReturnValue.new('std::string')) + SomeObject.add_static_attribute('staticData', ReturnValue.new('std::string')) - SomeObject.add_static_attribute(ReturnValue.new('int'), 'instance_count') + SomeObject.add_static_attribute('instance_count', ReturnValue.new('int')) SomeObject.add_method('add_prefix', ReturnValue.new('int'), [Parameter.new('std::string&', 'message', @@ -280,7 +280,7 @@ int %s::custom_method_added_by_a_hook(int x) ## A nested class NestedClass = mod.add_class('NestedClass', automatic_type_narrowing=True, outer_class=SomeObject) - NestedClass.add_static_attribute(ReturnValue.new('int'), 'instance_count') + NestedClass.add_static_attribute('instance_count', ReturnValue.new('int')) NestedClass.add_constructor([Parameter.new('std::string', 'datum')]) NestedClass.add_constructor([]) NestedClass.add_method('get_datum', ReturnValue.new('std::string'), [])