Typedefs, Ruby and SWIG

I am trying to create a Ruby wrapper for some C ++ classes. Generation succeeds, all methods are created, but the problem is that one of the C ++ methods uses this:

#ifdef USE_LONGLONG_COUNTS
typedef unsigned long long Count;   /* a count of something */
#else
typedef unsigned long Count;        /* a count of something */
#endif

When I run a method inside irb that returns a graph, I get something like this:

irb(main):006:0> ngram.numNgrams(0)
=> #<SWIG::TYPE_p_Count:0x00000001c52280>

I was expecting a number ... I tried to use reflection to see if I could somehow get the price, but not the cigar. Any suggestion?

+4
source share
2 answers

typedef SWIG , Counter - unboxed, . Counter , .

SWIG, Counter long long long.

typemap - SWIG. , SWIG typedef, Counter .

. :

// hacking.i
%module hacking
%{
// "Simulation" of the header included verbatim in the glue code.
#ifdef USE_LONGLONG_COUNTS
typedef unsigned long long Count;   /* a count of something */
#else
typedef unsigned long Count;        /* a count of something */
#endif
Count foo(Count count) { return count; }
%}

// The typemaps...
%typemap(in) Count n {
#ifdef USE_LONGLONG_COUNTS
 $1 = ULL2INT($input);
#else
 $1 = ULONG2INT($input);
#endif
}

%typemap(out) Count {
#ifdef USE_LONGLONG_COUNTS
 $result = ULL2NUM($1);
#else
 $result = ULONG2NUM($1);
#endif
}

// Now tell SWIG about the type equivalences and function prototype.
#ifdef USE_LONGLONG_COUNTS
typedef unsigned long long Count;   /* a count of something */
#else
typedef unsigned long Count;        /* a count of something */
#endif
Count foo(Count count);

, USE_LONGLONG_COUNTS , Ruby long long. .

, . , . Ruby SWIG docs, , .

:

# extconf.rb
require 'mkmf'
create_makefile('hacking')

( -DUSE_LONGLONGCOUNTS, unsigned long):

$ swig -c++ -DUSE_LONGLONG_COUNTS -ruby -o wrap.cpp hacking.i
$ ruby extconf.rb
$ make
compiling wrap.cpp
linking shared-object hacking.bundle
$ make install
/usr/bin/install -c -m 0755 hacking.bundle ...  
$ irb
2.2.1 :001 > require 'hacking'
 => true 
2.2.1 :002 > Hacking.foo(1234567890123454567)
 => 1234567890123454567 
+2

.

, swig "" typedef of Count? Count - - , , C, swig .

, swig 3.0.7, ( typemaps):

dummy.i( #define USE_LONGLONG_COUNTS )

%module dummy

%inline %{

#define USE_LONGLONG_COUNTS

#ifdef USE_LONGLONG_COUNTS
typedef unsigned long long Count;   /* a count of something */
#else
typedef unsigned long Count;        /* a count of something */
#endif

Count returnCount(Count count) { return count; }

%}

extconf.rb

require 'mkmf'
create_makefile('dummy')

ruby, :

swig -c++ -ruby dummy.i
ruby extconf.rb
make
irb

irb :

irb(main):001:0> require_relative 'dummy'
=> true
irb(main):002:0> Dummy.returnCount(2**64-1)
=> 18446744073709551615
irb(main):003:0> Dummy.returnCount(-1)
=> 18446744073709551615
irb(main):004:0> Dummy.returnCount(2**64)
TypeError: Expected argument 0 of type Count, but got Bignum 18446744073709551616
in SWIG method 'returnCount'
from (irb):4:in `returnCount'
from (irb):4
from /usr/bin/irb:12:in `<main>'

, unsigned long long, unsigned long - 64 /, ruby. , , , .

typedef, .

+1

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


All Articles