Replace two uses of build_array_helper with a new recurse_elem variant

For the item index generation and the member population for the C++
structs, a level-order variant of `recurse_elem` is sufficient - as also
indicate by the unused item index parameters.
This commit is contained in:
Simon Hausmann 2021-10-28 14:35:45 +02:00 committed by Simon Hausmann
parent 90abcf3066
commit cebb415b08
3 changed files with 20 additions and 2 deletions

View file

@ -1170,7 +1170,7 @@ fn generate_component(
let mut repeater_count = 0;
super::build_array_helper(component, |item_rc, _, _| {
crate::object_tree::recurse_elem_level_order(&component.root_element, &mut |item_rc| {
let item = item_rc.borrow();
if item.base_type == Type::Void {
assert!(component.is_global());

View file

@ -1419,6 +1419,24 @@ pub fn recurse_elem<State>(
}
}
/// Call the visitor for each children of the element recursively, starting with the element itself.
/// The traversal happens in level-order, meaning each level of the element tree is visisted before
/// going to the next depth.
///
/// The state returned by the visitor is passed to the children
pub fn recurse_elem_level_order(elem: &ElementRc, vis: &mut impl FnMut(&ElementRc)) {
vis(elem);
visit_children(elem, vis);
fn visit_children(elem: &ElementRc, vis: &mut impl FnMut(&ElementRc)) {
for child in &elem.borrow().children {
vis(child);
}
for child in &elem.borrow().children {
visit_children(child, vis);
}
}
}
/// Same as [`recurse_elem`] but include the elements form sub_components
pub fn recurse_elem_including_sub_components<State>(
component: &Component,

View file

@ -10,7 +10,7 @@ LICENSE END */
//! Assign the Element::item_index on each elements
pub fn generate_item_indices(component: &std::rc::Rc<crate::object_tree::Component>) {
let mut current_item_index: usize = 0;
crate::generator::build_array_helper(component, move |item_rc, _, _| {
crate::object_tree::recurse_elem_level_order(&component.root_element, &mut |item_rc| {
let item = item_rc.borrow();
if item.base_type == crate::langtype::Type::Void {
} else {