lua-users home
lua-l archive

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


If I am getting it right you are facing an inheritance issue.

I had a similar situation a while ago. And as suggested by others, I implemented an inheritance mechanism over userdata to solve it.

Let me share my solution.

// Carlos



El vie., 22 de mayo de 2020 17:35, Laurent FAILLIE <l_faillie@yahoo.com> escribió:
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
https://github.com/destroyedlolo/Selene/blob/v6/src/SelPlugins/DRMCairo/SelDCSurface.c

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