On May 14, 2012, at 2:33 AM, Mike Pall wrote: > GrrrWaaa wrote: >> According to [1] a metatype cannot be attached to a pointer >> type, but instead to the underlying object. This doesn't work >> out-of-the-box for opaque struct pointers, unless the struct is >> also typedef'd: > > This is not true. Ah - I misunderstood "Metatypes are always bound to the underlying aggregate and not to any pointer type." as prescriptive rather than descriptive. > >> ffi.cdef[[ >> typedef struct OpaqueObject OpaqueObject; // won't work without >> this line >> typedef struct OpaqueObject * ObjectPtr; >> ]] >> >> local mt = {} >> -- <configure mt here> >> ffi.metatype(ffi.typeof("OpaqueObject"), mt) > > Of course the first declaration is needed here because you are > referring to "OpaqueObject", which isn't defined without this. > We're talking about C declarations, not C++. Ok. The C header I'm parsing never declared "struct OpaqueObject", so I had to manually add it; but its definitely nicer they way you suggest: > Or simply use > "struct OpaqueObject" which works without the first declaration, > because it's implicitly declared by the second declaration. > > Also, you can strip that ffi.typeof(). ffi.metatype() directly > accepts an abstract type declaration (and returns a ctype object): > > local ootype = ffi.metatype("struct OpaqueObject", mt) A lot cleaner, thanks!