Commit 6e0b848a authored by Volker Krause's avatar Volker Krause
Browse files

Let JNI method wrappers return Jni::Array<T> for arrays directly

This allows user code to decide between conversion to a C++ container, or
iterating over the JNI array without needing a full copy.
parent 07f3a069
......@@ -119,11 +119,11 @@ public:
typedef jsize difference_type;
ArrayImplBase() = default;
ArrayImplBase(const QAndroidJniObject &array) : m_array(array) {}
inline ArrayImplBase(const QAndroidJniObject &array) : m_array(array) {}
ArrayImplBase(const ArrayImplBase &) = default;
ArrayImplBase(ArrayImplBase &&) = default;
size_type size() const
inline size_type size() const
{
if (!m_array.isValid()) {
return 0;
......@@ -133,6 +133,10 @@ public:
return env->GetArrayLength(a);
}
inline operator QAndroidJniObject() const {
return m_array;
}
protected:
typedef array_trait<T> _t;
......@@ -233,13 +237,6 @@ public:
namespace Jni {
/** Container-like wrapper for JNI arrays. */
template <typename T>
class Array : public Internal::ArrayImpl<T, Jni::is_basic_type<T>::value> {
public:
using Internal::ArrayImpl<T, Jni::is_basic_type<T>::value>::ArrayImpl;
};
/** Convert a JNI array to a C++ container.
* Container value types can be any of
* - QAndroidJniObject
......@@ -247,6 +244,18 @@ public:
* - a type with a conversion defined with @c JNI_DECLARE_CONVERTER
*/
template <typename Container> constexpr __attribute__((__unused__)) Internal::FromArray<Container, typename Container::value_type, Jni::is_basic_type<typename Container::value_type>::value> fromArray = {};
/** Container-like wrapper for JNI arrays. */
template <typename T>
class Array : public Internal::ArrayImpl<T, Jni::is_basic_type<T>::value> {
public:
using Internal::ArrayImpl<T, Jni::is_basic_type<T>::value>::ArrayImpl;
template <typename Container>
inline operator Container() const {
// ### should this be re-implemented in terms of Jni::Array API rather than direct JNI access?
return Jni::fromArray<Container>(this->m_array);
}
};
}
}
......
......@@ -96,27 +96,11 @@ namespace Internal {
QAndroidJniObject value;
};
template <typename RetT>
class ReturnValue<Jni::Array<RetT>> {
public:
explicit inline ReturnValue(const QAndroidJniObject &v) : value(v) {}
inline operator QAndroidJniObject() const {
return value;
}
template <typename Container>
inline operator Container() const {
return Jni::fromArray<Container>(value);
}
private:
QAndroidJniObject value;
};
// return type conversion
template <typename RetT>
struct call_return {
static inline constexpr bool is_basic = Jni::is_basic_type<RetT>::value;
static inline constexpr bool is_convertible = !std::is_same_v<typename Jni::converter<RetT>::type, void> || Jni::is_array<RetT>::value;
static inline constexpr bool is_convertible = !std::is_same_v<typename Jni::converter<RetT>::type, void>;
typedef std::conditional_t<is_basic, RetT, QAndroidJniObject> JniReturnT;
typedef std::conditional_t<is_basic || !is_convertible, JniReturnT, ReturnValue<RetT>> CallReturnT;
......@@ -130,6 +114,14 @@ namespace Internal {
}
}
};
template <typename RetT>
struct call_return<Jni::Array<RetT>> {
typedef Jni::Array<RetT> CallReturnT;
static inline constexpr CallReturnT toReturnValue(const QAndroidJniObject &value)
{
return CallReturnT(value);
}
};
template <>
struct call_return<void> {
typedef void CallReturnT;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment