[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: poll: [gs]etglobal tag methods
- From: Thatcher Ulrich <tu@...>
- Date: Mon, 22 Oct 2001 12:21:32 -0400 (EDT)
On Mon, 22 Oct 2001, Luiz Henrique de Figueiredo wrote:
> If you use "getglobal" or "setglobal" tag methods, please send me a summary
> of how they are used. Thanks. --lhf
I use setglobal/getglobal for a user-defined floating-point type, so that
when Lua code assigns a number to a global that contains such a type, the
new value is stored in the existing userdata (instead of a lua number
replacing the userdata).
Example:
// C++ code
cfloat f("f", 1.0f);
...
// Use f just like an ordinary float, with native speed and no
// type checking or conversion.
f += 10.0f;
printf("%f\n", f);
-- Lua code:
-- but the global 'f' is accessible to Lua as well...
f = 27; -- setglobal() invoked here.
print(f); -- getglobal() invoked here.
// Here's the cfloat setglobal tag method:
static int cfloat_set( lua_State* L )
// Lua CFunction which sets the value of the given cfloat to the given lua
// number.
{
// arg 1: varname
// arg 2: previous var value (in this case the cfloat*)
// arg 3: new var value
cfloat* cf = static_cast<cfloat*>( lua_touserdata( L, 2 ) );
*cf = lua_tonumber( L, 3 );
return 0;
}
The getglobal tag method is similarly trivial.
I'd be much happier if setglobal/getglobal were replaced by generic
"setvalue/getvalue" tag methods, which would be called whenever Lua tried
to get/set a tagged type. That way I could embed cfloats in tables
without the table having to know about it.
I'm still feeling my way towards a good transparent C++/Lua binding, so
this may not be the best way to do what I'm trying to do.
-Thatcher