From df887f4efd904945ffe713fc8d655d0ac534afbc Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Wed, 27 Sep 2006 20:22:20 +0000 Subject: [PATCH] simpler Lua boilerplate update singleton registration method --- stepmania/src/ProfileManager.cpp | 30 +++++++++++++++--------------- stepmania/src/WorkoutManager.cpp | 30 +++++++++++++++--------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/stepmania/src/ProfileManager.cpp b/stepmania/src/ProfileManager.cpp index 45a1942db0..b7863a344d 100644 --- a/stepmania/src/ProfileManager.cpp +++ b/stepmania/src/ProfileManager.cpp @@ -66,10 +66,22 @@ ProfileManager::ProfileManager() m_pMachineProfile = new Profile; FOREACH_PlayerNumber(pn) m_pMemoryCardProfile[pn] = new Profile; + + // Register with Lua. + { + Lua *L = LUA->Get(); + lua_pushstring( L, "PROFILEMAN" ); + this->PushSelf( L ); + lua_settable( L, LUA_GLOBALSINDEX ); + LUA->Release( L ); + } } ProfileManager::~ProfileManager() { + // Unregister with Lua. + LUA->UnsetGlobal( "PROFILEMAN" ); + SAFE_DELETE( m_pMachineProfile ); FOREACH_PlayerNumber(pn) SAFE_DELETE( m_pMemoryCardProfile[pn] ); @@ -821,8 +833,6 @@ int ProfileManager::GetNumLocalProfiles() const class LunaProfileManager: public Luna { public: - LunaProfileManager() { LUA->Register( Register ); } - static int IsPersistentProfile( T* p, lua_State *L ) { lua_pushboolean(L, p->IsPersistentProfile(Enum::Check(L, 1)) ); return 1; } static int GetProfile( T* p, lua_State *L ) { PlayerNumber pn = Enum::Check(L, 1); Profile* pP = p->GetProfile(pn); ASSERT(pP); pP->PushSelf(L); return 1; } static int GetMachineProfile( T* p, lua_State *L ) { p->GetMachineProfile()->PushSelf(L); return 1; } @@ -841,8 +851,10 @@ public: static int GetLocalProfileIndexFromID( T* p, lua_State *L ) { lua_pushnumber(L, p->GetLocalProfileIndexFromID(SArg(1)) ); return 1; } static int GetNumLocalProfiles( T* p, lua_State *L ) { lua_pushnumber(L, p->GetNumLocalProfiles() ); return 1; } - static void Register(lua_State *L) + LunaProfileManager() { + LUA->Register( Register ); + ADD_METHOD( IsPersistentProfile ); ADD_METHOD( GetProfile ); ADD_METHOD( GetMachineProfile ); @@ -852,18 +864,6 @@ public: ADD_METHOD( GetLocalProfileIDFromIndex ); ADD_METHOD( GetLocalProfileIndexFromID ); ADD_METHOD( GetNumLocalProfiles ); - - 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( PROFILEMAN ) - { - lua_pushstring(L, "PROFILEMAN"); - PROFILEMAN->PushSelf( L ); - lua_settable(L, LUA_GLOBALSINDEX); - } } }; diff --git a/stepmania/src/WorkoutManager.cpp b/stepmania/src/WorkoutManager.cpp index a41a9aab55..3c4d4b5e17 100644 --- a/stepmania/src/WorkoutManager.cpp +++ b/stepmania/src/WorkoutManager.cpp @@ -17,10 +17,22 @@ WorkoutManager::WorkoutManager() { m_pCurWorkout = NULL; m_pTempCourse = new Course; + + // Register with Lua. + { + Lua *L = LUA->Get(); + lua_pushstring( L, "WORKOUTMAN" ); + this->PushSelf( L ); + lua_settable(L, LUA_GLOBALSINDEX); + LUA->Release( L ); + } } WorkoutManager::~WorkoutManager() { + // Unregister with Lua. + LUA->UnsetGlobal( "WORKOUTMAN" ); + FOREACH( Workout*, m_vpAllWorkouts, p ) SAFE_DELETE( *p ); m_vpAllWorkouts.clear(); @@ -165,29 +177,17 @@ static RString GetWorkoutSongTitleText() class LunaWorkoutManager: public Luna { public: - LunaWorkoutManager() { LUA->Register( Register ); } - static int GetCurrentWorkout( T* p, lua_State *L ) { if(p->m_pCurWorkout) p->m_pCurWorkout->PushSelf(L); else lua_pushnil(L); return 1; } static int GetWorkoutSongsOverview( T* p, lua_State *L ) { lua_pushstring( L, ::GetWorkoutSongsOverview() ); return 1; } static int GetWorkoutSongTitleText( T* p, lua_State *L ) { lua_pushstring( L, ::GetWorkoutSongTitleText() ); return 1; } - static void Register(lua_State *L) + LunaWorkoutManager() { + LUA->Register( Register ); + ADD_METHOD( GetCurrentWorkout ); ADD_METHOD( GetWorkoutSongsOverview ); ADD_METHOD( GetWorkoutSongTitleText ); - - 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( true ) - { - lua_pushstring(L, "WORKOUTMAN"); - WORKOUTMAN->PushSelf( L ); - lua_settable(L, LUA_GLOBALSINDEX); - } } };