lua-users home
lua-l archive

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


On 06/22/2011 09:52 AM, Roberto Ierusalimschy wrote:

The advantage of exporting the structure is that the user may add her
own extra fields, if needed. (Are there real cases for this need?)


In that case the library likely also provides its own functions that use the extra data. And since the purpose of customizing FILE* is to be able to use the built-in functions, it seems superfluous. However, the exported FILE* factory could offer to add extra size to the structure that can be written to. Any internal Lua fields would be in front of the public fields, then extended data after. It also allows the userdata to be created prior to opening the handle. Something like:

lua_file* F = luaL_createfilehandle(L, extrasize);
F->fclose = myclose;
F->fptr = fopen(...);
((MyFile*)F)->extra = whatever;

--
- tom
telliamed@whoopdedo.org