Skip to content

Commit

Permalink
cxx-qt-gen: share QSignal free namespace generation in naming module
Browse files Browse the repository at this point in the history
Related to KDAB#577
  • Loading branch information
ahayzen-kdab committed Aug 24, 2023
1 parent 4f6f339 commit 48e7b61
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
20 changes: 10 additions & 10 deletions crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::{
generator::cpp::signal::generate_cpp_free_signal,
generator::{
cpp::signal::generate_cpp_free_signal,
naming::namespace::namespace_externcxxqt_with_qobject_namespace,
},
parser::{externcxxqt::ParsedExternCxxQt, mappings::ParsedCxxMappings},
CppFragment,
};
Expand All @@ -27,18 +30,15 @@ pub fn generate(
for block in blocks {
for signal in &block.signals {
// Build a namespace that includes any namespace for the T
let ident_namespace = if let Some(namespace) = cxx_mappings
.namespaces
.get(&signal.qobject_ident.to_string())
{
format!("::{namespace}")
} else {
"".to_owned()
};
let namespace = namespace_externcxxqt_with_qobject_namespace(
cxx_mappings
.namespaces
.get(&signal.qobject_ident.to_string()),
);

out.push(GeneratedCppExternCxxQtBlocks {
method: generate_cpp_free_signal(signal, cxx_mappings)?,
namespace: format!("rust::cxxqtgen1::externcxxqt{ident_namespace}"),
namespace,
});
}
}
Expand Down
10 changes: 10 additions & 0 deletions crates/cxx-qt-gen/src/generator/naming/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ pub fn namespace_combine_ident(namespace: &str, ident: &Ident) -> String {
format!("{namespace}::{ident}")
}

/// Build the externcxxqt namespace combined with the given qobject_namespace
pub fn namespace_externcxxqt_with_qobject_namespace(qobject_namespace: Option<&String>) -> String {
let mut free_namespace = vec!["rust::cxxqtgen1::externcxxqt"];
if let Some(qobject_namespace) = qobject_namespace {
free_namespace.push(qobject_namespace);
}

free_namespace.join("::")
}

/// For a given base namespace and QObject ident generate the internal namespace
///
/// The base namespace could be from the module bridge or from the QObject
Expand Down
23 changes: 9 additions & 14 deletions crates/cxx-qt-gen/src/generator/rust/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use std::collections::BTreeMap;

use crate::{
generator::{
naming::{qobject::QObjectName, signals::QSignalName},
naming::{
namespace::namespace_externcxxqt_with_qobject_namespace, qobject::QObjectName,
signals::QSignalName,
},
rust::{
externcxxqt::GeneratedExternCxxQt, fragment::RustFragmentPair,
qobject::GeneratedRustQObject,
Expand Down Expand Up @@ -38,20 +41,12 @@ pub fn generate_rust_free_signal(
let on_ident_rust = idents.on_name;
let original_method = &signal.method;

// TODO: share this in the naming module with generator/cpp
let connect_namespace = {
// Build a namespace that includes any namespace for the T
let ident_namespace = if let Some(namespace) = cxx_mappings
// Build a namespace that includes any namespace for the T
let connect_namespace = namespace_externcxxqt_with_qobject_namespace(
cxx_mappings
.namespaces
.get(&signal.qobject_ident.to_string())
{
format!("::{namespace}")
} else {
"".to_owned()
};

format!("rust::cxxqtgen1::externcxxqt{ident_namespace}")
};
.get(&signal.qobject_ident.to_string()),
);

let parameters_cxx: Vec<FnArg> = signal
.parameters
Expand Down

0 comments on commit 48e7b61

Please sign in to comment.