QJniArray: add deduction guide for direct construction from container
That way, QList list{1, 2, 3}; QJniArray array(list); works. Change-Id: If04f8115c51fce533cb4287bf36841ff0daeb11b Reviewed-by: Marc Mutz <marc.mutz@qt.io> (cherry picked from commit f199d4f78d528f7a70170d2469ae4807e4a5e765) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
4ef415825c
commit
e4c386b0b7
@ -363,6 +363,13 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
// Deduction guide so that we can construct as 'QJniArray list(Container<T>)'. Since
|
||||
// fromContainer() maps several C++ types to the same JNI type (e.g. both jboolean and
|
||||
// bool become QJniArray<jboolean>), we have to deduce to what fromContainer() would
|
||||
// give us.
|
||||
template <typename Container, QJniArrayBase::if_contiguous_container<Container> = true>
|
||||
QJniArray(Container) -> QJniArray<typename decltype(QJniArrayBase::fromContainer(std::declval<Container>()))::value_type>;
|
||||
|
||||
template <typename ElementType, typename List, typename NewFn, typename SetFn>
|
||||
auto QJniArrayBase::makeArray(List &&list, NewFn &&newArray, SetFn &&setRegion)
|
||||
{
|
||||
|
@ -81,6 +81,7 @@ VERIFY_RETURN_FOR_TYPE(QJniArray<List>, QJniArray<List>);
|
||||
|
||||
void tst_QJniArray::construct()
|
||||
{
|
||||
// explicit
|
||||
{
|
||||
QStringList strings;
|
||||
for (int i = 0; i < 10000; ++i)
|
||||
@ -89,13 +90,36 @@ void tst_QJniArray::construct()
|
||||
QCOMPARE(list.size(), 10000);
|
||||
}
|
||||
{
|
||||
QJniArray<jint> list{1, 2, 3};
|
||||
QJniArray bytes = QJniArrayBase::fromContainer(QByteArray("abc"));
|
||||
static_assert(std::is_same_v<decltype(bytes)::value_type, jbyte>);
|
||||
QCOMPARE(bytes.size(), 3);
|
||||
}
|
||||
{
|
||||
QJniArray list{1, 2, 3};
|
||||
static_assert(std::is_same_v<decltype(list), QJniArray<int>>);
|
||||
QCOMPARE(list.size(), 3);
|
||||
list = {4, 5};
|
||||
QCOMPARE(list.size(), 2);
|
||||
}
|
||||
{
|
||||
QJniArray<jint> list(QList<int>{1, 2, 3});
|
||||
QCOMPARE(list.size(), 3);
|
||||
}
|
||||
// CTAD with deduction guide
|
||||
{
|
||||
QJniArray list(QList<int>{1, 2, 3});
|
||||
QCOMPARE(list.size(), 3);
|
||||
}
|
||||
{
|
||||
QJniArray bytes(QByteArray("abc"));
|
||||
static_assert(std::is_same_v<decltype(bytes)::value_type, jbyte>);
|
||||
QCOMPARE(bytes.size(), 3);
|
||||
}
|
||||
{
|
||||
QStringList strings{"a", "b", "c"};
|
||||
QJniArray list(strings);
|
||||
QCOMPARE(list.size(), 3);
|
||||
}
|
||||
{
|
||||
QJniArray<jint> list{QList<int>{1, 2, 3}};
|
||||
QCOMPARE(list.size(), 3);
|
||||
@ -110,7 +134,7 @@ void tst_QJniArray::size()
|
||||
|
||||
QList<int> intList;
|
||||
intList.resize(10);
|
||||
auto intArray = QJniArrayBase::fromContainer(intList);
|
||||
auto intArray = QJniArray(intList);
|
||||
QCOMPARE(intArray.size(), 10);
|
||||
}
|
||||
|
||||
|
@ -1627,7 +1627,7 @@ void tst_QJniObject::templateApiCheck()
|
||||
QCOMPARE(array.size(), 3);
|
||||
QCOMPARE(array.toContainer(), "abc");
|
||||
|
||||
QJniArray<jbyte> newArray = QJniArrayBase::fromContainer(QByteArray{"cba"});
|
||||
QJniArray newArray = QJniArray(QByteArray{"cba"});
|
||||
QVERIFY(newArray.isValid());
|
||||
const auto reverse = testClass.callMethod<jbyte[]>("reverseByteArray", newArray);
|
||||
QVERIFY(reverse.isValid());
|
||||
|
Loading…
x
Reference in New Issue
Block a user