Browse Source

Add love.thread.getKeys (issue #276)

Bart van Strien 14 years ago
parent
commit
efb04ab27c

+ 15 - 0
src/modules/thread/Thread.cpp

@@ -185,6 +185,16 @@ namespace thread
 		shared[name] = v;
 	}
 
+	std::vector<std::string> ThreadData::getKeys()
+	{
+		std::vector<std::string> keys;
+		for (std::map<std::string, ThreadVariant*>::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<std::string> Thread::getKeys()
+	{
+		return comm->getKeys();
+	}
+
 	ThreadVariant *Thread::demand(const std::string & name)
 	{
 		ThreadVariant *v = comm->getValue(name);

+ 4 - 1
src/modules/thread/Thread.h

@@ -24,7 +24,8 @@
 // STL
 #include <map>
 #include <string>
-#include <string.h>
+#include <vector>
+#include <cstring>
 
 // LOVE
 #include <filesystem/File.h>
@@ -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<std::string> getKeys();
 
 		void *mutex;
 		void *cond;
@@ -129,6 +131,7 @@ namespace thread
 		void wait();
 		std::string getName();
 		ThreadVariant *get(const std::string & name);
+		std::vector<std::string> getKeys();
 		ThreadVariant *demand(const std::string & name);
 		void clear(const std::string & name);
 		void set(const std::string & name, ThreadVariant *v);

+ 18 - 0
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<std::string> keys = t->getKeys();
+		t->unlock();
+		lua_createtable(L, keys.size(), 0);
+		int i = 0;
+		for (std::vector<std::string>::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 },

+ 1 - 0
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);