Mercurial > code
changeset 202:99d0887395cc
Luae: Add set() function for LuaeTable
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 04 Jan 2014 18:02:06 +0100 |
parents | 8ee4a34e166c |
children | 1ffe6d4937b7 |
files | C++/Luae.cpp C++/Luae.h |
diffstat | 2 files changed, 105 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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;
--- 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 <cassert> +#include <cstddef> #include <functional> #include <memory> #include <string> @@ -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 <typename T> + 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 <size_t N> + static void set(lua_State *L, int idx, const std::string &name, const char (&s)[N]) + { + set<const char *>(L, idx, name, s); + } + + /** * Require a field from a table. * * @param L the Lua state