Update tests/examples to use new add_constructor API
This commit is contained in:
1 parent
0ae02adc89
commit
ee6589e95d
12 files changed
+48
-54
No files matched your search
@@ -14,7 +14,7 @@ def my_module_gen(out_file):
|
||||
mod.add_include('"b.h"')
|
||||
|
||||
B = mod.add_class('B')
|
||||
B.add_constructor(CppConstructor([]))
|
||||
B.add_constructor([])
|
||||
B.add_instance_attribute(ReturnValue.new('uint32_t'), 'b_a')
|
||||
B.add_instance_attribute(ReturnValue.new('uint32_t'), 'b_b')
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ def my_module_gen(out_file):
|
||||
mod.add_include('"c.h"')
|
||||
|
||||
C = mod.add_class('C')
|
||||
C.add_constructor(CppConstructor([]))
|
||||
C.add_constructor(CppConstructor([Parameter.new('uint32_t', 'c')]))
|
||||
C.add_constructor([])
|
||||
C.add_constructor([Parameter.new('uint32_t', 'c')])
|
||||
C.add_method('DoA', None, [], is_static=True)
|
||||
C.add_method('DoB', None, [])
|
||||
C.add_method('DoC', None, [Parameter.new('uint32_t', 'c')])
|
||||
|
||||
@@ -15,7 +15,7 @@ def my_module_gen(out_file):
|
||||
|
||||
D = mod.add_class('D', free_function='DDestroy')
|
||||
D.add_instance_attribute(ReturnValue.new('bool'), 'd')
|
||||
D.add_constructor(Function("DCreate", ReturnValue.new("D*", caller_owns_return=True), []))
|
||||
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)])
|
||||
mod.add_function('DDoC', None, [Parameter.new('D&', 'd',
|
||||
|
||||
@@ -15,10 +15,10 @@ def my_module_gen(out_file):
|
||||
|
||||
E = mod.add_class('E', decref_method='Unref', incref_method='Ref')
|
||||
if 1:
|
||||
E.add_constructor(Function("E::CreateWithRef", ReturnValue.new("E*", caller_owns_return=True), []))
|
||||
E.add_function_as_constructor("E::CreateWithRef", ReturnValue.new("E*", caller_owns_return=True), [])
|
||||
else:
|
||||
## alternative:
|
||||
E.add_constructor(Function(ReturnValue.new("E*", caller_owns_return=False), "E::CreateWithoutRef", []))
|
||||
E.add_function_as_constructor("E::CreateWithoutRef", ReturnValue.new("E*", caller_owns_return=False), [])
|
||||
E.add_method("Do", None, [])
|
||||
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ def my_module_gen(out_file):
|
||||
|
||||
FBase = mod.add_class('FBase', allow_subclassing=True)
|
||||
|
||||
FBase.add_constructor(CppConstructor([]))
|
||||
FBase.add_constructor([])
|
||||
FBase.add_method('DoA', None, [], is_virtual=True, is_pure_virtual=True)
|
||||
FBase.add_method('PrivDoB', None, [], is_virtual=True, is_pure_virtual=True, visibility='private')
|
||||
FBase.add_method('DoB', None, [])
|
||||
|
||||
@@ -12,15 +12,15 @@ def my_module_gen(out_file):
|
||||
mod.add_include('"h.h"')
|
||||
|
||||
H = mod.add_class('H')
|
||||
H.add_constructor(CppConstructor([]))
|
||||
H.add_constructor([])
|
||||
H.add_method('Do', None, [])
|
||||
|
||||
Inner = mod.add_class('Inner', outer_class=H)
|
||||
Inner.add_constructor(CppConstructor([]))
|
||||
Inner.add_constructor([])
|
||||
Inner.add_method('Do', None, [])
|
||||
|
||||
MostInner = mod.add_class('MostInner', outer_class=Inner)
|
||||
MostInner.add_constructor(CppConstructor([]))
|
||||
MostInner.add_constructor([])
|
||||
MostInner.add_method('Do', None, [])
|
||||
|
||||
mod.generate(FileCodeSink(out_file))
|
||||
|
||||
@@ -815,10 +815,10 @@ class CppClass(object):
|
||||
return
|
||||
for cons in self.parent.constructors:
|
||||
if len(cons.parameters) == 0:
|
||||
self.add_constructor(CppConstructor([]))
|
||||
self.add_constructor([])
|
||||
elif (len(cons.parameters) == 1
|
||||
and isinstance(cons.parameters[0], self.parent.ThisClassRefParameter)):
|
||||
self.add_constructor(CppConstructor([self.ThisClassRefParameter()]))
|
||||
self.add_constructor([self.ThisClassRefParameter()])
|
||||
|
||||
def get_helper_class(self):
|
||||
"""gets the "helper class" for this class wrapper, creating it if necessary"""
|
||||
@@ -1042,12 +1042,12 @@ public:
|
||||
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")
|
||||
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")
|
||||
raise TypeError("FIXME: remove this exception")
|
||||
func = args[0]
|
||||
constructor = CppFunctionAsConstructor(func.function_name, func.parameters)
|
||||
constructor.module = self.module
|
||||
|
||||
@@ -383,11 +383,14 @@ class CppFunctionAsConstructor(CppConstructor):
|
||||
"""
|
||||
Class that generates a wrapper to a C/C++ function that appears as a contructor.
|
||||
"""
|
||||
def __init__(self, c_function_name, parameters, unblock_threads=None):
|
||||
def __init__(self, c_function_name, return_value, parameters, unblock_threads=None):
|
||||
"""
|
||||
@param c_function_name: name of the C/C++ function; it is
|
||||
@param c_function_name: name of the C/C++ function; FIXME: for now it is
|
||||
implied that this function returns a pointer to the a class
|
||||
instance with caller_owns_return semantics.
|
||||
instance with caller_owns_return=True semantics.
|
||||
|
||||
@param return_value: function return value type
|
||||
@type return_value: L{ReturnValue}
|
||||
|
||||
@param parameters: the function/constructor parameters
|
||||
@type parameters: list of L{Parameter}
|
||||
@@ -397,6 +400,7 @@ class CppFunctionAsConstructor(CppConstructor):
|
||||
unblock_threads = settings.unblock_threads
|
||||
super(CppFunctionAsConstructor, self).__init__(parameters)
|
||||
self.c_function_name = c_function_name
|
||||
self.function_return_value = return_value
|
||||
|
||||
def generate_call(self, class_=None):
|
||||
"virtual method implementation; do not call"
|
||||
@@ -404,6 +408,7 @@ class CppFunctionAsConstructor(CppConstructor):
|
||||
class_ = self._class
|
||||
#assert isinstance(class_, CppClass)
|
||||
assert class_.helper_class is None
|
||||
## FIXME: check caller_owns_return in self.function_return_value
|
||||
self.before_call.write_code("self->obj = %s(%s);" %
|
||||
(self.c_function_name, ", ".join(self.call_params)))
|
||||
|
||||
|
||||
@@ -1147,9 +1147,8 @@ class ModuleParser(object):
|
||||
ok = True
|
||||
if not ok:
|
||||
continue
|
||||
constructor_wrapper = CppConstructor(arguments, visibility=member.access_type)
|
||||
constructor_wrapper = class_wrapper.add_constructor(arguments, visibility=member.access_type)
|
||||
constructor_wrapper.gccxml_definition = member
|
||||
class_wrapper.add_constructor(constructor_wrapper)
|
||||
for hook in self._post_scan_hooks:
|
||||
hook(self, member, constructor_wrapper)
|
||||
|
||||
@@ -1158,7 +1157,7 @@ class ModuleParser(object):
|
||||
have_copy_constructor = True
|
||||
|
||||
arglist_repr = ("[" + ', '.join([arg._pygen_repr for arg in arguments]) + "]")
|
||||
self.pygen_sink.writeln("cls.add_constructor(CppConstructor(%s))" %
|
||||
self.pygen_sink.writeln("cls.add_constructor(%s)" %
|
||||
", ".join([arglist_repr, "visibility=%r" % member.access_type]))
|
||||
|
||||
## ------------ attribute --------------------
|
||||
@@ -1197,8 +1196,8 @@ class ModuleParser(object):
|
||||
## thankfully pygccxml comes to the rescue!
|
||||
if not have_trivial_constructor:
|
||||
if type_traits.has_trivial_constructor(cls):
|
||||
class_wrapper.add_constructor(CppConstructor([]))
|
||||
self.pygen_sink.writeln("cls.add_constructor(CppConstructor([]))")
|
||||
class_wrapper.add_constructor([])
|
||||
self.pygen_sink.writeln("cls.add_constructor([])")
|
||||
|
||||
if not have_copy_constructor:
|
||||
try: # pygccxml > 0.9
|
||||
@@ -1206,9 +1205,9 @@ class ModuleParser(object):
|
||||
except AttributeError: # pygccxml <= 0.9
|
||||
has_copy_constructor = type_traits.has_trivial_copy(cls)
|
||||
if has_copy_constructor:
|
||||
class_wrapper.add_constructor(CppConstructor([
|
||||
class_wrapper.add_constructor([
|
||||
class_wrapper.ThisClassRefParameter("%s const &" % class_wrapper.full_name,
|
||||
'ctor_arg', is_const=True)]))
|
||||
'ctor_arg', is_const=True)])
|
||||
|
||||
|
||||
def scan_functions(self):
|
||||
@@ -1317,12 +1316,11 @@ class ModuleParser(object):
|
||||
if is_constructor_of is not None:
|
||||
#cpp_class = type_registry.find_class(is_constructor_of, (self.module_namespace_name or '::'))
|
||||
cpp_class = root_module[normalize_class_name(is_constructor_of, (self.module_namespace_name or '::'))]
|
||||
function_wrapper = Function(fun.name, return_type, arguments)
|
||||
cpp_class.add_constructor(function_wrapper)
|
||||
function_wrapper = cpp_class.add_function_as_constructor(fun.name, return_type, arguments)
|
||||
|
||||
function_wrapper.gccxml_definition = fun
|
||||
|
||||
self.pygen_sink.writeln("root_module[%r].add_constructor(Function(%s))" %
|
||||
self.pygen_sink.writeln("root_module[%r].add_function_as_constructor(%s)" %
|
||||
(cpp_class.full_name,
|
||||
", ".join([repr(fun.name), return_type._pygen_repr, arglist_repr]),))
|
||||
|
||||
|
||||
+1
-1
@@ -221,7 +221,7 @@ class ModuleBase(dict):
|
||||
"""
|
||||
if len(args) >= 1 and isinstance(args[0], Function):
|
||||
func = args[0]
|
||||
warnings.warn("add_class has changed API; see the API documentation",
|
||||
warnings.warn("add_function has changed API; see the API documentation",
|
||||
DeprecationWarning, stacklevel=2)
|
||||
if len(args) == 2:
|
||||
func.custom_name = args[1]
|
||||
|
||||
+14
-20
@@ -25,16 +25,14 @@ 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_constructor(
|
||||
CppConstructor([Parameter.new('std::string', 'datum')]))
|
||||
Foo.add_constructor(CppConstructor([]))
|
||||
Foo.add_constructor([Parameter.new('std::string', 'datum')])
|
||||
Foo.add_constructor([])
|
||||
Foo.add_method('get_datum', ReturnValue.new('std::string'), [])
|
||||
Foo.add_method('is_initialized', ReturnValue.new('bool'), [], is_const=True)
|
||||
|
||||
Zoo = mod.add_class('Zoo', automatic_type_narrowing=True)
|
||||
Zoo.add_constructor(
|
||||
CppConstructor([Parameter.new('std::string', 'datum')]))
|
||||
Zoo.add_constructor(CppConstructor([]))
|
||||
Zoo.add_constructor([Parameter.new('std::string', 'datum')])
|
||||
Zoo.add_constructor([])
|
||||
Zoo.add_method('get_datum', ReturnValue.new('std::string'), [])
|
||||
Zoo.implicitly_converts_to(Foo)
|
||||
|
||||
@@ -69,8 +67,7 @@ int %s::custom_method_added_by_a_hook(int x)
|
||||
helper_class.add_post_generation_code("// this comment was written by a helper class hook function")
|
||||
Zbr.add_helper_class_hook(helper_class_hook)
|
||||
|
||||
Zbr.add_constructor(
|
||||
CppConstructor([Parameter.new('std::string', 'datum')]))
|
||||
Zbr.add_constructor([Parameter.new('std::string', 'datum')])
|
||||
Zbr.add_method('get_datum', ReturnValue.new('std::string'), [])
|
||||
Zbr.add_method('get_int', ReturnValue.new('int'), [Parameter.new('int', 'x')],
|
||||
is_virtual=True)
|
||||
@@ -109,10 +106,8 @@ int %s::custom_method_added_by_a_hook(int x)
|
||||
SomeObject.add_method('add_prefix', ReturnValue.new('int'),
|
||||
[Parameter.new('std::string&', 'message',
|
||||
direction=Parameter.DIRECTION_INOUT)])
|
||||
SomeObject.add_constructor(
|
||||
CppConstructor([Parameter.new('std::string', 'prefix')]))
|
||||
SomeObject.add_constructor(
|
||||
CppConstructor([Parameter.new('int', 'prefix_len')]))
|
||||
SomeObject.add_constructor([Parameter.new('std::string', 'prefix')])
|
||||
SomeObject.add_constructor([Parameter.new('int', 'prefix_len')])
|
||||
|
||||
# --- some virtual methods ---
|
||||
SomeObject.add_method('get_prefix', ReturnValue.new('std::string'), [],
|
||||
@@ -246,7 +241,7 @@ int %s::custom_method_added_by_a_hook(int x)
|
||||
|
||||
|
||||
xpto_SomeClass = xpto.add_class('SomeClass')
|
||||
xpto_SomeClass.add_constructor(CppConstructor([]))
|
||||
xpto_SomeClass.add_constructor([])
|
||||
|
||||
## ---- some implicity conversion APIs
|
||||
mod.add_function('function_that_takes_foo', ReturnValue.new('void'),
|
||||
@@ -254,7 +249,7 @@ int %s::custom_method_added_by_a_hook(int x)
|
||||
mod.add_function('function_that_returns_foo', ReturnValue.new('Foo'), [])
|
||||
|
||||
cls = mod.add_class('ClassThatTakesFoo')
|
||||
cls.add_constructor(CppConstructor([Parameter.new('Foo', 'foo')]))
|
||||
cls.add_constructor([Parameter.new('Foo', 'foo')])
|
||||
cls.add_method('get_foo', ReturnValue.new('Foo'), [])
|
||||
|
||||
cls = mod.add_class('SingletonClass', is_singleton=True)
|
||||
@@ -286,9 +281,8 @@ 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_constructor(
|
||||
CppConstructor([Parameter.new('std::string', 'datum')]))
|
||||
NestedClass.add_constructor(CppConstructor([]))
|
||||
NestedClass.add_constructor([Parameter.new('std::string', 'datum')])
|
||||
NestedClass.add_constructor([])
|
||||
NestedClass.add_method('get_datum', ReturnValue.new('std::string'), [])
|
||||
|
||||
## A nested enum..
|
||||
@@ -302,7 +296,7 @@ int %s::custom_method_added_by_a_hook(int x)
|
||||
[Parameter.new('int', 'x')], is_const=True)
|
||||
AbstractBaseClass2.add_method('invoke_protected_virtual', ReturnValue.new('int'),
|
||||
[Parameter.new('int', 'x')], is_const=True)
|
||||
AbstractBaseClass2.add_constructor(CppConstructor([], visibility='protected'))
|
||||
AbstractBaseClass2.add_constructor([], visibility='protected')
|
||||
|
||||
AbstractBaseClass2.add_method('protected_virtual',
|
||||
ReturnValue.new('int'),
|
||||
@@ -320,13 +314,13 @@ int %s::custom_method_added_by_a_hook(int x)
|
||||
AbstractXpto.add_method('something', ReturnValue.new('void'),
|
||||
[Parameter.new('int', 'x')], is_const=True,
|
||||
is_virtual=True, is_pure_virtual=True)
|
||||
AbstractXpto.add_constructor(CppConstructor([]))
|
||||
AbstractXpto.add_constructor([])
|
||||
|
||||
AbstractXptoImpl = mod.add_class('AbstractXptoImpl', parent=AbstractXpto)
|
||||
AbstractXptoImpl.add_method('something', ReturnValue.new('void'),
|
||||
[Parameter.new('int', 'x')], is_const=True,
|
||||
is_virtual=True, is_pure_virtual=False)
|
||||
AbstractXptoImpl.add_constructor(CppConstructor([]))
|
||||
AbstractXptoImpl.add_constructor([])
|
||||
|
||||
|
||||
#### --- error handler ---
|
||||
|
||||
@@ -142,8 +142,7 @@ public:
|
||||
else:
|
||||
retval = return_handler(return_type)
|
||||
|
||||
wrapper = Function(function_name, retval, [])
|
||||
module.add_function(wrapper)
|
||||
module.add_function(function_name, retval, [])
|
||||
|
||||
for param_type, param_handler in typehandlers.base.param_type_matcher.items():
|
||||
if os.name == 'nt':
|
||||
@@ -167,8 +166,7 @@ public:
|
||||
## declare a fake prototype
|
||||
print "void %s(%s %s);" % (function_name, param_type, name)
|
||||
print
|
||||
wrapper = Function(function_name, ReturnValue.new('void'), [param])
|
||||
module.add_function(wrapper)
|
||||
module.add_function(function_name, ReturnValue.new('void'), [param])
|
||||
else:
|
||||
param = param_handler(param_type, param_name, direction)
|
||||
wrapper_number += 1
|
||||
@@ -176,8 +174,7 @@ public:
|
||||
## declare a fake prototype
|
||||
print "void %s(%s);" % (function_name, param_type)
|
||||
print
|
||||
wrapper = Function(function_name, ReturnValue.new('void'), [param])
|
||||
module.add_function(wrapper)
|
||||
module.add_function(function_name, ReturnValue.new('void'), [param])
|
||||
|
||||
module.generate(code_out)
|
||||
|
||||
|
||||
Reference in new issue
Block a user