Skip to content

Commit

Permalink
cxx-qt-gen: simplify C++ header include writer
Browse files Browse the repository at this point in the history
  • Loading branch information
ahayzen-kdab committed Nov 15, 2023
1 parent 0135140 commit 805a762
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions crates/cxx-qt-gen/src/writer/cpp/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,40 @@ fn qobjects_header(generated: &GeneratedCppBlocks) -> Vec<String> {

/// For a given GeneratedCppBlocks write this into a C++ header
pub fn write_cpp_header(generated: &GeneratedCppBlocks) -> String {
// Headers included:
// <memory> - 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::<Vec<String>>()
.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::<Vec<String>>()
.join("\n");

formatdoc! {r#"
#pragma once
Expand All @@ -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::<String>::default(), |mut acc, block| {
acc.extend(block.includes.iter().cloned());
acc
})).collect::<Vec<String>>().join("\n"),
}
}

Expand Down

0 comments on commit 805a762

Please sign in to comment.