Skip to content

Commit 5a921ef

Browse files
committed
Refactor output_conversion
1 parent 0465f23 commit 5a921ef

File tree

1 file changed

+61
-52
lines changed

1 file changed

+61
-52
lines changed

autowrap/ConversionProvider.py

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"""
3232

3333
from collections import defaultdict
34+
from distutils.log import debug
3435
from typing import List, Tuple, Optional, Union, AnyStr
3536

3637
from autowrap.DeclResolver import ResolvedClass
@@ -1108,90 +1109,98 @@ def output_conversion(
11081109

11091110
it = mangle("it_" + input_cpp_var)
11101111

1112+
code_text = self.output_code_template()
11111113
# Code for key that is wrapped
11121114
if not cy_tt_key.is_enum and self.is_wrapper_class(py_tt_key.base_type):
11131115
key_conv = "deref(<%s *> (<%s> key).inst.get())" % (cy_tt_key, py_tt_key)
11141116
value_conv = "<%s>(deref(%s).second)" % (cy_tt_value, it)
11151117
item_key = mangle("itemk_" + output_py_var)
1116-
code = Code().add(
1117-
"""
1118-
|$output_py_var = dict()
1119-
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
1120-
|cdef $py_tt_key $item_key
1121-
|while $it != $input_cpp_var.end():
1122-
| #$output_py_var[$key_conv] = $value_conv
1123-
| $item_key = $py_tt_key.__new__($py_tt_key)
1124-
| $item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))
1125-
| # $output_py_var[$key_conv] = $value_conv
1126-
| $output_py_var[$item_key] = $value_conv
1127-
| inc($it)
1128-
""",
1129-
locals(),
1118+
code_text = code_text.replace(
1119+
"<ITEM OR KEY DECLARATION>", "cdef $py_tt_key $item_key"
1120+
)
1121+
code_text = code_text.replace(
1122+
"<ITEM OR KEY ASSIGNMENT>", "$item_key = $py_tt_key.__new__($py_tt_key)"
1123+
)
1124+
code_text = code_text.replace(
1125+
"<SHARED POINTER>",
1126+
"$item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))",
11301127
)
1128+
code_text = code_text.replace(
1129+
"<OUTPUT PY VAR>", "$output_py_var[$item_key] = $value_conv"
1130+
)
1131+
code = Code().add(code_text, locals())
11311132
return code
11321133
else:
11331134
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it)
11341135

11351136
# Code for value that is wrapped
11361137
if not cy_tt_value.is_enum and self.is_wrapper_class(tt_value.base_type):
11371138
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it)
1138-
cy_tt = tt_value.base_type
1139+
cy_tt_base = tt_value.base_type
11391140
item = mangle("item_" + output_py_var)
1140-
code = Code().add(
1141-
"""
1142-
|$output_py_var = dict()
1143-
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
1144-
|cdef $cy_tt $item
1145-
|while $it != $input_cpp_var.end():
1146-
| $item = $cy_tt.__new__($cy_tt)
1147-
| $item.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))
1148-
| $output_py_var[$key_conv] = $item
1149-
| inc($it)
1150-
""",
1151-
locals(),
1141+
code_text = code_text.replace(
1142+
"<ITEM OR KEY DECLARATION>", "cdef $cy_tt_base $item"
1143+
)
1144+
code_text = code_text.replace(
1145+
"<ITEM OR KEY ASSIGNMENT>", "$item = $cy_tt_base.__new__($cy_tt_base)"
1146+
)
1147+
code_text = code_text.replace(
1148+
"<SHARED POINTER>",
1149+
"$item.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))",
11521150
)
1151+
code_text = code_text.replace(
1152+
"<OUTPUT PY VAR>", "$output_py_var[$key_conv] = $item"
1153+
)
1154+
code = Code().add(code_text, locals())
11531155
return code
11541156
# Code for value AND key that is wrapped
11551157
elif (
11561158
not cy_tt_value.is_enum
11571159
and self.is_wrapper_class(tt_value.base_type)
11581160
and self.is_wrapper_class(py_tt_key.base_type)
11591161
):
1160-
key_conv = "deref(<%s *> (<%s> key).inst.get())" % (cy_tt_key, py_tt_key)
1161-
cy_tt = tt_value.base_type
11621162
item_key = mangle("itemk_" + output_py_var)
11631163
item_val = mangle("item_" + output_py_var)
1164-
code = Code().add(
1165-
"""
1166-
|$output_py_var = dict()
1167-
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
1168-
|cdef $py_tt_key $item_key
1169-
|while $it != $input_cpp_var.end():
1170-
| #$output_py_var[$key_conv] = $value_conv
1171-
| $item_key = $py_tt_key.__new__($py_tt_key)
1172-
| $item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))
1173-
| $item_val = $cy_tt.__new__($cy_tt)
1174-
| $item_val.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))
1175-
| inc($it)
1176-
""",
1177-
locals(),
1164+
code_text = code_text.replace(
1165+
"<ITEM OR KEY DECLARATION>", "cdef $py_tt_key $item_key"
1166+
)
1167+
code_text = code_text.replace(
1168+
"<ITEM OR KEY ASSIGNMENT>", "$item_key = $py_tt_key.__new__($py_tt_key)"
1169+
)
1170+
code_text = code_text.replace(
1171+
"<SHARED POINTER>",
1172+
"$item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))",
11781173
)
1174+
code_text = code_text.replace(
1175+
"<OUTPUT PY VAR>",
1176+
"$item_val.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))",
1177+
)
1178+
code = Code().add(code_text, locals())
11791179
return code
11801180
else:
11811181
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it)
11821182
value_conv = "<%s>(deref(%s).second)" % (cy_tt_value, it)
1183-
code = Code().add(
1184-
"""
1185-
|$output_py_var = dict()
1186-
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
1187-
|while $it != $input_cpp_var.end():
1188-
| $output_py_var[$key_conv] = $value_conv
1189-
| inc($it)
1190-
""",
1191-
locals(),
1183+
code_text = code_text.replace("<ITEM OR KEY DECLARATION>", "")
1184+
code_text = code_text.replace("<ITEM OR KEY ASSIGNMENT>", "")
1185+
code_text = code_text.replace("<SHARED POINTER>", "")
1186+
code_text = code_text.replace(
1187+
"<OUTPUT PY VAR>", "$output_py_var[$key_conv] = $value_conv"
11921188
)
1189+
code = Code().add(code_text, locals())
11931190
return code
11941191

1192+
def output_code_template(self):
1193+
return """
1194+
|$output_py_var = dict()
1195+
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
1196+
|<ITEM OR KEY DECLARATION>
1197+
|while $it != $input_cpp_var.end():
1198+
| <ITEM OR KEY ASSIGNMENT>
1199+
| <SHARED POINTER>
1200+
| <OUTPUT PY VAR>
1201+
| inc($it)
1202+
"""
1203+
11951204

11961205
class StdSetConverter(TypeConverterBase):
11971206
def get_base_types(self) -> List[str]:

0 commit comments

Comments
 (0)