views:

288

answers:

1

I am trying to wrap a native C++ library using swig, and I am stuck at trying to convert time_t in C, to long in Java. I have successfully used swig with python, but so far I am unable to get the above typemap to work in Java. In python it looks like this

%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 guess the in map from Java to C would be:

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

How would I complete the out map from C to Java?

%typemap(out) time_t ???

Would I need typemaps like the ones below?

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

I need this in order to wrap C functions like this:

time_t manipulate_time (time_t dt);
+1  A: 

You should take a look at these sections of swig documentation:

There are also a lot of "examples" in the basic typemaps which are implemented for primitive types. You can find them in \swig\Lib\java\java.swg
I don't know if this working or not, but maybe something like this will suite your needs?

%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"
Dmitriy Matveev