Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HashSet of flags to ParsedQPoperty for optional getters and setters #994

Merged
merged 19 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
dc49d33
Add HashSet of flags to ParsedQPoperty for optional getters and setters
BenFordTytherington Jul 9, 2024
452ba32
Add support to parse input into a hashset of flags in the ParsedQProp…
BenFordTytherington Jul 10, 2024
4e82194
Refactor for QPropertyFlag to use Idents instead of Strings
BenFordTytherington Jul 10, 2024
1eead3f
Make setter optional
BenFordTytherington Jul 11, 2024
50df978
Make Notify optional
BenFordTytherington Jul 11, 2024
eb0bdb5
Improve error handling
BenFordTytherington Jul 12, 2024
fe3ecfd
Misc changes before QPropertyFlag rewrite
BenFordTytherington Jul 11, 2024
1fa65fa
Refactor to QPropertyFlags struct with Options<> instead of a HashSet.
BenFordTytherington Jul 11, 2024
865d7fe
Rewrite property parser with new layout for storing flags
BenFordTytherington Jul 12, 2024
209bff4
Improve Error checking and refactor the generators
BenFordTytherington Jul 16, 2024
eeb2ba6
Fix Issues raised in code review, including refactor to use Enum in Q…
BenFordTytherington Jul 17, 2024
73e3cc9
Refactor to use a new NameState enum to simplify checks before genera…
BenFordTytherington Jul 17, 2024
8570970
Merge remote-tracking branch 'upstream/main' into feature-optional-rw
BenFordTytherington Jul 17, 2024
5dfe239
Cleanup and comments
BenFordTytherington Jul 17, 2024
dd50978
Fix nitpicks in PR and update docs and changelog with changes
BenFordTytherington Jul 19, 2024
4c3b3fd
Fix md lints
BenFordTytherington Jul 19, 2024
eea3a53
Improve docs and remove some commented code
BenFordTytherington Jul 22, 2024
ffccc51
Fix typo in docs
BenFordTytherington Jul 22, 2024
5f4917f
Fix asserts in tests
BenFordTytherington Jul 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/cxx-qt-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
[package]
name = "cxx-qt-gen"
version.workspace = true
authors = ["Andrew Hayzen <[email protected]>", "Gerhard de Clercq <[email protected]>", "Leon Matthes <[email protected]>"]
authors = [
"Andrew Hayzen <[email protected]>",
"Gerhard de Clercq <[email protected]>",
"Leon Matthes <[email protected]>",
]
edition.workspace = true
license.workspace = true
description = "Code generation for integrating `cxx-qt` into higher level tools"
Expand Down
19 changes: 15 additions & 4 deletions crates/cxx-qt-gen/src/generator/cpp/property/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@ use crate::generator::naming::property::QPropertyNames;

