[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: SWIG and 0/1 based arrays
- From: Mark Gossage <mark@...>
- Date: Fri, 8 Aug 2008 00:44:01 +0000 (GMT)
Hello Joey,
> Is there a way to make it an option in SWIG to do the
> -1's for me for
> every array access, or do I have write %luacode's for
> all my
> routines? That would not be pleasant! :-)
Currently no, you could add it. If you look at Lib\Lua\std_vector.i, its quite simple. You could make a copy of it and modify the __getitem__ and __setitem__ functions. You will have to remember to %include your modified version instead of the original.
There seems to be some kind of code for converting std::vector to Lua tables at the end of std_vector.i, I will need to look at it. Give me a few days on that.
>
> One more thing, is there a way to have SWIG/Lua do
> type-checking on
> bound data? Here is what I am doing:
>
> I have code that binds my C++ object to Lua which is as
> follows:
>
> lua_pushlightuserdata ((lua_State *) _LState,
> const_cast <void
> *> (ptr));
> lua_setglobal ((lua_State *) _LState, name);
>
When I first worked on SWIG I looked at light userdata, but didn't use it because of the type issue. SWIG uses the full userdata. If you look in Examples\Lua\embed3\embed3.cpp, you can find the following code:
================================================
// this code pushes a C++ pointer as well as the SWIG type onto the Lua stack
bool push_pointer(lua_State*L, void* ptr, const char* type_name, int owned = 0) {
// task 1: get the object 'type' which is registered with SWIG
// you need to call SWIG_TypeQuery() with the class name
// (normally, just look in the wrapper file to get this)
swig_type_info * pTypeInfo = SWIG_TypeQuery(L, type_name);
if (pTypeInfo == 0)
return false; // error
// task 2: push the pointer to the Lua stack
// this requires a pointer & the type
// the last param specifies if Lua is responsible for deleting the object
SWIG_NewPointerObj(L, ptr, pTypeInfo, owned);
return true;
}
....
Engine engine;
/* this code will pass a pointer into lua, but C++ still owns the object
this is a little tedious, to do, but lets do it
we need to pass the pointer (obviously), the type name
and a flag which states if Lua should delete the pointer once its finished with it
The type name is a class name string which is registered with SWIG
(normally, just look in the wrapper file to get this)
in this case we don't want Lua to delete the pointer so the ownership flag is 0
*/
push_pointer(L,&engine,"Engine *",0);
lua_setglobal(L, "pEngine"); // set as global variable
...
========================================================================
This uses the same code as SWIG does SWIG_NewPointerObj() to put the object onto the stack.
Hope this helps,
Regards,
Mark
__________________________________________________________
Not happy with your email address?.
Get the one you really want - millions of new email addresses available now at Yahoo! http://uk.docs.yahoo.com/ymail/new.html