Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f25409e
cxx-qt: use qobject_cast for downcastPtr
jnbooth Mar 1, 2025
1f06a66
cxx-qt-lib: implement Upcast<QVector<QPoint>> for QPolygon
jnbooth Mar 1, 2025
155df23
cxx-qt-lib: implement Upcast<QVector<QPointF>> for QPolygonF
jnbooth Mar 1, 2025
b939a8d
cxx-qt-lib: implement Deref for QQmlApplicationEngine
jnbooth Mar 1, 2025
11fcba4
cxx-qt-lib: remove null-pointer dereference from QStringList's downcast
jnbooth Mar 1, 2025
8e829ef
cxx-qt-lib: implement Upcast<QCoreApplication> for QGuiApplication
jnbooth Mar 1, 2025
311ddd9
cxx-qt-lib: implement Upcast<QGuiApplication> for QApplication
jnbooth Mar 1, 2025
17dae93
Revert "cxx-qt-lib: implement Upcast<QGuiApplication> for QApplication"
jnbooth Mar 3, 2025
582a5d2
Revert "cxx-qt-lib: implement Upcast<QCoreApplication> for QGuiApplic…
jnbooth Mar 3, 2025
95a8a32
Revert "cxx-qt-lib: implement Deref for QQmlApplicationEngine"
jnbooth Mar 3, 2025
210cce8
cxx-qt: use qobject_cast for downcastPtr
jnbooth Mar 1, 2025
d51614a
cxx-qt-lib: implement Upcast<QVector<QPoint>> for QPolygon
jnbooth Mar 1, 2025
cc7fac9
cxx-qt-lib: implement Upcast<QVector<QPointF>> for QPolygonF
jnbooth Mar 1, 2025
36ff51b
cxx-qt-lib: remove null-pointer dereference from QStringList's downcast
jnbooth Mar 1, 2025
fdd65e9
Improve downcast C++ implementations
Mar 31, 2025
65e9e51
Merge remote-tracking branch 'origin/main' into non-qobject-casting
jnbooth Apr 1, 2025
5075d50
Merge remote-tracking branch 'LeonMatthesKDAB/non-qobject-casting' in…
jnbooth Apr 1, 2025
ee38624
Merge branch 'main' into non-qobject-casting
Apr 28, 2025
1baaae1
Merge branch 'main' into non-qobject-casting
May 19, 2025
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
37 changes: 36 additions & 1 deletion crates/cxx-qt-lib-extras/src/gui/qapplication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use core::pin::Pin;
use cxx_qt_lib::{QByteArray, QFont, QString, QStringList, QVector};
use cxx_qt::Upcast;
use cxx_qt_lib::{QByteArray, QFont, QGuiApplication, QString, QStringList, QVector};
use std::ops::Deref;

