lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2011/4/18 Mike Pall <mikelu-1104@mike.de>:
> Generating the textual representation of a C type is rather
> expensive. Also, it wouldn't really be useful for anonymous
> structs (you get e.g. "struct 95", but the number varies).
>
>> Otherwise, am I missing something obvious to obtain the informations
>> that I need ?
>
> Yes, I acknowledge there's a need for this. But I'm not sure of
> the best way to achieve this. Something like an ffi.isctype()
> function. Or maybe an equality operator for ctype objects (but
> that has other implications).
>
> Not sure this covers all use cases. Other languages offer subclass
> checks or compatibility checks (e.g. to handle pointer compat. or
> the pointer/struct duality). But I fear this might add too much
> complexity. I'd prefer a simpler solution, if anyone knows of one.

Mike,

I see your point. I was thinking to a possible solution that could be
either efficient and in the Lua spirit.

The ffi.typeof function return a function that creates an object of a
given ctype. Why do not add a function:

ffi.istype

that return a function that check the type of a cdata. For example

ffi.cdef [[
   typedef struct { int x; int y; } point;
]]

point = ffi.typeof('point') -- contructor
ispoint = ffi.istype('point') -- type checking function

p = point(3, 4)
x = 3.14
b = ffi.new('double[5]')

ispoint(p) -- true
ispoint(x) -- false
ispoint(b) -- false

I'm sure this proposition cannot be refused... ;-)

-- 
Francesco