From 8d0768bd089ec92efd6525f0a45e05f523160e5d Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Fri, 20 Oct 2023 16:26:07 +0100 Subject: [PATCH] cxx-qt-gen: simplify C++ header include writer --- crates/cxx-qt-gen/src/writer/cpp/header.rs | 53 ++++++++++++++-------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/crates/cxx-qt-gen/src/writer/cpp/header.rs b/crates/cxx-qt-gen/src/writer/cpp/header.rs index 247607a73..800e4eaae 100644 --- a/crates/cxx-qt-gen/src/writer/cpp/header.rs +++ b/crates/cxx-qt-gen/src/writer/cpp/header.rs @@ -103,8 +103,40 @@ fn qobjects_header(generated: &GeneratedCppBlocks) -> Vec { /// For a given GeneratedCppBlocks write this into a C++ header pub fn write_cpp_header(generated: &GeneratedCppBlocks) -> String { - // Headers included: - // - unique_ptr to the Rust object. + let includes = { + let mut include_set = BTreeSet::new(); + include_set.extend( + generated + .includes + .iter() + .chain( + generated + .qobjects + .iter() + .flat_map(|qobject| &qobject.blocks.includes), + ) + .chain( + generated + .extern_cxx_qt + .iter() + .flat_map(|block| &block.includes), + ), + ); + include_set + .into_iter() + .cloned() + .collect::>() + .join("\n") + }; + let extern_cxx_qt = generated + .extern_cxx_qt + .iter() + .filter_map(|block| { + pair_as_header(&block.method).map(|method| namespaced(&block.namespace, &method)) + }) + .collect::>() + .join("\n"); + formatdoc! {r#" #pragma once @@ -119,23 +151,6 @@ pub fn write_cpp_header(generated: &GeneratedCppBlocks) -> String { cxx_file_stem = generated.cxx_file_stem, forward_declare = forward_declare(generated).join("\n"), qobjects = qobjects_header(generated).join("\n"), - extern_cxx_qt = { - let mut out = vec![]; - for block in &generated.extern_cxx_qt { - if let Some(method) = pair_as_header(&block.method) { - out.push(namespaced(&block.namespace, &method)); - } - } - out.join("\n") - }, - includes = generated.qobjects.iter() - .fold(generated.includes.clone(), |mut acc, qobject| { - acc.extend(qobject.blocks.includes.iter().cloned()); - acc - }).into_iter().chain(generated.extern_cxx_qt.iter().fold(BTreeSet::::default(), |mut acc, block| { - acc.extend(block.includes.iter().cloned()); - acc - })).collect::>().join("\n"), } }