# HG changeset patch # User David Demelier # Date 1388854926 -3600 # Node ID 99d0887395cc00419be9167b70c449f6281263db # Parent 8ee4a34e166c6d8e4c2c21a418eee556cc47c7b1 Luae: Add set() function for LuaeTable diff -r 8ee4a34e166c -r 99d0887395cc C++/Luae.cpp --- a/C++/Luae.cpp Sat Jan 04 16:59:09 2014 +0100 +++ b/C++/Luae.cpp Sat Jan 04 18:02:06 2014 +0100 @@ -205,6 +205,84 @@ return value; } +template <> +void LuaeTable::set(lua_State *L, + int idx, + const std::string &name, + const bool &value) +{ + LUAE_STACK_CHECKBEGIN(L); + lua_pushboolean(L, value); + lua_setfield(L, (idx < 0) ? idx - 1 : idx, name.c_str()); + LUAE_STACK_CHECKEQUALS(L); +} + +template <> +void LuaeTable::set(lua_State *L, + int idx, + const std::string &name, + const double &value) +{ + LUAE_STACK_CHECKBEGIN(L); + lua_pushnumber(L, value); + lua_setfield(L, (idx < 0) ? idx - 1 : idx, name.c_str()); + LUAE_STACK_CHECKEQUALS(L); +} + +template <> +void LuaeTable::set(lua_State *L, + int idx, + const std::string &name, + const int &value) +{ + LUAE_STACK_CHECKBEGIN(L); + lua_pushinteger(L, value); + lua_setfield(L, (idx < 0) ? idx - 1 : idx, name.c_str()); + LUAE_STACK_CHECKEQUALS(L); +} + +template <> +void LuaeTable::set(lua_State *L, + int idx, + const std::string &name, + const std::string &value) +{ + LUAE_STACK_CHECKBEGIN(L); + lua_pushlstring(L, value.c_str(), value.length()); + lua_setfield(L, (idx < 0) ? idx - 1 : idx, name.c_str()); + LUAE_STACK_CHECKEQUALS(L); +} + +/* + * Version for const char * + */ +template <> +void LuaeTable::set(lua_State *L, + int idx, + const std::string &name, + const char * const &value) +{ + LUAE_STACK_CHECKBEGIN(L); + lua_pushstring(L, value); + lua_setfield(L, (idx < 0) ? idx - 1 : idx, name.c_str()); + LUAE_STACK_CHECKEQUALS(L); +} + +/* + * Version for char * + */ +template <> +void LuaeTable::set(lua_State *L, + int idx, + const std::string &name, + char * const &value) +{ + LUAE_STACK_CHECKBEGIN(L); + lua_pushstring(L, value); + lua_setfield(L, (idx < 0) ? idx - 1 : idx, name.c_str()); + LUAE_STACK_CHECKEQUALS(L); +} + int LuaeTable::type(lua_State *L, int idx, const std::string &name) { int type; diff -r 8ee4a34e166c -r 99d0887395cc C++/Luae.h --- a/C++/Luae.h Sat Jan 04 16:59:09 2014 +0100 +++ b/C++/Luae.h Sat Jan 04 18:02:06 2014 +0100 @@ -20,6 +20,7 @@ #define _LUA_H_ #include +#include #include #include #include @@ -169,6 +170,32 @@ static T get(lua_State *L, int idx, const std::string &name); /** + * Set a table field. Specialized for the same fields as get. + * + * @param L the Lua state + * @param idx the index + * @param name the field name + * @param value the value + * @see get + */ + template + static void set(lua_State *L, int idx, const std::string &name, const T &value); + + /** + * Overload for string literals. + * + * @param L the Lua state + * @param idx the index + * @param name the field name + * @param s the string + */ + template + static void set(lua_State *L, int idx, const std::string &name, const char (&s)[N]) + { + set(L, idx, name, s); + } + + /** * Require a field from a table. * * @param L the Lua state