lua-users home
lua-l archive

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


Well well well,

After some test and looking (again) in the documentation.
It looks to me, the only solution is to have the same meta for all derived object and add a field in my C structure to tell if it's the mother class or a derived.

Any other clue ?

Le samedi 23 mai 2020 à 00:35:55 UTC+2, Laurent FAILLIE <l_faillie@yahoo.com> a écrit :


Hello,

I'm working to a graphical framework in C dealing with Cairo graphics.

I'm creating an object named SelDCSurface that deals with Cairo's surface

Some methods are attached to it using luaL_setfuncs() or luaL_register().

Now, I'm working on SelDCImageSurface which is basically an extension of SelDCSurface : it has to know all methods of SelDCSurface plus its own.

1/ How to do that ?
I guess I can use luaL_setfuncs() or luaL_register() more than once on the same object, to add additional methods, isn't it ?

2/ my main concern is the function used if "self" object has the right type

I.E. for method

mySelDCSurface:GetSize()

the C code is

----------
static struct SelDCSurface *checkSelDCSurface(lua_State *L, int where){
    void *r = luaL_checkudata(L, where, "SelDCSurface");
    luaL_argcheck(L, r != NULL, where, "'SelDCSurface' expected");
    return (struct SelDCSurface *)r;
}

static int GetSize(lua_State *L){
    /* Return surface's size
     * <- width, hight
     */
    struct SelDCSurface *srf = checkSelDCSurface(L, 1);

    lua_pushnumber(L, srf->w);
    lua_pushnumber(L, srf->h);
    return 2;
}
---


The problem, is when called from a SelDCImageSurface object, GetSize() will still call checkSelDCSurface() which fails as facing a "SelDCImageSurface" and not a "SelDCSurface".

Any smart way to avoid that ?

Thanks

Laurent