Quantcast

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

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

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

Glen F
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 = 5
function MyClass:__init(name)
    self.name = name
end

-- Test on an instance of the class:
a = MyClass('example')

testKey(a, 'name')
testKey(a, 'doesNotExist') -- not an error to access an unset key; simply returns nil
testKey(a, 'someFlag')
a.someFlag = -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.


------------------------------------------------------------------------------
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
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Christian N.

Catching the error with pcall (and maybe checking if the error message is an expected one) is the only solution I can think of.

 

From: Glen Fraser [mailto:[hidden email]]
Sent: Saturday, October 26, 2013 7:45 PM
To: [hidden email]
Subject: [luabind] Test for key on luabind class (userdata vs table?)

 

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?

 

[snip]

 

Thanks,

Glen.

 


------------------------------------------------------------------------------
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
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Glen F
Thanks, that seems to do the trick.

Glen.

On 26/10/2013, at 20:46, "Christian" <[hidden email]> wrote:

Catching the error with pcall (and maybe checking if the error message is an expected one) is the only solution I can think of.

 

From: Glen Fraser [[hidden email]]
Sent: Saturday, October 26, 2013 7:45 PM
To: [hidden email]
Subject: [luabind] Test for key on luabind class (userdata vs table?)

 

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?

 

[snip]

 

Thanks,

Glen.


------------------------------------------------------------------------------
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
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Loading...