simpler Lua boilerplate

update singleton registration method
This commit is contained in:
Glenn Maynard
2006-09-27 20:22:20 +00:00
parent 8ef5aeb341
commit df887f4efd
2 changed files with 30 additions and 30 deletions
+15 -15
View File
@@ -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<ProfileManager>
{
public:
LunaProfileManager() { LUA->Register( Register ); }
static int IsPersistentProfile( T* p, lua_State *L ) { lua_pushboolean(L, p->IsPersistentProfile(Enum::Check<PlayerNumber>(L, 1)) ); return 1; }
static int GetProfile( T* p, lua_State *L ) { PlayerNumber pn = Enum::Check<PlayerNumber>(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<T>::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);
}
}
};
+15 -15
View File
@@ -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<WorkoutManager>
{
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<T>::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);
}
}
};