From c423e070aa6a67a24a30f7fd475e5da89f39aea1 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 13 Feb 2005 04:00:48 +0000 Subject: [PATCH] Move Lua reference handling into a generic base class. --- stepmania/src/LuaReference.cpp | 124 +++++++++++++++++++++++++++++++++ stepmania/src/LuaReference.h | 68 ++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 stepmania/src/LuaReference.cpp create mode 100644 stepmania/src/LuaReference.h diff --git a/stepmania/src/LuaReference.cpp b/stepmania/src/LuaReference.cpp new file mode 100644 index 0000000000..906e66a1b7 --- /dev/null +++ b/stepmania/src/LuaReference.cpp @@ -0,0 +1,124 @@ +#include "global.h" +#include "LuaReference.h" +#include "LuaManager.h" +#include "LuaBinding.h" +#include "Foreach.h" +#include "SubscriptionManager.h" + +template<> +set* SubscriptionManager::s_pSubscribers = NULL; + +LuaReference::LuaReference() +{ + m_iReference = LUA_NOREF; + SubscriptionManager::Subscribe( this ); +} + +LuaReference::~LuaReference() +{ + Unregister(); + SubscriptionManager::Unsubscribe( this ); +} + +LuaReference::LuaReference( const LuaReference &cpy ) +{ + SubscriptionManager::Subscribe( this ); + + /* Make a new reference. */ + lua_rawgeti( LUA->L, LUA_REGISTRYINDEX, cpy.m_iReference ); + m_iReference = luaL_ref( LUA->L, LUA_REGISTRYINDEX ); +} + +LuaReference &LuaReference::operator=( const LuaReference &cpy ) +{ + if( this == &cpy ) + return *this; + + Unregister(); + + /* Make a new reference. */ + lua_rawgeti( LUA->L, LUA_REGISTRYINDEX, cpy.m_iReference ); + m_iReference = luaL_ref( LUA->L, LUA_REGISTRYINDEX ); + + return *this; +} + +void LuaReference::SetFromStack() +{ + Unregister(); + + m_iReference = luaL_ref( LUA->L, LUA_REGISTRYINDEX ); +} + +void LuaReference::SetFromNil() +{ + Unregister(); + m_iReference = LUA_REFNIL; +} + +void LuaReference::PushSelf( lua_State *L ) const +{ + lua_rawgeti( LUA->L, LUA_REGISTRYINDEX, m_iReference ); +} + +bool LuaReference::IsSet() const +{ + return m_iReference != LUA_NOREF; +} + +void LuaReference::Register() +{ +} + +void LuaReference::Unregister() +{ + if( LUA == NULL ) + return; // nothing to do + + luaL_unref( LUA->L, LUA_REGISTRYINDEX, m_iReference ); + m_iReference = LUA_NOREF; +} + +void LuaReference::ReRegisterAll() +{ + if( SubscriptionManager::s_pSubscribers == NULL ) + return; + FOREACHS( LuaReference*, *SubscriptionManager::s_pSubscribers, p ) + (*p)->ReRegister(); +} + +void LuaReference::ReRegister() +{ + /* When this is called, the Lua state has been wiped. Don't try to unregister our + * old function reference, since it's already gone (and the number may point + * somewhere else). */ + m_iReference = LUA_NOREF; + + Register(); +} + + +/* + * (c) 2005 Glenn Maynard, Chris Danford + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, and/or sell copies of the Software, and to permit persons to + * whom the Software is furnished to do so, provided that the above + * copyright notice(s) and this permission notice appear in all copies of + * the Software and that both the above copyright notice(s) and this + * permission notice appear in supporting documentation. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF + * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS + * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT + * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ diff --git a/stepmania/src/LuaReference.h b/stepmania/src/LuaReference.h new file mode 100644 index 0000000000..4ce5187bf1 --- /dev/null +++ b/stepmania/src/LuaReference.h @@ -0,0 +1,68 @@ +/* LuaReference - a self-cleaning Lua reference. */ + +#ifndef LUA_REFERENCE_H +#define LUA_REFERENCE_H + +struct lua_State; + +class LuaReference +{ +public: + LuaReference(); + virtual ~LuaReference(); + + /* Copying a reference makes a new reference pointing to the same object. */ + LuaReference( const LuaReference &cpy ); + LuaReference &operator=( const LuaReference &cpy ); + + /* Create a reference pointing to the item at the top of the stack, and pop + * the stack. */ + void SetFromStack(); + void SetFromNil(); + + /* Push the referenced object onto the stack. If not set (or set to nil), push nil. */ + virtual void PushSelf( lua_State *L ) const; + + /* Return true if set. (SetFromNil() counts as being set.) */ + bool IsSet() const; + + static void ReRegisterAll(); // call this after resetting Lua + +protected: + /* If this object is able to recreate itself, overload this, and ReRegisterAll + * will work; the reference will still exist after a theme change. If not + * implemented, the reference will be unset after ReRegister. */ + virtual void Register(); + +private: + void Unregister(); + void ReRegister(); + int m_iReference; +}; + +#endif + +/* + * (c) 2005 Glenn Maynard, Chris Danford + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, and/or sell copies of the Software, and to permit persons to + * whom the Software is furnished to do so, provided that the above + * copyright notice(s) and this permission notice appear in all copies of + * the Software and that both the above copyright notice(s) and this + * permission notice appear in supporting documentation. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF + * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS + * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT + * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */