Re: luabind: problem retrieving values from table indexed by non-built-in classes
qwer 1304 <[email protected]> Sun, 29 Apr 2012 12:15:15 +0300
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
I've traced the problem to the fact that Luabind creates a NEW DISTINCT object EVERY time you use a complex value as key (but it does NOT if you use a primitive one or an object).
Here's a small test case that demonstrates this:struct test_param : wrap_base{ int obj;
bool operator==(test_param const& rhs) const { return obj == rhs.obj ; }};
void test_main(lua_State* L){ using namespace luabind;
module(L) [ class_<test_param>("test_param") .def(constructor<>()) .def_readwrite("obj", &test_param::obj) .def(const_self == const_self) ];
object tabc, zzk, zzv; test_param tp; tp.obj = 123456; tabc = newtable(L); // o k v settable( tabc, tp, 5); iterator zzi(tabc), end; std::cerr << "value = " << *zzi << "\n"; zzk = zzi.key(); // o k v settable( tabc, tp, 6); settable( tabc, zzk, 7); for (zzi = iterator(tabc); zzi != end; ++zzi) { std::cerr << "value = " << *zzi << "\n"; }}
Notice how tabc[tp] first has the value 5 and then is overwritten with 7 when accessed through the key object. However, when accessed AGAIN through tp, a new entry gets created. This is why gettable() fails subsequently.
Any ideas?
From: [email protected]
To: [email protected]
Date: Sun, 29 Apr 2012 07:17:22 +0300
Subject: Re: [luabind] luabind: problem retrieving values from table indexed by non-built-in classes
As the following simple test case shows, there's definitely a bug in Luabind's conversion for complex types: struct test_param : wrap_base { int obj; bool operator==(test_param const& rhs) const { return obj == rhs.obj ; } }; void test_main(lua_State* L) { using namespace luabind; module(L) [ class_<test_param>("test_param") .def(constructor<>()) .def_readwrite("obj", &test_param::obj) .def(const_self == const_self) ]; object tabc, zzk, zzv; test_param tp, tp1; tp.obj = 123456; // create new table tabc = newtable(L); // set tabc[tp] = 5; // o k v settable( tabc, tp, 5); // get access to entry through iterator() API iterator zzi(tabc); // get the key object zzk = zzi.key(); // read back the value through gettable() API //
o k zzv = gettable(tabc, zzk); // check the entry has the same value // irrespective of access method TEST_CHECK ( *zzi == 5 && object_cast<int>(zzv) == 5 ); // convert key to its REAL type (test_param) tp1 = object_cast<test_param>(zzk); // check two keys are the same TEST_CHECK( tp == tp1 ); // read the value back from table using REAL key type zzv = gettable(tabc, tp1); // check the value TEST_CHECK( object_cast<int>(zzv) == 5 ); // the previous call FAILS with // Terminated with exception: "unable to make cast" // this is because gettable() doesn't return // a TRUE value, but nil instead } Hopefully, someone smarter than me can figure this out, Thx
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user