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 qcommandlineoption support #759

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 22 additions & 0 deletions crates/cxx-qt-lib-headers/include/core/qcommandlineoption.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// clang-format off
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// clang-format on
// SPDX-FileContributor: Laurent Montel <laurent.montel@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
#pragma once

#include <QtCore/QCommandLineOption>

#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<QCommandLineOption> : ::std::true_type
{
};

} // namespace rust
4 changes: 4 additions & 0 deletions crates/cxx-qt-lib-headers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub fn write_headers(directory: impl AsRef<Path>) {
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",
Expand Down
2 changes: 2 additions & 0 deletions crates/cxx-qt-lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn main() {

let mut rust_bridges = vec![
"core/qbytearray",
"core/qcommandlineoption",
"core/qcoreapplication",
"core/qdate",
"core/qhash/qhash_i32_qbytearray",
Expand Down Expand Up @@ -199,6 +200,7 @@ fn main() {

let mut cpp_files = vec![
"core/qbytearray",
"core/qcommandlineoption",
"core/qcoreapplication",
"core/qdate",
"core/qhash/qhash",
Expand Down
3 changes: 3 additions & 0 deletions crates/cxx-qt-lib/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
mod qbytearray;
pub use qbytearray::QByteArray;

mod qcommandlineoption;
pub use qcommandlineoption::QCommandLineOption;

mod qcoreapplication;
pub use qcoreapplication::QCoreApplication;

Expand Down
25 changes: 25 additions & 0 deletions crates/cxx-qt-lib/src/core/qcommandlineoption.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// clang-format off
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// clang-format on
// SPDX-FileContributor: Laurent Montel <laurent.montel@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
#include "cxx-qt-lib/qcommandlineoption.h"

#include "../assertion_utils.h"

#include <cstdint>

// 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<QCommandLineOption>::value);
static_assert(
!::std::is_trivially_copy_constructible<QCommandLineOption>::value);

static_assert(QTypeInfo<QCommandLineOption>::isRelocatable);
112 changes: 112 additions & 0 deletions crates/cxx-qt-lib/src/core/qcommandlineoption.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Laurent Montel <laurent.montel@kdab.com>
//
// 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 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;
}

#[namespace = "rust::cxxqtlib1"]
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;

#[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;
}
}

#[repr(C)]
pub struct QCommandLineOption {
_space: MaybeUninit<usize>,
}

impl Clone for QCommandLineOption {
/// Constructs a copy of other.
fn clone(&self) -> Self {
ffi::qcommandlineoption_init_from_qcommandlineoption(self)
}
}

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 {
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.
unsafe impl ExternType for QCommandLineOption {
type Id = type_id!("QCommandLineOption");
type Kind = cxx::kind::Trivial;
}
Loading