Add Lua::TableInsert() helper

develop
lethosor 2016-10-15 14:55:48 -04:00
parent 8a138fcc4c
commit 4c21bbd5ae
3 changed files with 15 additions and 22 deletions

@ -341,6 +341,14 @@ namespace DFHack {namespace Lua {
DFHACK_EXPORT int PushPosXYZ(lua_State *state, df::coord pos); DFHACK_EXPORT int PushPosXYZ(lua_State *state, df::coord pos);
DFHACK_EXPORT int PushPosXY(lua_State *state, df::coord2d pos); DFHACK_EXPORT int PushPosXY(lua_State *state, df::coord2d pos);
template <typename T_Key, typename T_Value>
inline void TableInsert(lua_State *state, T_Key key, T_Value value)
{
Lua::Push(state, key);
Lua::Push(state, value);
lua_settable(state, -3);
}
DFHACK_EXPORT void CheckPen(lua_State *L, Screen::Pen *pen, int index, bool allow_nil = false, bool allow_color = true); DFHACK_EXPORT void CheckPen(lua_State *L, Screen::Pen *pen, int index, bool allow_nil = false, bool allow_color = true);
DFHACK_EXPORT bool IsCoreContext(lua_State *state); DFHACK_EXPORT bool IsCoreContext(lua_State *state);

@ -176,19 +176,12 @@ namespace conf_lua {
{ {
Lua::Push(l_state, val); Lua::Push(l_state, val);
} }
template <typename KeyType, typename ValueType>
void table_set (lua_State *L, KeyType k, ValueType v)
{
Lua::Push(L, k);
Lua::Push(L, v);
lua_settable(L, -3);
}
namespace api { namespace api {
int get_ids (lua_State *L) int get_ids (lua_State *L)
{ {
lua_newtable(L); lua_newtable(L);
for (auto it = confirmations.begin(); it != confirmations.end(); ++it) for (auto it = confirmations.begin(); it != confirmations.end(); ++it)
table_set(L, it->first, true); Lua::TableInsert(L, it->first, true);
return 1; return 1;
} }
int get_conf_data (lua_State *L) int get_conf_data (lua_State *L)
@ -199,8 +192,8 @@ namespace conf_lua {
{ {
Lua::Push(L, i++); Lua::Push(L, i++);
lua_newtable(L); lua_newtable(L);
table_set(L, "id", it->first); Lua::TableInsert(L, "id", it->first);
table_set(L, "enabled", it->second->is_enabled()); Lua::TableInsert(L, "enabled", it->second->is_enabled());
lua_settable(L, -3); lua_settable(L, -3);
} }
return 1; return 1;

@ -187,14 +187,6 @@ namespace dm_lua {
return safe_call(nargs); return safe_call(nargs);
} }
template <typename KeyType, typename ValueType>
void table_set (lua_State *L, KeyType k, ValueType v)
{
Lua::Push(L, k);
Lua::Push(L, v);
lua_settable(L, -3);
}
namespace api { namespace api {
int monitor_state (lua_State *L) int monitor_state (lua_State *L)
{ {
@ -229,7 +221,7 @@ namespace dm_lua {
} }
} }
lua_newtable(L); lua_newtable(L);
#define WTYPE(type, name) dm_lua::table_set(L, #type, type); #define WTYPE(type, name) Lua::TableInsert(L, #type, type);
WEATHER_TYPES WEATHER_TYPES
#undef WTYPE #undef WTYPE
#undef WEATHER_TYPES #undef WEATHER_TYPES
@ -242,9 +234,9 @@ namespace dm_lua {
{ {
Lua::Push(L, i); Lua::Push(L, i);
lua_newtable(L); lua_newtable(L);
dm_lua::table_set(L, "value", misery[i]); Lua::TableInsert(L, "value", misery[i]);
dm_lua::table_set(L, "color", monitor_colors[i]); Lua::TableInsert(L, "color", monitor_colors[i]);
dm_lua::table_set(L, "last", (i == 6)); Lua::TableInsert(L, "last", (i == 6));
lua_settable(L, -3); lua_settable(L, -3);
} }
return 1; return 1;