The Problem
I'm working on an Android application in which I have to pass a OpenCV Mat between the Java code an the C++ code. For this purpose I created the following SWIG typemaps which are working fine:
%include "std_map.i"
%include "std_shared_ptr.i"
...
%shared_ptr(cv::Mat)
...
// normal typemaps for cv::Mat w/o shared_ptr
%typemap(jstype) cv::Mat, cv::Mat& "org.opencv.core.Mat"
%typemap(javain) cv::Mat, cv::Mat& "$javainput.getNativeObjAddr()"
%typemap(jtype) cv::Mat, cv::Mat& "long"
%typemap(jni) cv::Mat, cv::Mat& "jlong"
%typemap(in) cv::Mat, cv::Mat& {
$1 = *(cv::Mat **)&$input;
}
%typemap(javaout) cv::Mat, cv::Mat& {
return new org.opencv.core.Mat($jnicall);
}
// rather hacky javaout typemap override for the shared_ptr
%typemap(javaout) std::shared_ptr< cv::Mat > {
long cPtr = $jnicall;
return (cPtr == 0) ? null : new org.opencv.core.Mat(cPtr);
}
Anyhow, at some point I have to return a std::map<std::string, std::shared_ptr<cv::Mat>>
to Java. I did this using the map template with
%template(Map_String_Shared_ptr_Mat) std::map<std::string, std::shared_ptr<cv::Mat>>;
which produces the following get
method in Java:
public org.opencv.core.Mat get(String key) {
long cPtr = xyJNI.Map_String_Shared_ptr_Mat_get(swigCPtr, this, key);
return (cPtr == 0) ? null : new org.opencv.core.Mat(cPtr, true);
}
which is not using the javaout typemap provided earlier. (It works on a function returning a std::shared_ptr<cv::Mat>
but not in the map template)
What I've tried so far
I tried to insert my own get
method via
%typemap(javacode) std::map<std::string, std::shared_ptr<cv::Mat>> %{
public org.opencv.Mat get{
...
}
%};
but this results in a conflict since two get methods exist.
Also, when I tried to %ignore
the get
method first, the corresponding xyJNI.Map_String_Shared_ptr_Mat_get(swigCPtr, this, key);
is not created, and therefor I can't provide my own implementation of the get
The Question
Now I need either a way to tell SWIG to apply that javaout typemap. But I would also be fine with a way of overriding the get
method body, to use the correct constructor for a Mat.
I hope someone can help me with this issue
Note: I don't care about Map_String_Shared_ptr_Mat
not being a real Map
in Java. That's not a problem for me
Edit: Added the shared_ptr javaout typemap