[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: I have a subjestion
- From: Tom N Harris <telliamed@...>
- Date: Mon, 07 Apr 2014 14:46:38 -0400
On Monday, April 07, 2014 09:37:38 AM jesus manuel Castillo blanco wrote:
> I have a subjection that you may want to consider, you have chosen to use
> callbacks function in the C style because you needed to allow multiple
> callbacks to be allow and C++ make this difficult, so I thought you make
> want to use the method on the link bellow that I was discussing with some
> guys of about passing C++ methods pointer. I realized that it may be a bit
> risky but so is passing C callbacks with the advantage that now I don't
> have to create a function that some how has access to a instance of a
> class. I think this would make LUA easier to use.
>
> http://stackoverflow.com/questions/20359668/is-it-safe-to-calling-a-member-f
> unction-not-defined-on-the-base-class-from-of-a
Aside from the tautological reason that C callbacks are used because Lua is
written in C, it is also the most portable callback style that can be adapted
to any language. A bound method pointer is only callable from C++ code and
would require that Lua be built by a C++ compiler.
There are C++ wrapper libraries like Selene[1] that take care of interfacing
your C++ objects with plain Lua.
A simplified way of calling a method on a class would be:
class Frobulator {
int Sworl();
void tolua_Sworl(lua_State* L) {
lua_pushlightuserdata(L, this);
lua_pushcclosure(L, &fromlua_Sworl, 1);
}
static int fromlua_Sworl(lua_State* L) {
Frobulator* self =
(Frobulator*)lua_touserdata(L, lua_upvalueindex(1));
lua_pushinteger(L, self->Sworl());
return 1;
}
}
A more complete implementation would use metatables. But as I said, there are
libraries that do all this for you.
[1] https://github.com/jeremyong/Selene
--
tom <telliamed@whoopdedo.org>