Skip to content

Commit

Permalink
WIP: cxx-qt-gen: add support for nested object properties
Browse files Browse the repository at this point in the history
Closes KDAB#299
  • Loading branch information
ahayzen-kdab committed Nov 28, 2022
1 parent 3b61444 commit 77aa829
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/src/generator/cpp/property/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ use indoc::formatdoc;
pub fn generate(idents: &QPropertyName, qobject_ident: &str, cxx_ty: &CppType) -> CppFragmentPair {
CppFragmentPair {
header: format!(
"const {cxx_ty}& {ident_getter}() const;",
"{cxx_ty} const& {ident_getter}() const;",
cxx_ty = cxx_ty.as_cxx_ty(),
ident_getter = idents.getter.cpp
),
source: formatdoc!(
r#"
const {cxx_ty}&
{cxx_ty} const&
{qobject_ident}::{ident_getter}() const
{{
{rust_obj_guard}
return {convert}<const {cxx_ty}&, const {rust_ty}&>{{}}(m_rustObj->{ident_getter}(*this));
return {convert}<{cxx_ty} const&, {rust_ty} const&>{{}}(m_rustObj->{ident_getter}(*this));
}}
"#,
convert = CXX_QT_CONVERT,
Expand Down
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/src/generator/cpp/property/setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ use indoc::formatdoc;
pub fn generate(idents: &QPropertyName, qobject_ident: &str, cxx_ty: &CppType) -> CppFragmentPair {
CppFragmentPair {
header: format!(
"void {ident_setter}(const {cxx_ty}& value);",
"void {ident_setter}({cxx_ty} const& value);",
cxx_ty = cxx_ty.as_cxx_ty(),
ident_setter = idents.setter.cpp,
),
source: formatdoc! {
r#"
void
{qobject_ident}::{ident_setter}(const {cxx_ty}& value)
{qobject_ident}::{ident_setter}({cxx_ty} const& value)
{{
{rust_obj_guard}
m_rustObj->{ident_setter}(*this, {convert}<{rust_ty}, const {cxx_ty}&>{{}}(value));
m_rustObj->{ident_setter}(*this, {convert}<{rust_ty}, {cxx_ty} const&>{{}}(value));
}}
"#,
convert = CXX_QT_CONVERT,
Expand Down
8 changes: 7 additions & 1 deletion crates/cxx-qt-gen/src/generator/rust/property/setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ pub fn generate(
let ident = &idents.name.rust;
let notify_ident = &idents.notify.rust;

// TODO: need to read the types of the method input and output to see if any of them are considered unsafe
// eg if there is a TypePtr in them
let has_unsafe = {
quote! { unsafe }
};

RustFragmentPair {
cxx_bridge: vec![quote! {
extern "Rust" {
#[cxx_name = #setter_cpp]
fn #setter_rust(self: &mut #rust_struct_name_rust, cpp: Pin<&mut #cpp_class_name_rust>, value: #ty);
#has_unsafe fn #setter_rust(self: &mut #rust_struct_name_rust, cpp: Pin<&mut #cpp_class_name_rust>, value: #ty);
}
}],
implementation: vec![
Expand Down
19 changes: 9 additions & 10 deletions examples/qml_features/rust/src/nested_qobjects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ mod ffi {
}

#[cxx_qt::qobject]
#[derive(Default)]
pub struct OuterObject {
// #[qproperty]
// inner: *mut InnerObjectPtr,
#[qproperty]
inner: *mut InnerObjectPtr,
}

// impl Default for OuterObject {
// fn default() -> Self {
// Self {
// // inner: // what goes here?
// }
// }
// }
impl Default for OuterObject {
fn default() -> Self {
Self {
inner: std::ptr::null_mut(),
}
}
}

impl qobject::OuterObject {
// InnerObjectPtr needs to be called InnerObject in our C++ generation...
Expand Down

0 comments on commit 77aa829

Please sign in to comment.