I am trying to generate java bindings through swig for these two c++ functions
C++ functions
void SetUserData(void* data);
void* GetUserData() const;
On the java side I want it to look like this
setUserData(Object object);
getUserData() //return java.lang.Object
I would like the void* pointer to point to any java object I pass to it. In my swig file I tried to add these lines
//swig file
%typemap(jstype) void* "java.lang.Object";
%apply Object {void*};
I get a compile error: b2Fixture.swig(22) : Warning 453: Can't apply (Object). No typemaps are defined. I have no idea how to make typemaps for this. Any ideas?
For completeness, here's another (simpler?) way to do it with JavaCPP. Assuming we have a
GlobalData.h
file like this in C++:We can use it from Java this way:
Where the output shows that
someObject
andsameObject
point to the same object, for example:You probably want to do:
because %apply copies the typemaps defined on one C++ type to another C++ type.
Object
is a Java type, not a C++ one so doesn't have any typemaps to be copied.jobject
on the other hand is the JNI equivalent.Additionally assuming you want to have 'normal' GC semantics (i.e. not be required to retain whatever data you pass in) you'll need to do some more work, for example:
This makes a new Global Reference for your data, which could be the only thing keeping it from getting freed by the GC.
Given a header file:
and an implementation:
That is sufficient to allow:
to work correctly.
As written these typemaps are pretty ugly - they'll impact all usage of
void *
. So if you really used this you would want to use%apply
or%clear
a second time to limit their impact. You could also name the arguments in the header file and use that to limit where the typemap gets applied.