forked from KDAB/cxx-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cxx-qt-lib: add support for QStringList
Related to KDAB#291
- Loading branch information
1 parent
4b100b5
commit 45f0cf5
Showing
13 changed files
with
313 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#pragma once | ||
|
||
#include <QtCore/QList> | ||
#include <QtCore/QString> | ||
#include <QtCore/QStringList> | ||
|
||
#include "rust/cxx.h" | ||
|
||
template<> | ||
struct rust::IsRelocatable<QStringList> : ::std::true_type | ||
{ | ||
}; | ||
|
||
namespace rust { | ||
namespace cxxqtlib1 { | ||
|
||
bool | ||
qstringlistContains(const QStringList& list, const QString& string); | ||
QStringList | ||
qstringlistFromQListQString(const QList<QString>& list); | ||
QList<QString> | ||
qstringlistAsQListQString(const QStringList& list); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#include "cxx-qt-lib/qstringlist.h" | ||
|
||
#include "assertion_utils.h" | ||
|
||
// The layout has changed between Qt 5 and Qt 6 | ||
// | ||
// Qt5 QStringList has one pointer as a member | ||
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qlist.h?h=v5.15.6-lts-lgpl#n157 | ||
// | ||
// Qt6 QStringList has one member, which contains two pointers and a size_t | ||
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qlist.h?h=v6.2.4#n110 | ||
// DataPointer is then a QArrayDataPointer<QString> | ||
// https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/tools/qarraydatapointer.h?h=v6.2.4#n390 | ||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) | ||
assert_alignment_and_size(QStringList, | ||
alignof(::std::size_t), | ||
sizeof(::std::size_t[3])); | ||
#else | ||
assert_alignment_and_size(QStringList, | ||
alignof(::std::size_t), | ||
sizeof(::std::size_t)); | ||
#endif | ||
|
||
static_assert(!::std::is_trivially_copy_assignable<QStringList>::value); | ||
static_assert(!::std::is_trivially_copy_constructible<QStringList>::value); | ||
static_assert(!::std::is_trivially_destructible<QStringList>::value); | ||
|
||
static_assert(QTypeInfo<QStringList>::isRelocatable); | ||
|
||
namespace rust { | ||
namespace cxxqtlib1 { | ||
|
||
bool | ||
qstringlistContains(const QStringList& list, const QString& string) | ||
{ | ||
return list.contains(string); | ||
} | ||
|
||
QStringList | ||
qstringlistFromQListQString(const QList<QString>& list) | ||
{ | ||
return QStringList(list); | ||
} | ||
|
||
QList<QString> | ||
qstringlistAsQListQString(const QStringList& list) | ||
{ | ||
// Cast to a QList then copy it | ||
const auto list_cast = static_cast<QList<QString>>(list); | ||
return QList<QString>(list_cast); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
use crate::{QList, QString}; | ||
use core::mem::MaybeUninit; | ||
use cxx::{type_id, ExternType}; | ||
|
||
#[cxx::bridge] | ||
mod ffi { | ||
unsafe extern "C++" { | ||
include!("cxx-qt-lib/qstring.h"); | ||
type QString = crate::QString; | ||
|
||
include!("cxx-qt-lib/qlist.h"); | ||
type QList_QString = crate::QList<QString>; | ||
|
||
include!("cxx-qt-lib/qstringlist.h"); | ||
type QStringList = super::QStringList; | ||
|
||
/// Joins all the string list's strings into a single string with each element | ||
/// separated by the given separator (which can be an empty string). | ||
fn join(self: &QStringList, separator: &QString) -> QString; | ||
} | ||
|
||
#[namespace = "rust::cxxqtlib1"] | ||
unsafe extern "C++" { | ||
include!("cxx-qt-lib/common.h"); | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "qstringlist_clone"] | ||
fn construct(list: &QStringList) -> QStringList; | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "qstringlist_drop"] | ||
fn drop(url: &mut QStringList); | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "qstringlist_default"] | ||
fn construct() -> QStringList; | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "qstringlist_from_qstring"] | ||
fn construct(string: &QString) -> QStringList; | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "qstringlist_from_qlist_qstring"] | ||
fn qstringlistFromQListQString(list: &QList_QString) -> QStringList; | ||
#[doc(hidden)] | ||
#[rust_name = "qstringlist_as_qlist_qstring"] | ||
fn qstringlistAsQListQString(list: &QStringList) -> QList_QString; | ||
|
||
#[doc(hidden)] | ||
#[rust_name = "qstringlist_contains"] | ||
fn qstringlistContains(list: &QStringList, string: &QString) -> bool; | ||
} | ||
} | ||
|
||
/// The QStringList class provides a list of strings. | ||
#[repr(C)] | ||
pub struct QStringList { | ||
/// The layout has changed between Qt 5 and Qt 6 | ||
/// | ||
/// Qt5 QStringList has one pointer as a member | ||
/// Qt6 QStringList has one member, which contains two pointers and a size_t | ||
#[cfg(qt_version_major = "5")] | ||
_space: MaybeUninit<usize>, | ||
#[cfg(qt_version_major = "6")] | ||
_space: MaybeUninit<[usize; 3]>, | ||
} | ||
|
||
impl QStringList { | ||
/// Returns true if the list contains the string str; otherwise returns false | ||
pub fn contains(&self, string: &QString) -> bool { | ||
ffi::qstringlist_contains(self, string) | ||
} | ||
} | ||
|
||
impl Clone for QStringList { | ||
/// Constructs a copy of other. | ||
fn clone(&self) -> Self { | ||
ffi::qstringlist_clone(self) | ||
} | ||
} | ||
|
||
impl Default for QStringList { | ||
/// Constructs an empty list. | ||
fn default() -> Self { | ||
ffi::qstringlist_default() | ||
} | ||
} | ||
|
||
impl Drop for QStringList { | ||
/// Destroys the list. | ||
fn drop(&mut self) { | ||
ffi::qstringlist_drop(self); | ||
} | ||
} | ||
|
||
impl From<&QString> for QStringList { | ||
/// Constructs a string list that contains the given string | ||
fn from(string: &QString) -> Self { | ||
ffi::qstringlist_from_qstring(string) | ||
} | ||
} | ||
|
||
impl From<&QList<QString>> for QStringList { | ||
/// Converts a QList<QString> into QStringList. | ||
fn from(list: &QList<QString>) -> Self { | ||
ffi::qstringlist_from_qlist_qstring(list) | ||
} | ||
} | ||
|
||
impl From<&QStringList> for QList<QString> { | ||
/// Converts a QStringList into a QList<QString> | ||
fn from(list: &QStringList) -> Self { | ||
ffi::qstringlist_as_qlist_qstring(list) | ||
} | ||
} | ||
|
||
// Safety: | ||
// | ||
// Static checks on the C++ side to ensure the size is the same. | ||
unsafe impl ExternType for QStringList { | ||
type Id = type_id!("QStringList"); | ||
type Kind = cxx::kind::Trivial; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// clang-format off | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// clang-format on | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
#pragma once | ||
|
||
#include <QtCore/QStringList> | ||
#include <QtTest/QTest> | ||
|
||
#include "cxx-qt-gen/qstringlist_cxx.cxx.h" | ||
|
||
class QStringListTest : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
private Q_SLOTS: | ||
void construct() | ||
{ | ||
const auto l = | ||
construct_qstringlist(QStringLiteral("https://kdab.com/"), | ||
QStringLiteral("https://github.com/KDAB/cxx-qt/")); | ||
QVERIFY(l.contains(QStringLiteral("https://github.com/KDAB/cxx-qt/"))); | ||
QCOMPARE(l.size(), 2); | ||
} | ||
|
||
void read() | ||
{ | ||
const auto l = QStringList() | ||
<< QStringLiteral("https://kdab.com/") | ||
<< QStringLiteral("https://github.com/KDAB/cxx-qt/"); | ||
QVERIFY(read_qstringlist(l)); | ||
} | ||
|
||
void clone() | ||
{ | ||
const auto l = QStringList() | ||
<< QStringLiteral("https://kdab.com/") | ||
<< QStringLiteral("https://github.com/KDAB/cxx-qt/"); | ||
const auto c = clone_qstringlist(l); | ||
QVERIFY(l.contains(QStringLiteral("https://github.com/KDAB/cxx-qt/"))); | ||
QCOMPARE(l.size(), 2); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ mod qset; | |
mod qsize; | ||
mod qsizef; | ||
mod qstring; | ||
mod qstringlist; | ||
mod qtime; | ||
mod qurl; | ||
mod qvariant; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
// SPDX-FileContributor: Andrew Hayzen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
use cxx_qt_lib::{QList, QString, QStringList}; | ||
|
||
#[cxx::bridge] | ||
mod qstringlist_cxx { | ||
unsafe extern "C++" { | ||
include!("cxx-qt-lib/qstring.h"); | ||
type QString = cxx_qt_lib::QString; | ||
include!("cxx-qt-lib/qstringlist.h"); | ||
type QStringList = cxx_qt_lib::QStringList; | ||
} | ||
|
||
extern "Rust" { | ||
fn construct_qstringlist(a: &QString, b: &QString) -> QStringList; | ||
fn read_qstringlist(l: &QStringList) -> bool; | ||
fn clone_qstringlist(l: &QStringList) -> QStringList; | ||
} | ||
} | ||
|
||
fn construct_qstringlist(a: &QString, b: &QString) -> QStringList { | ||
let mut list = QList::<QString>::default(); | ||
list.append_clone(a); | ||
list.append_clone(b); | ||
QStringList::from(&list) | ||
} | ||
|
||
fn read_qstringlist(l: &QStringList) -> bool { | ||
let qlist = QList::<QString>::from(l); | ||
l.contains(&QString::from("https://kdab.com/")) && qlist.len() == 2 | ||
} | ||
|
||
fn clone_qstringlist(l: &QStringList) -> QStringList { | ||
l.clone() | ||
} |