How to handle ptrdiff_t in SWIG generated Java wrapper?

102 Views Asked by At

I have this struct in C:

typedef struct THTensor {
  ...
  ptrdiff_t storageOffset;
  ...
} THTensor;

However, the SWIG-generated Java code is:

public SWIGTYPE_p_ptrdiff_t getStorageOffset() {
    return new SWIGTYPE_p_ptrdiff_t(THJNI.THFloatTensor_storageOffset_get(this.swigCPtr, this), true);
}

I'd like that ptrdiff_t is converted to long in Java, not this SWIGTYPE_p_ptrdiff_t, in which I cannot access the actual long value.

How can I control this in SWIG?

1

There are 1 best solutions below

4
On BEST ANSWER

There are several options... But ptrdiff_t is unknown to SWIG and to define it just as long is not the best idea.
I would do the following: add %include <stdint.i> to the interface file and then either add in the interface file:

%define ptrdiff_t
intptr_t
%enddef

or add in the source code:

#ifdef SWIG
  %define ptrdiff_t
  intptr_t
  %enddef
#endif // SWIG
...
typedef struct THTensor {
  ...
  ptrdiff_t storageOffset;
  ...
} THTensor;

In such way, the code being wrapped in the interface should have appropriate interpretation of ptrdiff_t, not just an opaque pointer.