From efb04ab27c4e54b122bef1ccda1398635ea64851 Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Mon, 22 Aug 2011 18:05:09 +0200 Subject: [PATCH] Add love.thread.getKeys (issue #276) --- src/modules/thread/Thread.cpp | 15 +++++++++++++++ src/modules/thread/Thread.h | 5 ++++- src/modules/thread/wrap_Thread.cpp | 18 ++++++++++++++++++ src/modules/thread/wrap_Thread.h | 1 + 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/modules/thread/Thread.cpp b/src/modules/thread/Thread.cpp index a159e8132..25288ba54 100644 --- a/src/modules/thread/Thread.cpp +++ b/src/modules/thread/Thread.cpp @@ -185,6 +185,16 @@ namespace thread shared[name] = v; } + std::vector ThreadData::getKeys() + { + std::vector keys; + for (std::map::iterator it = shared.begin(); it != shared.end(); it++) + { + keys.push_back(it->first); + } + return keys; + } + Thread::Thread(love::thread::ThreadModule *module, const std::string & name, love::Data *data) : handle(0), module(module), name(name), isThread(true) { @@ -270,6 +280,11 @@ namespace thread return v; } + std::vector Thread::getKeys() + { + return comm->getKeys(); + } + ThreadVariant *Thread::demand(const std::string & name) { ThreadVariant *v = comm->getValue(name); diff --git a/src/modules/thread/Thread.h b/src/modules/thread/Thread.h index 85ee791a8..444912a2e 100644 --- a/src/modules/thread/Thread.h +++ b/src/modules/thread/Thread.h @@ -24,7 +24,8 @@ // STL #include #include -#include +#include +#include // LOVE #include @@ -90,6 +91,7 @@ namespace thread ThreadVariant* getValue(const std::string & name); void clearValue(const std::string & name); void setValue(const std::string & name, ThreadVariant *v); + std::vector getKeys(); void *mutex; void *cond; @@ -129,6 +131,7 @@ namespace thread void wait(); std::string getName(); ThreadVariant *get(const std::string & name); + std::vector getKeys(); ThreadVariant *demand(const std::string & name); void clear(const std::string & name); void set(const std::string & name, ThreadVariant *v); diff --git a/src/modules/thread/wrap_Thread.cpp b/src/modules/thread/wrap_Thread.cpp index 62aa59cab..d82c26665 100644 --- a/src/modules/thread/wrap_Thread.cpp +++ b/src/modules/thread/wrap_Thread.cpp @@ -128,6 +128,23 @@ namespace thread return 1; } + int w_Thread_getKeys(lua_State *L) + { + Thread *t = luax_checkthread(L, 1); + t->lock(); + std::vector keys = t->getKeys(); + t->unlock(); + lua_createtable(L, keys.size(), 0); + int i = 0; + for (std::vector::iterator it = keys.begin(); it != keys.end(); it++) + { + lua_pushnumber(L, i++); + lua_pushlstring(L, it->c_str(), it->length()); + lua_settable(L, -3); + } + return 1; + } + int w_Thread_demand(lua_State *L) { Thread *t = luax_checkthread(L, 1); @@ -216,6 +233,7 @@ namespace thread { "wait", w_Thread_wait }, { "getName", w_Thread_getName }, { "get", w_Thread_get }, + { "getKeys", w_Thread_getKeys }, { "demand", w_Thread_demand }, { "peek", w_Thread_peek }, { "set", w_Thread_set }, diff --git a/src/modules/thread/wrap_Thread.h b/src/modules/thread/wrap_Thread.h index 4a9af3131..325c769d7 100644 --- a/src/modules/thread/wrap_Thread.h +++ b/src/modules/thread/wrap_Thread.h @@ -36,6 +36,7 @@ namespace thread int w_Thread_wait(lua_State *L); int w_Thread_getName(lua_State *L); int w_Thread_get(lua_State *L); + int w_Thread_getKeys(lua_State *L); int w_Thread_demand(lua_State *L); int w_Thread_peek(lua_State *L); int w_Thread_set(lua_State *L);