An example of a simple sample map in swig java

I am trying to wrap my own C ++ library with swig, and I am stuck trying to convert time_t to C, to long in Java. I have successfully used swig with python, but so far I cannot get the above sample map to work in Java. In python, it looks like

 %typemap(in) time_t { if (PyLong_Check($input)) $1 = (time_t) PyLong_AsLong($input); else if (PyInt_Check($input)) $1 = (time_t) PyInt_AsLong($input); else if (PyFloat_Check($input)) $1 = (time_t) PyFloat_AsDouble($input); else { PyErr_SetString(PyExc_TypeError,"Expected a large number"); return NULL; } } %typemap(out) time_t { $result = PyLong_FromLong((long)$1); } 

I assume the map from Java to C will have:

 %typemap(in) time_t { $1 = (time_t) $input; } 

How can I complete a map display from C to Java?

 %typemap(out) time_t ??? 

Do I need typemaps types like below?

 %typemap(jni) %typemap(jtype) %typemap(jstype) 

I need this to wrap C functions as follows:

 time_t manipulate_time (time_t dt); 
+4
source share
2 answers

You should familiarize yourself with these sections of the swig documentation:

The basic examples that are implemented for primitive types also have many β€œexamples”. They can be found in the \ swig \ Lib \ java \ java.swg folder
I don’t know if this works or not, but maybe something like this will suit you?

 %typemap(jni) time_t "jlong" %typemap(jtype) time_t "long" %typemap(jstype) time_t "long" %typemap(out) time_t %{ $result = (jlong)$1; %} %typemap(in) time_t "(time_t)$input" 
+9
source

You can just do and not use typemaps.

 typedef long long time_t; 
0
source

Source: https://habr.com/ru/post/1309510/


All Articles