[Lua Binding] Supports to bind more container: std::vector<std::string>, std::vector<int>, cocos2d::Map<std::string, T>.

This commit is contained in:
samuele3hu 2013-12-23 20:29:05 +08:00
parent 7c6b4f2838
commit 48a95cfa55
2 changed files with 135 additions and 7 deletions

View File

@ -1425,14 +1425,81 @@ bool luaval_to_ccvaluevector(lua_State* L, int lo, cocos2d::ValueVector* ret)
bool luaval_to_std_vector_string(lua_State* L, int lo, std::vector<std::string>* ret)
{
// TO BE DONE IN CPP FILE
return false;
if (nullptr == L || nullptr == ret || lua_gettop(L) < lo)
return false;
tolua_Error tolua_err;
bool ok = true;
if (!tolua_istable(L, lo, 0, &tolua_err))
{
#if COCOS2D_DEBUG >=1
luaval_to_native_err(L,"#ferror:",&tolua_err);
#endif
ok = false;
}
if (ok)
{
size_t len = lua_objlen(L, lo);
std::string value = "";
for (int i = 0; i < len; i++)
{
lua_pushnumber(L, i + 1);
lua_gettable(L,lo);
if(lua_isstring(L, -1))
{
ok = luaval_to_std_string(L, -1, &value);
if(ok)
ret->push_back(value);
}
else
{
CCASSERT(false, "string type is needed");
}
lua_pop(L, 1);
}
}
return ok;
}
bool luaval_to_std_vector_int(lua_State* L, int lo, std::vector<int>* ret)
{
// TO BE DONE IN CPP FILE
return false;
if (nullptr == L || nullptr == ret || lua_gettop(L) < lo)
return false;
tolua_Error tolua_err;
bool ok = true;
if (!tolua_istable(L, lo, 0, &tolua_err))
{
#if COCOS2D_DEBUG >=1
luaval_to_native_err(L,"#ferror:",&tolua_err);
#endif
ok = false;
}
if (ok)
{
size_t len = lua_objlen(L, lo);
for (int i = 0; i < len; i++)
{
lua_pushnumber(L, i + 1);
lua_gettable(L,lo);
if(lua_isnumber(L, -1))
{
ret->push_back((int)tolua_tonumber(L, -1, 0));
}
else
{
CCASSERT(false, "int type is needed");
}
lua_pop(L, 1);
}
}
return ok;
}
void point_to_luaval(lua_State* L,const Point& pt)

View File

@ -120,8 +120,47 @@ bool luaval_to_std_vector_int(lua_State* L, int lo, std::vector<int>* ret);
template <class T>
bool luaval_to_ccmap_string_key(lua_State* L, int lo, cocos2d::Map<std::string, T>* ret)
{
// TO BE DONE:
return false;
if(nullptr == L || nullptr == ret || lua_gettop(L) < lo)
return false;
tolua_Error tolua_err;
bool ok = true;
if (!tolua_istable(L, lo, 0, &tolua_err))
{
#if COCOS2D_DEBUG >=1
luaval_to_native_err(L,"#ferror:",&tolua_err);
#endif
ok = false;
}
if (ok)
{
std::string stringKey = "";
lua_pushnil(L); /* first key L: lotable ..... nil */
while ( 0 != lua_next(L, lo ) ) /* L: lotable ..... key value */
{
if (!lua_isstring(L, -2))
{
lua_pop(L, 1); /* removes 'value'; keep 'key' for next iteration*/
continue;
}
if (lua_isnil(L, -1) || !lua_isuserdata(L, -1))
{
lua_pop(L, 1);
continue;
}
luaval_to_std_string(L, -2, &stringKey);
T obj = static_cast<T>(tolua_tousertype(L, -1, NULL) );
if (nullptr != obj)
ret->insert(stringKey, obj);
lua_pop(L, 1); /* L: lotable ..... key */
}
}
return ok;
}
@ -179,7 +218,29 @@ void ccvector_to_luaval(lua_State* L,const cocos2d::Vector<T>& inValue)
template <class T>
void ccmap_string_key_to_luaval(lua_State* L, const cocos2d::Map<std::string, T>& v)
{
// TO BE DONE:
lua_newtable(L);
if(nullptr == L)
return;
for (auto iter = v.begin(); iter != v.end(); ++iter)
{
std::string key = iter->first;
T obj = iter->second;
if (nullptr != dynamic_cast<cocos2d::Object *>(obj))
{
std::string name = typeid(*obj).name();
auto typeIter = g_luaType.find(name);
if (g_luaType.end() != typeIter)
{
lua_pushstring(L, name.c_str());
int ID = (obj) ? (int)obj->_ID : -1;
int* luaID = (obj) ? &obj->_luaID : NULL;
toluafix_pushusertype_ccobject(L, ID, luaID, (void*)obj,typeIter->second.c_str());
lua_rawset(L, -3);
}
}
}
}
void ccvalue_to_luaval(lua_State* L,const cocos2d::Value& inValue);