[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Catch get/set of userdata in table?
- From: "Michael Flad" <Michael@...>
- Date: Fri, 7 Sep 2001 12:16:11 +0200
> -----Original Message-----
> From: owner-lua-l@tecgraf.puc-rio.br
> [mailto:owner-lua-l@tecgraf.puc-rio.br]On Behalf Of J. Perkins
> Sent: Thursday, September 06, 2001 23:34
> To: Multiple recipients of list
> Subject: Catch get/set of userdata in table?
>
>
> Is there a way to catch a get/set to a userdata object that is stored in
> a table? I know how to do it for a global variable, like LHF showed a
> while back (see example below) -- is there some way to do an equivalent
> operation on a table member?
>
> I have a C++/Lua component system, and rather than mirroring all of the
> get...() and set...() calls of C++ I'd like to make them look like
> regular table variables and have assignments transparently call the C++
> get...() and set...() methods under the hood. So if I had something like
> this in C++:
>
> struct MyComponent {
> int getValue();
> void setValue (int value);
> };
>
>
> In Lua it would look like:
>
> MyComponent.value = 22;
> x = MyComponent.value
>
> I got it working by *not* putting anything in the table, catching the
> "gettable" and "settable" calls and manually looking up the proper table
> method from an internal array of entry points. It's ugly. Is there a
> more elegant way to make this happen?
You could always check if it's a userdata of a type you want to
handle by yourself and in every other case, call the rawset/rawget
methods - in this case you could use the tables as usual and you
only need the special handling for your userdata.
That's the way i do it in a first implementation.
I think somtimes later i may optimize this by adding another type of
tag method, something like "setuserdata"/"getuserdata". After a
very quick look at the sources of Lua it seems easy to implement.
I think for tables, you could insert the required code in
void luaV_settable (lua_State *L, StkId t, StkId key)
I haven't done any tests right now, but i let you know when i've
done the changes (don't have the time right now).
Regards,
Michael Flad
--
Fantastic Realms Interactive GmbH
Birnenweg 15 - D-72766 Reutlingen
Germany