PageRenderTime 108ms CodeModel.GetById 52ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/dl/types.rb

http://github.com/agross/netopenspace
Ruby | 245 lines | 226 code | 11 blank | 8 comment | 30 complexity | 0d62b35cea75ae04163047b390b02655 MD5 | raw file
  1# -*- ruby -*-
  2
  3require 'dl'
  4
  5module DL
  6  class Types
  7    TYPES = [
  8      # FORMAT:
  9      # ["alias name",
 10      #  "type name", encoding_method, decoding_method,   for function prototypes
 11      #  "type name", encoding_method, decoding_method]   for structures (not implemented)
 12      
 13      # for Windows
 14      ["DWORD",  "unsigned long", nil, nil,
 15                 "unsigned long", nil, nil],
 16      ["PDWORD", "unsigned long *", nil, nil,
 17                 "unsigned long *", nil, nil],
 18      ["WORD",   "unsigned short", nil, nil,
 19                 "unsigned short", nil, nil],
 20      ["PWORD",  "unsigned int *", nil, nil,
 21                 "unsigned int *", nil, nil],
 22      ["BYTE",   "unsigned char",   nil, nil,
 23                 "unsigned char", nil, nil],
 24      ["PBYTE",  "unsigned char *", nil, nil,
 25                 "unsigned char *", nil, nil],
 26      ["BOOL",   "ibool", nil, nil,
 27                 "ibool", nil, nil],
 28      ["ATOM",   "int", nil, nil,
 29                 "int", nil, nil],
 30      ["BYTE",   "unsigned char", nil, nil,
 31                 "unsigned char", nil, nil],
 32      ["PBYTE",  "unsigned char *", nil, nil,
 33                 "unsigned char *", nil, nil],
 34      ["UINT",   "unsigned int", nil, nil,
 35                 "unsigned int", nil, nil],
 36      ["ULONG",  "unsigned long", nil, nil,
 37                 "unsigned long", nil, nil],
 38      ["UCHAR",  "unsigned char", nil, nil,
 39                 "unsigned char", nil, nil],
 40      ["HANDLE", "unsigned long", nil, nil,
 41                 "unsigned long", nil, nil],
 42      ["PHANDLE","void*", nil, nil,
 43                 "void*", nil, nil],
 44      ["PVOID",  "void*", nil, nil,
 45                 "void*", nil, nil],
 46      ["LPCSTR", "char*", nil, nil,
 47                 "char*", nil, nil],
 48      ["HDC",    "unsigned int", nil, nil,
 49                 "unsigned int", nil, nil],
 50      ["HWND",   "unsigned int", nil, nil,
 51                 "unsigned int", nil, nil],
 52      
 53      # Others
 54      ["uint",   "unsigned int", nil, nil,
 55                 "unsigned int", nil, nil],
 56      ["u_int",  "unsigned int", nil, nil,
 57                 "unsigned int", nil, nil],
 58      ["ulong",  "unsigned long", nil, nil,
 59                 "unsigned long", nil, nil],
 60      ["u_long", "unsigned long", nil, nil,
 61                 "unsigned long", nil, nil],
 62
 63      # DL::Importable primitive types
 64      ["ibool",
 65        "I",
 66	proc{|v| v ? 1 : 0},
 67	proc{|v| (v != 0) ? true : false},
 68        "I",
 69	proc{|v| v ? 1 : 0 },
 70	proc{|v| (v != 0) ? true : false} ],
 71      ["cbool",
 72        "C",
 73	proc{|v| v ? 1 : 0},
 74	proc{|v| (v != 0) ? true : false},
 75        "C",
 76	proc{|v,len| v ? 1 : 0},
 77	proc{|v,len| (v != 0) ? true : false}],
 78      ["lbool",
 79        "L",
 80	proc{|v| v ? 1 : 0},
 81	proc{|v| (v != 0) ? true : false},
 82        "L",
 83	proc{|v,len| v ? 1 : 0},
 84	proc{|v,len| (v != 0) ? true : false}],
 85      ["unsigned char",
 86        "C",
 87	proc{|v| [v].pack("C").unpack("c")[0]},
 88	proc{|v| [v].pack("c").unpack("C")[0]},
 89        "C",
 90	proc{|v| [v].pack("C").unpack("c")[0]},
 91	proc{|v| [v].pack("c").unpack("C")[0]}],
 92      ["unsigned short",
 93        "H",
 94	proc{|v| [v].pack("S").unpack("s")[0]},
 95	proc{|v| [v].pack("s").unpack("S")[0]},
 96        "H",
 97	proc{|v| [v].pack("S").unpack("s")[0]},
 98	proc{|v| [v].pack("s").unpack("S")[0]}],
 99      ["unsigned int",
100        "I",
101	proc{|v| [v].pack("I").unpack("i")[0]},
102	proc{|v| [v].pack("i").unpack("I")[0]},
103        "I",
104	proc{|v| [v].pack("I").unpack("i")[0]},
105	proc{|v| [v].pack("i").unpack("I")[0]}],
106      ["unsigned long",
107        "L",
108	proc{|v| [v].pack("L").unpack("l")[0]},
109	proc{|v| [v].pack("l").unpack("L")[0]},
110        "L",
111	proc{|v| [v].pack("L").unpack("l")[0]},
112	proc{|v| [v].pack("l").unpack("L")[0]}],
113      ["unsigned char ref",
114        "c",
115	proc{|v| [v].pack("C").unpack("c")[0]},
116	proc{|v| [v].pack("c").unpack("C")[0]},
117	nil, nil, nil],
118      ["unsigned int ref",
119        "i",
120	proc{|v| [v].pack("I").unpack("i")[0]},
121	proc{|v| [v].pack("i").unpack("I")[0]},
122	nil, nil, nil],
123      ["unsigned long ref",
124        "l",
125	proc{|v| [v].pack("L").unpack("l")[0]},
126	proc{|v| [v].pack("l").unpack("L")[0]},
127	nil, nil, nil],
128      ["char ref",  "c", nil, nil,
129                    nil, nil, nil],
130      ["short ref", "h", nil, nil,
131                    nil, nil, nil],
132      ["int ref",   "i", nil, nil,
133                    nil, nil, nil],
134      ["long ref",  "l", nil, nil,
135                    nil, nil, nil],
136      ["float ref", "f", nil, nil,
137                    nil, nil, nil],
138      ["double ref","d", nil, nil,
139                    nil, nil, nil],
140      ["char",   "C", nil, nil,
141                 "C", nil, nil],
142      ["short",  "H", nil, nil,
143                 "H", nil, nil],
144      ["int",    "I", nil, nil,
145                 "I", nil, nil],
146      ["long",   "L", nil, nil,
147                 "L", nil, nil],
148      ["float",  "F", nil, nil,
149                 "F", nil, nil],
150      ["double", "D", nil, nil,
151                 "D", nil, nil],
152      [/^char\s*\*$/,"s",nil, nil,
153                     "S",nil, nil],
154      [/^const char\s*\*$/,"S",nil, nil,
155                           "S",nil, nil],
156      [/^.+\*$/,   "P", nil, nil,
157                   "P", nil, nil],
158      [/^.+\[\]$/, "a", nil, nil,
159                   "a", nil, nil],
160      ["void",   "0", nil, nil,
161                 nil, nil, nil],
162    ]
163
164    def initialize
165      init_types()
166    end
167
168    def typealias(ty1, ty2, enc=nil, dec=nil, ty3=nil, senc=nil, sdec=nil)
169      @TYDEFS.unshift([ty1, ty2, enc, dec, ty3, senc, sdec])
170    end
171
172    def init_types
173      @TYDEFS = TYPES.dup
174    end
175
176    def encode_argument_type(alias_type)
177      proc_encode = nil
178      proc_decode = nil
179      @TYDEFS.each{|aty,ty,enc,dec,_,_,_|
180	if( (aty.is_a?(Regexp) && (aty =~ alias_type)) || (aty == alias_type) )
181	  alias_type = alias_type.gsub(aty,ty) if ty
182          alias_type.strip! if alias_type
183	  if( proc_encode )
184	    if( enc )
185	      conv1 = proc_encode
186	      proc_encode = proc{|v| enc.call(conv1.call(v))}
187	    end
188	  else
189	    if( enc )
190	      proc_encode = enc
191	    end
192	  end
193	  if( proc_decode )
194	    if( dec )
195	      conv2 = proc_decode
196	      proc_decode = proc{|v| dec.call(conv2.call(v))}
197	    end
198	  else
199	    if( dec )
200	      proc_decode = dec
201	    end
202	  end
203	end
204      }
205      return [alias_type, proc_encode, proc_decode]
206    end
207
208    def encode_return_type(ty)
209      ty, enc, dec = encode_argument_type(ty)
210      return [ty, enc, dec]
211    end
212
213    def encode_struct_type(alias_type)
214      proc_encode = nil
215      proc_decode = nil
216      @TYDEFS.each{|aty,_,_,_,ty,enc,dec|
217	if( (aty.is_a?(Regexp) && (aty =~ alias_type)) || (aty == alias_type) )
218	  alias_type = alias_type.gsub(aty,ty) if ty
219          alias_type.strip! if alias_type
220	  if( proc_encode )
221	    if( enc )
222	      conv1 = proc_encode
223	      proc_encode = proc{|v| enc.call(conv1.call(v))}
224	    end
225	  else
226	    if( enc )
227	      proc_encode = enc
228	    end
229	  end
230	  if( proc_decode )
231	    if( dec )
232	      conv2 = proc_decode
233	      proc_decode = proc{|v| dec.call(conv2.call(v))}
234	    end
235	  else
236	    if( dec )
237	      proc_decode = dec
238	    end
239	  end
240	end
241      }
242      return [alias_type, proc_encode, proc_decode]
243    end
244  end # end of Types
245end