From c7fbcabb50db5e83c7bae8157a8c4ad698f732e7 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Mon, 27 Nov 2023 16:47:57 +0100 Subject: [PATCH 1/8] Add qcommandlineoption support --- .../include/core/qcommandlineoption.h | 9 +++ crates/cxx-qt-lib-headers/src/lib.rs | 4 ++ crates/cxx-qt-lib/build.rs | 2 + crates/cxx-qt-lib/src/core/mod.rs | 3 + .../src/core/qcommandlineoption.cpp | 27 ++++++++ .../cxx-qt-lib/src/core/qcommandlineoption.rs | 69 +++++++++++++++++++ tests/qt_types_standalone/CMakeLists.txt | 1 + .../cpp/qcommandlineoption.h | 40 +++++++++++ 8 files changed, 155 insertions(+) create mode 100644 crates/cxx-qt-lib-headers/include/core/qcommandlineoption.h create mode 100644 crates/cxx-qt-lib/src/core/qcommandlineoption.cpp create mode 100644 crates/cxx-qt-lib/src/core/qcommandlineoption.rs create mode 100644 tests/qt_types_standalone/cpp/qcommandlineoption.h diff --git a/crates/cxx-qt-lib-headers/include/core/qcommandlineoption.h b/crates/cxx-qt-lib-headers/include/core/qcommandlineoption.h new file mode 100644 index 000000000..0ba98910b --- /dev/null +++ b/crates/cxx-qt-lib-headers/include/core/qcommandlineoption.h @@ -0,0 +1,9 @@ +// clang-format off +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Laurent Montel +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +#pragma once + +#include diff --git a/crates/cxx-qt-lib-headers/src/lib.rs b/crates/cxx-qt-lib-headers/src/lib.rs index c87cb2207..584ba25a3 100644 --- a/crates/cxx-qt-lib-headers/src/lib.rs +++ b/crates/cxx-qt-lib-headers/src/lib.rs @@ -18,6 +18,10 @@ pub fn write_headers(directory: impl AsRef) { std::fs::create_dir_all(directory).expect("Could not create cxx-qt-lib header directory"); for (file_contents, file_name) in [ (include_str!("../include/core/qbytearray.h"), "qbytearray.h"), + ( + include_str!("../include/core/qcommandlineoption.h"), + "qcommandlineoption.h", + ), ( include_str!("../include/core/qcoreapplication.h"), "qcoreapplication.h", diff --git a/crates/cxx-qt-lib/build.rs b/crates/cxx-qt-lib/build.rs index 1c77f747f..3bdf58352 100644 --- a/crates/cxx-qt-lib/build.rs +++ b/crates/cxx-qt-lib/build.rs @@ -33,6 +33,7 @@ fn main() { let mut rust_bridges = vec![ "core/qbytearray", + "core/qcommandlineoption", "core/qcoreapplication", "core/qdate", "core/qhash/qhash_i32_qbytearray", @@ -199,6 +200,7 @@ fn main() { let mut cpp_files = vec![ "core/qbytearray", + "core/qcommandlineoption", "core/qcoreapplication", "core/qdate", "core/qhash/qhash", diff --git a/crates/cxx-qt-lib/src/core/mod.rs b/crates/cxx-qt-lib/src/core/mod.rs index 53ee88917..1caeba1f6 100644 --- a/crates/cxx-qt-lib/src/core/mod.rs +++ b/crates/cxx-qt-lib/src/core/mod.rs @@ -6,6 +6,9 @@ mod qbytearray; pub use qbytearray::QByteArray; +mod qcommandlineoption; +pub use qcommandlineoption::QCommandLineOption; + mod qcoreapplication; pub use qcoreapplication::QCoreApplication; diff --git a/crates/cxx-qt-lib/src/core/qcommandlineoption.cpp b/crates/cxx-qt-lib/src/core/qcommandlineoption.cpp new file mode 100644 index 000000000..7bb4a8b52 --- /dev/null +++ b/crates/cxx-qt-lib/src/core/qcommandlineoption.cpp @@ -0,0 +1,27 @@ +// clang-format off +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Laurent Montel +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +#include "cxx-qt-lib/qcommandlineoption.h" + +#include "../assertion_utils.h" + +#include + + +// QCommandLineOption has 1 pointer +// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qcommandlineoption.h?h=v5.15.6-lts-lgpl#n59 +// +// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qcommandlineoption.h?h=v6.2.4#n96 +assert_alignment_and_size(QCommandLineOption, + alignof(::std::size_t), + sizeof(::std::size_t)); + +static_assert(!::std::is_trivially_copy_assignable::value); +static_assert(!::std::is_trivially_copy_constructible::value); + +static_assert(!::std::is_trivially_destructible::value); + +static_assert(QTypeInfo::isRelocatable); diff --git a/crates/cxx-qt-lib/src/core/qcommandlineoption.rs b/crates/cxx-qt-lib/src/core/qcommandlineoption.rs new file mode 100644 index 000000000..a63d5eb8f --- /dev/null +++ b/crates/cxx-qt-lib/src/core/qcommandlineoption.rs @@ -0,0 +1,69 @@ +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Laurent Montel +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use cxx::{type_id, ExternType}; +use std::mem::MaybeUninit; + +#[cxx::bridge] +mod ffi { + unsafe extern "C++" { + include!("cxx-qt-lib/qcommandlineoption.h"); + type QCommandLineOption = super::QCommandLineOption; + include!("cxx-qt-lib/qstring.h"); + type QString = crate::QString; + include!("cxx-qt-lib/qstringlist.h"); + type QStringList = crate::QStringList; + + /// Returns the description set for this option. + fn description(self: &QCommandLineOption) -> QString; + + /// Returns the names set for this option. + fn names(self: &QCommandLineOption) -> QStringList; + + /// Returns the name of the expected value. + #[rust_name = "value_name"] + fn valueName(self: &QCommandLineOption) -> QString; + } + + #[namespace = "rust::cxxqtlib1"] + unsafe extern "C++" { + include!("cxx-qt-lib/common.h"); + + #[doc(hidden)] + #[rust_name = "qcommandlineoption_init_default"] + fn construct() -> QCommandLineOption; + + #[doc(hidden)] + #[rust_name = "qcommandlineoption_init_from_qcommandlineoption"] + fn construct(url: &QCommandLineOption) -> QCommandLineOption; + } +} + +#[repr(C)] +pub struct QCommandLineOption { + _space: MaybeUninit, +} + +impl Clone for QCommandLineOption { + /// Constructs a copy of other. + fn clone(&self) -> Self { + ffi::qcommandlineoption_init_from_qcommandlineoption(self) + } +} + +impl Default for QCommandLineOption { + /// Constructs an empty QCommandLineOption object. + fn default() -> Self { + ffi::qcommandlineoption_init_default() + } +} + +// Safety: +// +// Static checks on the C++ side to ensure the size is the same. +unsafe impl ExternType for QCommandLineOption { + type Id = type_id!("QCommandLineOption"); + type Kind = cxx::kind::Trivial; +} diff --git a/tests/qt_types_standalone/CMakeLists.txt b/tests/qt_types_standalone/CMakeLists.txt index 91f1c95e0..4c23d0139 100644 --- a/tests/qt_types_standalone/CMakeLists.txt +++ b/tests/qt_types_standalone/CMakeLists.txt @@ -49,6 +49,7 @@ add_executable(${APP_NAME} cpp/main.cpp cpp/qbytearray.h cpp/qcolor.h + cpp/qcommandlineoption.h cpp/qcoreapplication.h cpp/qdate.h cpp/qdatetime.h diff --git a/tests/qt_types_standalone/cpp/qcommandlineoption.h b/tests/qt_types_standalone/cpp/qcommandlineoption.h new file mode 100644 index 000000000..674425293 --- /dev/null +++ b/tests/qt_types_standalone/cpp/qcommandlineoption.h @@ -0,0 +1,40 @@ +// clang-format off +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Laurent Montel +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +#pragma once + +#include +#include + +#include "cxx-qt-gen/qline_cxx.cxx.h" + +class QCommandLineOptionTest : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void construct() + { + const auto m = construct_qcommandlineoption(); + QCOMPARE(m.x1(), 1); + QCOMPARE(m.y1(), 2); + QCOMPARE(m.x2(), 3); + QCOMPARE(m.y2(), 4); + } + + void read() + { + const auto m = QCommandLineOption(QStringLiteral("foo")); + QVERIFY(read_qcommandlineoption(m)); + } + + void clone() + { + const auto m = QCommandLineOption(QStringLiteral("foo")); + const auto c = clone_qcommandlineoption(m); + QCOMPARE(c.names(), QStringList()); + } +}; From 9fd1f66b3b742f7b5e62c57dba42540fc65711f5 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Mon, 27 Nov 2023 16:56:41 +0100 Subject: [PATCH 2/8] Remove default constructor as QCommandLineOption doesn't have it --- crates/cxx-qt-lib/src/core/qcommandlineoption.rs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/crates/cxx-qt-lib/src/core/qcommandlineoption.rs b/crates/cxx-qt-lib/src/core/qcommandlineoption.rs index a63d5eb8f..d59c8bc19 100644 --- a/crates/cxx-qt-lib/src/core/qcommandlineoption.rs +++ b/crates/cxx-qt-lib/src/core/qcommandlineoption.rs @@ -31,13 +31,9 @@ mod ffi { unsafe extern "C++" { include!("cxx-qt-lib/common.h"); - #[doc(hidden)] - #[rust_name = "qcommandlineoption_init_default"] - fn construct() -> QCommandLineOption; - #[doc(hidden)] #[rust_name = "qcommandlineoption_init_from_qcommandlineoption"] - fn construct(url: &QCommandLineOption) -> QCommandLineOption; + fn construct(commandLineOption: &QCommandLineOption) -> QCommandLineOption; } } @@ -53,13 +49,6 @@ impl Clone for QCommandLineOption { } } -impl Default for QCommandLineOption { - /// Constructs an empty QCommandLineOption object. - fn default() -> Self { - ffi::qcommandlineoption_init_default() - } -} - // Safety: // // Static checks on the C++ side to ensure the size is the same. From 63db891023c152c7f91255bfbaf3eccaca5bc94a Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Tue, 28 Nov 2023 11:53:41 +0100 Subject: [PATCH 3/8] Add IsRelocatable --- .../include/core/qcommandlineoption.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/cxx-qt-lib-headers/include/core/qcommandlineoption.h b/crates/cxx-qt-lib-headers/include/core/qcommandlineoption.h index 0ba98910b..6c2476530 100644 --- a/crates/cxx-qt-lib-headers/include/core/qcommandlineoption.h +++ b/crates/cxx-qt-lib-headers/include/core/qcommandlineoption.h @@ -7,3 +7,16 @@ #pragma once #include + +#include "rust/cxx.h" + +// Define namespace otherwise we hit a GCC bug +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480 +namespace rust { + +template<> +struct IsRelocatable : ::std::true_type +{ +}; + +} // namespace rust From 5eb4f6792bec9c65038b207e390b39e7081d1897 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Tue, 28 Nov 2023 11:57:01 +0100 Subject: [PATCH 4/8] Remove test for the moment --- tests/qt_types_standalone/CMakeLists.txt | 1 - .../cpp/qcommandlineoption.h | 40 ------------------- 2 files changed, 41 deletions(-) delete mode 100644 tests/qt_types_standalone/cpp/qcommandlineoption.h diff --git a/tests/qt_types_standalone/CMakeLists.txt b/tests/qt_types_standalone/CMakeLists.txt index 4c23d0139..91f1c95e0 100644 --- a/tests/qt_types_standalone/CMakeLists.txt +++ b/tests/qt_types_standalone/CMakeLists.txt @@ -49,7 +49,6 @@ add_executable(${APP_NAME} cpp/main.cpp cpp/qbytearray.h cpp/qcolor.h - cpp/qcommandlineoption.h cpp/qcoreapplication.h cpp/qdate.h cpp/qdatetime.h diff --git a/tests/qt_types_standalone/cpp/qcommandlineoption.h b/tests/qt_types_standalone/cpp/qcommandlineoption.h deleted file mode 100644 index 674425293..000000000 --- a/tests/qt_types_standalone/cpp/qcommandlineoption.h +++ /dev/null @@ -1,40 +0,0 @@ -// clang-format off -// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company -// clang-format on -// SPDX-FileContributor: Laurent Montel -// -// SPDX-License-Identifier: MIT OR Apache-2.0 -#pragma once - -#include -#include - -#include "cxx-qt-gen/qline_cxx.cxx.h" - -class QCommandLineOptionTest : public QObject -{ - Q_OBJECT - -private Q_SLOTS: - void construct() - { - const auto m = construct_qcommandlineoption(); - QCOMPARE(m.x1(), 1); - QCOMPARE(m.y1(), 2); - QCOMPARE(m.x2(), 3); - QCOMPARE(m.y2(), 4); - } - - void read() - { - const auto m = QCommandLineOption(QStringLiteral("foo")); - QVERIFY(read_qcommandlineoption(m)); - } - - void clone() - { - const auto m = QCommandLineOption(QStringLiteral("foo")); - const auto c = clone_qcommandlineoption(m); - QCOMPARE(c.names(), QStringList()); - } -}; From eb07ff5592d37d462b7aa34210f2712833a18e2a Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Tue, 28 Nov 2023 12:10:32 +0100 Subject: [PATCH 5/8] Add missing methods --- .../cxx-qt-lib/src/core/qcommandlineoption.rs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/cxx-qt-lib/src/core/qcommandlineoption.rs b/crates/cxx-qt-lib/src/core/qcommandlineoption.rs index d59c8bc19..df12d2f4d 100644 --- a/crates/cxx-qt-lib/src/core/qcommandlineoption.rs +++ b/crates/cxx-qt-lib/src/core/qcommandlineoption.rs @@ -16,12 +16,33 @@ mod ffi { include!("cxx-qt-lib/qstringlist.h"); type QStringList = crate::QStringList; + /// Returns the default values set for this option. + #[rust_name = "default_values"] + fn defaultValues(self: &QCommandLineOption) -> QStringList; + /// Returns the description set for this option. fn description(self: &QCommandLineOption) -> QString; /// Returns the names set for this option. fn names(self: &QCommandLineOption) -> QStringList; + /// Sets the default value used for this option to defaultValue. + #[rust_name = "set_default_value"] + fn setDefaultValue(self: &mut QCommandLineOption, value: &QString); + + /// Sets the list of default values used for this option to defaultValues. + #[rust_name = "set_default_values"] + fn setDefaultValues(self: &mut QCommandLineOption, values: &QStringList); + + /// Sets the description used for this option to description. + /// It is customary to add a "." at the end of the description. + #[rust_name = "set_description"] + fn setDescription(self: &mut QCommandLineOption, description: &QString); + + /// Sets the name of the expected value, for the documentation, to valueName. + #[rust_name = "set_value_name"] + fn setValueName(self: &mut QCommandLineOption, valueName: &QString); + /// Returns the name of the expected value. #[rust_name = "value_name"] fn valueName(self: &QCommandLineOption) -> QString; From 2d8b1cb6d31e4295c98af6c7f0a6f023cf415aea Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Tue, 28 Nov 2023 12:31:16 +0100 Subject: [PATCH 6/8] Use clang-format --- crates/cxx-qt-lib/src/core/qcommandlineoption.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/cxx-qt-lib/src/core/qcommandlineoption.cpp b/crates/cxx-qt-lib/src/core/qcommandlineoption.cpp index 7bb4a8b52..c5e22f3e7 100644 --- a/crates/cxx-qt-lib/src/core/qcommandlineoption.cpp +++ b/crates/cxx-qt-lib/src/core/qcommandlineoption.cpp @@ -10,7 +10,6 @@ #include - // QCommandLineOption has 1 pointer // https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qcommandlineoption.h?h=v5.15.6-lts-lgpl#n59 // @@ -20,7 +19,8 @@ assert_alignment_and_size(QCommandLineOption, sizeof(::std::size_t)); static_assert(!::std::is_trivially_copy_assignable::value); -static_assert(!::std::is_trivially_copy_constructible::value); +static_assert( + !::std::is_trivially_copy_constructible::value); static_assert(!::std::is_trivially_destructible::value); From 2daf1cd948b9f1069cebac8bb38daa8b8235415e Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Tue, 28 Nov 2023 14:56:37 +0100 Subject: [PATCH 7/8] Add more constructor --- .../cxx-qt-lib/src/core/qcommandlineoption.rs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/cxx-qt-lib/src/core/qcommandlineoption.rs b/crates/cxx-qt-lib/src/core/qcommandlineoption.rs index df12d2f4d..a2ea8eaa4 100644 --- a/crates/cxx-qt-lib/src/core/qcommandlineoption.rs +++ b/crates/cxx-qt-lib/src/core/qcommandlineoption.rs @@ -55,6 +55,14 @@ mod ffi { #[doc(hidden)] #[rust_name = "qcommandlineoption_init_from_qcommandlineoption"] fn construct(commandLineOption: &QCommandLineOption) -> QCommandLineOption; + + #[doc(hidden)] + #[rust_name = "qcommandlineoption_init_from_qstring"] + fn construct(string: &QString) -> QCommandLineOption; + + #[doc(hidden)] + #[rust_name = "qcommandlineoption_init_from_qstringlist"] + fn construct(names: &QStringList) -> QCommandLineOption; } } @@ -70,6 +78,20 @@ impl Clone for QCommandLineOption { } } +impl From<&ffi::QString> for QCommandLineOption { + /// Constructs a command line option object with the name name. + fn from(name: &ffi::QString) -> Self { + ffi::qcommandlineoption_init_from_qstring(name) + } +} + +impl From<&ffi::QStringList> for QCommandLineOption { + /// Constructs a command line option object with the name name. + fn from(names: &ffi::QStringList) -> Self { + ffi::qcommandlineoption_init_from_qstringlist(names) + } +} + // Safety: // // Static checks on the C++ side to ensure the size is the same. From 97dd0bd7551b1485c39c06354ab9b101197ca748 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Wed, 29 Nov 2023 09:30:44 +0100 Subject: [PATCH 8/8] Implement drop --- crates/cxx-qt-lib/src/core/qcommandlineoption.cpp | 2 -- crates/cxx-qt-lib/src/core/qcommandlineoption.rs | 11 +++++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/cxx-qt-lib/src/core/qcommandlineoption.cpp b/crates/cxx-qt-lib/src/core/qcommandlineoption.cpp index c5e22f3e7..ddcc99efa 100644 --- a/crates/cxx-qt-lib/src/core/qcommandlineoption.cpp +++ b/crates/cxx-qt-lib/src/core/qcommandlineoption.cpp @@ -22,6 +22,4 @@ static_assert(!::std::is_trivially_copy_assignable::value); static_assert( !::std::is_trivially_copy_constructible::value); -static_assert(!::std::is_trivially_destructible::value); - static_assert(QTypeInfo::isRelocatable); diff --git a/crates/cxx-qt-lib/src/core/qcommandlineoption.rs b/crates/cxx-qt-lib/src/core/qcommandlineoption.rs index a2ea8eaa4..db5b355bb 100644 --- a/crates/cxx-qt-lib/src/core/qcommandlineoption.rs +++ b/crates/cxx-qt-lib/src/core/qcommandlineoption.rs @@ -52,6 +52,10 @@ mod ffi { unsafe extern "C++" { include!("cxx-qt-lib/common.h"); + #[doc(hidden)] + #[rust_name = "qcommandlineoption_drop"] + fn drop(option: &mut QCommandLineOption); + #[doc(hidden)] #[rust_name = "qcommandlineoption_init_from_qcommandlineoption"] fn construct(commandLineOption: &QCommandLineOption) -> QCommandLineOption; @@ -78,6 +82,13 @@ impl Clone for QCommandLineOption { } } +impl Drop for QCommandLineOption { + /// Destroys the qcommandlineoption. + fn drop(&mut self) { + ffi::qcommandlineoption_drop(self) + } +} + impl From<&ffi::QString> for QCommandLineOption { /// Constructs a command line option object with the name name. fn from(name: &ffi::QString) -> Self {