/// Generate the metaobject line for a given property
pub fn generate(idents: &QPropertyNames, cxx_ty: &str) -> String {
let mut parts = vec![format!(
"READ {ident_getter}",
ident_getter = idents.getter.cxx_unqualified()
)];
// Write
if let Some(name) = &idents.setter {
parts.push(format!("WRITE {}", name.cxx_unqualified()));
}
// Notify
if let Some(name) = &idents.notify {
parts.push(format!("NOTIFY {}", name.cxx_unqualified()));
}

format!(
"Q_PROPERTY({ty} {ident} READ {ident_getter} WRITE {ident_setter} NOTIFY {ident_notify})",
"Q_PROPERTY({ty} {ident} {meta_parts})",
ty = cxx_ty,
ident = idents.name.cxx_unqualified(),
ident_getter = idents.getter.cxx_unqualified(),
ident_setter = idents.setter.cxx_unqualified(),
ident_notify = idents.notify.cxx_unqualified()
meta_parts = parts.join(" ")
)
}
98 changes: 75 additions & 23 deletions crates/cxx-qt-gen/src/generator/cpp/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,49 @@ pub fn generate_cpp_properties(
let qobject_ident = qobject_idents.name.cxx_unqualified();

for property in properties {
// Cache the idents as they are used in multiple places
// Cache the idents and flags as they are used in multiple places
let idents = QPropertyNames::from(property);
let cxx_ty = syn_type_to_cpp_type(&property.ty, type_names)?;

let flags = &property.flags;

generated.metaobjects.push(meta::generate(&idents, &cxx_ty));
generated
.methods
.push(getter::generate(&idents, &qobject_ident, &cxx_ty));
generated
.private_methods
.push(getter::generate_wrapper(&idents, &cxx_ty));
generated
.methods
.push(setter::generate(&idents, &qobject_ident, &cxx_ty));
generated
.private_methods
.push(setter::generate_wrapper(&idents, &cxx_ty));
signals.push(signal::generate(&idents, qobject_idents));

// None indicates no custom identifier was provided
if flags.read.is_none() {
generated
.methods
.push(getter::generate(&idents, &qobject_ident, &cxx_ty));
generated
.private_methods
.push(getter::generate_wrapper(&idents, &cxx_ty));
ahayzen-kdab marked this conversation as resolved.
Show resolved Hide resolved
}
ahayzen-kdab marked this conversation as resolved.
Show resolved Hide resolved

// Checking custom setter wasn't provided
if flags
.write
.clone()
ahayzen-kdab marked this conversation as resolved.
Show resolved Hide resolved
.is_some_and(|ident_option| ident_option.is_none())
{
if let Some(setter) = setter::generate(&idents, &qobject_ident, &cxx_ty) {
generated.methods.push(setter)
}

if let Some(setter_wrapper) = setter::generate_wrapper(&idents, &cxx_ty) {
generated.private_methods.push(setter_wrapper)
}
}

// Checking custom notifier wasn't provided
if flags
.notify
.clone()
.is_some_and(|ident_option| ident_option.is_none())
ahayzen-kdab marked this conversation as resolved.
Show resolved Hide resolved
{
if let Some(notify) = signal::generate(&idents, qobject_idents) {
signals.push(notify)
}
}
}

generated.append(&mut generate_cpp_signals(
Expand All @@ -60,25 +85,49 @@ pub fn generate_cpp_properties(
mod tests {
use super::*;

use crate::parser::property::QPropertyFlags;

use crate::generator::naming::qobject::tests::create_qobjectname;
use crate::CppFragment;
use indoc::indoc;
use pretty_assertions::assert_str_eq;
use quote::format_ident;
use syn::parse_quote;
use syn::{parse_quote, ItemStruct};

#[test]
fn test_optional_write() {
let mut input: ItemStruct = parse_quote! {
#[qproperty(i32, num, read, write, notify)]
struct MyStruct;
};
let property = ParsedQProperty::parse(input.attrs.remove(0)).unwrap();

let properties = vec![property];

let qobject_idents = create_qobjectname();

let type_names = TypeNames::mock();
let _generated =
ahayzen-kdab marked this conversation as resolved.
Show resolved Hide resolved
generate_cpp_properties(&properties, &qobject_idents, &type_names).unwrap();
}

#[test]
fn test_generate_cpp_properties() {
let mut input1: ItemStruct = parse_quote! {
#[qproperty(i32, trivial_property, read, write, notify)]
struct MyStruct;
};

let mut input2: ItemStruct = parse_quote! {
#[qproperty(UniquePtr<QColor>, opaque_property)]
struct MyStruct;
};

let properties = vec![
ParsedQProperty {
ident: format_ident!("trivial_property"),
ty: parse_quote! { i32 },
},
ParsedQProperty {
ident: format_ident!("opaque_property"),
ty: parse_quote! { UniquePtr<QColor> },
},
ParsedQProperty::parse(input1.attrs.remove(0)).unwrap(),
ParsedQProperty::parse(input2.attrs.remove(0)).unwrap(),
];

let qobject_idents = create_qobjectname();

let mut type_names = TypeNames::mock();
Expand All @@ -97,6 +146,7 @@ mod tests {
} else {
panic!("Expected pair!")
};

assert_str_eq!(header, "::std::int32_t const& getTrivialProperty() const;");
assert_str_eq!(
source,
Expand Down Expand Up @@ -136,6 +186,7 @@ mod tests {
} else {
panic!("Expected pair!")
};

assert_str_eq!(
header,
"::std::unique_ptr<QColor> const& getOpaqueProperty() const;"
Expand Down Expand Up @@ -358,6 +409,7 @@ mod tests {
let properties = vec![ParsedQProperty {
ident: format_ident!("mapped_property"),
ty: parse_quote! { A },
flags: QPropertyFlags::default(),
}];
let qobject_idents = create_qobjectname();

Expand Down
57 changes: 32 additions & 25 deletions crates/cxx-qt-gen/src/generator/cpp/property/setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,39 @@
use crate::generator::{cpp::fragment::CppFragment, naming::property::QPropertyNames};
use indoc::formatdoc;

pub fn generate(idents: &QPropertyNames, qobject_ident: &str, cxx_ty: &str) -> CppFragment {
CppFragment::Pair {
header: format!(
"Q_SLOT void {ident_setter}({cxx_ty} const& value);",
ident_setter = idents.setter.cxx_unqualified(),
),
source: formatdoc! {
r#"
void
{qobject_ident}::{ident_setter}({cxx_ty} const& value)
{{
const ::rust::cxxqt1::MaybeLockGuard<{qobject_ident}> guard(*this);
{ident_setter_wrapper}(value);
}}
"#,
ident_setter = idents.setter.cxx_unqualified(),
ident_setter_wrapper = idents.setter_wrapper.cxx_unqualified(),
},
pub fn generate(idents: &QPropertyNames, qobject_ident: &str, cxx_ty: &str) -> Option<CppFragment> {
// Checking whether a setter should be generated based off if it has a name
if let (Some(setter), Some(setter_wrapper)) = (&idents.setter, &idents.setter_wrapper) {
ahayzen-kdab marked this conversation as resolved.
Show resolved Hide resolved
Some(CppFragment::Pair {
header: format!(
"Q_SLOT void {ident_setter}({cxx_ty} const& value);",
ident_setter = setter.cxx_unqualified(),
),
source: formatdoc! {
r#"
void
{qobject_ident}::{ident_setter}({cxx_ty} const& value)
{{
const ::rust::cxxqt1::MaybeLockGuard<{qobject_ident}> guard(*this);
{ident_setter_wrapper}(value);
}}
"#,
ident_setter = setter.cxx_unqualified(),
ident_setter_wrapper = setter_wrapper.cxx_unqualified(),
},
})
} else {
None
}
}

pub fn generate_wrapper(idents: &QPropertyNames, cxx_ty: &str) -> CppFragment {
CppFragment::Header(format!(
// Note that we pass T not const T& to Rust so that it is by-value
// https://github.com/KDAB/cxx-qt/issues/463
"void {ident_setter_wrapper}({cxx_ty} value) noexcept;",
ident_setter_wrapper = idents.setter_wrapper.cxx_unqualified()
))
pub fn generate_wrapper(idents: &QPropertyNames, cxx_ty: &str) -> Option<CppFragment> {
idents.setter_wrapper.as_ref().map(|setter_wrapper| {
CppFragment::Header(format!(
// Note that we pass T not const T& to Rust so that it is by-value
// https://github.com/KDAB/cxx-qt/issues/463
"void {ident_setter_wrapper}({cxx_ty} value) noexcept;",
ident_setter_wrapper = setter_wrapper.cxx_unqualified()
))
})
}
31 changes: 18 additions & 13 deletions crates/cxx-qt-gen/src/generator/cpp/property/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@ use crate::{
parser::signals::ParsedSignal,
};

pub fn generate(idents: &QPropertyNames, qobject_idents: &QObjectNames) -> ParsedSignal {
pub fn generate(idents: &QPropertyNames, qobject_idents: &QObjectNames) -> Option<ParsedSignal> {
// We build our signal in the generation phase as we need to use the naming
// structs to build the signal name
let cpp_class_rust = &qobject_idents.name.rust_unqualified();
ahayzen-kdab marked this conversation as resolved.
Show resolved Hide resolved
let notify_cpp = &idents.notify.cxx_unqualified();
let notify_rust = idents.notify.rust_unqualified();
let method: ForeignItemFn = syn::parse_quote! {
#[doc = "Notify for the Q_PROPERTY"]
#[cxx_name = #notify_cpp]
fn #notify_rust(self: Pin<&mut #cpp_class_rust>);
};
ParsedSignal::from_property_method(
method,
idents.notify.clone(),
qobject_idents.name.rust_unqualified().clone(),
)
if let Some(notify) = &idents.notify {
let notify_cpp = notify.cxx_unqualified();
let notify_rust = notify.rust_unqualified();
let method: ForeignItemFn = syn::parse_quote! {
#[doc = "Notify for the Q_PROPERTY"]
#[cxx_name = #notify_cpp]
fn #notify_rust(self: Pin<&mut #cpp_class_rust>);
};

Some(ParsedSignal::from_property_method(
method,
notify.clone(),
qobject_idents.name.rust_unqualified().clone(),
))
} else {
None
}
}
57 changes: 46 additions & 11 deletions crates/cxx-qt-gen/src/generator/naming/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,49 @@ pub struct QPropertyNames {
pub name: Name,
pub getter: Name,
pub getter_wrapper: Name,
pub setter: Name,
pub setter_wrapper: Name,
pub notify: Name,
pub setter: Option<Name>,
ahayzen-kdab marked this conversation as resolved.
Show resolved Hide resolved
pub setter_wrapper: Option<Name>,
pub notify: Option<Name>,
}

impl From<&ParsedQProperty> for QPropertyNames {
fn from(property: &ParsedQProperty) -> Self {
let property_name = property_name_from_rust_name(property.ident.clone());
let getter = getter_name_from_property(&property_name);
let setter = setter_name_from_property(&property_name);

let flags = &property.flags;

// Match for custom name or autogenerate one
let getter = match &flags.read {
Some(ident) => Name::new(ident.clone()),
None => getter_name_from_property(&property_name),
};

// Match for custom name, autogenerate, or None
// TODO: Refactor to use an enum type to represent these 3 states better
let setter = match &flags.write {
Some(ident_option) => match ident_option {
Some(ident) => Some(Name::new(ident.clone())),
None => Some(setter_name_from_property(&property_name)),
},
None => None,
};

let notify = match &flags.notify {
Some(ident_option) => match ident_option {
Some(ident) => Some(Name::new(ident.clone())),
None => Some(notify_name_from_property(&property_name)),
},
None => None,
};

let setter_wrapper = setter.as_ref().map(wrapper_name_from_function_name);

Self {
getter_wrapper: wrapper_name_from_function_name(&getter),
ahayzen-kdab marked this conversation as resolved.
Show resolved Hide resolved
getter,
setter_wrapper: wrapper_name_from_function_name(&setter),
setter_wrapper,
setter,
notify: notify_name_from_property(&property_name),
notify,
ahayzen-kdab marked this conversation as resolved.
Show resolved Hide resolved
name: property_name,
}
}
Expand Down Expand Up @@ -77,12 +104,14 @@ pub mod tests {
use syn::parse_quote;

use super::*;
use crate::parser::property::QPropertyFlags;

pub fn create_i32_qpropertyname() -> QPropertyNames {
let ty: syn::Type = parse_quote! { i32 };
let property = ParsedQProperty {
ident: format_ident!("my_property"),
ty,
flags: QPropertyFlags::default(),
};
QPropertyNames::from(&property)
}
Expand All @@ -97,14 +126,20 @@ pub mod tests {
names.getter.rust_unqualified(),
&format_ident!("my_property")
);
assert_eq!(names.setter.cxx_unqualified(), "setMyProperty");
assert_eq!(
names.setter.rust_unqualified(),
names.setter.clone().unwrap().cxx_unqualified(),
ahayzen-kdab marked this conversation as resolved.
Show resolved Hide resolved
"setMyProperty"
);
assert_eq!(
names.setter.clone().unwrap().rust_unqualified(),
&format_ident!("set_my_property")
);
assert_eq!(names.notify.cxx_unqualified(), "myPropertyChanged");
assert_eq!(
names.notify.rust_unqualified(),
names.notify.clone().unwrap().cxx_unqualified(),
"myPropertyChanged"
);
assert_eq!(
names.notify.clone().unwrap().rust_unqualified(),
&format_ident!("my_property_changed")
);
}
Expand Down
Loading
Loading