|
| 1 | +/** |
| 2 | + * Copyright © 2026, Evolved Binary Ltd |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * * Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * * Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * * Neither the name of the <organization> nor the |
| 13 | + * names of its contributors may be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
| 20 | + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | +#include <jni.h> |
| 28 | +#include "Iterator.h" |
| 29 | + |
| 30 | +#ifdef __cplusplus |
| 31 | +extern "C" { |
| 32 | +#endif |
| 33 | + |
| 34 | +/* |
| 35 | + * Class: com_evolvedbinary_jnibench_common_iterators_NativeIterator |
| 36 | + * Method: createIterator |
| 37 | + * Signature: (IJ)J |
| 38 | + */ |
| 39 | +JNIEXPORT jlong JNICALL Java_com_evolvedbinary_jnibench_common_iterators_NativeIterator_createIterator |
| 40 | + (JNIEnv *env, jobject obj, jint num_elements, jlong element_size) { |
| 41 | + Iterator* iterator = new Iterator(num_elements, static_cast<size_t>(element_size)); |
| 42 | + return reinterpret_cast<jlong>(iterator); |
| 43 | +} |
| 44 | + |
| 45 | +/* |
| 46 | + * Class: com_evolvedbinary_jnibench_common_iterators_NativeIterator |
| 47 | + * Method: hasNext |
| 48 | + * Signature: (J)B |
| 49 | + */ |
| 50 | +JNIEXPORT jboolean JNICALL Java_com_evolvedbinary_jnibench_common_iterators_NativeIterator_hasNext |
| 51 | + (JNIEnv *env, jobject obj, jlong handle) { |
| 52 | + Iterator* iterator = reinterpret_cast<Iterator*>(handle); |
| 53 | + return iterator->hasNext() ? JNI_TRUE : JNI_FALSE; |
| 54 | +} |
| 55 | + |
| 56 | +/* |
| 57 | + * Class: com_evolvedbinary_jnibench_common_iterators_NativeIterator |
| 58 | + * Method: next |
| 59 | + * Signature: (J)[B |
| 60 | + */ |
| 61 | +JNIEXPORT jbyteArray JNICALL Java_com_evolvedbinary_jnibench_common_iterators_NativeIterator_next |
| 62 | + (JNIEnv *env, jobject obj, jlong handle) { |
| 63 | + Iterator* iterator = reinterpret_cast<Iterator*>(handle); |
| 64 | + try { |
| 65 | + const std::vector<char>& data = iterator->next(); |
| 66 | + jbyteArray result = env->NewByteArray(static_cast<jsize>(data.size())); |
| 67 | + if (result == nullptr) { |
| 68 | + return nullptr; |
| 69 | + } |
| 70 | + env->SetByteArrayRegion(result, 0, static_cast<jsize>(data.size()), reinterpret_cast<const jbyte*>(data.data())); |
| 71 | + return result; |
| 72 | + } catch (const std::out_of_range& e) { |
| 73 | + jclass exClass = env->FindClass("java/util/NoSuchElementException"); |
| 74 | + if (exClass != nullptr) { |
| 75 | + env->ThrowNew(exClass, e.what()); |
| 76 | + } |
| 77 | + return nullptr; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/* |
| 82 | + * Class: com_evolvedbinary_jnibench_common_iterators_NativeIterator |
| 83 | + * Method: disposeInternal |
| 84 | + * Signature: (J)V |
| 85 | + */ |
| 86 | +JNIEXPORT void JNICALL Java_com_evolvedbinary_jnibench_common_iterators_NativeIterator_disposeInternal |
| 87 | + (JNIEnv *env, jobject obj, jlong handle) { |
| 88 | + Iterator* iterator = reinterpret_cast<Iterator*>(handle); |
| 89 | + delete iterator; |
| 90 | +} |
| 91 | + |
| 92 | +#ifdef __cplusplus |
| 93 | +} |
| 94 | +#endif |
0 commit comments