From eb376fca1d15284057d5948133ad30f35848444a Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 21 Sep 2006 04:20:08 +0000 Subject: [PATCH] simplify singleton registration. Add the singleton global to Lua when the singleton is constructed, and not in the binding class. The binding just adds the class bindings (the same as for a non-singleton class). This way, the binding is available to Lua as soon as the class is constructed. --- stepmania/src/ScreenManager.cpp | 22 ++++++++++++---------- stepmania/src/StatsManager.cpp | 25 +++++++++++++++---------- stepmania/src/StatsManager.h | 1 + stepmania/src/ThemeManager.cpp | 22 ++++++++++++---------- 4 files changed, 40 insertions(+), 30 deletions(-) diff --git a/stepmania/src/ScreenManager.cpp b/stepmania/src/ScreenManager.cpp index 64e79b7f78..9ae73da094 100644 --- a/stepmania/src/ScreenManager.cpp +++ b/stepmania/src/ScreenManager.cpp @@ -226,6 +226,15 @@ void RegisterScreenClass( const RString& sClassName, CreateScreenFn pfn ) ScreenManager::ScreenManager() { + // Register with Lua. + { + Lua *L = LUA->Get(); + lua_pushstring( L, "SCREENMAN" ); + this->PushSelf( L ); + lua_settable( L, LUA_GLOBALSINDEX ); + LUA->Release( L ); + } + g_pSharedBGA = new Actor; m_bZeroNextUpdate = false; @@ -249,6 +258,9 @@ ScreenManager::~ScreenManager() for( unsigned i=0; iUnsetGlobal( "SCREENMAN" ); } /* This is called when we start up, and when the theme changes or is reloaded. */ @@ -942,16 +954,6 @@ public: ADD_METHOD( ScreenIsPrepped ); Luna::Register( L ); - - // Add global singleton if constructed already. If it's not constructed yet, - // then we'll register it later when we reinit Lua just before - // initializing the display. - if( SCREENMAN ) - { - lua_pushstring(L, "SCREENMAN"); - SCREENMAN->PushSelf( L ); - lua_settable(L, LUA_GLOBALSINDEX); - } } }; diff --git a/stepmania/src/StatsManager.cpp b/stepmania/src/StatsManager.cpp index 5d40c40d37..bc2d8a7479 100644 --- a/stepmania/src/StatsManager.cpp +++ b/stepmania/src/StatsManager.cpp @@ -7,12 +7,27 @@ #include "PrefsManager.h" #include "Steps.h" #include "StyleUtil.h" +#include "LuaManager.h" StatsManager* STATSMAN = NULL; // global object accessable from anywhere in the program StatsManager::StatsManager() { + // Register with Lua. + { + Lua *L = LUA->Get(); + lua_pushstring( L, "STATSMAN" ); + STATSMAN->PushSelf( L ); + lua_settable(L, LUA_GLOBALSINDEX); + LUA->Release( L ); + } +} + +StatsManager::~StatsManager() +{ + // Unregister with Lua. + LUA->UnsetGlobal( "STATSMAN" ); } void StatsManager::Reset() @@ -253,16 +268,6 @@ public: ADD_METHOD( GetWorstGrade ); Luna::Register( L ); - - // Add global singleton if constructed already. If it's not constructed yet, - // then we'll register it later when we reinit Lua just before - // initializing the display. - if( STATSMAN ) - { - lua_pushstring(L, "STATSMAN"); - STATSMAN->PushSelf( L ); - lua_settable(L, LUA_GLOBALSINDEX); - } } }; diff --git a/stepmania/src/StatsManager.h b/stepmania/src/StatsManager.h index f0bdc0de24..d367f63c10 100644 --- a/stepmania/src/StatsManager.h +++ b/stepmania/src/StatsManager.h @@ -9,6 +9,7 @@ class StatsManager { public: StatsManager(); + ~StatsManager(); void Reset(); diff --git a/stepmania/src/ThemeManager.cpp b/stepmania/src/ThemeManager.cpp index 7e0f37286b..766e8bf55a 100644 --- a/stepmania/src/ThemeManager.cpp +++ b/stepmania/src/ThemeManager.cpp @@ -149,6 +149,15 @@ ThemeManager::ThemeManager() { THEME = this; // so that we can Register THEME on construction + // Register with Lua. + { + Lua *L = LUA->Get(); + lua_pushstring(L, "THEME"); + this->PushSelf( L ); + lua_settable( L, LUA_GLOBALSINDEX ); + LUA->Release( L ); + } + /* We don't have any theme loaded until SwitchThemeAndLanguage is called. */ m_sCurThemeName = ""; m_bPseudoLocalize = false; @@ -161,6 +170,9 @@ ThemeManager::~ThemeManager() { g_vThemes.clear(); SAFE_DELETE( g_pLoadedThemeData ); + + // Unregister with Lua. + LUA->UnsetGlobal( "THEME" ); } void ThemeManager::GetThemeNames( vector& AddTo ) @@ -1150,16 +1162,6 @@ public: ADD_METHOD( GetNumSelectableThemes ); Luna::Register( L ); - - // Add global singleton if constructed already. If it's not constructed yet, - // then we'll register it later when we reinit Lua just before - // initializing the display. - if( THEME ) - { - lua_pushstring(L, "THEME"); - THEME->PushSelf( L ); - lua_settable(L, LUA_GLOBALSINDEX); - } } };