]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Script.hh
settings subsystem now using lua
[chaz/yoink] / src / Moof / Script.hh
index b48f5f440ae04b49f3a6f778cf3e3e3eb4d3ccb1..f954e583488a786e2740c7042a4ab89782697577 100644 (file)
@@ -39,7 +39,9 @@
  */
 
 #include <list>
+#include <map>
 #include <string>
+#include <vector>
 
 #include <boost/bind.hpp>
 #include <boost/function.hpp>
@@ -48,6 +50,7 @@
 #include <lua.hpp>
 
 #include <Moof/Exception.hh>
+#include <Moof/Log.hh>
 
 
 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 <typename T>
-               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 <typename T>
+               bool get(std::vector<T>& 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 <typename T>
+               bool get(std::map<std::string,T>& 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 <typename T>
-               //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);
        }
This page took 0.022942 seconds and 4 git commands to generate.