X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fjava%2Forg%2Fsigrok%2Fcore%2Fclasses%2Fclasses.i;h=7a61bfd55d2610b7bfa5b0da333b7868fbfaaeaa;hb=865de99391194e45a0e80ea534fc5e27d5287925;hp=9979a61828c680be0091e5082003c71fe0738f8d;hpb=52ff4f6a04cbdb966ee538bd447ae206215b528a;p=libsigrok.git diff --git a/bindings/java/org/sigrok/core/classes/classes.i b/bindings/java/org/sigrok/core/classes/classes.i index 9979a618..7a61bfd5 100644 --- a/bindings/java/org/sigrok/core/classes/classes.i +++ b/bindings/java/org/sigrok/core/classes/classes.i @@ -36,27 +36,16 @@ import org.sigrok.core.interfaces.LogCallback; import org.sigrok.core.interfaces.DatafeedCallback; -import org.sigrok.core.interfaces.SourceCallback; %} -/* Map Java FileDescriptor objects to int fds */ -%typemap(jni) int fd "jobject" -%typemap(jtype) int fd "java.io.FileDescriptor" -%typemap(jstype) int fd "java.io.FileDescriptor" -%typemap(javain) int fd "$javainput" - -%typemap(in) int fd { - jclass FileDescriptor = jenv->FindClass("java/io/FileDescriptor"); - jfieldID fd = jenv->GetFieldID(FileDescriptor, "fd", "I"); - $1 = jenv->GetIntField($input, fd); -} - /* Map Glib::VariantBase to a Variant class in Java */ %rename(Variant) VariantBase; namespace Glib { class VariantBase {}; } +%include "bindings/swig/templates.i" + /* Map between std::vector and java.util.Vector */ %define VECTOR(CValue, JValue) @@ -65,20 +54,12 @@ namespace Glib { %typemap(jstype) std::vector< CValue > "java.util.Vector" %typemap(javain, - pre=" $javaclassname temp$javainput = $javaclassname.convertVector($javainput);", + pre=" $javaclassname temp$javainput = new $javaclassname(); + for (JValue value : $javainput) + temp$javainput.add(value);", pgcppname="temp$javainput") std::vector< CValue > "$javaclassname.getCPtr(temp$javainput)" -%typemap(javacode) std::vector< CValue > %{ - static $javaclassname convertVector(java.util.Vector in) - { - $javaclassname out = new $javaclassname(); - for (JValue value : in) - out.add(value); - return out; - } -%} - %typemap(javaout) std::vector< CValue > { return (java.util.Vector)$jnicall; } @@ -94,7 +75,7 @@ namespace Glib { jlong value; for (auto entry : $1) { - *(CValue **) &value = new CValue(entry); + value = reinterpret_cast(new CValue(entry)); jenv->CallObjectMethod($result, Vector_add, jenv->NewObject(Value, Value_init, value, true)); } @@ -113,20 +94,12 @@ VECTOR(std::shared_ptr, HardwareDevice) "java.util.Map" %typemap(javain, - pre=" $javaclassname temp$javainput = $javaclassname.convertMap($javainput);", + pre=" $javaclassname temp$javainput = new $javaclassname(); + for (java.util.Map.Entry entry : $javainput.entrySet()) + temp$javainput.set(entry.getKey(), entry.getValue());", pgcppname="temp$javainput") std::map< CKey, CValue > "$javaclassname.getCPtr(temp$javainput)" -%typemap(javacode) std::map< CKey, CValue > %{ - static $javaclassname convertMap(java.util.Map in) - { - $javaclassname out = new $javaclassname(); - for (java.util.Map.Entry entry : in.entrySet()) - out.set(entry.getKey(), entry.getValue()); - return out; - } -%} - %typemap(javaout) std::map< CKey, CValue > { return (java.util.Map)$jnicall; } @@ -176,8 +149,7 @@ MAP_COMMON(std::string, std::shared_ptr, String, ClassName) jlong value; for (auto entry : $1) { - *(std::shared_ptr< sigrok::ClassName > **)&value = - new std::shared_ptr< sigrok::ClassName>(entry.second); + value = reinterpret_cast(new std::shared_ptr< sigrok::ClassName>(entry.second)); jenv->CallObjectMethod($result, HashMap_put, jenv->NewStringUTF(entry.first.c_str()), jenv->NewObject(Value, Value_init, value, true)); @@ -212,39 +184,49 @@ MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant) jlong value; for (auto entry : $1) { - *(const sigrok::ConfigKey **) &key = entry.first; - *(Glib::VariantBase **) &value = new Glib::VariantBase(entry.second); + key = reinterpret_cast(entry.first); + value = reinterpret_cast(new Glib::VariantBase(entry.second)); jenv->CallObjectMethod($result, HashMap_put, jenv->NewObject(ConfigKey, ConfigKey_init, key, false)); jenv->NewObject(Variant, Variant_init, value, true)); } } -/* Support Driver.scan() with no arguments. */ -%extend sigrok::Driver { - std::vector > scan() - { - std::map options; - return $self->scan(options); - } -} +/* Specialisation for ConfigKey->set maps */ -/* Support InputFormat.create_input() with no options. */ -%extend sigrok::InputFormat { - std::shared_ptr create_input() - { - std::map options; - return $self->create_input(options); - } -} +MAP_COMMON(const sigrok::ConfigKey *, std::set, + ConfigKey, java.util.Set) + +%typemap(jni) std::map > "jobject" +%typemap(jtype) std::map > + "java.util.Map>" -/* Support OutputFormat.create_output(device) with no options. */ -%extend sigrok::OutputFormat { - std::shared_ptr create_output( - std::shared_ptr device) +%typemap(out) std::map > { + jclass HashMap = jenv->FindClass("java/util/HashMap"); + jmethodID HashMap_init = jenv->GetMethodID(HashMap, "", "()V"); + jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put", + "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); + jclass HashSet = jenv->FindClass("java/util/HashSet"); + jmethodID HashSet_init = jenv->GetMethodID(HashSet, "", "()V"); + jmethodID HashSet_add = jenv->GetMethodID(HashSet, "add", + "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); + jclass ConfigKey = jenv->FindClass("org/sigrok/core/classes/ConfigKey"); + jmethodID ConfigKey_init = jenv->GetMethodID(ConfigKey, "", "(JZ)V"); + jclass Capability = jenv->FindClass("org/sigrok/core/classes/Capability"); + jmethodID Capability_swigToEnum = jenv->GetStaticMethodID(Capability, + "swigToEnum", "(I)Lorg/sigrok/core/classes/Capability;"); + $result = jenv->NewObject(HashMap, HashMap_init); + jlong key; + for (auto map_entry : $1) { - std::map options; - return $self->create_output(device, options); + key = reinterpret_cast(map_entry.first); + jobject value = jenv->NewObject(HashSet, HashSet_init); + for (auto &set_entry : map_entry.second) + jenv->CallObjectMethod(value, HashSet_add, + jenv->CallStaticObjectMethod(Capability, + Capability_swigToEnum, set_entry)); + jenv->CallObjectMethod($result, HashMap_put, + jenv->NewObject(ConfigKey, ConfigKey_init, key, false), value); } } @@ -285,7 +267,7 @@ typedef jobject jlogcallback; std::string message) { jlong loglevel_addr; - *(const sigrok::LogLevel **) &loglevel_addr = loglevel; + loglevel_addr = reinterpret_cast(loglevel); jobject loglevel_obj = env->NewObject( LogLevel, LogLevel_init, loglevel_addr, false); jobject message_obj = env->NewStringUTF(message.c_str()); @@ -331,10 +313,8 @@ typedef jobject jdatafeedcallback; { jlong device_addr; jlong packet_addr; - *(std::shared_ptr **) &device_addr = - new std::shared_ptr(device); - *(std::shared_ptr **) &packet_addr = - new std::shared_ptr(packet); + device_addr = reinterpret_cast(new std::shared_ptr(device)); + packet_addr = reinterpret_cast(new std::shared_ptr(packet)); jobject device_obj = env->NewObject( Device, Device_init, device_addr, true); jobject packet_obj = env->NewObject( @@ -346,56 +326,15 @@ typedef jobject jdatafeedcallback; } } -/* Support Java event source callbacks. */ - -%typemap(javaimports) sigrok::EventSource - "import org.sigrok.core.interfaces.SourceCallback;" - -%inline { -typedef jobject jsourcecallback; -} - -%typemap(jni) jsourcecallback "jsourcecallback" -%typemap(jtype) jsourcecallback "SourceCallback" -%typemap(jstype) jsourcecallback "SourceCallback" -%typemap(javain) jsourcecallback "$javainput" - -%extend sigrok::EventSource -{ - std::shared_ptr create( - int fd, Glib::IOCondition events, int timeout, - JNIEnv *env, jsourcecallback obj) - { - (void) $self; - jclass obj_class = env->GetObjectClass(obj); - jmethodID method = env->GetMethodID(obj_class, "run", "(I)V"); - jobject obj_ref = env->NewGlobalRef(obj); - - return sigrok::EventSource::create(fd, events, timeout, [=] (int revents) - { - bool result = env->CallBooleanMethod(obj_ref, method, revents); - if (env->ExceptionCheck()) - throw sigrok::Error(SR_ERR); - return result; - }); - } -} - %include "doc.i" -%define %attributevector(Class, Type, Name, Get) -%attributeval(sigrok::Class, Type, Name, Get); -%enddef - -%define %attributemap(Class, Type, Name, Get) -%attributeval(sigrok::Class, Type, Name, Get); -%enddef - %define %enumextras(Class) %enddef -/* Ignore this for now, needs a fix. */ +/* Ignore these for now, need fixes. */ %ignore sigrok::Context::create_analog_packet; +%ignore sigrok::Context::create_meta_packet; +%ignore sigrok::Meta::config; %include "bindings/swig/classes.i"