ext/dl/lib/dl/types.rb


DEFINITIONS

This source file includes following functions.


   1  # -*- ruby -*-
   2  
   3  require 'dl'
   4  
   5  module DL
   6    class Types
   7      TYPES = [
   8        # FORMAT:
   9        # ["alias name", "type name",
  10        #    encoding_method, decoding_method,   for function prototypes
  11        #    encoding_method, decoding_method]   for structures (not implemented)
  12        
  13        # for Windows
  14        ["DWORD",  "unsigned long", nil, nil, nil, nil],
  15        ["PDWORD", "unsigned long *", nil, nil, nil, nil],
  16        ["WORD",   "unsigned short", nil, nil, nil, nil],
  17        ["PWORD",  "unsigned int *", nil, nil, nil, nil],
  18        ["BOOL",   "ibool", nil, nil, nil, nil],
  19        ["ATOM",   "int", nil, nil, nil, nil],
  20        ["BYTE",   "unsigned char", nil, nil, nil, nil],
  21        ["PBYTE",  "unsigned char *", nil, nil, nil, nil],
  22        ["UINT",   "unsigned int", nil, nil, nil, nil],
  23        ["ULONG",  "unsigned long", nil, nil, nil, nil],
  24        ["UCHAR",  "unsigned char", nil, nil, nil, nil],
  25        ["HANDLE", "unsigned long", nil, nil, nil, nil],
  26        ["PHANDLE","void*", nil, nil, nil, nil],
  27        ["PVOID",  "void*", nil, nil, nil, nil],
  28        ["LPCSTR", "char*", nil, nil, nil, nil],
  29        ["HDC",    "unsigned int", nil, nil, nil, nil],
  30        ["HWND",   "unsigned int", nil, nil, nil, nil],
  31        
  32        # Others
  33        ["uint",   "unsigned int", nil, nil, nil, nil],
  34        ["u_int",  "unsigned int", nil, nil, nil, nil],
  35        ["ulong",  "unsigned long", nil, nil, nil, nil],
  36        ["u_long", "unsigned long", nil, nil, nil, nil],
  37        
  38        # DL::Importable primitive types
  39        ["ibool",   "I",
  40          proc{|v| v ? 1 : 0},
  41          proc{|v| (v != 0) ? true : false},
  42          nil, nil],
  43        ["cbool",   "C",
  44          proc{|v| v ? 1 : 0},
  45          proc{|v| (v != 0) ? true : false},
  46          nil, nil],
  47        ["lbool",   "L",
  48          proc{|v| v ? 1 : 0},
  49          proc{|v| (v != 0) ? true : false},
  50          nil, nil],
  51        ["unsigned char", "I",
  52          proc{|v| [v].pack("C").unpack("c")[0]},
  53          proc{|v| [v].pack("c").unpack("C")[0]},
  54          nil, nil],
  55        ["unsigned short", "H",
  56          proc{|v| [v].pack("S").unpack("s")[0]},
  57          proc{|v| [v].pack("s").unpack("S")[0]},
  58          nil, nil],
  59        ["unsigned int", "I",
  60          proc{|v| [v].pack("I").unpack("i")[0]},
  61          proc{|v| [v].pack("i").unpack("I")[0]},
  62          nil, nil],
  63        ["unsigned long", "L",
  64          proc{|v| [v].pack("L").unpack("l")[0]},
  65          proc{|v| [v].pack("l").unpack("L")[0]},
  66          nil, nil],
  67        ["unsigned char ref", "i",
  68          proc{|v| [v].pack("C").unpack("c")[0]},
  69          proc{|v| [v].pack("c").unpack("C")[0]},
  70          nil, nil],
  71        ["unsigned int ref", "i",
  72          proc{|v| [v].pack("I").unpack("i")[0]},
  73          proc{|v| [v].pack("i").unpack("I")[0]},
  74          nil, nil],
  75        ["unsigned long ref", "l",
  76          proc{|v| [v].pack("L").unpack("l")[0]},
  77          proc{|v| [v].pack("l").unpack("L")[0]},
  78          nil, nil],
  79        ["char ref",  "c", nil, nil, nil, nil],
  80        ["short ref", "h", nil, nil, nil, nil],
  81        ["int ref",   "i", nil, nil, nil, nil],
  82        ["long ref",  "l", nil, nil, nil, nil],
  83        ["float ref", "f", nil, nil, nil, nil],
  84        ["double ref","d", nil, nil, nil, nil],
  85        ["char",   "C", nil, nil, nil, nil],
  86        ["short",  "H", nil, nil, nil, nil],
  87        ["int",    "I", nil, nil, nil, nil],
  88        ["long",   "L", nil, nil, nil, nil],
  89        ["float",  "F", nil, nil, nil, nil],
  90        ["double", "D", nil, nil, nil, nil],
  91        [/char\s*\*/,"S",nil, nil, nil, nil],
  92        [/.+\*/,   "P", nil, nil, nil, nil],
  93        [/.+\[\]/, "a", nil, nil, nil, nil],
  94        ["void",   "0", nil, nil, nil, nil],
  95      ]
  96  
  97      def initialize
  98        init_types()
  99      end
 100  
 101      def typealias(ty1, ty2, enc=nil, dec=nil, senc=nil, sdec=nil)
 102        @TYDEFS.unshift([ty1,ty2, enc,dec, senc, sdec])
 103      end
 104  
 105      def init_types
 106        @TYDEFS = TYPES.dup
 107      end
 108  
 109      def encode_type(ty)
 110        orig_ty = ty
 111        enc = nil
 112        dec = nil
 113        senc = nil
 114        sdec = nil
 115        @TYDEFS.each{|t1,t2,c1,c2,c3,c4|
 116          if( t1.is_a?(String) )
 117            t1 = Regexp.new("^" + t1 + "$")
 118          end
 119          if( ty =~ t1 )
 120            ty = ty.gsub(t1,t2)
 121            if( enc )
 122              if( c1 )
 123                conv1 = enc
 124                enc = proc{|v| c1.call(conv1.call(v))}
 125              end
 126            else
 127              if( c1 )
 128                enc = c1
 129              end
 130            end
 131            if( dec )
 132              if( c2 )
 133                conv2 = dec
 134                dec = proc{|v| c2.call(conv2.call(v))}
 135              end
 136            else
 137              if( c2 )
 138                dec = c2
 139              end
 140            end
 141            if( senc )
 142              if( c3 )
 143                conv3 = senc
 144                senc = proc{|v| c3.call(conv3.call(v))}
 145              end
 146            else
 147              if( c3 )
 148                senc = c3
 149              end
 150            end
 151            if( sdec )
 152              if( c4 )
 153                conv4 = sdec
 154                sdec = proc{|v| c4.call(conv4.call(v))}
 155              end
 156            else
 157              if( c4 )
 158                sdec = c4
 159              end
 160            end
 161          end
 162        }
 163        ty = ty.strip
 164        if( ty.length != 1 )
 165          raise(TypeError, "unknown type: #{orig_ty}.")
 166        end
 167        return [ty,enc,dec,senc,sdec]
 168      end
 169    end # end of Types
 170  end