我使用Ryan Pavlik的Lua 5.1的主分布的luabind 0.9.1,Win XP SP3上的cygwin +最新的补丁x86,boost 1.48,gcc 4.3.4。 Lua和boost是cygwin预编译版本。luabind:无法从非内置类索引表中检索值
我已经在静态和共享版本中成功构建了luabind。
两个版本都通过了test_object_identity.cpp测试的所有测试,其中两个版本均失败。
我追查到以下问题: 如果为NON内置类(即不是int,string等)创建表中的条目,则无法检索值。
这里有一个代码块演示了此:
#include "test.hpp"
#include <luabind/luabind.hpp>
#include <luabind/detail/debug.hpp>
using namespace luabind;
struct test_param
{
int obj;
};
void test_main(lua_State* L)
{
using namespace luabind;
module(L)
[
class_<test_param>("test_param")
.def_readwrite("obj", &test_param::obj)
];
test_param temp_object;
object tabc = newtable(L);
tabc[1] = 10;
tabc[temp_object] = 30;
TEST_CHECK(tabc[1] == 10); // passes
TEST_CHECK(tabc[temp_object] == 30); // FAILS!!!
}
TABC [1]的确是10,而TABC [temp_object]不是30! (实际上,它似乎是零)
但是,如果我使用迭代来检查tabc条目,那么有两个条目与正确的键/值对。
任何想法?
BTW,重载==操作符是这样的:
#include <luabind/operator.hpp>
struct test_param
{
int obj;
bool operator==(test_param const& rhs) const
{
return obj == rhs.obj;
}
};
和
module(L)
[
class_<test_param>("test_param")
.def_readwrite("obj", &test_param::obj)
.def(const_self == const_self)
];
不改变结果。
我也尝试从[]运算符切换到settable()和gettable()。结果是一样的。我可以用调试器看到调用了默认的密钥转换,所以我想这个错误来自其中的某个地方,但是我无法弄清楚究竟是什么问题。
如下面的简单的测试案例显示,有是肯定的Luabind的转换为复杂类型的错误:
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
}
希望有人比我聪明可以算出来, THX
我已经将问题追溯到Luabind在每次使用复杂值作为关键字时创建一个NEW DISTINCT对象这一事实(但如果您使用原始对象或对象,则不会这样做)。
这里的演示此小测试用例:
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";
}
}
通知如何TABC [TP]第一具有值5,然后通过按键对象访问时改写为7。但是,当通过tp重新访问时,会创建一个新条目。这就是gettable()随后失败的原因。
THX, 大卫
您是否曾经解决过这个问题?使用int值作为表格的键时,我已经遇到了这个问题。 local testTable = {[10] =“green”,[9] =“orange”,[8] =“yellow”} - 如果我使用字符串而不是数字作为键 - 它工作正常 - 我把这个表作为参数到一个C++函数,我也得到了铸造错误 – Steve 2012-07-08 22:22:02