diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bc1058a8..731d0b0e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/KDAB/cxx-qt/compare/v0.7.0...HEAD) +### Added + + - Support for further types: `qreal`, `qint64`, `qintptr`, `qsizetype`, `quint64`, `quintptr` + ## [0.7.0](https://github.com/KDAB/cxx-qt/compare/v0.6.1...v0.7.0) - 2024-10-30 ### Added diff --git a/crates/cxx-qt-lib/build.rs b/crates/cxx-qt-lib/build.rs index bf4132961..9f1e76ab2 100644 --- a/crates/cxx-qt-lib/build.rs +++ b/crates/cxx-qt-lib/build.rs @@ -147,6 +147,7 @@ fn main() { "core/qstringlist", "core/qt", "core/qtime", + "core/qtypes", "core/qurl", "core/qvariant/mod", "core/qvariant/qvariant_bool", @@ -271,6 +272,7 @@ fn main() { "core/qstring", "core/qstringlist", "core/qtime", + "core/qtypes", "core/qurl", "core/qvariant/qvariant", "core/qvector/qvector", diff --git a/crates/cxx-qt-lib/include/core/qtypes.h b/crates/cxx-qt-lib/include/core/qtypes.h new file mode 100644 index 000000000..16239f4e6 --- /dev/null +++ b/crates/cxx-qt-lib/include/core/qtypes.h @@ -0,0 +1,9 @@ +// clang-format off +// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +#pragma once + +#include diff --git a/crates/cxx-qt-lib/src/core/mod.rs b/crates/cxx-qt-lib/src/core/mod.rs index a2e9bfa33..123b9bbda 100644 --- a/crates/cxx-qt-lib/src/core/mod.rs +++ b/crates/cxx-qt-lib/src/core/mod.rs @@ -83,6 +83,9 @@ pub use qt::{ mod qtime; pub use qtime::QTime; +mod qtypes; +pub use qtypes::{qint64, qintptr, qreal, qsizetype, quint64, quintptr}; + #[cfg(not(target_os = "emscripten"))] mod qtimezone; #[cfg(not(target_os = "emscripten"))] diff --git a/crates/cxx-qt-lib/src/core/qtypes.cpp b/crates/cxx-qt-lib/src/core/qtypes.cpp new file mode 100644 index 000000000..883a0150b --- /dev/null +++ b/crates/cxx-qt-lib/src/core/qtypes.cpp @@ -0,0 +1,19 @@ +// clang-format off +// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +#include "cxx-qt-lib/qtypes.h" + +#include + +#include "cxx-qt-lib/assertion_utils.h" + +assert_alignment_and_size(qint64, { ::std::int64_t a0; }); +assert_alignment_and_size(qintptr, { ::std::intptr_t a0; }); +assert_alignment_and_size(quint64, { ::std::uint64_t a0; }); +assert_alignment_and_size(quintptr, { ::std::uintptr_t a0; }); +assert_alignment_and_size(qsizetype, { ::std::size_t a0; }); +// We only support qreal being a double +assert_alignment_and_size(qreal, { double a0; }); diff --git a/crates/cxx-qt-lib/src/core/qtypes.rs b/crates/cxx-qt-lib/src/core/qtypes.rs new file mode 100644 index 000000000..3da130aba --- /dev/null +++ b/crates/cxx-qt-lib/src/core/qtypes.rs @@ -0,0 +1,173 @@ +// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company +// SPDX-FileContributor: Andrew Hayzen +// +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use cxx::{type_id, ExternType}; + +#[cxx::bridge] +mod ffi { + unsafe extern "C++" { + include!("cxx-qt-lib/qtypes.h"); + } +} + +/// Typedef for long long int. This type is guaranteed to be 64-bit on all platforms supported by Qt. +#[repr(transparent)] +#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[allow(non_camel_case_types)] +pub struct qint64(i64); + +impl From for qint64 { + fn from(value: i64) -> Self { + Self(value) + } +} + +impl From for i64 { + fn from(value: qint64) -> Self { + value.0 + } +} + +// Safety: +// +// Static checks on the C++ side to ensure the size is the same. +unsafe impl ExternType for qint64 { + type Id = type_id!("qint64"); + type Kind = cxx::kind::Trivial; +} + +/// Integral type for representing pointers in a signed integer (useful for hashing, etc.). +#[repr(transparent)] +#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[allow(non_camel_case_types)] +pub struct qintptr(isize); + +impl From for qintptr { + fn from(value: isize) -> Self { + qintptr(value) + } +} + +impl From for isize { + fn from(value: qintptr) -> Self { + value.0 + } +} + +// Safety: +// +// Static checks on the C++ side to ensure the size is the same. +unsafe impl ExternType for qintptr { + type Id = type_id!("qintptr"); + type Kind = cxx::kind::Trivial; +} + +/// Typedef for double +/// +/// Note that configuring Qt with -qreal float is not supported +#[repr(transparent)] +#[derive(Clone, Copy, Default, Debug, PartialEq, PartialOrd)] +#[allow(non_camel_case_types)] +pub struct qreal(f64); + +impl From for qreal { + fn from(value: f64) -> Self { + qreal(value) + } +} + +impl From for f64 { + fn from(value: qreal) -> Self { + value.0 + } +} + +// Safety: +// +// Static checks on the C++ side to ensure the size is the same. +unsafe impl ExternType for qreal { + type Id = type_id!("qreal"); + type Kind = cxx::kind::Trivial; +} + +/// Typedef for unsigned long long int. This type is guaranteed to be 64-bit on all platforms supported by Qt. +#[repr(transparent)] +#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[allow(non_camel_case_types)] +pub struct quint64(u64); + +impl From for quint64 { + fn from(value: u64) -> Self { + quint64(value) + } +} + +impl From for u64 { + fn from(value: quint64) -> Self { + value.0 + } +} + +// Safety: +// +// Static checks on the C++ side to ensure the size is the same. +unsafe impl ExternType for quint64 { + type Id = type_id!("quint64"); + type Kind = cxx::kind::Trivial; +} + +/// Integral type for representing pointers in an unsigned integer (useful for hashing, etc.). +#[repr(transparent)] +#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[allow(non_camel_case_types)] +pub struct quintptr(usize); + +impl From for quintptr { + fn from(value: usize) -> Self { + quintptr(value) + } +} + +impl From for usize { + fn from(value: quintptr) -> Self { + value.0 + } +} + +// Safety: +// +// Static checks on the C++ side to ensure the size is the same. +unsafe impl ExternType for quintptr { + type Id = type_id!("quintptr"); + type Kind = cxx::kind::Trivial; +} + +/// Integral type providing Posix' ssize_t for all platforms. +/// +/// This type is guaranteed to be the same size as a size_t on all platforms supported by Qt. +#[derive(Clone, Copy, Default, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[repr(transparent)] +#[allow(non_camel_case_types)] +pub struct qsizetype(isize); + +impl From for qsizetype { + fn from(value: isize) -> Self { + qsizetype(value) + } +} + +impl From for isize { + fn from(value: qsizetype) -> Self { + value.0 + } +} + +// Safety: +// +// Static checks on the C++ side to ensure the size is the same. +unsafe impl ExternType for qsizetype { + type Id = type_id!("qsizetype"); + type Kind = cxx::kind::Trivial; +}