This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
Re: Integration with C
Geert Bevin writes:
>
> thanks for the answers.
>
> > Well, it depends on what exactly you mean. I suppose that you want to
> > call Java from C, and in turn you want Java to be able to call back
> > into C.
>
> What I want is a backend java library which can perform long operations
> in a background thread. A C/C++ GUI can link against this lib and
> register a number of callback hooks (as function pointers). Then a long
> operation is started in a seperate thread and the java library goes over
> the registered hooks (listeners) to report progress, messages, errors
> and finish. When the process is finished, the GUI is able to unregister
> the hooks too.
>
> This would make is possible to write a reponsive GUI, that remains
> usable by a user while a long operation is being executed in the background.
>
> > Define a Java Class thusly:
> >
> >
> > class Callback
> > {
> > RawData method;
> > public static native int foo (int bar, int baz);
> > }
> >
> > and the C++ thusly:
> >
> > typedef int (*fptr)(int, int):
> > jint foo (jint bar, jint baz)
> > {
> > fptr f = (fptr)method;
> > (*f)(bar, baz);
> > }
>
> I see how this would work, but my main question is how do you get the
> function point in the method variable. Here you only use it, you never
> provide it. Since the interface needs to be declared in java as being
> native, there's no way to write a method that takes a function pointer
> argument, is there?
I'm totally mystified by where you think the problem might be.
Declare a Java native method that takes a RawData arg, and cast a
function pointer to RawData. It's not legal C++, but it will work on
many architectures.
> Would it be possible to derive the Callback class in C++ alone and
> add a setCallback(fptr callback) method?
Sure, why not? You can write all of a Java class's implementation in
C++ if you wish.
> It would even be better if this would be possible in the Callback
> class inself, but I suppose that's not an option.
>
> > But why bother? It might be easier just to keep a cache of function
> > pointers somewhere and pass around indexes instead.
>
> So you'd store a list of function pointers in a C++ class and work with
> integer position identifiers instead. That feels a bit 'dirty' to me ;-)
Perhaps, but it is legal C++, and casting function pointers to data
pointers isn't.
> I like to work was as much typed information as possible.
You lose no typed information by doing this. Declare the array of
function pointers with the correct type.
Andrew.