Move Lua reference handling into a generic base class.
This commit is contained in:
@@ -0,0 +1,124 @@
|
|||||||
|
#include "global.h"
|
||||||
|
#include "LuaReference.h"
|
||||||
|
#include "LuaManager.h"
|
||||||
|
#include "LuaBinding.h"
|
||||||
|
#include "Foreach.h"
|
||||||
|
#include "SubscriptionManager.h"
|
||||||
|
|
||||||
|
template<>
|
||||||
|
set<LuaReference*>* SubscriptionManager<LuaReference>::s_pSubscribers = NULL;
|
||||||
|
|
||||||
|
LuaReference::LuaReference()
|
||||||
|
{
|
||||||
|
m_iReference = LUA_NOREF;
|
||||||
|
SubscriptionManager<LuaReference>::Subscribe( this );
|
||||||
|
}
|
||||||
|
|
||||||
|
LuaReference::~LuaReference()
|
||||||
|
{
|
||||||
|
Unregister();
|
||||||
|
SubscriptionManager<LuaReference>::Unsubscribe( this );
|
||||||
|
}
|
||||||
|
|
||||||
|
LuaReference::LuaReference( const LuaReference &cpy )
|
||||||
|
{
|
||||||
|
SubscriptionManager<LuaReference>::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<LuaReference>::s_pSubscribers == NULL )
|
||||||
|
return;
|
||||||
|
FOREACHS( LuaReference*, *SubscriptionManager<LuaReference>::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.
|
||||||
|
*/
|
||||||
@@ -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.
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user