#[cxx::bridge]
mod ffi {
Expand All @@ -19,11 +21,26 @@ mod ffi {
type QVector_QByteArray = cxx_qt_lib::QVector<QByteArray>;
include!("cxx-qt-lib/qfont.h");
type QFont = cxx_qt_lib::QFont;
include!("cxx-qt-lib/qguiapplication.h");
type QGuiApplication = cxx_qt_lib::QGuiApplication;

include!("cxx-qt-lib-extras/qapplication.h");
type QApplication;
}

#[namespace = "rust::cxxqt1"]
unsafe extern "C++" {
include!("cxx-qt/casting.h");

#[doc(hidden)]
#[rust_name = "upcast_qapplication"]
unsafe fn upcastPtr(thiz: *const QApplication) -> *const QGuiApplication;

#[doc(hidden)]
#[rust_name = "downcast_qapplication"]
unsafe fn downcastPtr(base: *const QGuiApplication) -> *const QApplication;
}
Comment thread
jnbooth marked this conversation as resolved.
Outdated

#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
#[doc(hidden)]
Expand Down Expand Up @@ -93,6 +110,24 @@ mod ffi {

pub use ffi::QApplication;

impl Upcast<QGuiApplication> for QApplication {
unsafe fn upcast_ptr(this: *const Self) -> *const QGuiApplication {
ffi::upcast_qapplication(this)
}

unsafe fn from_base_ptr(base: *const QGuiApplication) -> *const Self {
ffi::downcast_qapplication(base)
}
}

impl Deref for QApplication {
Comment thread
jnbooth marked this conversation as resolved.
Outdated
type Target = QGuiApplication;

fn deref(&self) -> &Self::Target {
self.upcast()
}
}

impl QApplication {
/// Prepends path to the beginning of the library path list,
/// ensuring that it is searched for libraries first.
Expand Down
11 changes: 0 additions & 11 deletions crates/cxx-qt-lib/include/gui/qpolygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,3 @@ struct IsRelocatable<QPolygon> : ::std::true_type
{};

} // namespace rust

namespace rust {
namespace cxxqtlib1 {

const QVector<QPoint>&
qpolygonAsQVectorQPointRef(const QPolygon& shape);
QVector<QPoint>&
qpolygonAsQVectorQPointRef(QPolygon& shape);

}
}
11 changes: 0 additions & 11 deletions crates/cxx-qt-lib/include/gui/qpolygonf.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,3 @@ struct IsRelocatable<QPolygonF> : ::std::true_type
{};

} // namespace rust

namespace rust {
namespace cxxqtlib1 {

const QVector<QPointF>&
qpolygonfAsQVectorQPointFRef(const QPolygonF& shape);
QVector<QPointF>&
qpolygonfAsQVectorQPointFRef(QPolygonF& shape);

}
}
41 changes: 16 additions & 25 deletions crates/cxx-qt-lib/src/core/qstringlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
use crate::core::qstringlist::ffi::QList_QString;
use crate::{QList, QString};
use core::mem::MaybeUninit;
use cxx::{type_id, ExternType};
Expand All @@ -28,21 +27,6 @@ mod ffi {
include!("cxx-qt-lib/qstringlist.h");
type QStringList = super::QStringList;

include!("cxx-qt/casting.h");

#[doc(hidden)]
#[rust_name = "upcast_qstringlist"]
#[cxx_name = "upcastPtr"]
#[namespace = "rust::cxxqt1"]
unsafe fn upcast(thiz: *const QStringList) -> *const QList_QString;

#[doc(hidden)]
#[rust_name = "downcast_qlist_qstring"]
#[cxx_name = "downcastPtr"]
#[namespace = "rust::cxxqt1"]
#[cfg(cxxqt_qt_version_at_least_6)]
unsafe fn downcast(base: *const QList_QString) -> *const QStringList;

/// Returns true if the list contains the string str; otherwise returns false.
fn contains(self: &QStringList, str: &QString, cs: CaseSensitivity) -> bool;

Expand All @@ -67,6 +51,19 @@ mod ffi {
) -> &mut QStringList;
}

#[namespace = "rust::cxxqt1"]
unsafe extern "C++" {
include!("cxx-qt/casting.h");

#[doc(hidden)]
#[rust_name = "upcast_qstringlist"]
unsafe fn upcastPtr(thiz: *const QStringList) -> *const QList_QString;

#[doc(hidden)]
#[rust_name = "downcast_qlist_qstring"]
unsafe fn downcastPtrStatic(base: *const QList_QString) -> *const QStringList;
}

#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
include!("cxx-qt-lib/common.h");
Expand Down Expand Up @@ -207,20 +204,14 @@ impl DerefMut for QStringList {
}
}

