I have a header file with a gazillion things in it and I need to call just a couple of them from Java - here is an example I cooked up (of course the real one is a monster and I cannot split it up as it is provided by a third party):
#ifndef _HIDING_H_
#define _HIDING_H_
template<class T> class SomeClassIWant {
public:
void yupWantThis();
T nopeDoNoWantThis();
};
class SomeClassIDoNotWant {
public:
void definatelyDoNotWantToCallThisFromJava();
};
SomeClassIWant<int> createTheClassIWant();
#endif
I figured, therefore that SWIG 3.0.2 would do the job nicely for me so I created the following SWIG interface file:
%module hiding;
%ignore "";
%rename("%s") createTheClassIWant;
%rename("%s") SomeClassIWant;
%rename(IntSomeClassIWant) SomeClassIWant<int>;
%rename("%s") SomeClassIWant<int>::yupWantThis;
%{
#include "hiding.h"
%}
%include "hiding.h"
%template(IntSomeClassIWant) SomeClassIWant<int>;
And I ran swig (swig -c++ -java hiding.i) and listed the *.java files:
hiding.java
hidingJNI.java
IntSomeClassIWant.java
OK, so the ignore everything and un-ignoring what I want seems to have worked except there is one fly in the ointment...
The IntSomeClassIWant.java file has the following in it:
/* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 3.0.2
*
* Do not make changes to this file unless you know what you are doing--modify
* the SWIG interface file instead.
* ----------------------------------------------------------------------------- */
public class IntSomeClassIWant {
private long swigCPtr;
protected boolean swigCMemOwn;
protected IntSomeClassIWant(long cPtr, boolean cMemoryOwn) {
swigCMemOwn = cMemoryOwn;
swigCPtr = cPtr;
}
protected static long getCPtr(IntSomeClassIWant obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}
public synchronized void delete() {
if (swigCPtr != 0) {
if (swigCMemOwn) {
swigCMemOwn = false;
throw new UnsupportedOperationException(
"C++ destructor does not have public access");
}
swigCPtr = 0;
}
}
public IntSomeClassIWant() {
this(hidingJNI.new_SomeClassIWant(), true);
}
}
There is no yupWantThis method available to me - I thought I had unignored that with the line %rename("%s") SomeClassIWant<int>::yupWantThis; but it seems not.
Try as I might, I cannot figure out how to get this method visible - perhaps I need to do something in the %template line or something, I am flummoxed, so...
How do I un-ignore a specific method on a templated class in SWIG?
It's not comprehensible research, but you can just reenable all entities named yupWantThis. This will include your method.
This should work:
%rename("%s") yupWantThis;
However, it will reenable all methods/classes named like this and i'm not sure if that works for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With