Search in sources :

Example 6 with Pointer

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

the class Generator method methods.

boolean methods(Class<?> cls) {
    if (!checkPlatform(cls)) {
        return false;
    }
    Set<String> memberList = members.get(cls);
    if (!cls.isAnnotationPresent(Opaque.class) && !FunctionPointer.class.isAssignableFrom(cls) && cls.getEnclosingClass() != Pointer.class) {
        if (memberList == null) {
            members.put(cls, memberList = new LinkedHashSet<String>());
        }
        if (!memberList.contains("sizeof")) {
            memberList.add("sizeof");
        }
    }
    boolean didSomething = false;
    for (Class<?> c : cls.getDeclaredClasses()) {
        if (Pointer.class.isAssignableFrom(c) || Pointer.class.isAssignableFrom(c.getEnclosingClass())) {
            didSomething |= methods(c);
        }
    }
    Method[] methods = cls.getDeclaredMethods();
    MethodInformation[] methodInfos = new MethodInformation[methods.length];
    for (int i = 0; i < methods.length; i++) {
        methodInfos[i] = methodInformation(methods[i]);
    }
    Class<?> c = cls.getSuperclass();
    while (c != null && c != Object.class) {
        // consider non-duplicate virtual functions from superclasses as well
        for (Method m : c.getDeclaredMethods()) {
            if (m.isAnnotationPresent(Virtual.class)) {
                boolean found = false;
                String name = m.getName();
                Class<?>[] types = m.getParameterTypes();
                for (Method m2 : methods) {
                    if (name.equals(m2.getName()) && Arrays.equals(types, m2.getParameterTypes())) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    methods = Arrays.copyOf(methods, methods.length + 1);
                    methods[methods.length - 1] = m;
                    methodInfos = Arrays.copyOf(methodInfos, methodInfos.length + 1);
                    methodInfos[methods.length - 1] = methodInformation(m);
                    methodInfos[methods.length - 1].cls = cls;
                }
            }
        }
        c = c.getSuperclass();
    }
    boolean[] callbackAllocators = new boolean[methods.length];
    Method functionMethod = functionMethod(cls, callbackAllocators);
    boolean firstCallback = true;
    for (int i = 0; i < methods.length; i++) {
        if (!checkPlatform(methods[i].getAnnotation(Platform.class), null)) {
            continue;
        }
        MethodInformation methodInfo = methodInfos[i];
        String nativeName = mangle(cls.getName()) + "_" + mangle(methods[i].getName());
        String callbackName = callbackAllocators[i] && methodInfo.parameterTypes.length > 0 ? null : "JavaCPP_" + nativeName + "_callback";
        if (callbackAllocators[i] && functionMethod == null) {
            logger.warn("No callback method call() or apply() has been not declared in \"" + cls.getCanonicalName() + "\". No code will be generated for callback allocator.");
            continue;
        } else if (callbackAllocators[i] || (methods[i].equals(functionMethod) && !Modifier.isNative(methods[i].getModifiers()))) {
            functions.index(cls);
            Name name = methods[i].getAnnotation(Name.class);
            if (name != null && name.value().length > 0 && name.value()[0].length() > 0) {
                callbackName = name.value()[0];
            }
            callback(cls, functionMethod, callbackName, firstCallback, null);
            firstCallback = false;
            didSomething = true;
        }
        if ((Modifier.isNative(methods[i].getModifiers()) || Modifier.isAbstract(methods[i].getModifiers())) && !methodInfo.valueGetter && !methodInfo.valueSetter && !methodInfo.memberGetter && !methodInfo.memberSetter && !cls.isInterface() && (methods[i].isAnnotationPresent(Virtual.class) || methodInfo.allocator)) {
            callback(cls, methods[i], methodInfo.memberName[0], !methodInfo.allocator, methodInfo);
        }
        if (!Modifier.isNative(methods[i].getModifiers())) {
            continue;
        }
        if ((methodInfo.memberGetter || methodInfo.memberSetter) && !methodInfo.noOffset && memberList != null && !Modifier.isStatic(methodInfo.modifiers)) {
            if (!memberList.contains(methodInfo.memberName[0])) {
                memberList.add(methodInfo.memberName[0]);
            }
        }
        didSomething = true;
        out.print("JNIEXPORT " + jniTypeName(methodInfo.returnType) + " JNICALL Java_" + nativeName);
        if (methodInfo.overloaded) {
            out.print("__" + mangle(signature(methodInfo.parameterTypes)));
        }
        if (Modifier.isStatic(methodInfo.modifiers)) {
            out.print("(JNIEnv* env, jclass cls");
        } else {
            out.print("(JNIEnv* env, jobject obj");
        }
        for (int j = 0; j < methodInfo.parameterTypes.length; j++) {
            out.print(", " + jniTypeName(methodInfo.parameterTypes[j]) + " arg" + j);
        }
        out.println(") {");
        if (callbackAllocators[i]) {
            callbackAllocator(cls, callbackName);
            continue;
        } else if (!Modifier.isStatic(methodInfo.modifiers) && !methodInfo.allocator && !methodInfo.arrayAllocator && !methodInfo.deallocator) {
            // get our "this" pointer
            String[] typeName = cppTypeName(cls);
            if ("void*".equals(typeName[0]) && !cls.isAnnotationPresent(Opaque.class)) {
                typeName[0] = "char*";
            } else if (FunctionPointer.class.isAssignableFrom(cls)) {
                functions.index(cls);
                typeName[0] = functionClassName(cls) + "*";
                typeName[1] = "";
            }
            out.println("    " + typeName[0] + " ptr" + typeName[1] + " = (" + typeName[0] + typeName[1] + ")jlong_to_ptr(env->GetLongField(obj, JavaCPP_addressFID));");
            out.println("    if (ptr == NULL) {");
            out.println("        env->ThrowNew(JavaCPP_getClass(env, " + jclasses.index(NullPointerException.class) + "), \"This pointer address is NULL.\");");
            out.println("        return" + (methodInfo.returnType == void.class ? ";" : " 0;"));
            out.println("    }");
            if (FunctionPointer.class.isAssignableFrom(cls)) {
                out.println("    if (ptr->ptr == NULL) {");
                out.println("        env->ThrowNew(JavaCPP_getClass(env, " + jclasses.index(NullPointerException.class) + "), \"This function pointer address is NULL.\");");
                out.println("        return" + (methodInfo.returnType == void.class ? ";" : " 0;"));
                out.println("    }");
            }
            if (!cls.isAnnotationPresent(Opaque.class)) {
                out.println("    jlong position = env->GetLongField(obj, JavaCPP_positionFID);");
                out.println("    ptr += position;");
                if (methodInfo.bufferGetter) {
                    out.println("    jlong size = env->GetLongField(obj, JavaCPP_limitFID);");
                    out.println("    size -= position;");
                }
            }
        }
        parametersBefore(methodInfo);
        String returnPrefix = returnBefore(methodInfo);
        call(methodInfo, returnPrefix, false);
        returnAfter(methodInfo);
        parametersAfter(methodInfo);
        if (methodInfo.throwsException != null) {
            out.println("    if (exc != NULL) {");
            out.println("        env->Throw(exc);");
            out.println("    }");
        }
        if (methodInfo.returnType != void.class) {
            out.println("    return rarg;");
        }
        out.println("}");
    }
    out.println();
    return didSomething;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FunctionPointer(org.bytedeco.javacpp.FunctionPointer) Opaque(org.bytedeco.javacpp.annotation.Opaque) CLongPointer(org.bytedeco.javacpp.CLongPointer) CharPointer(org.bytedeco.javacpp.CharPointer) IntPointer(org.bytedeco.javacpp.IntPointer) BytePointer(org.bytedeco.javacpp.BytePointer) PointerPointer(org.bytedeco.javacpp.PointerPointer) FunctionPointer(org.bytedeco.javacpp.FunctionPointer) LongPointer(org.bytedeco.javacpp.LongPointer) ShortPointer(org.bytedeco.javacpp.ShortPointer) BoolPointer(org.bytedeco.javacpp.BoolPointer) DoublePointer(org.bytedeco.javacpp.DoublePointer) FloatPointer(org.bytedeco.javacpp.FloatPointer) Pointer(org.bytedeco.javacpp.Pointer) SizeTPointer(org.bytedeco.javacpp.SizeTPointer) Virtual(org.bytedeco.javacpp.annotation.Virtual) Method(java.lang.reflect.Method) Name(org.bytedeco.javacpp.annotation.Name)

Example 7 with Pointer

use of org.bytedeco.javacpp.Pointer in project javacpp by bytedeco.

the class Generator method returnAfter.

void returnAfter(MethodInformation methodInfo) {
    String indent = methodInfo.throwsException != null ? "        " : "    ";
    String[] typeName = methodInfo.returnRaw ? new String[] { "" } : cppCastTypeName(methodInfo.returnType, methodInfo.annotations);
    Annotation returnBy = by(methodInfo.annotations);
    String valueTypeName = valueTypeName(typeName);
    AdapterInformation adapterInfo = adapterInformation(false, valueTypeName, methodInfo.annotations);
    String suffix = methodInfo.deallocator ? "" : ";";
    if (!methodInfo.returnType.isPrimitive() && adapterInfo != null) {
        suffix = ")" + suffix;
    }
    if ((Pointer.class.isAssignableFrom(methodInfo.returnType) || (methodInfo.returnType.isArray() && methodInfo.returnType.getComponentType().isPrimitive()) || Buffer.class.isAssignableFrom(methodInfo.returnType)) || methodInfo.returnType == String.class) {
        if (returnBy instanceof ByVal && adapterInfo == null) {
            suffix = ")" + suffix;
        } else if (returnBy instanceof ByPtrPtr) {
            out.println(suffix);
            suffix = "";
            out.println(indent + "if (rptrptr == NULL) {");
            out.println(indent + "    env->ThrowNew(JavaCPP_getClass(env, " + jclasses.index(NullPointerException.class) + "), \"Return pointer address is NULL.\");");
            out.println(indent + "} else {");
            out.println(indent + "    rptr = *rptrptr;");
            out.println(indent + "}");
        }
    }
    out.println(suffix);
    if (methodInfo.returnType == void.class) {
        if (methodInfo.allocator || methodInfo.arrayAllocator) {
            out.println(indent + "jlong rcapacity = " + (methodInfo.arrayAllocator ? "arg0;" : "1;"));
            boolean noDeallocator = methodInfo.cls == Pointer.class || methodInfo.cls.isAnnotationPresent(NoDeallocator.class) || methodInfo.method.isAnnotationPresent(NoDeallocator.class);
            out.print(indent + "JavaCPP_initPointer(env, obj, rptr, rcapacity, rptr, ");
            if (noDeallocator) {
                out.println("NULL);");
            } else if (methodInfo.arrayAllocator) {
                out.println("&JavaCPP_" + mangle(methodInfo.cls.getName()) + "_deallocateArray);");
                arrayDeallocators.index(methodInfo.cls);
            } else {
                out.println("&JavaCPP_" + mangle(methodInfo.cls.getName()) + "_deallocate);");
                deallocators.index(methodInfo.cls);
            }
            if (virtualFunctions.containsKey(methodInfo.cls)) {
                typeName = cppTypeName(methodInfo.cls);
                valueTypeName = valueTypeName(typeName);
                String subType = "JavaCPP_" + mangle(valueTypeName);
                out.println(indent + "((" + subType + "*)rptr)->obj = env->NewWeakGlobalRef(obj);");
            }
        }
    } else {
        if (methodInfo.valueSetter || methodInfo.memberSetter || methodInfo.noReturnGetter) {
        // nothing
        } else if (methodInfo.returnType.isPrimitive()) {
            out.println(indent + "rarg = (" + jniTypeName(methodInfo.returnType) + ")rval;");
        } else if (methodInfo.returnRaw) {
            out.println(indent + "rarg = rptr;");
        } else if (Enum.class.isAssignableFrom(methodInfo.returnType)) {
            accessesEnums = true;
            String s = enumValueType(methodInfo.returnType);
            if (s != null) {
                String S = Character.toUpperCase(s.charAt(0)) + s.substring(1);
                out.println(indent + "if (rarg != NULL) {");
                out.println(indent + "    env->Set" + S + "Field(rarg, JavaCPP_" + s + "ValueFID, (j" + s + ")rval);");
                out.println(indent + "}");
            }
        } else {
            boolean needInit = false;
            if (adapterInfo != null) {
                out.println(indent + "rptr = radapter;");
                if (methodInfo.returnType != String.class) {
                    out.println(indent + "jlong rcapacity = (jlong)radapter.size;");
                    if (Pointer.class.isAssignableFrom(methodInfo.returnType)) {
                        out.println(indent + "void* rowner = radapter.owner;");
                    }
                    out.println(indent + "void (*deallocator)(void*) = &" + adapterInfo.name + "::deallocate;");
                }
                needInit = true;
            } else if (returnBy instanceof ByVal || FunctionPointer.class.isAssignableFrom(methodInfo.returnType)) {
                out.println(indent + "jlong rcapacity = 1;");
                out.println(indent + "void* rowner = (void*)rptr;");
                out.println(indent + "void (*deallocator)(void*) = &JavaCPP_" + mangle(methodInfo.returnType.getName()) + "_deallocate;");
                deallocators.index(methodInfo.returnType);
                needInit = true;
            }
            if (Pointer.class.isAssignableFrom(methodInfo.returnType)) {
                out.print(indent);
                if (!(returnBy instanceof ByVal)) {
                    // check if we can reuse one of the Pointer objects from the arguments
                    if (Modifier.isStatic(methodInfo.modifiers) && methodInfo.parameterTypes.length > 0) {
                        for (int i = 0; i < methodInfo.parameterTypes.length; i++) {
                            String cast = cast(methodInfo, i);
                            if (Arrays.equals(methodInfo.parameterAnnotations[i], methodInfo.annotations) && methodInfo.parameterTypes[i] == methodInfo.returnType && !(returnBy instanceof ByPtrPtr) && !(returnBy instanceof ByPtrRef)) {
                                out.println("if (rptr == " + cast + "ptr" + i + ") {");
                                out.println(indent + "    rarg = arg" + i + ";");
                                out.print(indent + "} else ");
                            }
                        }
                    } else if (!Modifier.isStatic(methodInfo.modifiers) && methodInfo.cls == methodInfo.returnType) {
                        out.println("if (rptr == ptr) {");
                        out.println(indent + "    rarg = obj;");
                        out.print(indent + "} else ");
                    }
                }
                out.println("if (rptr != NULL) {");
                out.println(indent + "    rarg = JavaCPP_createPointer(env, " + jclasses.index(methodInfo.returnType) + (methodInfo.parameterTypes.length > 0 && methodInfo.parameterTypes[0] == Class.class ? ", arg0);" : ");"));
                out.println(indent + "    if (rarg != NULL) {");
                if (needInit) {
                    out.println(indent + "        JavaCPP_initPointer(env, rarg, rptr, rcapacity, rowner, deallocator);");
                } else {
                    out.println(indent + "        env->SetLongField(rarg, JavaCPP_addressFID, ptr_to_jlong(rptr));");
                }
                out.println(indent + "    }");
                out.println(indent + "}");
            } else if (methodInfo.returnType == String.class) {
                passesStrings = true;
                out.println(indent + "if (rptr != NULL) {");
                out.println(indent + "    rarg = JavaCPP_createString(env, rptr);");
                out.println(indent + "}");
            } else if (methodInfo.returnType.isArray() && methodInfo.returnType.getComponentType().isPrimitive()) {
                if (adapterInfo == null && !(returnBy instanceof ByVal)) {
                    out.println(indent + "jlong rcapacity = rptr != NULL ? 1 : 0;");
                }
                String componentName = methodInfo.returnType.getComponentType().getName();
                String componentNameUpperCase = Character.toUpperCase(componentName.charAt(0)) + componentName.substring(1);
                out.println(indent + "if (rptr != NULL) {");
                out.println(indent + "    rarg = env->New" + componentNameUpperCase + "Array(rcapacity < INT_MAX ? rcapacity : INT_MAX);");
                out.println(indent + "    env->Set" + componentNameUpperCase + "ArrayRegion(rarg, 0, rcapacity < INT_MAX ? rcapacity : INT_MAX, (j" + componentName + "*)rptr);");
                out.println(indent + "}");
                if (adapterInfo != null) {
                    out.println(indent + "if (deallocator != 0 && rptr != NULL) {");
                    out.println(indent + "    (*(void(*)(void*))jlong_to_ptr(deallocator))((void*)rptr);");
                    out.println(indent + "}");
                }
            } else if (Buffer.class.isAssignableFrom(methodInfo.returnType)) {
                if (methodInfo.bufferGetter) {
                    out.println(indent + "jlong rcapacity = size;");
                } else if (adapterInfo == null && !(returnBy instanceof ByVal)) {
                    out.println(indent + "jlong rcapacity = rptr != NULL ? 1 : 0;");
                }
                out.println(indent + "if (rptr != NULL) {");
                out.println(indent + "    jlong rcapacityptr = rcapacity * sizeof(rptr[0]);");
                out.println(indent + "    rarg = env->NewDirectByteBuffer((void*)rptr, rcapacityptr < INT_MAX ? rcapacityptr : INT_MAX);");
                out.println(indent + "}");
            }
        }
    }
}
Also used : 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) ByPtrRef(org.bytedeco.javacpp.annotation.ByPtrRef) CLongPointer(org.bytedeco.javacpp.CLongPointer) CharPointer(org.bytedeco.javacpp.CharPointer) IntPointer(org.bytedeco.javacpp.IntPointer) BytePointer(org.bytedeco.javacpp.BytePointer) PointerPointer(org.bytedeco.javacpp.PointerPointer) FunctionPointer(org.bytedeco.javacpp.FunctionPointer) LongPointer(org.bytedeco.javacpp.LongPointer) ShortPointer(org.bytedeco.javacpp.ShortPointer) BoolPointer(org.bytedeco.javacpp.BoolPointer) DoublePointer(org.bytedeco.javacpp.DoublePointer) FloatPointer(org.bytedeco.javacpp.FloatPointer) Pointer(org.bytedeco.javacpp.Pointer) SizeTPointer(org.bytedeco.javacpp.SizeTPointer) ByPtrPtr(org.bytedeco.javacpp.annotation.ByPtrPtr) NoDeallocator(org.bytedeco.javacpp.annotation.NoDeallocator) Annotation(java.lang.annotation.Annotation) ByVal(org.bytedeco.javacpp.annotation.ByVal)

