Skip to content

Commit

Permalink
cxx-qt-gen: use generator phase for rust and rust_mut in C++ too
Browse files Browse the repository at this point in the history
This simplifies the writer phase of C++.
  • Loading branch information
ahayzen-kdab authored and Be-ing committed Aug 1, 2023
1 parent 365906c commit 45c323a
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 102 deletions.
109 changes: 109 additions & 0 deletions crates/cxx-qt-gen/src/generator/cpp/cxxqttype.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::generator::{
cpp::{fragment::CppFragment, qobject::GeneratedCppQObjectBlocks},
naming::qobject::QObjectName,
};
use indoc::formatdoc;
use syn::Result;

pub fn generate(qobject_idents: &QObjectName) -> Result<GeneratedCppQObjectBlocks> {
let mut result = GeneratedCppQObjectBlocks::default();

let rust_ident = qobject_idents.rust_struct.cpp.to_string();
let qobject_ident = qobject_idents.cpp_class.cpp.to_string();

result.includes.insert("#include <memory>".to_owned());

result.methods.push(CppFragment::Pair {
header: formatdoc! {
r#"
{rust_ident} const& unsafeRust() const;
{rust_ident}& unsafeRustMut();
"#
},
source: formatdoc! {
r#"
{rust_ident} const&
{qobject_ident}::unsafeRust() const
{{
return *m_rustObj;
}}
{rust_ident}&
{qobject_ident}::unsafeRustMut()
{{
return *m_rustObj;
}}
"#
},
});

result
.members
.push(format!("::rust::Box<{rust_ident}> m_rustObj;"));

Ok(result)
}

