summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2024-01-22 13:45:14 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2024-01-29 16:06:47 +0100
commit8f04615bcddbe4d8d00f2f7e977e13f93f90fddb (patch)
tree18b9036865dc38bdb6ec590bae5bde426bc751c7 /src/corelib/kernel
parent0db7d7e54c2399739d1e44a88a5d21997bade08b (diff)
QJniArray: add missing typedefs
Add missing nested typedefs for both the QJniArray container and the QJniArrayIterator. Expand test case to make sure that some standard algorithms (such as std::distance and ranged for) work with those types. Found during header review. Pick-to: 6.7 Task-number: QTBUG-119952 Change-Id: I96f348215c6f1e0e1ce777d9bdd2f172d7e52974 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r--src/corelib/kernel/qjniarray.h29
1 files changed, 22 insertions, 7 deletions
diff --git a/src/corelib/kernel/qjniarray.h b/src/corelib/kernel/qjniarray.h
index 66688c393d..0c8dcc395d 100644
--- a/src/corelib/kernel/qjniarray.h
+++ b/src/corelib/kernel/qjniarray.h
@@ -26,6 +26,13 @@ struct QJniArrayIterator
constexpr QJniArrayIterator &operator=(const QJniArrayIterator &other) noexcept = default;
constexpr QJniArrayIterator &operator=(QJniArrayIterator &&other) noexcept = default;
+ using difference_type = jsize;
+ using value_type = T;
+ using pointer = T *;
+ using reference = T; // difference to container requirements
+ using const_reference = reference;
+ using iterator_category = std::forward_iterator_tag;
+
friend bool operator==(const QJniArrayIterator &lhs, const QJniArrayIterator &rhs) noexcept
{
return lhs.m_array == rhs.m_array && lhs.m_index == rhs.m_index;
@@ -34,7 +41,7 @@ struct QJniArrayIterator
{
return !(lhs == rhs);
}
- T operator*() const
+ const_reference operator*() const
{
return m_array->at(m_index);
}
@@ -73,8 +80,10 @@ class QJniArrayBase : public QJniObject
> : std::true_type {};
public:
+ using size_type = jsize;
+ using difference_type = size_type;
- qsizetype size() const
+ size_type size() const
{
if (jarray array = object<jarray>())
return jniEnv()->GetArrayLength(array);
@@ -156,6 +165,12 @@ class QJniArray : public QJniArrayBase
friend struct QJniArrayIterator<T>;
public:
using Type = T;
+
+ using value_type = T;
+ using reference = T;
+ using const_reference = const reference;
+
+ // read-only container, so no iterator typedef
using const_iterator = QJniArrayIterator<const T>;
QJniArray() = default;
@@ -212,8 +227,8 @@ public:
const_iterator constEnd() const { return {end()}; }
const_iterator cend() const { return {end()}; }
- T operator[](qsizetype i) const { return at(i); }
- T at(qsizetype i) const
+ const_reference operator[](size_type i) const { return at(i); }
+ const_reference at(size_type i) const
{
JNIEnv *env = jniEnv();
if constexpr (std::is_convertible_v<jobject, T>) {
@@ -295,7 +310,7 @@ public:
template <typename ElementType, typename List, typename NewFn, typename SetFn>
auto QJniArrayBase::makeArray(List &&list, NewFn &&newArray, SetFn &&setRegion)
{
- const int length = int(list.size());
+ const size_type length = size_type(list.size());
JNIEnv *env = QJniEnvironment::getJniEnv();
auto localArray = (env->*newArray)(length);
if (QJniEnvironment::checkAndClearExceptions(env))
@@ -317,7 +332,7 @@ auto QJniArrayBase::makeObjectArray(List &&list)
return QJniArray<jobject>();
JNIEnv *env = QJniEnvironment::getJniEnv();
- const int length = int(list.size());
+ const size_type length = size_type(list.size());
// this assumes that all objects in the list have the same class
jclass elementClass = nullptr;
@@ -328,7 +343,7 @@ auto QJniArrayBase::makeObjectArray(List &&list)
auto localArray = env->NewObjectArray(length, elementClass, nullptr);
if (QJniEnvironment::checkAndClearExceptions(env))
return QJniArray<jobject>();
- for (int i = 0; i < length; ++i) {
+ for (size_type i = 0; i < length; ++i) {
jobject object;
if constexpr (std::is_same_v<ElementType, QJniObject>)
object = list.at(i).object();