Example 8 with Pointer

use of org.bytedeco.javacpp.Pointer in project javacpp by bytedeco.

the class Generator method methods.

boolean methods(Class<?> cls) {
    if (!Loader.checkPlatform(cls, properties)) {
        return false;
    }
    Set<String> memberList = members.get(cls);
    if (!cls.isAnnotationPresent(Opaque.class) && cls != Loader.class && !FunctionPointer.class.isAssignableFrom(cls) && cls.getEnclosingClass() != Pointer.class) {
        if (memberList == null) {
            members.put(cls, memberList = new LinkedHashSet<String>());
        }
        if (!memberList.contains("sizeof")) {
            memberList.add("sizeof");
        }
    }
    boolean didSomething = false;
    for (Class<?> c : cls.getDeclaredClasses()) {
        if (Pointer.class.isAssignableFrom(c) || Pointer.class.isAssignableFrom(c.getEnclosingClass())) {
            didSomething |= methods(c);
        }
    }
    Method[] methods = cls.getDeclaredMethods();
    MethodInformation[] methodInfos = new MethodInformation[methods.length];
    for (int i = 0; i < methods.length; i++) {
        methodInfos[i] = methodInformation(methods[i]);
    }
    Class<?> c = cls.getSuperclass();
    while (c != null && c != Object.class) {
        // consider non-duplicate virtual functions from superclasses as well
        for (Method m : c.getDeclaredMethods()) {
            if (m.isAnnotationPresent(Virtual.class)) {
                boolean found = false;
                String name = m.getName();
                Class<?>[] types = m.getParameterTypes();
                for (Method m2 : methods) {
                    if (name.equals(m2.getName()) && Arrays.equals(types, m2.getParameterTypes())) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    methods = Arrays.copyOf(methods, methods.length + 1);
                    methods[methods.length - 1] = m;
                    methodInfos = Arrays.copyOf(methodInfos, methodInfos.length + 1);
                    methodInfos[methods.length - 1] = methodInformation(m);
                    methodInfos[methods.length - 1].cls = cls;
                }
            }
        }
        c = c.getSuperclass();
    }
    boolean[] callbackAllocators = new boolean[methods.length];
    Method[] functionMethods = functionMethods(cls, callbackAllocators);
    boolean firstCallback = true;
    for (int i = 0; i < methods.length; i++) {
        if (!Loader.checkPlatform(methods[i].getAnnotation(Platform.class), properties)) {
            continue;
        }
        MethodInformation methodInfo = methodInfos[i];
        String nativeName = mangle(cls.getName()) + "_" + mangle(methods[i].getName());
        String callbackName = callbackAllocators[i] && methodInfo.parameterTypes.length > 0 ? null : "JavaCPP_" + nativeName + "_callback";
        if (callbackAllocators[i] && functionMethods == null) {
            logger.warn("No callback method call() or apply() has been not declared in \"" + cls.getCanonicalName() + "\". No code will be generated for callback allocator.");
            continue;
        } else if (functionMethods != null) {
            for (Method functionMethod : functionMethods) {
                if (functionMethod != null && (callbackAllocators[i]) || (methods[i].equals(functionMethod) && !Modifier.isNative(methods[i].getModifiers()))) {
                    functions.index(cls);
                    Name name = methods[i].getAnnotation(Name.class);
                    if (name != null && name.value().length > 0 && name.value()[0].length() > 0) {
                        callbackName = name.value()[0];
                    }
                    callback(cls, functionMethod, callbackName, firstCallback, null);
                    firstCallback = false;
                    didSomething = true;
                }
            }
        }
        if ((Modifier.isNative(methods[i].getModifiers()) || Modifier.isAbstract(methods[i].getModifiers())) && !methodInfo.valueGetter && !methodInfo.valueSetter && !methodInfo.memberGetter && !methodInfo.memberSetter && !cls.isInterface() && (methods[i].isAnnotationPresent(Virtual.class) || methodInfo.allocator)) {
            callback(cls, methods[i], methodInfo.memberName[0], !methodInfo.allocator, methodInfo);
        }
        if (!Modifier.isNative(methods[i].getModifiers())) {
            continue;
        }
        if ((methodInfo.memberGetter || methodInfo.memberSetter) && !methodInfo.noOffset && memberList != null && !Modifier.isStatic(methodInfo.modifiers)) {
            if (!memberList.contains(methodInfo.memberName[0])) {
                memberList.add(methodInfo.memberName[0]);
            }
        }
        didSomething = true;
        out.print("JNIEXPORT " + jniTypeName(methodInfo.returnType) + " JNICALL Java_" + nativeName);
        if (methodInfo.overloaded) {
            out.print("__" + mangle(signature(methodInfo.parameterTypes)));
        }
        if (Modifier.isStatic(methodInfo.modifiers)) {
            out.print("(JNIEnv* env, jclass cls");
        } else {
            out.print("(JNIEnv* env, jobject obj");
        }
        for (int j = 0; j < methodInfo.parameterTypes.length; j++) {
            out.print(", " + jniTypeName(methodInfo.parameterTypes[j]) + " arg" + j);
        }
        out.println(") {");
        if (callbackAllocators[i]) {
            callbackAllocator(cls, callbackName);
            continue;
        } else if (!Modifier.isStatic(methodInfo.modifiers) && Pointer.class.isAssignableFrom(cls) && !methodInfo.allocator && !methodInfo.arrayAllocator && !methodInfo.deallocator) {
            // get our "this" pointer
            String[] typeName = cppTypeName(cls);
            if ("void*".equals(typeName[0]) && !cls.isAnnotationPresent(Opaque.class)) {
                typeName[0] = "char*";
            } else if (FunctionPointer.class.isAssignableFrom(cls)) {
                functions.index(cls);
                typeName[0] = functionClassName(cls) + "*";
                typeName[1] = "";
            }
            out.println("    " + typeName[0] + " ptr" + typeName[1] + " = (" + typeName[0] + typeName[1] + ")jlong_to_ptr(env->GetLongField(obj, JavaCPP_addressFID));");
            out.println("    if (ptr == NULL) {");
            out.println("        env->ThrowNew(JavaCPP_getClass(env, " + jclasses.index(NullPointerException.class) + "), \"This pointer address is NULL.\");");
            out.println("        return" + (methodInfo.returnType == void.class ? ";" : " 0;"));
            out.println("    }");
            if (FunctionPointer.class.isAssignableFrom(cls)) {
                out.println("    if (ptr->ptr == NULL) {");
                out.println("        env->ThrowNew(JavaCPP_getClass(env, " + jclasses.index(NullPointerException.class) + "), \"This function pointer address is NULL.\");");
                out.println("        return" + (methodInfo.returnType == void.class ? ";" : " 0;"));
                out.println("    }");
            }
            if (!cls.isAnnotationPresent(Opaque.class)) {
                out.println("    jlong position = env->GetLongField(obj, JavaCPP_positionFID);");
                out.println("    ptr += position;");
                if (methodInfo.bufferGetter) {
                    out.println("    jlong size = env->GetLongField(obj, JavaCPP_limitFID);");
                    out.println("    size -= position;");
                }
            }
        }
        parametersBefore(methodInfo);
        String returnPrefix = returnBefore(methodInfo);
        call(methodInfo, returnPrefix, false);
        returnAfter(methodInfo);
        parametersAfter(methodInfo);
        if (methodInfo.throwsException != null) {
            out.println("    if (exc != NULL) {");
            out.println("        env->Throw(exc);");
            out.println("    }");
        }
        if (methodInfo.returnType != void.class) {
            out.println("    return rarg;");
        }
        out.println("}");
    }
    out.println();
    return didSomething;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FunctionPointer(org.bytedeco.javacpp.FunctionPointer) Opaque(org.bytedeco.javacpp.annotation.Opaque) Loader(org.bytedeco.javacpp.Loader) CLongPointer(org.bytedeco.javacpp.CLongPointer) CharPointer(org.bytedeco.javacpp.CharPointer) IntPointer(org.bytedeco.javacpp.IntPointer) BytePointer(org.bytedeco.javacpp.BytePointer) PointerPointer(org.bytedeco.javacpp.PointerPointer) FunctionPointer(org.bytedeco.javacpp.FunctionPointer) LongPointer(org.bytedeco.javacpp.LongPointer) ShortPointer(org.bytedeco.javacpp.ShortPointer) BoolPointer(org.bytedeco.javacpp.BoolPointer) DoublePointer(org.bytedeco.javacpp.DoublePointer) FloatPointer(org.bytedeco.javacpp.FloatPointer) Pointer(org.bytedeco.javacpp.Pointer) SizeTPointer(org.bytedeco.javacpp.SizeTPointer) Virtual(org.bytedeco.javacpp.annotation.Virtual) Method(java.lang.reflect.Method) Name(org.bytedeco.javacpp.annotation.Name)

Example 9 with Pointer

use of org.bytedeco.javacpp.Pointer in project javacpp-presets by bytedeco.

the class openblas method blas_set_num_threads.

public static void blas_set_num_threads(int num) {
    Loader.load(openblas.class);
    Pointer mklSetNumThreads = Loader.addressof("MKL_Set_Num_Threads");
    Pointer mklSetNumThreadsLocal = Loader.addressof("MKL_Set_Num_Threads_Local");
    Pointer mklDomainSetNumThreads = Loader.addressof("MKL_Domain_Set_Num_Threads");
    Pointer openblasSetNumThreads = Loader.addressof("openblas_set_num_threads");
    vendor = 0;
    if (mklSetNumThreads != null) {
        new SetNumThreads().put(mklSetNumThreads).call(num);
        vendor = 3;
    }
    if (mklSetNumThreadsLocal != null) {
        new MklSetNumThreadsLocal().put(mklSetNumThreadsLocal).call(num);
        vendor = 3;
    }
    if (mklDomainSetNumThreads != null) {
        MklDomainSetNumThreads f = new MklDomainSetNumThreads().put(mklDomainSetNumThreads);
        // DOMAIN_ALL
        f.call(num, 0);
        // DOMAIN_BLAS
        f.call(num, 1);
        vendor = 3;
    }
    if (openblasSetNumThreads != null) {
        new SetNumThreads().put(openblasSetNumThreads).call(num);
        vendor = 2;
    }
    if (vendor != 0) {
        maxThreads = num;
    } else {
        System.out.println("Unable to tune runtime. Please set OMP_NUM_THREADS manually.");
    }
}
Also used : FunctionPointer(org.bytedeco.javacpp.FunctionPointer) Pointer(org.bytedeco.javacpp.Pointer)

Example 10 with Pointer

use of org.bytedeco.javacpp.Pointer in project javacv by bytedeco.

the class FFmpegStreamingTimeout method testWithCallback.

private static void testWithCallback() {
    try {
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(SOURCE_RTSP);
        /**
         * grabber.getFormatContext() is null before grabber.start().
         *
         * But if network is disabled grabber.start() will never return.
         *
         * That's why interrupt_callback not suitable for "network disabled
         * case".
         */
        grabber.start();
        final AtomicBoolean interruptFlag = new AtomicBoolean(false);
        avformat.AVIOInterruptCB.Callback_Pointer cp = new avformat.AVIOInterruptCB.Callback_Pointer() {

            @Override
            public int call(Pointer pointer) {
                // 0 - continue, 1 - exit
                int interruptFlagInt = interruptFlag.get() ? 1 : 0;
                System.out.println("callback, interrupt flag == " + interruptFlagInt);
                return interruptFlagInt;
            }
        };
        avformat.AVFormatContext oc = grabber.getFormatContext();
        avformat.avformat_alloc_context();
        avformat.AVIOInterruptCB cb = new avformat.AVIOInterruptCB();
        cb.callback(cp);
        oc.interrupt_callback(cb);
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(TIMEOUT);
                interruptFlag.set(true);
                System.out.println("interrupt flag was changed");
            } catch (InterruptedException ex) {
                System.out.println("exception in interruption thread: " + ex);
            }
        }).start();
        Frame frame = null;
        /**
         * On one of my RTSP cams grabber stops calling callback on
         * connection lost. I think it's has something to do with message:
         * "[swscaler @ 0000000029af49e0] deprecated pixel format used, make
         * sure you did set range correctly".
         *
         * So there is at least one case when grabber stops calling
         * callback.
         */
        while ((frame = grabber.grab()) != null) {
            System.out.println("frame grabbed at " + grabber.getTimestamp());
        }
        System.out.println("loop end with frame: " + frame);
    } catch (FrameGrabber.Exception ex) {
        System.out.println("exception: " + ex);
    }
    System.out.println("end");
}
Also used : Frame(org.bytedeco.javacv.Frame) Pointer(org.bytedeco.javacpp.Pointer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FFmpegFrameGrabber(org.bytedeco.javacv.FFmpegFrameGrabber) FrameGrabber(org.bytedeco.javacv.FrameGrabber) FFmpegFrameGrabber(org.bytedeco.javacv.FFmpegFrameGrabber) org.bytedeco.javacpp.avformat(org.bytedeco.javacpp.avformat)

Aggregations

Pointer (org.bytedeco.javacpp.Pointer)61 FloatPointer (org.bytedeco.javacpp.FloatPointer)29 DoublePointer (org.bytedeco.javacpp.DoublePointer)27 IntPointer (org.bytedeco.javacpp.IntPointer)23 CudaContext (org.nd4j.linalg.jcublas.context.CudaContext)23 INDArray (org.nd4j.linalg.api.ndarray.INDArray)21 CudaPointer (org.nd4j.jita.allocator.pointers.CudaPointer)19 BytePointer (org.bytedeco.javacpp.BytePointer)18 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)18 ShortPointer (org.bytedeco.javacpp.ShortPointer)16 GridExecutioner (org.nd4j.linalg.api.ops.executioner.GridExecutioner)16 PointerPointer (org.bytedeco.javacpp.PointerPointer)11 ByteBuffer (java.nio.ByteBuffer)10 CUstream_st (org.bytedeco.javacpp.cuda.CUstream_st)10 org.nd4j.jita.allocator.pointers.cuda.cusolverDnHandle_t (org.nd4j.jita.allocator.pointers.cuda.cusolverDnHandle_t)10 CublasPointer (org.nd4j.linalg.jcublas.CublasPointer)10 FunctionPointer (org.bytedeco.javacpp.FunctionPointer)9 BoolPointer (org.bytedeco.javacpp.BoolPointer)8 CLongPointer (org.bytedeco.javacpp.CLongPointer)8 CharPointer (org.bytedeco.javacpp.CharPointer)8