Skip to content

Commit cabf5b6

Browse files
committed
Refactor output_conversion
1 parent 0465f23 commit cabf5b6

File tree

1 file changed

+60
-52
lines changed

1 file changed

+60
-52
lines changed

autowrap/ConversionProvider.py

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,90 +1108,98 @@ def output_conversion(
11081108

11091109
it = mangle("it_" + input_cpp_var)
11101110

1111+
code_text = self.output_code_template()
11111112
# Code for key that is wrapped
11121113
if not cy_tt_key.is_enum and self.is_wrapper_class(py_tt_key.base_type):
11131114
key_conv = "deref(<%s *> (<%s> key).inst.get())" % (cy_tt_key, py_tt_key)
11141115
value_conv = "<%s>(deref(%s).second)" % (cy_tt_value, it)
11151116
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(),
1117+
code_text = code_text.replace(
1118+
"<ITEM OR KEY DECLARATION>", "cdef $py_tt_key $item_key"
1119+
)
1120+
code_text = code_text.replace(
1121+
"<ITEM OR KEY ASSIGNMENT>", "$item_key = $py_tt_key.__new__($py_tt_key)"
1122+
)
1123+
code_text = code_text.replace(
1124+
"<SHARED POINTER>",
1125+
"$item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))",
11301126
)
1127+
code_text = code_text.replace(
1128+
"<OUTPUT PY VAR>", "$output_py_var[$item_key] = $value_conv"
1129+
)
1130+
code = Code().add(code_text, locals())
11311131
return code
11321132
else:
11331133
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it)
11341134

11351135
# Code for value that is wrapped
11361136
if not cy_tt_value.is_enum and self.is_wrapper_class(tt_value.base_type):
11371137
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it)
1138-
cy_tt = tt_value.base_type
1138+
cy_tt_base = tt_value.base_type
11391139
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(),
1140+
code_text = code_text.replace(
1141+
"<ITEM OR KEY DECLARATION>", "cdef $cy_tt_base $item"
1142+
)
1143+
code_text = code_text.replace(
1144+
"<ITEM OR KEY ASSIGNMENT>", "$item = $cy_tt_base.__new__($cy_tt_base)"
1145+
)
1146+
code_text = code_text.replace(
1147+
"<SHARED POINTER>",
1148+
"$item.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))",
11521149
)
1150+
code_text = code_text.replace(
1151+
"<OUTPUT PY VAR>", "$output_py_var[$key_conv] = $item"
1152+
)
1153+
code = Code().add(code_text, locals())
11531154
return code
11541155
# Code for value AND key that is wrapped
11551156
elif (
11561157
not cy_tt_value.is_enum
11571158
and self.is_wrapper_class(tt_value.base_type)
11581159
and self.is_wrapper_class(py_tt_key.base_type)
11591160
):
1160-
key_conv = "deref(<%s *> (<%s> key).inst.get())" % (cy_tt_key, py_tt_key)
1161-
cy_tt = tt_value.base_type
11621161
item_key = mangle("itemk_" + output_py_var)
11631162
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(),
1163+
code_text = code_text.replace(
1164+
"<ITEM OR KEY DECLARATION>", "cdef $py_tt_key $item_key"
1165+
)
1166+
code_text = code_text.replace(
1167+
"<ITEM OR KEY ASSIGNMENT>", "$item_key = $py_tt_key.__new__($py_tt_key)"
1168+
)
1169+
code_text = code_text.replace(
1170+
"<SHARED POINTER>",
1171+
"$item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))",
11781172
)
1173+
code_text = code_text.replace(
1174+
"<OUTPUT PY VAR>",
1175+
"$item_val.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))",
1176+
)
1177+
code = Code().add(code_text, locals())
11791178
return code
11801179
else:
11811180
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it)
11821181
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(),
1182+
code_text = code_text.replace("<ITEM OR KEY DECLARATION>", "")
1183+
code_text = code_text.replace("<ITEM OR KEY ASSIGNMENT>", "")
1184+
code_text = code_text.replace("<SHARED POINTER>", "")
1185+
code_text = code_text.replace(
1186+
"<OUTPUT PY VAR>", "$output_py_var[$key_conv] = $value_conv"
11921187
)
1188+
code = Code().add(code_text, locals())
11931189
return code
11941190

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

11961204
class StdSetConverter(TypeConverterBase):
11971205
def get_base_types(self) -> List[str]:

0 commit comments

Comments
 (0)