From 66ed9ddbe3b9c2db182afb73683b16458a668dbc Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Fri, 29 Aug 2014 18:06:16 +0400 Subject: [PATCH] Added Ptr support to Java wrappers --- modules/java/generator/gen_java.py | 31 +++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/modules/java/generator/gen_java.py b/modules/java/generator/gen_java.py index a4df8175b..53b874979 100755 --- a/modules/java/generator/gen_java.py +++ b/modules/java/generator/gen_java.py @@ -879,6 +879,11 @@ public class %(jc)s { self.add_class_code_stream(name, classinfo.base) if classinfo.base: self.get_imports(name, classinfo.base) + type_dict["Ptr_"+name] = \ + { "j_type" : name, + "jn_type" : "long", "jn_args" : (("__int64", ".nativeObj"),), + "jni_name" : "Ptr<"+name+">(("+name+"*)%(n)s_nativeObj)", "jni_type" : "jlong", + "suffix" : "J" } def add_const(self, decl): # [ "const cname", val, [], [] ] @@ -1253,6 +1258,9 @@ extern "C" { j_prologue.append( j_type + ' retVal = new Array' + j_type+'();') self.classes[fi.classname or self.Module].imports.add('java.util.ArrayList') j_epilogue.append('Converters.Mat_to_' + ret_type + '(retValMat, retVal);') + elif ret_type.startswith("Ptr_"): + ret_val = type_dict[fi.ctype]["j_type"] + " retVal = new " + type_dict[ret_type]["j_type"] + "(" + tail = ")" elif ret_type == "void": ret_val = "" ret = "return;" @@ -1325,6 +1333,9 @@ extern "C" { default = 'return env->NewStringUTF("");' elif fi.ctype in self.classes: # wrapped class: ret = "return (jlong) new %s(_retval_);" % fi.ctype + elif fi.ctype.startswith('Ptr_'): + c_prologue.append("typedef Ptr<%s> %s;" % (fi.ctype[4:], fi.ctype)) + ret = "return (jlong)(new %(ctype)s(_retval_));" % { 'ctype':fi.ctype } elif ret_type in self.classes: # pointer to wrapped class: ret = "return (jlong) _retval_;" elif type_dict[fi.ctype]["jni_type"] == "jdoubleArray": @@ -1355,10 +1366,10 @@ extern "C" { elif fi.static: cvname = "%s::%s" % (fi.classname, name) else: - cvname = "me->" + name + cvname = ("me->" if not self.isSmartClass(fi.classname) else "(*me)->") + name c_prologue.append(\ "%(cls)s* me = (%(cls)s*) self; //TODO: check for NULL" \ - % { "cls" : fi.classname} \ + % { "cls" : self.smartWrap(fi.classname)} \ ) cvargs = [] for a in args: @@ -1511,9 +1522,23 @@ JNIEXPORT void JNICALL Java_org_opencv_%(module)s_%(j_cls)s_delete delete (%(cls)s*) self; } -""" % {"module" : module, "cls" : name, "j_cls" : ci.jname.replace('_', '_1')} +""" % {"module" : module, "cls" : self.smartWrap(name), "j_cls" : ci.jname.replace('_', '_1')} ) + def isSmartClass(self, classname): + ''' + Check if class stores Ptr* instead of T* in nativeObj field + ''' + return classname in self.classes and self.classes[classname].base + + def smartWrap(self, classname): + ''' + Wraps class name with Ptr<> if needed + ''' + if self.isSmartClass(classname): + return "Ptr<" + classname + ">" + return classname + if __name__ == "__main__": if len(sys.argv) < 4: