[wpiutil] jni_util: Add JSpan and CriticalJSpan (#5554)

These replace JArrayRef et al and support statically sized arrays similar to std::span.
This commit is contained in:
Joseph Eng
2023-08-24 00:02:56 -07:00
committed by GitHub
parent 8f3d6a1d4b
commit 2e4ad35e36
19 changed files with 333 additions and 317 deletions

View File

@@ -201,7 +201,7 @@ Java_edu_wpi_first_util_datalog_DataLogJNI_appendRaw
wpi::ThrowIndexOobException(env, "length must be >= 0");
return;
}
CriticalJByteArrayRef cvalue{env, value};
CriticalJSpan<const jbyte> cvalue{env, value};
if (static_cast<unsigned int>(start + length) > cvalue.size()) {
wpi::ThrowIndexOobException(
env, "start + len must be smaller than array length");
@@ -237,7 +237,7 @@ Java_edu_wpi_first_util_datalog_DataLogJNI_appendRawBuffer
wpi::ThrowIndexOobException(env, "length must be >= 0");
return;
}
JByteArrayRef cvalue{env, value, start + length};
JSpan<const jbyte> cvalue{env, value, static_cast<size_t>(start + length)};
if (!cvalue) {
wpi::ThrowIllegalArgumentException(env,
"value must be a native ByteBuffer");
@@ -347,7 +347,7 @@ Java_edu_wpi_first_util_datalog_DataLogJNI_appendBooleanArray
return;
}
reinterpret_cast<DataLog*>(impl)->AppendBooleanArray(
entry, JBooleanArrayRef{env, value}, timestamp);
entry, JSpan<const jboolean>{env, value}, timestamp);
}
/*
@@ -368,17 +368,15 @@ Java_edu_wpi_first_util_datalog_DataLogJNI_appendIntegerArray
wpi::ThrowNullPointerException(env, "value is null");
return;
}
JLongArrayRef jarr{env, value};
JSpan<const jlong> jarr{env, value};
if constexpr (sizeof(jlong) == sizeof(int64_t)) {
reinterpret_cast<DataLog*>(impl)->AppendIntegerArray(
entry,
{reinterpret_cast<const int64_t*>(jarr.array().data()),
jarr.array().size()},
entry, {reinterpret_cast<const int64_t*>(jarr.data()), jarr.size()},
timestamp);
} else {
wpi::SmallVector<int64_t, 16> arr;
arr.reserve(jarr.size());
for (auto v : jarr.array()) {
for (auto v : jarr) {
arr.push_back(v);
}
reinterpret_cast<DataLog*>(impl)->AppendIntegerArray(entry, arr, timestamp);
@@ -404,7 +402,7 @@ Java_edu_wpi_first_util_datalog_DataLogJNI_appendFloatArray
return;
}
reinterpret_cast<DataLog*>(impl)->AppendFloatArray(
entry, JFloatArrayRef{env, value}, timestamp);
entry, JSpan<const jfloat>{env, value}, timestamp);
}
/*
@@ -426,7 +424,7 @@ Java_edu_wpi_first_util_datalog_DataLogJNI_appendDoubleArray
return;
}
reinterpret_cast<DataLog*>(impl)->AppendDoubleArray(
entry, JDoubleArrayRef{env, value}, timestamp);
entry, JSpan<const jdouble>{env, value}, timestamp);
}
/*

View File

@@ -275,11 +275,11 @@ JNIEXPORT jintArray JNICALL
Java_edu_wpi_first_util_WPIUtilJNI_waitForObjects
(JNIEnv* env, jclass, jintArray handles)
{
JIntArrayRef handlesArr{env, handles};
JSpan<const jint> handlesArr{env, handles};
wpi::SmallVector<WPI_Handle, 8> signaledBuf;
signaledBuf.resize(handlesArr.size());
std::span<const WPI_Handle> handlesArr2{
reinterpret_cast<const WPI_Handle*>(handlesArr.array().data()),
reinterpret_cast<const WPI_Handle*>(handlesArr.data()),
handlesArr.size()};
auto signaled = wpi::WaitForObjects(handlesArr2, signaledBuf);
@@ -299,11 +299,11 @@ JNIEXPORT jintArray JNICALL
Java_edu_wpi_first_util_WPIUtilJNI_waitForObjectsTimeout
(JNIEnv* env, jclass, jintArray handles, jdouble timeout)
{
JIntArrayRef handlesArr{env, handles};
JSpan<const jint> handlesArr{env, handles};
wpi::SmallVector<WPI_Handle, 8> signaledBuf;
signaledBuf.resize(handlesArr.size());
std::span<const WPI_Handle> handlesArr2{
reinterpret_cast<const WPI_Handle*>(handlesArr.array().data()),
reinterpret_cast<const WPI_Handle*>(handlesArr.data()),
handlesArr.size()};
bool timedOut;