Test for key on luabind class (userdata vs table?)

Glen Fraser <[email protected]> Sat, 26 Oct 2013 19:44:53 +0200
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
--===============7788886192110118745==
Content-Type: multipart/alternative;
	boundary="Apple-Mail=_AA3948A3-0946-4734-8872-4258C4475F5D"


--Apple-Mail=_AA3948A3-0946-4734-8872-4258C4475F5D
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

Hi there, I know it's possible to set "properties" on luabind classes in =
Lua.  You can set and get these keys, no problem.  However, it doesn't =
seem possible to "get" (or test for the existence of) an unset key -- =
you'd expect it to return 'nil', but it crashes.  Is there a way to test =
a luabind class for the existence of a key (as though it were a table), =
without crashing?  Here is a made-up example:

function testKey(obj, key)
    if obj[key] then
        print(type(obj) .. " key: '" .. key .. "' value: " .. obj[key])
    else
        print(type(obj) .. " key: '" .. key .. "' doesn't exist.")
    end
end

class 'MyClass'
MyClass.someFlag =3D 5
function MyClass:__init(name)
    self.name =3D name
end

-- Test on an instance of the class:
a =3D MyClass('example')
testKey(a, 'name')
testKey(a, 'doesNotExist') -- not an error to access an unset key; =
simply returns nil
testKey(a, 'someFlag')
a.someFlag =3D -12
testKey(a, 'someFlag')

-- Now do a similar test on the class itself (not an instance):
testKey(MyClass, 'someFlag') -- works fine; the key has already been set

-- But for undefined keys, it fails (if MyClass were a table instead of
-- a userdata (class), you'd expect it to simply return nil).
testKey(MyClass, 'doesNotExist') -- crash

So, is there a way I can test if a luabind class has a particular key?  =
(I'm using Lua-derived C++ classes, but I have the same problem even =
with simple luabind classes, as shown in the above simple example).

Thanks,
Glen.


--Apple-Mail=_AA3948A3-0946-4734-8872-4258C4475F5D
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=us-ascii

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html =
charset=3Dus-ascii"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi =
there, I know it's possible to set "properties" on luabind classes in =
Lua. &nbsp;You can set and get these keys, no problem. &nbsp;However, it =
doesn't seem possible to "get" (or test for the existence of) an unset =
key -- you'd expect it to return 'nil', but it crashes. &nbsp;Is there a =
way to test a luabind class for the existence of a key (as though it =
were a table), without crashing? &nbsp;Here is a made-up =
example:<div><br></div><div><font face=3D"Monaco"><span =
style=3D"font-size: 11px;">function testKey(obj, key)</span><br><span =
style=3D"font-size: 11px;">&nbsp; &nbsp;&nbsp;if obj[key] =
then</span><br><span style=3D"font-size: 11px;">&nbsp; &nbsp; &nbsp; =
&nbsp;&nbsp;print(type(obj) .. " key: '" .. key .. "' value: " .. =
obj[key])</span><br><span style=3D"font-size: 11px;">&nbsp; =
&nbsp;&nbsp;else</span><br><span style=3D"font-size: 11px;">&nbsp; =
&nbsp; &nbsp; &nbsp;&nbsp;print(type(obj) .. " key: '" .. key .. "' =
doesn't exist.")</span><br><span style=3D"font-size: 11px;">&nbsp; =
&nbsp;&nbsp;end</span><br><span style=3D"font-size: =
11px;">end</span><br><br><span style=3D"font-size: 11px;">class =
'MyClass'</span><br><span style=3D"font-size: 11px;">MyClass.someFlag =3D =
5</span><br><span style=3D"font-size: 11px;">function =
MyClass:__init(name)</span><br><span style=3D"font-size: 11px;">&nbsp; =
&nbsp;&nbsp;self.name =3D name</span><br><span style=3D"font-size: =
11px;">end</span><br><br><span style=3D"font-size: 11px;">-- Test on an =
instance of the class:<br>a =3D MyClass('example')</span><br><span =
style=3D"font-size: 11px;">testKey(a, 'name')</span><br><span =
style=3D"font-size: 11px;">testKey(a, 'doesNotExist') -- not an error to =
access an unset key; simply&nbsp;returns nil</span><br><span =
style=3D"font-size: 11px;">testKey(a, 'someFlag')</span><br><span =
style=3D"font-size: 11px;">a.someFlag =3D -12</span><br><span =
style=3D"font-size: 11px;">testKey(a, 'someFlag')</span><br><br><span =
style=3D"font-size: 11px;">-- Now do a similar test on the class itself =
(not an instance):<br></span><span style=3D"font-size: =
11px;">testKey(MyClass, 'someFlag') -- works fine; the key has already =
been set</span><br><br><span style=3D"font-size: 11px;">-- But for =
undefined keys, it fails (if MyClass were a table instead =
of</span><br><span style=3D"font-size: 11px;">-- a userdata (class), =
you'd expect it to simply return nil).</span><br><span style=3D"font-size:=
 11px;">testKey(MyClass, 'doesNotExist') -- =
crash</span><br></font><br>So, is there a way I can test if a luabind =
class has a particular key? &nbsp;(I'm using Lua-derived C++ classes, =
but&nbsp;I have the same problem even with simple luabind =
classes,&nbsp;as shown in the above simple =
example).</div><div><br></div><div>Thanks,</div><div>Glen.</div><div><br><=
/div></body></html>=

--Apple-Mail=_AA3948A3-0946-4734-8872-4258C4475F5D--


--===============7788886192110118745==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60135991&iu=/4140/ostg.clktrk
--===============7788886192110118745==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user

--===============7788886192110118745==--