From 627e5eb16a1d4e59b06e60db6ee28e2643f1c933 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Fri, 18 Mar 2005 22:40:09 +0000 Subject: [PATCH] add LuaTable --- stepmania/src/LuaReference.cpp | 32 ++++++++++++++++++++++++++++++++ stepmania/src/LuaReference.h | 17 +++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/stepmania/src/LuaReference.cpp b/stepmania/src/LuaReference.cpp index 06130a398d..1a16b08da1 100644 --- a/stepmania/src/LuaReference.cpp +++ b/stepmania/src/LuaReference.cpp @@ -201,6 +201,38 @@ void LuaData::Register() m_sSerializedData.erase( m_sSerializedData.begin(), m_sSerializedData.end() ); } +LuaTable::LuaTable() +{ + lua_newtable( LUA->L ); + this->SetFromStack(); +} + +void LuaTable::Set( const CString &sKey ) +{ + int iTop = lua_gettop( LUA->L ); + this->PushSelf( LUA->L ); + lua_pushstring( LUA->L, sKey ); // push the key + lua_pushvalue( LUA->L, iTop ); // push the value + lua_settable( LUA->L, iTop+1 ); + lua_settop( LUA->L, iTop-1 ); // remove all of the above +} + +void LuaTable::Unset( const CString &sKey ) +{ + lua_pushnil( LUA->L ); + Set( sKey ); +} + +void LuaTable::SetKeyAndValue() +{ + int iTop = lua_gettop( LUA->L ); + this->PushSelf( LUA->L ); + lua_pushvalue( LUA->L, iTop-1 ); // push the value after the table + lua_pushvalue( LUA->L, iTop ); // push the key after the value + lua_settable( LUA->L, iTop+1 ); + lua_settop( LUA->L, iTop-1 ); // remove all of the above +} + /* * (c) 2005 Glenn Maynard, Chris Danford * All rights reserved. diff --git a/stepmania/src/LuaReference.h b/stepmania/src/LuaReference.h index 4dc2a1b99d..8f15348b2e 100644 --- a/stepmania/src/LuaReference.h +++ b/stepmania/src/LuaReference.h @@ -80,6 +80,23 @@ protected: bool m_bWasSet; }; +class LuaTable: public LuaData +{ +public: + LuaTable(); + + /* Set a key by the given name to a value on the stack, and pop the value + * off the stack. */ + void Set( const CString &sKey ); + + /* Unset the given key (set it to nil). */ + void Unset( const CString &sKey ); + + /* Set a key on the stack to a value on the stack; push the key first. Pop + * both the key and the value off the stack. */ + void SetKeyAndValue(); +}; + #endif /*