When using Java, C++, Swig, and Swig's directors I can pass a Java object that inherits a C++ class to C++. This works great.
Now, when I pass that same Java object back to Java from the C++ code, Swig creates a new Java object to wrap the C++ pointer. The problem with this is that the new object does not have the same type as the old object. I inherited the C++ class in Java and I need that Java object back.
Why do I want to do this? I have a pool of resources in Java and the C++ code is checking out these resources then returning them to the pool.
Follows is the SSCE:
Here's the C++ code that checks out the resource and returns it:
// c_backend.cpp
#include "c_backend.h"
#include <stdio.h>
void Server::doSomething( JobPool *jp ) {
printf("In doSomthing\n");
Person *person = jp->hireSomeone();
person->doSomeWorkForMe(3);
jp->returnToJobPool(person);
printf("exiting doSomthing\n");
}
Here's the Java code that overrides the C++ classes:
//JavaFrontend.java
import java.util.List;
import java.util.ArrayList;
public class JavaFrontend {
static {
System.loadLibrary("CBackend");
}
public static void main( String[] args ) {
JobPool jobPool = new JobPoolImpl();
new Server().doSomething(jobPool);
}
public static class JobPoolImpl extends JobPool {
private List<PersonImpl> people = new ArrayList<>();
public Person hireSomeone() {
if ( people.size() > 0 ) {
Person person = people.get(0);
people.remove(person);
return person;
} else {
System.out.println("returning new PersonImpl");
return new PersonImpl();
}
}
public void returnToJobPool(Person person) {
people.add((PersonImpl)person);
}
}
public static class PersonImpl extends Person {
public void doSomeWorkForMe(int i) {
System.out.println("Java working for me: "+i);
}
}
}
Here's the Swig interface file:
//c_backend.i
%module(directors="1") c_backend
%{
#include "c_backend.h"
%}
%feature("director") Person;
%feature("director") JobPool;
%include "c_backend.h"
And finally, the C++ header file with the base classes and then a Makefile that compiles it all:
// c_backend.h
#ifndef C_BACKEND_H
#define C_BACKEND_H
#include <stdio.h>
class Person {
public:
virtual ~Person() {}
virtual void doSomeWorkForMe(int i) {
printf("in C++ doSomeWorkForMe %i\n",i);
}
};
class JobPool {
public:
virtual ~JobPool() {}
virtual Person *hireSomeone() {
printf("in C++ hireSomeone\n");
return NULL;
}
virtual void returnToJobPool(Person *person) {
printf("in C++ returnToJobPool\n");
}
};
class Server {
public:
void doSomething( JobPool * );
};
#endif
The Makefile:
# Makefile
JAVA_INCLUDE=-I/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/include/darwin
all:
c++ -c c_backend.cpp
swig -java -c++ $(JAVA_INCLUDE) c_backend.i
c++ $(JAVA_INCLUDE) -c c_backend_wrap.cxx
c++ -dynamiclib -o libCBackend.jnilib *.o -framework JavaVM
javac *.java
clean:
rm -rf *.class *.o *_wrap.cxx *_wrap.h Server.java SWIGTYPE*.java c_backend*.java JobPool.java Person.java
Here's a snippet from the swig code that creates the new Java object replacing my original Java object:
public static void SwigDirector_JobPool_returnToJobPool(JobPool jself, long person) {
jself.returnToJobPool((person == 0) ? null : new Person(person, false));
}
How can I make this work without relying on maintaining a HashMap
inside Java?