lua-users home
lua-l archive

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


> Unfortunately, by the clever macro stuff, this will not work at all of
> course...  Well, OK...

So maybe we could take a more C++ approach (using just a single convenience
macro) along the following lines (operator new remains unchanged):

template<class T> struct lua_traits
{
    static const char *const name;
    // other interesting stuff about T could go here as well...
};

template<class T>
T *lua_userdata_cast(lua_State *L, int pos)
{
    return static_cast<T *>(luaL_checkudata(L, pos, lua_traits<T>::name));
}

template<class T>
int GCMethod(lua_State *L)
{
    T *p = lua_userdata_cast<T>(L, 1);
    if (p)
        p->~T();
    else
        ;  // hum?
    return 0;
}

// this macro just avoids some tedious typing...
#define lua_userdata_type(T) \
    template<> const char *const lua_traits<T>::name = #T

All type specific stuff for T, like its name, goes into lua_traits<T>.  The
lua_userdata_type can quickly introduce the name of T:

    lua_userdata_type(X);  // now lua_traits<X>::name equals "X"

Makes sense??

--
Wim