impl Upcast<QList_QString> for QStringList {
unsafe fn upcast_ptr(this: *const Self) -> *const QList_QString {
impl Upcast<QList<QString>> for QStringList {
unsafe fn upcast_ptr(this: *const Self) -> *const QList<QString> {
ffi::upcast_qstringlist(this)
}

#[cfg(cxxqt_qt_version_at_least_6)]
unsafe fn from_base_ptr(base: *const QList_QString) -> *const Self {
unsafe fn from_base_ptr(base: *const QList<QString>) -> *const Self {
ffi::downcast_qlist_qstring(base)
}

#[cfg(cxxqt_qt_version_major = "5")]
unsafe fn from_base_ptr(_base: *const QList_QString) -> *const Self {
std::ptr::null()
}
}

// Safety:
Expand Down
35 changes: 20 additions & 15 deletions crates/cxx-qt-lib/src/gui/qguiapplication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::{QByteArray, QFont, QString, QStringList, QVector};
use crate::{QByteArray, QCoreApplication, QFont, QString, QStringList, QVector};
use core::pin::Pin;
use cxx_qt::Upcast;
use std::ops::Deref;

#[cxx::bridge]
mod ffi {
Expand All @@ -21,26 +22,24 @@ mod ffi {
type QVector_QByteArray = crate::QVector<QByteArray>;
include!("cxx-qt-lib/qfont.h");
type QFont = crate::QFont;
include!("cxx-qt-lib/qcoreapplication.h");
type QCoreApplication = crate::QCoreApplication;

include!("cxx-qt-lib/qguiapplication.h");
type QGuiApplication;
Comment thread
jnbooth marked this conversation as resolved.
Outdated
}

include!("cxx-qt-lib/qcoreapplication.h");
type QCoreApplication;

#[namespace = "rust::cxxqt1"]
unsafe extern "C++" {
include!("cxx-qt/casting.h");

#[doc(hidden)]
#[rust_name = "upcast_qguiapplication"]
#[cxx_name = "upcastPtr"]
#[namespace = "rust::cxxqt1"]
unsafe fn upcast(thiz: *const QGuiApplication) -> *const QCoreApplication;
unsafe fn upcastPtr(thiz: *const QGuiApplication) -> *const QCoreApplication;

#[doc(hidden)]
#[rust_name = "downcast_qcoreapplication"]
#[cxx_name = "downcastPtr"]
#[namespace = "rust::cxxqt1"]
unsafe fn downcast(base: *const QCoreApplication) -> *const QGuiApplication;
unsafe fn downcastPtr(base: *const QCoreApplication) -> *const QGuiApplication;
}

#[namespace = "rust::cxxqtlib1"]
Expand Down Expand Up @@ -116,17 +115,23 @@ mod ffi {
impl UniquePtr<QGuiApplication> {}
}

pub use ffi::{
downcast_qcoreapplication, upcast_qguiapplication, QCoreApplication, QGuiApplication,
};
pub use ffi::QGuiApplication;

impl Deref for QGuiApplication {
type Target = QCoreApplication;

fn deref(&self) -> &Self::Target {
self.upcast()
}
}

impl Upcast<QCoreApplication> for QGuiApplication {
unsafe fn upcast_ptr(this: *const Self) -> *const QCoreApplication {
upcast_qguiapplication(this)
ffi::upcast_qguiapplication(this)
}

unsafe fn from_base_ptr(base: *const QCoreApplication) -> *const Self {
downcast_qcoreapplication(base)
ffi::downcast_qcoreapplication(base)
}
}

Expand Down
17 changes: 0 additions & 17 deletions crates/cxx-qt-lib/src/gui/qpolygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,3 @@ static_assert(!::std::is_trivially_copy_constructible<QPolygon>::value);
static_assert(!::std::is_trivially_destructible<QPolygon>::value);

static_assert(QTypeInfo<QPolygon>::isRelocatable);

namespace rust {
namespace cxxqtlib1 {
const QVector<QPoint>&
qpolygonAsQVectorQPointRef(const QPolygon& shape)
{
return static_cast<const QVector<QPoint>&>(shape);
}

QVector<QPoint>&
qpolygonAsQVectorQPointRef(QPolygon& shape)
{
return static_cast<QVector<QPoint>&>(shape);
}

}
}
43 changes: 29 additions & 14 deletions crates/cxx-qt-lib/src/gui/qpolygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use crate::{QPoint, QRect, QVector};
use core::mem::MaybeUninit;
use cxx::{type_id, ExternType};
use cxx_qt::Upcast;
use std::fmt;
use std::ops::{Deref, DerefMut};

Expand All @@ -17,9 +18,6 @@ mod ffi {
}

unsafe extern "C++" {
include!("cxx-qt-lib/qvector.h");
type QVector_QPoint = crate::QVector<QPoint>;

include!("cxx-qt-lib/qpoint.h");
type QPoint = crate::QPoint;
include!("cxx-qt-lib/qrect.h");
Expand All @@ -30,6 +28,9 @@ mod ffi {
#[allow(dead_code)]
type QPolygonF = crate::QPolygonF;

include!("cxx-qt-lib/qvector.h");
type QVector_QPoint = crate::QVector<QPoint>;

include!("cxx-qt-lib/qpolygon.h");
type QPolygon = super::QPolygon;
Comment thread
jnbooth marked this conversation as resolved.

Expand Down Expand Up @@ -74,6 +75,19 @@ mod ffi {
fn united(self: &QPolygon, r: &QPolygon) -> QPolygon;
}

#[namespace = "rust::cxxqt1"]
unsafe extern "C++" {
include!("cxx-qt/casting.h");

#[doc(hidden)]
#[rust_name = "upcast_qpolygon"]
unsafe fn upcastPtr(thiz: *const QPolygon) -> *const QVector_QPoint;

#[doc(hidden)]
#[rust_name = "downcast_qvector_qpoint"]
unsafe fn downcastPtrStatic(base: *const QVector_QPoint) -> *const QPolygon;
}

#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
include!("cxx-qt-lib/common.h");
Expand Down Expand Up @@ -102,15 +116,6 @@ mod ffi {
#[rust_name = "qpolygon_to_debug_qstring"]
fn toDebugQString(value: &QPolygon) -> QString;
}

#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
#[doc(hidden)]
#[rust_name = "qpolygon_as_qvector_qpoint_ref"]
fn qpolygonAsQVectorQPointRef(shape: &QPolygon) -> &QVector_QPoint;
#[rust_name = "qpolygon_as_qvector_qpoint_ref_mut"]
fn qpolygonAsQVectorQPointRef(shape: &mut QPolygon) -> &mut QVector_QPoint;
}
}

/// The QPolygon class provides a list of QPoint.
Expand Down Expand Up @@ -178,13 +183,23 @@ impl Deref for QPolygon {
type Target = QVector<QPoint>;

fn deref(&self) -> &Self::Target {
ffi::qpolygon_as_qvector_qpoint_ref(self)
self.upcast()
}
}

impl DerefMut for QPolygon {
fn deref_mut(&mut self) -> &mut Self::Target {
ffi::qpolygon_as_qvector_qpoint_ref_mut(self)
self.upcast_mut()
}
}

impl Upcast<QVector<QPoint>> for QPolygon {
unsafe fn upcast_ptr(this: *const Self) -> *const QVector<QPoint> {
ffi::upcast_qpolygon(this)
}

unsafe fn from_base_ptr(base: *const QVector<QPoint>) -> *const Self {
ffi::downcast_qvector_qpoint(base)
}
}

Expand Down
17 changes: 0 additions & 17 deletions crates/cxx-qt-lib/src/gui/qpolygonf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,3 @@ static_assert(!::std::is_trivially_copy_constructible<QPolygonF>::value);
static_assert(!::std::is_trivially_destructible<QPolygonF>::value);

static_assert(QTypeInfo<QPolygonF>::isRelocatable);

namespace rust {
namespace cxxqtlib1 {
const QVector<QPointF>&
qpolygonfAsQVectorQPointFRef(const QPolygonF& shape)
{
return static_cast<const QVector<QPointF>&>(shape);
}

QVector<QPointF>&
qpolygonfAsQVectorQPointFRef(QPolygonF& shape)
{
return static_cast<QVector<QPointF>&>(shape);
}

}
}
Loading