[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Is this access to Table members "safe"?
- From: Quinn Tyler Jackson <quinn-j@...>
- Date: Thu, 06 May 2004 07:35:38 -0700
> > const Table* pTable = (Table*)lua_topointer(L, -1);
> > const TObject* pString1 = (TObject*)luaH_getnum((Table*)pTable, 1);
>
> Why use Lua internal functions and types? They are not part of
> the official API.
> Use lua_lua_gettable or lua_rawgeti instead.
> --lhf
Because I couldn't for the life of me find the official API -- which you've
now just pointed out.
Thanks. The code now looks like this:
else if(lua_istable(L, -1))
{
CLua* pLua = CLua::get_interface(L);
SHU_ASSERT(pLua != NULL);
CLpmClassTable* pClassContext =
pLua->get_grammar()->GetClassContext();
if(pClassContext != NULL && sClass != NULL)
{
int ii = 1;
do
{
lua_rawgeti(L, -1, ii);
const char* sValue = lua_tostring(L, 3);
lua_pop(L, 1);
if(sValue == NULL)
{
break;
}
bSuccess = pClassContext->Add(sClass, sValue);
ii++;
} while(bSuccess);
}
lua_pushboolean(L, bSuccess ? 1 : 0);
}
This allows me to now do this:
function reduction_event(P)
if(P:MATCHED()) then
P:ADDCLASSMEMBER("foobar", {"apple", "orange", "banana"});
else
P:ADDCLASSMEMBER("foobar", "tangerine");
end
end
The idea is that ADDCLASSMEMBER either adds one element or many, depending
on whether or not a simple string or a table of strings is passed in.
I suspected there was an "official" way to access tables from C/C++
callbacks, but it had me stumped!! ;-)
Quinn