1

I am using Swig to wrap a c library for use with Ruby. There are several functions that return an array of of uint32_t and uint64_t as pointers.

For example something like:

const uint64_t *foo();
const uint32_t *bar();

I feel I should be able to map these into ruby arrays of Integer with a %typemap, but can't find a good example.

The default mapping is returning a swig pointer , ie #<SWIG::TYPE_p_unsigned_long_long:0x0000000002204200>

including stdint.i via %include "stdint.i" seems to enable handling the uint64_t, but not the pointers to them.

Update

I tried the following typemap. And it seems to work, is there a better way?

%typemap(out) const uint64_t* {
   VALUE arr;
   arr = rb_ary_new();
   VALUE v1 = INT2NUM(*$1);
   VALUE v2 = INT2NUM(*($1+1));
   rb_ary_push(arr, v1 );
   rb_ary_push(arr, v2 );
   $result = arr;
}
nPn
  • 16,254
  • 9
  • 35
  • 58
  • Are these single element pointers, or pointers to arrays? – tadman Mar 03 '20 at 00:11
  • 1
    pointers to arrays, for example the documentation for one of the functions that returns `const uint64_t *` says it returns a pair of elements. – nPn Mar 03 '20 at 00:52
  • Doing a blanket mapping here for all pointers of that type seems like trouble, that could wildly misinterpret things, but it's a start. What about writing a thin C wrapper around those functions to bridge it? – tadman Mar 03 '20 at 01:35
  • 1
    yes, I had the same concern. I was thinking of doing a %clear after the definition that needs it, moving the typemap and definition to the end of the file, or as you say, just writing a wrapper for those few functions. – nPn Mar 03 '20 at 02:12

0 Answers0