Commit 248d1e4f authored by gsamain's avatar gsamain Committed by Xavier Thompson

Cpp: Allow c++ classes to have multiple constructors

parent 1ff2f2a2
...@@ -993,7 +993,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -993,7 +993,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
if attr.type.is_cfunction and attr.type.is_static_method: if attr.type.is_cfunction and attr.type.is_static_method:
code.put("static ") code.put("static ")
elif attr.name == "<init>": elif attr.name == "<init>":
constructor = attr #constructor = attr
constructor = scope.lookup_here("<init>")
elif attr.name == "<del>": elif attr.name == "<del>":
destructor = attr destructor = attr
elif attr.type.is_cfunction: elif attr.type.is_cfunction:
...@@ -1001,28 +1002,14 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1001,28 +1002,14 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
has_virtual_methods = True has_virtual_methods = True
code.putln("%s;" % attr.type.declaration_code(attr.cname)) code.putln("%s;" % attr.type.declaration_code(attr.cname))
is_implementing = 'init_module' in code.globalstate.parts is_implementing = 'init_module' in code.globalstate.parts
if constructor or py_attrs:
if constructor: def generate_cpp_constructor_code(arg_decls, arg_names, is_implementing, py_attrs, 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"]
else:
arg_decls = ["void"]
arg_names = []
if is_implementing: if is_implementing:
code.putln("%s(%s) {" % (type.cname, ", ".join(arg_decls))) code.putln("%s(%s) {" % (type.cname, ", ".join(arg_decls)))
if py_attrs: if py_attrs:
code.put_ensure_gil() code.put_ensure_gil()
for attr in py_attrs: for attr in py_attrs:
code.put_init_var_to_py_none(attr, nanny=False) code.put_init_var_to_py_none(attr, nanny=False);
if constructor: if constructor:
code.putln("%s(%s);" % (constructor.cname, ", ".join(arg_names))) code.putln("%s(%s);" % (constructor.cname, ", ".join(arg_names)))
if py_attrs: if py_attrs:
...@@ -1030,6 +1017,28 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1030,6 +1017,28 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln("}") code.putln("}")
else: else:
code.putln("%s(%s);" % (type.cname, ", ".join(arg_decls))) code.putln("%s(%s);" % (type.cname, ", ".join(arg_decls)))
if constructor or py_attrs:
if constructor:
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 = []
generate_cpp_constructor_code(arg_decls, arg_names, is_implementing, py_attrs, constructor)
if destructor or py_attrs or has_virtual_methods: if destructor or py_attrs or has_virtual_methods:
if has_virtual_methods: if has_virtual_methods:
code.put("virtual ") 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