Skip to content

Commit

Permalink
* In addition to direct NIO buffers, also accept as function argumen…
Browse files Browse the repository at this point in the history
…ts non-direct ones backed by arrays (issue bytedeco/javacpp-presets#36)
  • Loading branch information
saudet committed Feb 18, 2015
1 parent 52b7ee1 commit ab7237e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* In addition to direct NIO buffers, also accept as function arguments non-direct ones backed by arrays ([issue bytedeco/javacpp-presets#36](https://github.com/bytedeco/javacpp-presets/issues/36))
* Fix `@Virtual` callback functions defined inside a `@Namespace`
* Adjust `TokenIndexer` and `Parser` to handle `#if`, `#ifdef`, `#ifndef`, `#elif`, `#else`, and `#endif` preprocessor directives more appropriately, even when placed in the middle of declarations
* Append `@Documented` to annotation types to have them picked up by Javadoc
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/bytedeco/javacpp/tools/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, String classPa
out.println("static jfieldID JavaCPP_limitFID = NULL;");
out.println("static jfieldID JavaCPP_capacityFID = NULL;");
out.println("static jmethodID JavaCPP_initMID = NULL;");
out.println("static jmethodID JavaCPP_arrayMID = NULL;");
out.println("static jmethodID JavaCPP_toStringMID = NULL;");
out.println();
out.println("static inline void JavaCPP_log(const char* fmt, ...) {");
Expand Down Expand Up @@ -805,6 +806,11 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, String classPa
out.println(" if (JavaCPP_initMID == NULL) {");
out.println(" return JNI_ERR;");
out.println(" }");
out.println(" JavaCPP_arrayMID = JavaCPP_getMethodID(env, " +
jclasses.index(Buffer.class) + ", \"array\", \"()Ljava/lang/Object;\");");
out.println(" if (JavaCPP_arrayMID == NULL) {");
out.println(" return JNI_ERR;");
out.println(" }");
out.println(" JavaCPP_toStringMID = JavaCPP_getMethodID(env, " +
jclasses.index(Object.class) + ", \"toString\", \"()Ljava/lang/String;\");");
out.println(" if (JavaCPP_toStringMID == NULL) {");
Expand Down Expand Up @@ -1100,6 +1106,24 @@ void parametersBefore(MethodInformation methodInfo) {
out.println(" jint size" + j +
" = arg" + j + " == NULL ? 0 : env->GetDirectBufferCapacity(arg" + j + ");");
}
if (methodInfo.parameterTypes[j] != Buffer.class) {
// given the component type, we can also fetch the array of non-direct buffers
String S = methodInfo.parameterTypes[j].getSimpleName();
S = S.substring(0, S.length() - 6);
String s = Character.toLowerCase(S.charAt(0)) + S.substring(1);
out.println(" j" + s + "Array arr" + j + " = NULL;");
out.println(" if (arg" + j + " != NULL && ptr" + j + " == NULL) {");
out.println(" arr" + j + " = (j" + s + "Array)env->CallObjectMethod(arg" + j + ", JavaCPP_arrayMID);");
out.println(" if (env->ExceptionOccurred() != NULL) {");
out.println(" env->ExceptionClear();");
out.println(" } else {");
out.println(" ptr" + j + " = arr" + j + " == NULL ? NULL : env->Get" + S + "ArrayElements(arr" + j + ", NULL);");
if (adapterInfo != null || prevAdapterInfo != null) {
out.println(" size" + j + " = env->GetArrayLength(arr" + j + ");");
}
out.println(" }");
out.println(" }");
}
} else {
out.println("arg" + j + ";");
logger.warn("Method \"" + methodInfo.method + "\" has an unsupported parameter of type \"" +
Expand Down Expand Up @@ -1607,6 +1631,13 @@ void parametersAfter(MethodInformation methodInfo) {
String S = Character.toUpperCase(s.charAt(0)) + s.substring(1);
out.println("env->Release" + S + "ArrayElements(arg" + j + ", (j" + s + "*)ptr" + j + ", 0);");
}
} else if (Buffer.class.isAssignableFrom(methodInfo.parameterTypes[j])
&& methodInfo.parameterTypes[j] != Buffer.class) {
out.print(" if (arr" + j + " != NULL) ");
String S = methodInfo.parameterTypes[j].getSimpleName();
S = S.substring(0, S.length() - 6);
String s = Character.toLowerCase(S.charAt(0)) + S.substring(1);
out.println("env->Release" + S + "ArrayElements(arr" + j + ", (j" + s + "*)ptr" + j + ", 0);");
}
}
}
Expand Down

0 comments on commit ab7237e

Please sign in to comment.