From 18c882f1a0f1188b201b00afe4e04d6209ffd431 Mon Sep 17 00:00:00 2001 From: Henrique Santiago Date: Sun, 5 Aug 2012 10:56:15 -0300 Subject: [PATCH] Allow list bindings --- src/framework/luaengine/luavaluecasts.h | 34 +++++++++++++++++++++++++ src/framework/luafunctions.cpp | 1 + 2 files changed, 35 insertions(+) diff --git a/src/framework/luaengine/luavaluecasts.h b/src/framework/luaengine/luavaluecasts.h index d93361b8..c87dff1a 100644 --- a/src/framework/luaengine/luavaluecasts.h +++ b/src/framework/luaengine/luavaluecasts.h @@ -124,6 +124,13 @@ template typename std::enable_if::value, bool>::type luavalue_cast(int index, std::function& func); +// list +template +int push_luavalue(const std::list& list); + +template +bool luavalue_cast(int index, std::list& list); + // vector template int push_luavalue(const std::vector& vec); @@ -274,6 +281,33 @@ luavalue_cast(int index, std::function& func) { return false; } +template +int push_luavalue(const std::list& list) { + g_lua.newTable(); + int i = 1; + for(const T& v : list) { + push_internal_luavalue(v); + g_lua.rawSeti(i); + i++; + } + return 1; +} + +template +bool luavalue_cast(int index, std::list& list) +{ + if(g_lua.isTable(index)) { + g_lua.pushNil(); + while(g_lua.next(index < 0 ? index-1 : index)) { + T value; + if(luavalue_cast(-1, value)) + list.push_back(value); + g_lua.pop(); + } + return true; + } + return false; +} template int push_luavalue(const std::vector& vec) { diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 98ec767f..835d59f6 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -151,6 +151,7 @@ void Application::registerLuaFunctions() g_lua.bindSingletonFunction("g_resources", "getRealDir", &ResourceManager::getRealDir, &g_resources); g_lua.bindSingletonFunction("g_resources", "getWorkDir", &ResourceManager::getWorkDir, &g_resources); g_lua.bindSingletonFunction("g_resources", "getSearchPaths", &ResourceManager::getSearchPaths, &g_resources); + g_lua.bindSingletonFunction("g_resources", "listDirectoryFiles", &ResourceManager::listDirectoryFiles, &g_resources); // Module g_lua.registerClass();