Commit 3c7824f0 authored by gsamain's avatar gsamain Committed by Xavier Thompson

Cpp: Allow c++ classes to have multiple constructors

parent 827b4e3f
......@@ -992,7 +992,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
if attr.type.is_cfunction and attr.type.is_static_method:
code.put("static ")
elif attr.name == "<init>":
constructor = attr
#constructor = attr
constructor = scope.lookup_here("<init>")
elif attr.name == "<del>":
destructor = attr
elif attr.type.is_cfunction:
......@@ -1000,35 +1001,43 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
has_virtual_methods = True
code.putln("%s;" % attr.type.declaration_code(attr.cname))
is_implementing = 'init_module' in code.globalstate.parts
def generate_cpp_constructor_code(arg_decls, arg_names, is_implementing, py_attrs, constructor):
if is_implementing:
code.putln("%s(%s) {" % (type.cname, ", ".join(arg_decls)))
if py_attrs:
code.put_ensure_gil()
for attr in py_attrs:
code.put_init_var_to_py_none(attr, nanny=False);
if constructor:
code.putln("%s(%s);" % (constructor.cname, ", ".join(arg_names)))
if py_attrs:
code.put_release_ensured_gil()
code.putln("}")
else:
code.putln("%s(%s);" % (type.cname, ", ".join(arg_decls)))
if constructor or py_attrs:
if constructor:
arg_decls = []
arg_names = []
for arg in constructor.type.original_args[
:len(constructor.type.args)-constructor.type.optional_arg_count]:
arg_decls.append(arg.declaration_code())
arg_names.append(arg.cname)
if constructor.type.optional_arg_count:
arg_decls.append(constructor.type.op_arg_struct.declaration_code(Naming.optional_args_cname))
arg_names.append(Naming.optional_args_cname)
if not arg_decls:
arg_decls = ["void"]
for constructor_alternative in constructor.all_alternatives():
arg_decls = []
arg_names = []
for arg in constructor_alternative.type.original_args[
:len(constructor_alternative.type.args)-constructor_alternative.type.optional_arg_count]:
arg_decls.append(arg.declaration_code())
arg_names.append(arg.cname)
if constructor_alternative.type.optional_arg_count:
arg_decls.append(constructor_alternative.type.op_arg_struct.declaration_code(Naming.optional_args_cname))
arg_names.append(Naming.optional_args_cname)
if not arg_decls:
default_constructor = True
arg_decls = ["void"]
generate_cpp_constructor_code(arg_decls, arg_names, is_implementing, py_attrs, constructor_alternative)
else:
arg_decls = ["void"]
arg_names = []
if is_implementing:
code.putln("%s(%s) {" % (type.cname, ", ".join(arg_decls)))
if py_attrs:
code.put_ensure_gil()
for attr in py_attrs:
code.put_init_var_to_py_none(attr, nanny=False)
if constructor:
code.putln("%s(%s);" % (constructor.cname, ", ".join(arg_names)))
if py_attrs:
code.put_release_ensured_gil()
code.putln("}")
else:
code.putln("%s(%s);" % (type.cname, ", ".join(arg_decls)))
generate_cpp_constructor_code(arg_decls, arg_names, is_implementing, py_attrs, constructor)
if destructor or py_attrs or has_virtual_methods:
if has_virtual_methods:
code.put("virtual ")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment