Skip to content

Commit 4dc213a

Browse files
committed
Changed parameters in functions to references and const references
1 parent 4b63def commit 4dc213a

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

matlab2cpp/rules/_func_return.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ def Params(node):
8181

8282
out = ""
8383
for child in node:
84-
out += ", " + type_string(child) + " " + str(child)
84+
if child.dim > 0:
85+
out += ", " + "const " + type_string(child) + "& " + str(child)
86+
else:
87+
out += ", " + type_string(child) + " " + str(child)
8588
return out[2:]
8689

8790

matlab2cpp/rules/_func_returns.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ def Params(node):
155155
# Create list of parameters
156156
out = ""
157157
for child in node[:]:
158-
out += ", " + type_string(child) + " " + str(child)
158+
if child.dim > 0:
159+
out += ", " + "const " + type_string(child) + "& " + str(child)
160+
else:
161+
out += ", " + type_string(child) + " " + str(child)
159162
return out[2:]
160163

161164

matlab2cpp/rules/_program.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
22
import matlab2cpp as mc
33
import armadillo as arma
4+
from function import type_string
45

56
def add_indenting(text):
67
"""Add identing to text
@@ -91,17 +92,50 @@ def Headers(node):
9192
def Header(node):
9293
func = node.program[1][node.program[1].names.index(node.name)]
9394
if func.backend == "func_return":
94-
code = func[1][0].type + " " + func.name + "(" +\
95-
", ".join([p.type + " " + p.name for p in func[2]]) + ") ;"
95+
code = func[1][0].type + " " + func.name + "("
96+
97+
params = ""
98+
for p in func[2]:
99+
if p.dim > 0:
100+
params += ", " + "const " + type_string(p) + "& " + str(p)
101+
else:
102+
params += ", " + type_string(p) + " " + str(p)
103+
104+
code += params[2:] + ") ;"
96105

97106
elif func.backend == "func_returns" and not func[1]:
98-
code = "void " + func.name + "(" +\
99-
", ".join([p.type + " " + p.name for p in func[2]]) + ") ;"
107+
code = "void " + func.name + "("
108+
109+
params = ""
110+
for p in func[2]:
111+
if p.dim > 0:
112+
params += ", " + "const " + type_string(p) + "& " + str(p)
113+
else:
114+
params += ", " + type_string(p) + " " + str(p)
115+
116+
code += params[2:] + ") ;"
100117

101118
elif func.backend == "func_returns" and func[1]:
102-
code = "void " + func.name + "(" +\
103-
", ".join([p.type + " " + p.name for p in func[2]]) + ", " +\
104-
", ".join([p.type + "& " + p.name for p in func[1]]) + ") ;"
119+
code = "void " + func.name + "("
120+
121+
params = ""
122+
for p in func[2]:
123+
if p.dim > 0:
124+
params += ", " + "const " + type_string(p) + "& " + str(p)
125+
else:
126+
params += ", " + type_string(p) + " " + str(p)
127+
128+
#return_params = ""
129+
return_params = ", ".join([p.type + "& " + p.name for p in func[1]])
130+
131+
if return_params and params:
132+
params += ", " + return_params
133+
else:
134+
params += return_params
135+
136+
params += ") ;"
137+
138+
code += params[2:]
105139
return code
106140

107141
Include = "%(name)s"

0 commit comments

Comments
 (0)