Search in sources :

Example 1 with FunctionPointer

use of org.bytedeco.javacpp.FunctionPointer in project bigbluebutton by bigbluebutton.

the class Generator method parametersBefore.

void parametersBefore(MethodInformation methodInfo) {
    String adapterLine = "";
    AdapterInformation prevAdapterInfo = null;
    int skipParameters = methodInfo.parameterTypes.length > 0 && methodInfo.parameterTypes[0] == Class.class ? 1 : 0;
    for (int j = skipParameters; j < methodInfo.parameterTypes.length; j++) {
        if (!methodInfo.parameterTypes[j].isPrimitive()) {
            Annotation passBy = by(methodInfo, j);
            String cast = cast(methodInfo, j);
            String[] typeName = methodInfo.parameterRaw[j] ? new String[] { "" } : cppTypeName(methodInfo.parameterTypes[j]);
            AdapterInformation adapterInfo = methodInfo.parameterRaw[j] ? null : adapterInformation(false, methodInfo, j);
            if (FunctionPointer.class.isAssignableFrom(methodInfo.parameterTypes[j])) {
                functions.index(methodInfo.parameterTypes[j]);
                if (methodInfo.parameterTypes[j] == FunctionPointer.class) {
                    logger.warn("Method \"" + methodInfo.method + "\" has an abstract FunctionPointer parameter, " + "but a concrete subclass is required. Compilation will most likely fail.");
                }
                typeName[0] = functionClassName(methodInfo.parameterTypes[j]) + "*";
                typeName[1] = "";
            }
            if (typeName[0].length() == 0 || methodInfo.parameterRaw[j]) {
                methodInfo.parameterRaw[j] = true;
                typeName[0] = jniTypeName(methodInfo.parameterTypes[j]);
                out.println("    " + typeName[0] + " ptr" + j + " = arg" + j + ";");
                continue;
            }
            if ("void*".equals(typeName[0]) && !methodInfo.parameterTypes[j].isAnnotationPresent(Opaque.class)) {
                typeName[0] = "char*";
            }
            out.print("    " + typeName[0] + " ptr" + j + typeName[1] + " = ");
            if (Pointer.class.isAssignableFrom(methodInfo.parameterTypes[j])) {
                out.println("arg" + j + " == NULL ? NULL : (" + typeName[0] + typeName[1] + ")jlong_to_ptr(env->GetLongField(arg" + j + ", JavaCPP_addressFID));");
                if ((j == 0 && FunctionPointer.class.isAssignableFrom(methodInfo.cls) && methodInfo.cls.isAnnotationPresent(Namespace.class)) || (passBy instanceof ByVal && ((ByVal) passBy).nullValue().length() == 0) || (passBy instanceof ByRef && ((ByRef) passBy).nullValue().length() == 0)) {
                    // in the case of member ptr, ptr0 is our object pointer, which cannot be NULL
                    out.println("    if (ptr" + j + " == NULL) {");
                    out.println("        env->ThrowNew(JavaCPP_getClass(env, " + jclasses.index(NullPointerException.class) + "), \"Pointer address of argument " + j + " is NULL.\");");
                    out.println("        return" + (methodInfo.returnType == void.class ? ";" : " 0;"));
                    out.println("    }");
                }
                if (adapterInfo != null || prevAdapterInfo != null) {
                    out.println("    jlong size" + j + " = arg" + j + " == NULL ? 0 : env->GetLongField(arg" + j + ", JavaCPP_limitFID);");
                    out.println("    void* owner" + j + " = JavaCPP_getPointerOwner(env, arg" + j + ");");
                }
                if (!methodInfo.parameterTypes[j].isAnnotationPresent(Opaque.class)) {
                    out.println("    jlong position" + j + " = arg" + j + " == NULL ? 0 : env->GetLongField(arg" + j + ", JavaCPP_positionFID);");
                    out.println("    ptr" + j + " += position" + j + ";");
                    if (adapterInfo != null || prevAdapterInfo != null) {
                        out.println("    size" + j + " -= position" + j + ";");
                    }
                }
            } else if (methodInfo.parameterTypes[j] == String.class) {
                passesStrings = true;
                out.println("JavaCPP_getStringBytes(env, arg" + j + ");");
                if (adapterInfo != null || prevAdapterInfo != null) {
                    out.println("    jlong size" + j + " = 0;");
                    out.println("    void* owner" + j + " = (void*)ptr" + j + ";");
                }
            } else if (methodInfo.parameterTypes[j].isArray() && methodInfo.parameterTypes[j].getComponentType().isPrimitive()) {
                out.print("arg" + j + " == NULL ? NULL : ");
                String s = methodInfo.parameterTypes[j].getComponentType().getName();
                if (methodInfo.valueGetter || methodInfo.valueSetter || methodInfo.memberGetter || methodInfo.memberSetter) {
                    out.println("(j" + s + "*)env->GetPrimitiveArrayCritical(arg" + j + ", NULL);");
                } else {
                    s = Character.toUpperCase(s.charAt(0)) + s.substring(1);
                    out.println("env->Get" + s + "ArrayElements(arg" + j + ", NULL);");
                }
                if (adapterInfo != null || prevAdapterInfo != null) {
                    out.println("    jlong size" + j + " = arg" + j + " == NULL ? 0 : env->GetArrayLength(arg" + j + ");");
                    out.println("    void* owner" + j + " = (void*)ptr" + j + ";");
                }
            } else if (Buffer.class.isAssignableFrom(methodInfo.parameterTypes[j])) {
                out.println("arg" + j + " == NULL ? NULL : (" + typeName[0] + typeName[1] + ")env->GetDirectBufferAddress(arg" + j + ");");
                if (adapterInfo != null || prevAdapterInfo != null) {
                    out.println("    jlong size" + j + " = arg" + j + " == NULL ? 0 : env->GetDirectBufferCapacity(arg" + j + ");");
                    out.println("    void* owner" + j + " = (void*)ptr" + j + ";");
                }
                if (methodInfo.parameterTypes[j] != Buffer.class) {
                    // given the component type, we can also fetch the array of non-direct buffers
                    String paramName = methodInfo.parameterTypes[j].getSimpleName();
                    paramName = paramName.substring(0, paramName.length() - 6);
                    String paramNameLowerCase = Character.toLowerCase(paramName.charAt(0)) + paramName.substring(1);
                    out.println("    j" + paramNameLowerCase + "Array arr" + j + " = NULL;");
                    out.println("    if (arg" + j + " != NULL && ptr" + j + " == NULL) {");
                    out.println("        arr" + j + " = (j" + paramNameLowerCase + "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" + paramName + "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 \"" + methodInfo.parameterTypes[j].getCanonicalName() + "\". Compilation will most likely fail.");
            }
            if (adapterInfo != null) {
                usesAdapters = true;
                adapterLine = "    " + adapterInfo.name + " adapter" + j + "(";
                prevAdapterInfo = adapterInfo;
            }
            if (prevAdapterInfo != null) {
                if (!FunctionPointer.class.isAssignableFrom(methodInfo.cls)) {
                    // sometimes we need to use the Cast annotation for declaring functions only
                    adapterLine += cast;
                }
                adapterLine += "ptr" + j + ", size" + j + ", owner" + j;
                if (--prevAdapterInfo.argc > 0) {
                    adapterLine += ", ";
                }
            }
            if (prevAdapterInfo != null && prevAdapterInfo.argc <= 0) {
                out.println(adapterLine + ");");
                prevAdapterInfo = null;
            }
        }
    }
}
Also used : ByVal(org.bytedeco.javacpp.annotation.ByVal) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) IntBuffer(java.nio.IntBuffer) CharBuffer(java.nio.CharBuffer) ShortBuffer(java.nio.ShortBuffer) Buffer(java.nio.Buffer) DoubleBuffer(java.nio.DoubleBuffer) LongBuffer(java.nio.LongBuffer) FunctionPointer(org.bytedeco.javacpp.FunctionPointer) ByRef(org.bytedeco.javacpp.annotation.ByRef) Opaque(org.bytedeco.javacpp.annotation.Opaque) Annotation(java.lang.annotation.Annotation)

Aggregations

Annotation (java.lang.annotation.Annotation)1 Buffer (java.nio.Buffer)1 ByteBuffer (java.nio.ByteBuffer)1 CharBuffer (java.nio.CharBuffer)1 DoubleBuffer (java.nio.DoubleBuffer)1 FloatBuffer (java.nio.FloatBuffer)1 IntBuffer (java.nio.IntBuffer)1 LongBuffer (java.nio.LongBuffer)1 ShortBuffer (java.nio.ShortBuffer)1 FunctionPointer (org.bytedeco.javacpp.FunctionPointer)1 ByRef (org.bytedeco.javacpp.annotation.ByRef)1 ByVal (org.bytedeco.javacpp.annotation.ByVal)1 Opaque (org.bytedeco.javacpp.annotation.Opaque)1