Hello everyone.

 

In C, I have a fonction :

 

void set_p_callback( void (*)(log_t level, const char *message));

 

Which set a function pointer for writing log. Another function uses this pointer to write the log with the given function.

Those functions are wrapped in C++ in a class Log.

We use SWIG to create bindings to different language (Python, Perl ...) and I have to add the Java bindings.

 

To implements this features, my plan was to create an "abstract" class in C++, Log_Interface, and create a Java class which inherit of it, and overload the run function

by the code written by the developer.

 

To do that, I use the “director” feature of swig on Log_Interface

 

The code compile, the shared library is generated and loaded without problem,

but I cannot set the global variable "log_p", pointing to the Log_Interface’s son object, even when I use the setter generated by SWIG.

 

When I run the test.java, I get “No log function set” and the log message.

 

I have read the doc and try to follow it but I still have no idea where my mistake is and could use some help.

 

 

I don’t think that matters but I’m on CentOS 6.4 x86_64 and use swig 1.3.40 (the last one on the repo).

 

bindings-java.i

 

%module(director="1") bindingsjava

 

%inline %{

 

class Log_Interface{

            virtual ~LogInterface(){}

            virtual void run(int level, char *message) {}

}

 

Log_Interface * log_p = NULL;

 

            void callback_proxy(int level, char *message){

 

                        std::cout << "C++ callback proxy : " << message << "." <<std::endl;

                        if(log_p != NULL)

                                   log_p->run(level, message);

                        else{

                                   std::cerr << "No log function set" << std::endl;

                                   std::cout << "Log : " << message << "." <<std::endl;

                        }

            }

           

            void setcallback(Log_Interface *LI){

                        log_p = LI;

                        set_p_callback((void (*)(log_t, const char *)) &callback_proxy);

            }

           

%}

 

feature("director") Log_Interface;

 

test.java

 

public class test{

 

            static{

            try{

                        System.loadLibrary("bindingsjava");

            }catch(UnsatisfiedLinkError e){

                        System.err.println("Native code library failed to load. "+e);

                        System.exit(1);

            }

            }

           

            static void main(String argv[]){

                        Log_cb javalog = new Log_cb();

                        javalog.run(1,"Hello World !!");

                       

                        com.project.easy.Easy.setLog_p(javalog);

                        com.project.easy.Easy.setcallback(javalog);

                        ...

            }

 

}

 

class Log_cb extends Log_Interface{

 

            public Log_cb(){}

           

            public void run(int level, String message){

                        System.out.println("JAVA level " + level + " : " + message + ".");

            }

 

}