#[cfg(test)]
mod tests {
use super::*;

use crate::generator::naming::qobject::tests::create_qobjectname;
use indoc::indoc;
use pretty_assertions::assert_str_eq;

#[test]
fn test_generate_cpp_locking() {
let qobject_idents = create_qobjectname();

let generated = generate(&qobject_idents).unwrap();

// includes
assert_eq!(generated.includes.len(), 1);
assert!(generated.includes.contains("#include <memory>"));

// members
assert_eq!(generated.members.len(), 1);
assert_str_eq!(
&generated.members[0],
"::rust::Box<MyObjectRust> m_rustObj;"
);

// methods
assert_eq!(generated.methods.len(), 1);

let (header, source) = if let CppFragment::Pair { header, source } = &generated.methods[0] {
(header, source)
} else {
panic!("Expected pair")
};
assert_str_eq!(
header,
indoc! {r#"
MyObjectRust const& unsafeRust() const;
MyObjectRust& unsafeRustMut();
"#}
);
assert_str_eq!(
source,
indoc! {r#"
MyObjectRust const&
MyObject::unsafeRust() const
{
return *m_rustObj;
}
MyObjectRust&
MyObject::unsafeRustMut()
{
return *m_rustObj;
}
"#}
);
}
}
1 change: 1 addition & 0 deletions crates/cxx-qt-gen/src/generator/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

mod constructor;
pub mod cxxqttype;
pub mod fragment;
pub mod inherit;
pub mod locking;
Expand Down
10 changes: 8 additions & 2 deletions crates/cxx-qt-gen/src/generator/cpp/qobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

use crate::generator::{
cpp::{
constructor, fragment::CppFragment, inherit, locking, method::generate_cpp_methods,
property::generate_cpp_properties, signal::generate_cpp_signals, threading,
constructor, cxxqttype, fragment::CppFragment, inherit, locking,
method::generate_cpp_methods, property::generate_cpp_properties,
signal::generate_cpp_signals, threading,
},
naming::{namespace::NamespaceName, qobject::QObjectName},
};
Expand Down Expand Up @@ -107,6 +108,11 @@ impl GeneratedCppQObject {
None
};

// Add the CxxQtType rust and rust_mut methods
generated
.blocks
.append(&mut cxxqttype::generate(&qobject_idents)?);

// Generate methods for the properties, invokables, signals
generated.blocks.append(&mut generate_cpp_properties(
&qobject.properties,
Expand Down
16 changes: 2 additions & 14 deletions crates/cxx-qt-gen/src/writer/cpp/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,10 @@ fn qobjects_header(generated: &GeneratedCppBlocks) -> Vec<String> {
public:
~{ident}();
{rust_ident} const& unsafeRust() const;
{rust_ident}& unsafeRustMut();
{public_methods}
{private_methods}
private:
{members}
{private_members}
}};
static_assert(::std::is_base_of<QObject, {ident}>::value, "{ident} must inherit from QObject");
Expand All @@ -93,19 +90,11 @@ fn qobjects_header(generated: &GeneratedCppBlocks) -> Vec<String> {
ident = qobject.ident,
namespace_start = namespace_start,
namespace_end = namespace_end,
rust_ident = qobject.rust_ident,
base_class = qobject.base_class,
metaobjects = qobject.blocks.metaobjects.join("\n "),
public_methods = create_block("public", &qobject.blocks.methods.iter().filter_map(pair_as_header).collect::<Vec<String>>()),
private_methods = create_block("private", &qobject.blocks.private_methods.iter().filter_map(pair_as_header).collect::<Vec<String>>()),
members = {
let mut members = vec![
format!("::rust::Box<{rust_ident}> m_rustObj;", rust_ident = qobject.rust_ident),
];

members.extend(qobject.blocks.members.iter().cloned());
members.join("\n ")
},
private_members = create_block("private", &qobject.blocks.members),
metatype = if generated.namespace.is_empty() {
qobject.ident.clone()
} else {
Expand All @@ -122,7 +111,6 @@ pub fn write_cpp_header(generated: &GeneratedCppBlocks) -> String {
formatdoc! {r#"
#pragma once
#include <memory>
{includes}
namespace rust::cxxqtlib1 {{
Expand Down
71 changes: 4 additions & 67 deletions crates/cxx-qt-gen/src/writer/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ mod tests {
indoc! {r#"
#pragma once
#include <memory>
#include <test>
namespace rust::cxxqtlib1 {
Expand All @@ -317,8 +316,6 @@ mod tests {
public:
~MyObject();
MyObjectRust const& unsafeRust() const;
MyObjectRust& unsafeRustMut();
public:
int count() const;
Expand All @@ -334,8 +331,7 @@ mod tests {
void privateMethod() const;
void privateMethod();
private:
::rust::Box<MyObjectRust> m_rustObj;
};
static_assert(::std::is_base_of<QObject, MyObject>::value, "MyObject must inherit from QObject");
Expand All @@ -351,7 +347,6 @@ mod tests {
indoc! {r#"
#pragma once
#include <memory>
#include <test>
namespace rust::cxxqtlib1 {
Expand Down Expand Up @@ -379,17 +374,14 @@ mod tests {
public:
~FirstObject();
FirstObjectRust const& unsafeRust() const;
FirstObjectRust& unsafeRustMut();
public:
int count() const;
Q_SLOT void setCount(int count);
Q_SIGNAL void countChanged();
private:
::rust::Box<FirstObjectRust> m_rustObj;
};
static_assert(::std::is_base_of<QObject, FirstObject>::value, "FirstObject must inherit from QObject");
Expand All @@ -405,8 +397,6 @@ mod tests {
public:
~SecondObject();
SecondObjectRust const& unsafeRust() const;
SecondObjectRust& unsafeRustMut();
public:
int count() const;
Expand All @@ -416,8 +406,7 @@ mod tests {
private:
void privateMethod() const;
private:
::rust::Box<SecondObjectRust> m_rustObj;
};
static_assert(::std::is_base_of<QObject, SecondObject>::value, "SecondObject must inherit from QObject");
Expand All @@ -433,7 +422,6 @@ mod tests {
indoc! {r#"
#pragma once
#include <memory>
#include <test>
namespace rust::cxxqtlib1 {
Expand All @@ -457,8 +445,6 @@ mod tests {
public:
~MyObject();
MyObjectRust const& unsafeRust() const;
MyObjectRust& unsafeRustMut();
public:
int count() const;
Expand All @@ -474,8 +460,7 @@ mod tests {
void privateMethod() const;
void privateMethod();
private:
::rust::Box<MyObjectRust> m_rustObj;
};
static_assert(::std::is_base_of<QObject, MyObject>::value, "MyObject must inherit from QObject");
Expand All @@ -498,18 +483,6 @@ mod tests {
}
MyObjectRust const&
MyObject::unsafeRust() const
{
return *m_rustObj;
}
MyObjectRust&
MyObject::unsafeRustMut()
{
return *m_rustObj;
}
int
MyObject::count() const
{
Expand Down Expand Up @@ -572,18 +545,6 @@ mod tests {
}
FirstObjectRust const&
FirstObject::unsafeRust() const
{
return *m_rustObj;
}
FirstObjectRust&
FirstObject::unsafeRustMut()
{
return *m_rustObj;
}
int
FirstObject::count() const
{
Expand All @@ -605,18 +566,6 @@ mod tests {
}
SecondObjectRust const&
SecondObject::unsafeRust() const
{
return *m_rustObj;
}
SecondObjectRust&
SecondObject::unsafeRustMut()
{
return *m_rustObj;
}
int
SecondObject::count() const
{
Expand Down Expand Up @@ -650,18 +599,6 @@ mod tests {
}
MyObjectRust const&
MyObject::unsafeRust() const
{
return *m_rustObj;
}
MyObjectRust&
MyObject::unsafeRustMut()
{
return *m_rustObj;
}
int
MyObject::count() const
{
Expand Down
13 changes: 0 additions & 13 deletions crates/cxx-qt-gen/src/writer/cpp/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,12 @@ fn qobjects_source(generated: &GeneratedCppBlocks) -> Vec<String> {
{deconstructors}
}}
{rust_ident} const&
{ident}::unsafeRust() const
{{
return *m_rustObj;
}}
{rust_ident}&
{ident}::unsafeRustMut()
{{
return *m_rustObj;
}}
{methods}
{namespace_end}
"#,
ident = qobject.ident,
namespace_start = namespace_start,
namespace_end = namespace_end,
rust_ident = qobject.rust_ident,
methods = qobject.blocks.methods.iter().chain(qobject.blocks.private_methods.iter()).filter_map(pair_as_source).collect::<Vec<String>>().join("\n"),
deconstructors = qobject.blocks.deconstructors.join("\n "),
}
Expand Down
Loading

0 comments on commit 45c323a

Please sign in to comment.