X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FScript.hh;fp=src%2FMoof%2FScript.hh;h=f954e583488a786e2740c7042a4ab89782697577;hp=b48f5f440ae04b49f3a6f778cf3e3e3eb4d3ccb1;hb=ca0f7bdfba63140dca0bd20586d31980f3938eb2;hpb=660e768e64c2c30928c7f157d5ff34195a4347fa diff --git a/src/Moof/Script.hh b/src/Moof/Script.hh index b48f5f4..f954e58 100644 --- a/src/Moof/Script.hh +++ b/src/Moof/Script.hh @@ -39,7 +39,9 @@ */ #include +#include #include +#include #include #include @@ -48,6 +50,7 @@ #include #include +#include namespace Mf { @@ -119,11 +122,7 @@ struct Script * Value will be invalid even if index is manually changed to a valid * index. You have to index the script itself to get a valid Value. */ - Value() : - index(0), - state(0) {} - - Value(lua_State* s, int i) : + Value(lua_State* s = 0, int i = 0) : index(i), state(s) {} @@ -159,6 +158,23 @@ struct Script return std::string(lua_typename(state, (int)getType())); } + + /** + * Get the length of the value according to the definition given by Lua. + */ + + size_t getLength() const + { + return lua_objlen(state, index); + } + + int getRealIndex() const + { + if (index < 0) return lua_gettop(state) + 1 + index; + else return index; + } + + /** * Get a pointer value (for userdata, tables, threads, and functions). */ @@ -206,61 +222,115 @@ struct Script } - /** - * Get the length of the value according to the definition given by Lua. - */ - - size_t getLength() const - { - return lua_objlen(state, index); - } - - /** * Convert the underlying value to a C++ type. */ template - void get(T& value) const + bool get(T& value) const { - value = (T)lua_tointeger(state, index); + if (isNumber()) + { + value = (T)lua_tointeger(state, index); + return true; + } + return false; } - void get(bool& value) const + bool get(float& value) const { - value = (bool)lua_toboolean(state, index); + if (isNumber()) + { + value = (float)lua_tonumber(state, index); + return true; + } + return false; } - - void get(float& value) const + bool get(double& value) const { - value = (float)lua_tonumber(state, index); + if (isNumber()) + { + value = (double)lua_tonumber(state, index); + return true; + } + return false; } - void get(double& value) const + + bool get(bool& value) const { - value = (double)lua_tonumber(state, index); + if (isBoolean()) + { + value = (bool)lua_toboolean(state, index); + return true; + } + return false; } - void get(std::string& value) const + bool get(std::string& value) const { - size_t size; - const char* str = lua_tolstring(state, index, &size); - value.assign(str, size); + if (isString()) + { + size_t size; + const char* str = lua_tolstring(state, index, &size); + value.assign(str, size); + return true; + } + return false; } + template + bool get(std::vector& array) const + { + if (!isTable()) return false; + + array.clear(); - void set(std::string& value) + Value value(state, -1); + int realIndex = getRealIndex(); + + bool done = false; + for (int i = 1; !done; ++i) + { + lua_rawgeti(state, realIndex, i); + + T v; + if (value.get(v)) array.push_back(v); + else done = true; + + lua_pop(state, 1); + } + + return true; + } + + template + bool get(std::map& dictionary) const { + if (!isTable()) return false; + + dictionary.clear(); + + Value key(state, -2); + Value value(state, -1); + int realIndex = getRealIndex(); + + lua_pushnil(state); + while (lua_next(state, realIndex) != 0) + { + std::string k; + if (!key.isNumber() && key.get(k)) + { + T v; + if (value.get(v)) dictionary[k] = v; + } + lua_pop(state, 1); + } + lua_pop(state, 1); + + return true; } - //template - //void get(const std::string& field, T& value) const - //{ - ////lua_getfield(state_, field.c_str()); - //pushField(field); - //get(-1, value); - //lua_pop(state_, 1); - //} - + /** * Copy the value and push the copy to the stack. @@ -557,7 +627,7 @@ struct Script * Pops n values from the top of the stack. */ - void pop(int n) + void pop(int n = 1) { lua_pop(state_, n); }