SWIG, Java and callbacks

View: New views
3 Messages — Rating Filter:   Alert me  

SWIG, Java and callbacks

by Ruediger Haertel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I have a set of functions (pure C) that need to make use of
callbacks into java code.
As basis I for my first tries I use the example that comes with the
tgz of swig-1.31. This example creates a caller object and a
callback object. The Java code registers the callback at the
caller. Then the callback can be executed from the C++ code.

To use this mechanism for my C code I see 2 possibilities:
1) Get access to the caller object and use the registered callback.
2) Have a global reference to a callback object and provide a
separate function to the Caller that sets this global reference.

I hope my understanding is correct so far. However my knowledge of
C++ is limited and my changes were not successful.

Here are some snippets for the second approach:

--- example.h

class Callback {
...
}

/* my global reference to the callback */
Callback *myCallback;

class Caller {
..
     void setMyVCallback(Callback *cb) { myCallback = cb;}
}

--- main.java

public class main {

   public static void main(String argv[]) {
     System.loadLibrary("example");
     
     Caller   caller = new Caller();
     
     callback = new JavaCallback();
     caller.setMyCallback(callback);
     caller.call();
   }
}

class JavaCallback extends Callback
{
...
}

---

I am thankful for any pointers.

Ruediger


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: SWIG, Java and callbacks

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Rüdiger Härtel wrote:

> Hello,
>
> I have a set of functions (pure C) that need to make use of
> callbacks into java code.
> As basis I for my first tries I use the example that comes with the
> tgz of swig-1.31. This example creates a caller object and a
> callback object. The Java code registers the callback at the
> caller. Then the callback can be executed from the C++ code.
>
> To use this mechanism for my C code I see 2 possibilities:
> 1) Get access to the caller object and use the registered callback.
> 2) Have a global reference to a callback object and provide a
> separate function to the Caller that sets this global reference.
>
> I hope my understanding is correct so far. However my knowledge of
> C++ is limited and my changes were not successful.
>
> Here are some snippets for the second approach:
>
> --- example.h
>
> class Callback {
> ...
> }
>
> /* my global reference to the callback */
> Callback *myCallback;
>
> class Caller {
> ..
>      void setMyVCallback(Callback *cb) { myCallback = cb;}
> }
>
> --- main.java
>
> public class main {
>
>    public static void main(String argv[]) {
>      System.loadLibrary("example");
>      
>      Caller   caller = new Caller();
>      
>      callback = new JavaCallback();
>      caller.setMyCallback(callback);
>      caller.call();
>    }
> }
>
> class JavaCallback extends Callback
> {
> ...
> }
>
> ---
>
> I am thankful for any pointers.
>

If you have to use C code and you want the easiest approach to getting
this going, then write a small global C++ callback function with C
linkage. Within the C++ callback function, just make a call to a global
SWIG director object, which in turn will make the call into Java. This
sounds like your idea 2).

Alternatively, take a working director C++ example that SWIG generates
(the one you were looking at for example) and manually downgrade it into
C code. The alternative is to write the JNI code from scratch, but then
you can't automate any code generation.

William

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: SWIG, Java and callbacks

by Prashanth BK :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
Can you post your solution for "pure C functions calling the callback function in Java".
I am having similar problem, no luck with suggested solution yet.
Thanks in advance.

Ruediger Haertel wrote:
Hello,

I have a set of functions (pure C) that need to make use of
callbacks into java code.
As basis I for my first tries I use the example that comes with the
tgz of swig-1.31. This example creates a caller object and a
callback object. The Java code registers the callback at the
caller. Then the callback can be executed from the C++ code.

To use this mechanism for my C code I see 2 possibilities:
1) Get access to the caller object and use the registered callback.
2) Have a global reference to a callback object and provide a
separate function to the Caller that sets this global reference.

I hope my understanding is correct so far. However my knowledge of
C++ is limited and my changes were not successful.

Here are some snippets for the second approach:

--- example.h

class Callback {
...
}

/* my global reference to the callback */
Callback *myCallback;

class Caller {
..
     void setMyVCallback(Callback *cb) { myCallback = cb;}
}

--- main.java

public class main {

   public static void main(String argv[]) {
     System.loadLibrary("example");
     
     Caller   caller = new Caller();
     
     callback = new JavaCallback();
     caller.setMyCallback(callback);
     caller.call();
   }
}

class JavaCallback extends Callback
{
...
}

---

I am thankful for any pointers.

Ruediger


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Swig-user mailing list
Swig-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user