*/
#include <list>
+#include <map>
#include <string>
+#include <vector>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <lua.hpp>
#include <Moof/Exception.hh>
+#include <Moof/Log.hh>
namespace Mf {
* 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) {}
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).
*/
}
- /**
- * 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.
* Pops n values from the top of the stack.
*/
- void pop(int n)
+ void pop(int n = 1)
{
lua_pop(state_, n);
}