Skip to content

Commit

Permalink
Refactor output_conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-whiting committed Jul 12, 2022
1 parent 0465f23 commit 20bb65d
Showing 1 changed file with 34 additions and 54 deletions.
88 changes: 34 additions & 54 deletions autowrap/ConversionProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"""

from collections import defaultdict
from distutils.log import debug
from typing import List, Tuple, Optional, Union, AnyStr

from autowrap.DeclResolver import ResolvedClass
Expand Down Expand Up @@ -1108,26 +1109,17 @@ def output_conversion(

it = mangle("it_" + input_cpp_var)

code_text = self.output_code_template()
# Code for key that is wrapped
if not cy_tt_key.is_enum and self.is_wrapper_class(py_tt_key.base_type):
key_conv = "deref(<%s *> (<%s> key).inst.get())" % (cy_tt_key, py_tt_key)
value_conv = "<%s>(deref(%s).second)" % (cy_tt_value, it)
item_key = mangle("itemk_" + output_py_var)
code = Code().add(
"""
|$output_py_var = dict()
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
|cdef $py_tt_key $item_key
|while $it != $input_cpp_var.end():
| #$output_py_var[$key_conv] = $value_conv
| $item_key = $py_tt_key.__new__($py_tt_key)
| $item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))
| # $output_py_var[$key_conv] = $value_conv
| $output_py_var[$item_key] = $value_conv
| inc($it)
""",
locals(),
)
code_text = code_text.replace("<ITEM OR KEY DECLARATION>", "cdef $py_tt_key $item_key")
code_text = code_text.replace("<ITEM OR KEY ASSIGNMENT>", "$item_key = $py_tt_key.__new__($py_tt_key)")
code_text = code_text.replace("<SHARED POINTER>", "$item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))")
code_text = code_text.replace("<OUTPUT PY VAR>", "$output_py_var[$item_key] = $value_conv")
code = Code().add(code_text, locals())
return code
else:
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it)
Expand All @@ -1137,19 +1129,11 @@ def output_conversion(
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it)
cy_tt = tt_value.base_type
item = mangle("item_" + output_py_var)
code = Code().add(
"""
|$output_py_var = dict()
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
|cdef $cy_tt $item
|while $it != $input_cpp_var.end():
| $item = $cy_tt.__new__($cy_tt)
| $item.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))
| $output_py_var[$key_conv] = $item
| inc($it)
""",
locals(),
)
code_text = code_text.replace("<ITEM OR KEY DECLARATION>", "cdef $cy_tt $item")
code_text = code_text.replace("<ITEM OR KEY ASSIGNMENT>", "$item = $cy_tt.__new__($cy_tt)")
code_text = code_text.replace("<SHARED POINTER>", "$item.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))")
code_text = code_text.replace("<OUTPUT PY VAR>", "$output_py_var[$key_conv] = $item")
code = Code().add(code_text, locals())
return code
# Code for value AND key that is wrapped
elif (
Expand All @@ -1161,37 +1145,33 @@ def output_conversion(
cy_tt = tt_value.base_type
item_key = mangle("itemk_" + output_py_var)
item_val = mangle("item_" + output_py_var)
code = Code().add(
"""
|$output_py_var = dict()
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
|cdef $py_tt_key $item_key
|while $it != $input_cpp_var.end():
| #$output_py_var[$key_conv] = $value_conv
| $item_key = $py_tt_key.__new__($py_tt_key)
| $item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))
| $item_val = $cy_tt.__new__($cy_tt)
| $item_val.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))
| inc($it)
""",
locals(),
)
code_text = code_text.replace("<ITEM OR KEY DECLARATION>", "cdef $py_tt_key $item_key")
code_text = code_text.replace("<ITEM OR KEY ASSIGNMENT>", "$item_key = $py_tt_key.__new__($py_tt_key)")
code_text = code_text.replace("<SHARED POINTER>", "$item_key.inst = shared_ptr[$cy_tt_key](new $cy_tt_key((deref($it)).first))")
code_text = code_text.replace("<OUTPUT PY VAR>", "$item_val.inst = shared_ptr[$cy_tt_value](new $cy_tt_value((deref($it)).second))")
code = Code().add(code_text, locals())
return code
else:
key_conv = "<%s>(deref(%s).first)" % (cy_tt_key, it)
value_conv = "<%s>(deref(%s).second)" % (cy_tt_value, it)
code = Code().add(
"""
|$output_py_var = dict()
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
|while $it != $input_cpp_var.end():
| $output_py_var[$key_conv] = $value_conv
| inc($it)
""",
locals(),
)
code_text = code_text.replace("<ITEM OR KEY DECLARATION>", "")
code_text = code_text.replace("<ITEM OR KEY ASSIGNMENT>", "")
code_text = code_text.replace("<SHARED POINTER>", "")
code_text = code_text.replace("<OUTPUT PY VAR>", "$output_py_var[$key_conv] = $value_conv")
code = Code().add(code_text, locals())
return code


def output_code_template(self):
return """
|$output_py_var = dict()
|cdef libcpp_map[$cy_tt_key, $cy_tt_value].iterator $it = $input_cpp_var.begin()
|<ITEM OR KEY DECLARATION>
|while $it != $input_cpp_var.end():
| <ITEM OR KEY ASSIGNMENT>
| <SHARED POINTER>
| <OUTPUT PY VAR>
| inc($it)
"""

class StdSetConverter(TypeConverterBase):
def get_base_types(self) -> List[str]:
Expand Down

0 comments on commit 20bb65d

Please sign in to comment.