Little help with Getters, Setters and Native integration

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Little help with Getters, Setters and Native integration

jdarling
I'm having a bit of a problem that I'm sure someone knows the answer
too.  I've surfaced the API for my engine to LUA with no problems.
I've now started to work out some classes to wrap up the API (for those
of us working on the project that are used to OO) and have run into a
small annoiance that I can't figure out.

If I have: GetWidth and SetWidth is their a way to have a table entry of
Width that calls GetWidth and SetWidth automatically?  I know I have to
play with __index, but I can't seem to wrap my brain around it.

Thanks ahead of time,
 - Jeremy

"Help I suffer from the oxymoron Corporate Security."


Reply | Threaded
Open this post in threaded view
|

Re: Little help with Getters, Setters and Native integration

David Given
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[hidden email] wrote:
[...]
> If I have: GetWidth and SetWidth is their a way to have a table entry of
> Width that calls GetWidth and SetWidth automatically?  I know I have to
> play with __index, but I can't seem to wrap my brain around it.

__index is called when reading from a non-existent table entry,
__newindex when writing to one. So you can use a metatable of:

{
  __index = function(t, k)
    if (k == "width") then
      return GetWidth()
    end
  end,

  __newindex = function(t, k, v)
    if (k == "width") then
      SetWidth(v)
    end
  end
}

You have to make sure never to set t.width, though, or these will never
get called.

- --
+- David Given --McQ-+
|  [hidden email]    | "Those that repeat truisms, are also forced to
| ([hidden email]) | repeat them." --- Anonymous from Slashdot
+- www.cowlark.com --+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFELT2zf9E0noFvlzgRAleAAJ91gXUAjfvuNQez9J5Elc8hL4y/FACgpm6g
99eOGwXsjTIQ2VO5yfu94tw=
=rdeP
-----END PGP SIGNATURE-----
Reply | Threaded
Open this post in threaded view
|

RE: Little help with Getters, Setters and Native integration

jdarling
In reply to this post by jdarling
I'm using the code from: http://lua-users.org/wiki/InheritanceTutorial

If I understand properly then I'll want to modify the code quite a bit
to allow for my stuff to work.  For example right now they use:

  local new_class = {}
  local class_mt = { __index = new_class }
  newinst = {}
  setmetatable( newinst, class_mt )

If I understand properly I'll want to modify thusly:
  local new_class = { __index = MyFindGetter, __newindex = MyFindSetter
}
  local class_mt = { __index = new_class }
  newinst = {}
  setmetatable( newinst, class_mt )

  function MyFindGetter(self, PropertyName)
    if PropertyName=='Width' then
      return GetWidth(self.ID)
    end
    return new_class.__index(self)
  end

  function MyFindSetter(self, ProeprtyName, Value)
    if PropertyName=='Width' then
      SetWidth(self.ID, Value)
    else
      new_class[PropertyName] = Value
    end
  end

Or am I completely off on this?

 - Jeremy

"Help I suffer from the oxymoron Corporate Security."


> -------- Original Message --------
> Subject: Re: Little help with Getters, Setters and Native integration
> From: David Given <[hidden email]>
> Date: Fri, March 31, 2006 8:33 am
> To: Lua list <[hidden email]>
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> [hidden email] wrote:
> [...]
> > If I have: GetWidth and SetWidth is their a way to have a table entry of
> > Width that calls GetWidth and SetWidth automatically?  I know I have to
> > play with __index, but I can't seem to wrap my brain around it.
>
> __index is called when reading from a non-existent table entry,
> __newindex when writing to one. So you can use a metatable of:
>
> {
>   __index = function(t, k)
>     if (k == "width") then
>       return GetWidth()
>     end
>   end,
>
>   __newindex = function(t, k, v)
>     if (k == "width") then
>       SetWidth(v)
>     end
>   end
> }
>
> You have to make sure never to set t.width, though, or these will never
> get called.
>
> - --
> +- David Given --McQ-+
> |  [hidden email]    | "Those that repeat truisms, are also forced to
> | ([hidden email]) | repeat them." --- Anonymous from Slashdot
> +- www.cowlark.com --+
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.1 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFELT2zf9E0noFvlzgRAleAAJ91gXUAjfvuNQez9J5Elc8hL4y/FACgpm6g
> 99eOGwXsjTIQ2VO5yfu94tw=
> =rdeP
> -----END PGP SIGNATURE-----