class VariantBase {};
}
+%include "bindings/swig/templates.i"
+
/* Map between std::vector and java.util.Vector */
%define VECTOR(CValue, JValue)
%typemap(jstype) std::vector< CValue > "java.util.Vector<JValue>"
%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<JValue> in)
- {
- $javaclassname out = new $javaclassname();
- for (JValue value : in)
- out.add(value);
- return out;
- }
-%}
-
%typemap(javaout) std::vector< CValue > {
return (java.util.Vector<JValue>)$jnicall;
}
jclass Value = jenv->FindClass("org/sigrok/core/classes/" #JValue);
jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
$result = jenv->NewObject(Vector, Vector_init);
- jlong value;
+ jlong value = 0;
for (auto entry : $1)
{
*(CValue **) &value = new CValue(entry);
- jenv->CallObjectMethod($result, Vector_add,
+ jenv->CallBooleanMethod($result, Vector_add,
jenv->NewObject(Value, Value_init, value, true));
}
}
"java.util.Map<JKey, JValue>"
%typemap(javain,
- pre=" $javaclassname temp$javainput = $javaclassname.convertMap($javainput);",
+ pre=" $javaclassname temp$javainput = new $javaclassname();
+ for (java.util.Map.Entry<JKey, JValue> 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<JKey,JValue> in)
- {
- $javaclassname out = new $javaclassname();
- for (java.util.Map.Entry<JKey, JValue> entry : in.entrySet())
- out.set(entry.getKey(), entry.getValue());
- return out;
- }
-%}
-
%typemap(javaout) std::map< CKey, CValue > {
return (java.util.Map<JKey, JValue>)$jnicall;
}
jclass Value = jenv->FindClass("org/sigrok/core/classes/" #ClassName);
jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
$result = jenv->NewObject(HashMap, HashMap_init);
- jlong value;
+ jlong value = 0;
for (auto entry : $1)
{
*(std::shared_ptr< sigrok::ClassName > **)&value =
jclass Variant = jenv->FindClass("org/sigrok/core/classes/Variant");
jmethodID Variant_init = jenv->GetMethodID(Variant, "<init>", "(JZ)V");
$result = jenv->NewObject(HashMap, HashMap_init);
- jlong key;
- jlong value;
+ jlong key = 0;
+ jlong value = 0;
for (auto entry : $1)
{
*(const sigrok::ConfigKey **) &key = entry.first;
}
}
-/* Support Driver.scan() with no arguments. */
-%ignore sigrok::Driver::scan;
-
-%extend sigrok::Driver {
- std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan()
- {
- std::map<const sigrok::ConfigKey *, Glib::VariantBase> options;
- return $self->scan(options);
- }
+/* Pass JNIEnv parameter to C++ extension methods requiring it. */
- std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan(
- std::map<const sigrok::ConfigKey *, Glib::VariantBase> options)
- {
- return $self->scan(options);
- }
-}
+%typemap(in, numinputs=0) JNIEnv * %{
+ $1 = jenv;
+%}
-/* Support InputFormat.create_input() with or without options. */
-%ignore sigrok::InputFormat::create_input;
+/* Thread safe JNIEnv handling */
-%extend sigrok::InputFormat {
- std::shared_ptr<sigrok::Input> create_input()
- {
- return $self->create_input();
+%inline {
+namespace {
+ class ScopedEnv {
+ public:
+ ScopedEnv(JavaVM *jvm);
+ ~ScopedEnv();
+ JNIEnv* operator-> () { return env; }
+ operator bool () const { return (bool)env; }
+ private:
+ JavaVM *jvm;
+ JNIEnv *env;
+ int env_status;
+ };
+ ScopedEnv::ScopedEnv(JavaVM *jvm) : jvm(jvm), env(NULL) {
+ env_status = jvm->GetEnv((void **)&env, JNI_VERSION_1_2);
+ if (env_status == JNI_EDETACHED) {
+%#if defined(__ANDROID__)
+ jvm->AttachCurrentThread(&env, NULL);
+%#else
+ jvm->AttachCurrentThread((void **)&env, NULL);
+%#endif
+ }
}
-
- std::shared_ptr<sigrok::Input> create_input(
- std::map<std::string, Glib::VariantBase> options)
- {
- return $self->create_input(options);
+ ScopedEnv::~ScopedEnv() {
+ if (env_status == JNI_EDETACHED) {
+ jvm->DetachCurrentThread();
+ }
}
}
+}
-/* Support OutputFormat.create_output() with or without options. */
-%ignore sigrok::OutputFormat::create_output;
+/* "Smartpointer" for Java references. */
-%extend sigrok::OutputFormat {
- std::shared_ptr<sigrok::Output> create_output(
- std::shared_ptr<sigrok::Device> device)
+%inline {
+namespace {
+ class GlobalRefBase
{
- return $self->create_output(device);
+ protected:
+ GlobalRefBase (JavaVM *jvm, jobject ref);
+ ~GlobalRefBase ();
+ JavaVM *jvm;
+ jobject jref;
+ };
+ GlobalRefBase::GlobalRefBase (JavaVM *jvm, jobject ref) : jvm(jvm), jref(0) {
+ ScopedEnv env(jvm);
+ if (env && ref)
+ jref = env->NewGlobalRef(ref);
}
-
- std::shared_ptr<sigrok::Output> create_output(
- std::shared_ptr<sigrok::Device> device,
- std::map<std::string, Glib::VariantBase> options)
- {
- return $self->create_output(device, options);
+ GlobalRefBase::~GlobalRefBase () {
+ ScopedEnv env(jvm);
+ if(env && jref)
+ env->DeleteGlobalRef(jref);
}
-
- std::shared_ptr<sigrok::Output> create_output(
- std::string filename,
- std::shared_ptr<sigrok::Device> device)
+ template <class Jtype>
+ class GlobalRef : private GlobalRefBase
{
- return $self->create_output(filename, device);
- }
-
- std::shared_ptr<sigrok::Output> create_output(
- std::string filename,
- std::shared_ptr<sigrok::Device> device,
- std::map<std::string, Glib::VariantBase> options)
- {
- return $self->create_output(filename, device, options);
- }
+ public:
+ GlobalRef (JavaVM *jvm, Jtype ref) : GlobalRefBase(jvm, ref) {}
+ GlobalRef (const GlobalRef &ref) : GlobalRefBase(ref.jvm, ref.jref) {}
+ operator Jtype () const { return static_cast<Jtype>(jref); }
+ };
+}
}
-
-/* Pass JNIEnv parameter to C++ extension methods requiring it. */
-
-%typemap(in, numinputs=0) JNIEnv * %{
- $1 = jenv;
-%}
/* Support Java log callbacks. */
{
void add_log_callback(JNIEnv *env, jlogcallback obj)
{
+ JavaVM *jvm = NULL;
+ env->GetJavaVM(&jvm);
jclass obj_class = env->GetObjectClass(obj);
jmethodID method = env->GetMethodID(obj_class, "run",
"(Lorg/sigrok/core/classes/LogLevel;Ljava/lang/String;)V");
- jclass LogLevel = (jclass) env->NewGlobalRef(
- env->FindClass("org/sigrok/core/classes/LogLevel"));
+ GlobalRef<jclass> LogLevel(jvm, env->FindClass("org/sigrok/core/classes/LogLevel"));
jmethodID LogLevel_init = env->GetMethodID(LogLevel, "<init>", "(JZ)V");
- jobject obj_ref = env->NewGlobalRef(obj);
+ GlobalRef<jobject> obj_ref(jvm, obj);
$self->set_log_callback([=] (
const sigrok::LogLevel *loglevel,
std::string message)
{
- jlong loglevel_addr;
+ ScopedEnv env(jvm);
+ if (!env)
+ throw sigrok::Error(SR_ERR);
+ jlong loglevel_addr = 0;
*(const sigrok::LogLevel **) &loglevel_addr = loglevel;
jobject loglevel_obj = env->NewObject(
LogLevel, LogLevel_init, loglevel_addr, false);
{
void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj)
{
+ JavaVM *jvm = NULL;
+ env->GetJavaVM(&jvm);
jclass obj_class = env->GetObjectClass(obj);
jmethodID method = env->GetMethodID(obj_class, "run",
"(Lorg/sigrok/core/classes/Device;Lorg/sigrok/core/classes/Packet;)V");
- jclass Device = (jclass) env->NewGlobalRef(
- env->FindClass("org/sigrok/core/classes/Device"));
+ GlobalRef<jclass> Device(jvm, env->FindClass("org/sigrok/core/classes/Device"));
jmethodID Device_init = env->GetMethodID(Device, "<init>", "(JZ)V");
- jclass Packet = (jclass) env->NewGlobalRef(
- env->FindClass("org/sigrok/core/classes/Packet"));
+ GlobalRef<jclass> Packet(jvm, env->FindClass("org/sigrok/core/classes/Packet"));
jmethodID Packet_init = env->GetMethodID(Packet, "<init>", "(JZ)V");
- jobject obj_ref = env->NewGlobalRef(obj);
+ GlobalRef<jobject> obj_ref(jvm, obj);
$self->add_datafeed_callback([=] (
std::shared_ptr<sigrok::Device> device,
std::shared_ptr<sigrok::Packet> packet)
{
- jlong device_addr;
- jlong packet_addr;
+ ScopedEnv env(jvm);
+ if (!env)
+ throw sigrok::Error(SR_ERR);
+ jlong device_addr = 0;
+ jlong packet_addr = 0;
*(std::shared_ptr<sigrok::Device> **) &device_addr =
new std::shared_ptr<sigrok::Device>(device);
*(std::shared_ptr<sigrok::Packet> **) &packet_addr =
%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"