Skip to content

Commit

Permalink
cxx-qt-gen: move QObject struct outside the bridge and use a type alias
Browse files Browse the repository at this point in the history
Related to #559
  • Loading branch information
ahayzen-kdab committed Jul 17, 2023
1 parent 8097226 commit 998a00f
Show file tree
Hide file tree
Showing 42 changed files with 769 additions and 685 deletions.
18 changes: 12 additions & 6 deletions crates/cxx-qt-gen/src/generator/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -71,8 +73,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(cxx_file_stem = "my_object")]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -88,8 +92,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand Down
30 changes: 20 additions & 10 deletions crates/cxx-qt-gen/src/generator/cpp/qobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -188,8 +190,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject(base = "QStringListModel")]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject(base = "QStringListModel")]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -209,8 +213,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_name = "MyQmlElement")]
pub struct MyNamedObject;
extern "RustQt" {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_name = "MyQmlElement")]
type MyNamedObject = super::MyNamedObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -233,8 +239,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_singleton)]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_singleton)]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -258,8 +266,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_uncreatable)]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_uncreatable)]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/src/generator/naming/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct NamespaceName {

impl From<&ParsedQObject> for NamespaceName {
fn from(qobject: &ParsedQObject) -> Self {
NamespaceName::from_pair_str(&qobject.namespace, &qobject.qobject_struct.ident)
NamespaceName::from_pair_str(&qobject.namespace, &qobject.qobject_ty.ident)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/src/generator/naming/qobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct QObjectName {

impl From<&ParsedQObject> for QObjectName {
fn from(qobject: &ParsedQObject) -> Self {
Self::from(&qobject.qobject_struct.ident)
Self::from(&qobject.qobject_ty.ident)
}
}

Expand Down
24 changes: 16 additions & 8 deletions crates/cxx-qt-gen/src/generator/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -100,8 +102,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -121,8 +125,10 @@ mod tests {
mod ffi {
use std::collections::HashMap;

#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -140,8 +146,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(cxx_file_stem = "my_object")]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand Down
34 changes: 22 additions & 12 deletions crates/cxx-qt-gen/src/generator/rust/qobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
parser::qobject::ParsedQObject,
};
use quote::quote;
use syn::{Ident, ImplItem, Item, Result};
use syn::{parse_quote, Ident, ImplItem, Item, Result};

#[derive(Default)]
pub struct GeneratedRustQObjectBlocks {
Expand Down Expand Up @@ -62,11 +62,15 @@ impl GeneratedRustQObject {
.blocks
.append(&mut generate_qobject_definitions(&qobject_idents)?);

// Add our QObject struct to the implementation blocks
generated
.blocks
.cxx_qt_mod_contents
.push(syn::Item::Struct(qobject.qobject_struct.clone()));
// Add a type alias so that generated code can still find T
//
// TODO: this should be removed once generated methods aren't in the hidden module
generated.blocks.cxx_qt_mod_contents.push({
let rust_struct_name_rust = &qobject_idents.rust_struct.rust;
parse_quote! {
type #rust_struct_name_rust = super::#rust_struct_name_rust;
}
});

// Generate methods for the properties, invokables, signals
generated.blocks.append(&mut generate_rust_properties(
Expand Down Expand Up @@ -218,8 +222,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -236,8 +242,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -254,8 +262,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_singleton)]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_singleton)]
type MyObject = super::MyObject;
}
}
};
let parser = Parser::from(module).unwrap();
Expand Down

0 comments on commit 998a00f

Please sign in to comment.