Loading...
Searching...
No Matches
array.hpp
Go to the documentation of this file.
#pragma once
#include <jni/functions.hpp>
#include <jni/object.hpp>
#include <jni/tagging.hpp>
#include <jni/make.hpp>
namespace jni
{
template < class E, class Enable >
class Array;
template < class E >
class Array< E, std::enable_if_t<IsPrimitive<E>::value> > : public Object< ArrayTag<E> >
{
public:
using SuperType = Object< ArrayTag<E> >;
using UntaggedType = typename SuperType::UntaggedType;
using ElementType = E;
protected:
explicit Array(std::nullptr_t = nullptr)
{}
explicit Array(UntaggedType* p)
: SuperType(p)
{}
Array(const Array&) = delete;
Array& operator=(const Array&) = delete;
public:
jsize Length(JNIEnv& env) const
{
return GetArrayLength(env, SafeDereference(env, this->get()));
}
ElementType Get(JNIEnv& env, jsize index) const
{
ElementType e;
GetArrayRegion(env, SafeDereference(env, this->get()), index, 1, &e);
return e;
}
void Set(JNIEnv& env, jsize index, const ElementType& value)
{
SetArrayRegion(env, SafeDereference(env, this->get()), index, 1, &value);
}
template < class Array >
void GetRegion(JNIEnv& env, jsize start, Array& buf) const
{
GetArrayRegion(env, SafeDereference(env, this->get()), start, buf);
}
template < class Array >
void SetRegion(JNIEnv& env, jsize start, const Array& buf)
{
SetArrayRegion(env, SafeDereference(env, this->get()), start, buf);
}
static Local<Array<E>> New(JNIEnv& env, jsize length)
{
return Local<Array<E>>(env, &NewArray<E>(env, length));
}
};
template < class TheTag >
class Array< Object<TheTag> > : public Object< ArrayTag<Object<TheTag>> >
{
public:
using SuperType = Object< ArrayTag<Object<TheTag>> >;
using UntaggedType = typename SuperType::UntaggedType;
using TagType = TheTag;
using ElementType = Object<TagType>;
using UntaggedElementType = typename ElementType::UntaggedType;
protected:
explicit Array(std::nullptr_t = nullptr)
{}
explicit Array(UntaggedType* p)
: SuperType(p)
{}
Array(const Array&) = delete;
Array& operator=(const Array&) = delete;
public:
jsize Length(JNIEnv& env) const
{
return GetArrayLength(env, SafeDereference(env, this->get()));
}
Local<ElementType> Get(JNIEnv& env, jsize index) const
{
return Local<ElementType>(env,
reinterpret_cast<UntaggedElementType*>(
GetObjectArrayElement(env, SafeDereference(env, this->get()), index)));
}
void Set(JNIEnv& env, jsize index, const ElementType& value)
{
SetObjectArrayElement(env, SafeDereference(env, this->get()), index, Untag(value));
}
static Local<Array<Object<TheTag>>> New(JNIEnv& env, jsize length, const Object<TheTag>* initialElement = nullptr)
{
return Local<Array<Object<TheTag>>>(env, &NewObjectArray(env, length, *Class<TheTag>::Singleton(env), initialElement ? initialElement->get() : nullptr));
}
};
template < class T >
std::vector<T> MakeAnything(ThingToMake<std::vector<T>>, JNIEnv& env, const Array<T>& array)
{
NullCheck(env, array.get());
std::vector<T> result(GetArrayLength(env, *array));
GetArrayRegion(env, *array, 0, result);
return result;
}
template < class T >
Local<Array<T>> MakeAnything(ThingToMake<Array<T>>, JNIEnv& env, const std::vector<T>& array)
{
Local<Array<T>> result = Local<Array<T>>(env, &NewArray<T>(env, array.size()));
SetArrayRegion(env, *result, 0, array);
return result;
}
inline
std::string MakeAnything(ThingToMake<std::string>, JNIEnv& env, const Array<jbyte>& array)
{
NullCheck(env, array.get());
std::string result;
result.resize(GetArrayLength(env, *array));
GetArrayRegion(env, *array, 0, result.size(), reinterpret_cast<jbyte*>(&result[0]));
return result;
}
inline
Local<Array<jbyte>> MakeAnything(ThingToMake<Array<jbyte>>, JNIEnv& env, const std::string& string)
{
Local<Array<jbyte>> result(env, &NewArray<jbyte>(env, string.size()));
SetArrayRegion(env, *result, 0, string.size(), reinterpret_cast<const jbyte*>(&string[0]));
return result;
}
}
jni::Array< E, std::enable_if_t< IsPrimitive< E >::value > >::New
static Local< Array< E > > New(JNIEnv &env, jsize length)
Definition array.hpp:62
jni::Array< E, std::enable_if_t< IsPrimitive< E >::value > >::operator=
Array & operator=(const Array &)=delete
jni::Array< E, std::enable_if_t< IsPrimitive< E >::value > >::GetRegion
void GetRegion(JNIEnv &env, jsize start, Array &buf) const
Definition array.hpp:51
jni::Array< E, std::enable_if_t< IsPrimitive< E >::value > >::SuperType
Object< ArrayTag< E > > SuperType
Definition array.hpp:17
jni::Array< E, std::enable_if_t< IsPrimitive< E >::value > >::ElementType
E ElementType
Definition array.hpp:19
jni::Array< E, std::enable_if_t< IsPrimitive< E >::value > >::SetRegion
void SetRegion(JNIEnv &env, jsize start, const Array &buf)
Definition array.hpp:57
jni::Array< E, std::enable_if_t< IsPrimitive< E >::value > >::Array
Array(const Array &)=delete
jni::Array< E, std::enable_if_t< IsPrimitive< E >::value > >::Get
ElementType Get(JNIEnv &env, jsize index) const
Definition array.hpp:38
jni::Array< E, std::enable_if_t< IsPrimitive< E >::value > >::UntaggedType
typename SuperType::UntaggedType UntaggedType
Definition array.hpp:18
jni::Array< E, std::enable_if_t< IsPrimitive< E >::value > >::Array
Array(UntaggedType *p)
Definition array.hpp:25
jni::Array< E, std::enable_if_t< IsPrimitive< E >::value > >::Length
jsize Length(JNIEnv &env) const
Definition array.hpp:33
jni::Array< E, std::enable_if_t< IsPrimitive< E >::value > >::Array
Array(std::nullptr_t=nullptr)
Definition array.hpp:22
jni::Array< E, std::enable_if_t< IsPrimitive< E >::value > >::Set
void Set(JNIEnv &env, jsize index, const ElementType &value)
Definition array.hpp:45
jni::Array< Object< TheTag > >::Set
void Set(JNIEnv &env, jsize index, const ElementType &value)
Definition array.hpp:102
jni::Array< Object< TheTag > >::Length
jsize Length(JNIEnv &env) const
Definition array.hpp:90
jni::Array< Object< TheTag > >::SuperType
Object< ArrayTag< Object< TheTag > > > SuperType
Definition array.hpp:72
jni::Array< Object< TheTag > >::TagType
TheTag TagType
Definition array.hpp:74
jni::Array< Object< TheTag > >::Get
Local< ElementType > Get(JNIEnv &env, jsize index) const
Definition array.hpp:95
jni::Array< Object< TheTag > >::UntaggedElementType
typename ElementType::UntaggedType UntaggedElementType
Definition array.hpp:76
jni::Array< Object< TheTag > >::Array
Array(UntaggedType *p)
Definition array.hpp:82
jni::Array< Object< TheTag > >::Array
Array(const Array &)=delete
jni::Array< Object< TheTag > >::UntaggedType
typename SuperType::UntaggedType UntaggedType
Definition array.hpp:73
jni::Array< Object< TheTag > >::operator=
Array & operator=(const Array &)=delete
jni::Array< Object< TheTag > >::New
static Local< Array< Object< TheTag > > > New(JNIEnv &env, jsize length, const Object< TheTag > *initialElement=nullptr)
Definition array.hpp:107
jni::Array< Object< TheTag > >::ElementType
Object< TagType > ElementType
Definition array.hpp:75
jni::Array< Object< TheTag > >::Array
Array(std::nullptr_t=nullptr)
Definition array.hpp:79
jni::Array
Definition array.hpp:11
jni::Class::Singleton
static const Class & Singleton(JNIEnv &env)
Definition class.hpp:101
jni::Object< ArrayTag< E > >::get
UntaggedType * get() const
Definition object.hpp:63
jni::Object< ArrayTag< E > >::Object
Object(std::nullptr_t=nullptr)
Definition object.hpp:52
jni::Object< ArrayTag< E > >::UntaggedType
typename TagTraits< ArrayTag< E > >::UntaggedType UntaggedType
Definition object.hpp:49
functions.hpp
make.hpp
jni
Definition advanced_ownership.hpp:6
jni::NewObjectArray
jarray< jobject > & NewObjectArray(JNIEnv &env, jsize length, jclass &elementClass, jobject *initialElement=nullptr)
Definition functions.hpp:553
jni::GetArrayLength
jsize GetArrayLength(JNIEnv &env, jarray< E > &array)
Definition functions.hpp:465
jni::SafeDereference
T & SafeDereference(JNIEnv &env, T *ptr, const char *message=nullptr)
Definition npe.hpp:19
jni::MakeAnything
std::vector< T > MakeAnything(ThingToMake< std::vector< T > >, JNIEnv &env, const Array< T > &array)
Definition array.hpp:114
jni::SetArrayRegion
void SetArrayRegion(JNIEnv &env, jarray< T > &array, jsize start, jsize len, const T *buf)
Definition functions.hpp:539
jni::NewArray
jarray< E > & NewArray(JNIEnv &env, jsize length)
Definition functions.hpp:472
jni::GetObjectArrayElement
jobject * GetObjectArrayElement(JNIEnv &env, jarray< jobject > &array, jsize index)
Definition functions.hpp:559
jni::SetObjectArrayElement
void SetObjectArrayElement(JNIEnv &env, jarray< jobject > &array, jsize index, jobject *value)
Definition functions.hpp:565
jni::NullCheck
void NullCheck(JNIEnv &env, T *ptr, const char *message=nullptr)
Definition npe.hpp:13
jni::Local
Unique< T, DefaultRefDeleter<&JNIEnv::DeleteLocalRef > > Local
Definition unique.hpp:110
jni::GetArrayRegion
void GetArrayRegion(JNIEnv &env, jarray< T > &array, jsize start, jsize len, T *buf)
Definition functions.hpp:525
jni::jsize
std::size_t jsize
Definition types.hpp:28
jni::Untag
auto Untag(T primitive) -> std::enable_if_t< IsPrimitive< T >::value, T >
Definition tagging.hpp:116
std
Definition errors.hpp:10
object.hpp
jni::ThingToMake
Definition make.hpp:5
tagging.hpp