|
31 | 31 | """ |
32 | 32 |
|
33 | 33 | from collections import defaultdict |
| 34 | +from distutils.log import debug |
34 | 35 | from typing import List, Tuple, Optional, Union, AnyStr |
35 | 36 |
|
36 | 37 | from autowrap.DeclResolver import ResolvedClass |
@@ -1108,90 +1109,98 @@ def output_conversion( |
1108 | 1109 |
|
1109 | 1110 | it = mangle("it_" + input_cpp_var) |
1110 | 1111 |
|
| 1112 | + code_text = self.output_code_template() |
1111 | 1113 | # Code for key that is wrapped |
1112 | 1114 | if not cy_tt_key.is_enum and self.is_wrapper_class(py_tt_key.base_type): |
1113 | 1115 | key_conv = "deref(<%s *> (<%s> key).inst.get())" % (cy_tt_key, py_tt_key) |
1114 | 1116 | value_conv = "<%s>(deref(%s).second)" % (cy_tt_value, it) |
1115 | 1117 | 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))", |
1130 | 1127 | ) |
| 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()) |
1131 | 1132 | return code |
1132 | 1133 | else: |
1133 | 1134 | key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it) |
1134 | 1135 |
|
1135 | 1136 | # Code for value that is wrapped |
1136 | 1137 | if not cy_tt_value.is_enum and self.is_wrapper_class(tt_value.base_type): |
1137 | 1138 | 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 |
1139 | 1140 | 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))", |
1152 | 1150 | ) |
| 1151 | + code_text = code_text.replace( |
| 1152 | + "<OUTPUT PY VAR>", "$output_py_var[$key_conv] = $item" |
| 1153 | + ) |
| 1154 | + code = Code().add(code_text, locals()) |
1153 | 1155 | return code |
1154 | 1156 | # Code for value AND key that is wrapped |
1155 | 1157 | elif ( |
1156 | 1158 | not cy_tt_value.is_enum |
1157 | 1159 | and self.is_wrapper_class(tt_value.base_type) |
1158 | 1160 | and self.is_wrapper_class(py_tt_key.base_type) |
1159 | 1161 | ): |
1160 | | - key_conv = "deref(<%s *> (<%s> key).inst.get())" % (cy_tt_key, py_tt_key) |
1161 | | - cy_tt = tt_value.base_type |
1162 | 1162 | item_key = mangle("itemk_" + output_py_var) |
1163 | 1163 | 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))", |
1178 | 1173 | ) |
| 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()) |
1179 | 1179 | return code |
1180 | 1180 | else: |
1181 | 1181 | key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it) |
1182 | 1182 | 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" |
1192 | 1188 | ) |
| 1189 | + code = Code().add(code_text, locals()) |
1193 | 1190 | return code |
1194 | 1191 |
|
| 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 | + |
1195 | 1204 |
|
1196 | 1205 | class StdSetConverter(TypeConverterBase): |
1197 | 1206 | def get_base_types(self) -> List[str]: |
|
0 commit comments