Skip to content

Commit f32116e

Browse files
committed
[wpiutil] Add JNI array factories for more types
The Sleipnir Java bindings needed these.
1 parent 603a59f commit f32116e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

wpiutil/src/main/native/include/wpi/util/jni_util.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,19 @@ WPI_JNI_MAKEJARRAY(jdouble, Double)
607607

608608
#undef WPI_JNI_MAKEJARRAY
609609

610+
template <class T>
611+
requires(sizeof(typename T::value_type) == sizeof(jint) &&
612+
std::integral<typename T::value_type>)
613+
inline jintArray MakeJIntArray(JNIEnv* env, const T& arr) {
614+
jintArray jarr = env->NewIntArray(arr.size());
615+
if (!jarr) {
616+
return nullptr;
617+
}
618+
env->SetIntArrayRegion(jarr, 0, arr.size(),
619+
reinterpret_cast<const jint*>(arr.data()));
620+
return jarr;
621+
}
622+
610623
template <class T>
611624
requires(sizeof(typename T::value_type) == sizeof(jlong) &&
612625
std::integral<typename T::value_type>)
@@ -620,6 +633,19 @@ inline jlongArray MakeJLongArray(JNIEnv* env, const T& arr) {
620633
return jarr;
621634
}
622635

636+
template <class T>
637+
requires(sizeof(typename T::value_type) == sizeof(jdouble) &&
638+
std::floating_point<typename T::value_type>)
639+
inline jdoubleArray MakeJDoubleArray(JNIEnv* env, const T& arr) {
640+
jdoubleArray jarr = env->NewDoubleArray(arr.size());
641+
if (!jarr) {
642+
return nullptr;
643+
}
644+
env->SetDoubleArrayRegion(jarr, 0, arr.size(),
645+
reinterpret_cast<const jdouble*>(arr.data()));
646+
return jarr;
647+
}
648+
623649
/**
624650
* Convert an array of std::string into a jarray of jstring.
625651
*

0 commit comments

Comments
 (0)