ext/dl/type.rb


DEFINITIONS

This source file includes following functions.


   1  # example:
   2  #  DLTYPE[INT][:rb2c]["arg0"] => "NUM2INT(arg0)"
   3  #  DLTYPE[DOUBLE][:c2rb]["r"] => "rb_float_new(r)"
   4  
   5  DLTYPE = {
   6    VOID  = 0x00 => {
   7      :name => 'VOID',
   8      :rb2c => nil,
   9      :c2rb => nil,
  10      :ctype => "void",
  11      :stmem => "v",
  12      :sym => true,
  13      :cb => true,
  14    },
  15    CHAR  = 0x01 => {
  16      :name => 'CHAR',
  17      :rb2c => proc{|x| "NUM2CHR(#{x})"},
  18      :c2rb => proc{|x| "CHR2FIX(#{x})"},
  19      :ctype => "char",
  20      :stmem => "c",
  21      :sym => false,
  22      :cb => false,
  23    },
  24    SHORT = 0x02 => {
  25      :name => 'SHORT',
  26      :rb2c => proc{|x| "FIX2INT(#{x})"},
  27      :c2rb => proc{|x| "INT2FIX(#{x})"},
  28      :ctype => "short",
  29      :stmem => "h",
  30      :sym => false,
  31      :cb => false,
  32    },
  33    INT   = 0x03 => {
  34      :name => 'INT',
  35      :rb2c => proc{|x| "NUM2INT(#{x})"},
  36      :c2rb => proc{|x| "INT2NUM(#{x})"},
  37      :ctype => "int",
  38      :stmem => "i",
  39      :sym => true,
  40      :cb => false,
  41    },
  42    LONG  = 0x04 => {
  43      :name => 'LONG',
  44      :rb2c => proc{|x| "NUM2INT(#{x})"},
  45      :c2rb => proc{|x| "INT2NUM(#{x})"},
  46      :ctype => "long",
  47      :stmem => "l",
  48      :sym => true,
  49      :cb => true,
  50    },
  51    FLOAT = 0x05 => {
  52      :name => 'FLOAT',
  53      :rb2c => proc{|x| "(float)(RFLOAT(#{x})->value)"},
  54      :c2rb => proc{|x| "rb_float_new((double)#{x})"},
  55      :ctype => "float",
  56      :stmem => "f",
  57      :sym => false,
  58      :cb => false,
  59    },
  60    DOUBLE = 0x06 => {
  61      :name => 'DOUBLE',
  62      :rb2c => proc{|x| "RFLOAT(#{x})->value"},
  63      :c2rb => proc{|x| "rb_float_new(#{x})"},
  64      :ctype => "double",
  65      :stmem => "d",
  66      :sym => true,
  67      :cb => true,
  68    },
  69    VOIDP = 0x07 => {
  70      :name => 'VOIDP',
  71      :rb2c => proc{|x| "rb_dlptr2cptr(#{x})"},
  72      :c2rb => proc{|x| "rb_dlptr_new(#{x},sizeof(void*),0)"},
  73      :ctype => "void *",
  74      :stmem => "p",
  75      :sym => true,
  76      :cb => true,
  77    },
  78  }
  79  
  80  def tpush(t, x)
  81    (t << 3)|x
  82  end
  83  
  84  def tget(t, i)
  85    (t & (0x07 << (i * 3))) >> (i * 3)
  86  end
  87  
  88  def types2num(types)
  89    res = 0x00
  90    r = types.reverse
  91    r.each{|t|
  92      res = tpush(res,t)
  93    }
  94    res
  95  end
  96  
  97  def num2types(num)
  98    ts = []
  99    i  = 0
 100    t = tget(num,i)
 101    while( (t != VOID && i > 0) || (i == 0) )
 102      ts.push(DLTYPE[t][:ctype])
 103      i += 1
 104      t = tget(num,i)
 105    end
 106    ts
 107  end
 108  
 109  def types2ctypes(types)
 110    res = []
 111    types.each{|t|
 112      res.push(DLTYPE[t][:ctype])
 113    }
 114    res
 115  end