add RageDriver to merge duplicate boilerplate code

This commit is contained in:
Glenn Maynard
2006-12-17 07:06:53 +00:00
parent 519e61ee48
commit 2b9a167455
4 changed files with 108 additions and 19 deletions
+47
View File
@@ -0,0 +1,47 @@
#include "global.h"
#include "RageDriver.h"
void DriverList::Add( const istring &sName, CreateRageDriverFn pfn )
{
if( m_pRegistrees == NULL )
m_pRegistrees = new map<istring, CreateRageDriverFn>;
ASSERT( m_pRegistrees->find(sName) == m_pRegistrees->end() );
(*m_pRegistrees)[sName] = pfn;
}
RageDriver *DriverList::Create( const RString &sDriverName )
{
if( m_pRegistrees == NULL )
return NULL;
map<istring, CreateRageDriverFn>::const_iterator iter = m_pRegistrees->find( istring(sDriverName) );
if( iter == m_pRegistrees->end() )
return NULL;
return (iter->second)();
}
/*
* (c) 2006 Glenn Maynard
* 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.
*/
+47
View File
@@ -0,0 +1,47 @@
#ifndef RAGE_DRIVER_H
#define RAGE_DRIVER_H
#include "RageUtil.h"
class RageDriver
{
public:
virtual ~RageDriver() { }
};
typedef RageDriver *(*CreateRageDriverFn)();
/* This is created and accessed during C++ static initialization; it must be a POD. */
struct DriverList
{
void Add( const istring &sName, CreateRageDriverFn pfn );
RageDriver *Create( const RString &sDriverName );
map<istring, CreateRageDriverFn> *m_pRegistrees;
};
#endif
/*
* (c) 2006 Glenn Maynard
* 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.
*/
+10 -14
View File
@@ -4,14 +4,11 @@
#include "RageUtil.h"
#include "Foreach.h"
map<istring, CreateSoundDriverFn> *RegisterSoundDriver::g_pRegistrees;
RegisterSoundDriver::RegisterSoundDriver( const istring &sName, CreateSoundDriverFn pfn )
static DriverList g_pRegistrees;
RegisterSoundDriver::RegisterSoundDriver( const istring &sName, CreateRageDriverFn pfn )
{
if( g_pRegistrees == NULL )
g_pRegistrees = new map<istring, CreateSoundDriverFn>;
ASSERT( g_pRegistrees->find(sName) == g_pRegistrees->end() );
(*g_pRegistrees)[sName] = pfn;
g_pRegistrees.Add( sName, pfn );
}
RageSoundDriver *MakeRageSoundDriver( const RString &drivers )
@@ -21,18 +18,17 @@ RageSoundDriver *MakeRageSoundDriver( const RString &drivers )
FOREACH_CONST( RString, DriversToTry, Driver )
{
map<istring, CreateSoundDriverFn>::const_iterator iter = RegisterSoundDriver::g_pRegistrees->find( istring(*Driver) );
if( iter == RegisterSoundDriver::g_pRegistrees->end() )
RageDriver *pDriver = g_pRegistrees.Create( *Driver );
if( pDriver == NULL )
{
LOG->Trace( "Unknown sound driver: %s", Driver->c_str() );
continue;
}
RageSoundDriver *pRet = (iter->second)();
DEBUG_ASSERT( pRet );
RageSoundDriver *pRet = dynamic_cast<RageSoundDriver *>( pDriver );
ASSERT( pRet != NULL );
const RString sError = pRet->Init();
if( sError.empty() )
{
LOG->Info( "Sound driver: %s", Driver->c_str() );
+4 -5
View File
@@ -2,9 +2,10 @@
#define RAGE_SOUND_DRIVER
#include "RageUtil.h"
#include "arch/RageDriver.h"
class RageSoundBase;
class RageSoundDriver
class RageSoundDriver: public RageDriver
{
public:
friend class RageSoundManager;
@@ -50,15 +51,13 @@ public:
virtual ~RageSoundDriver() { }
};
typedef RageSoundDriver *(*CreateSoundDriverFn)();
struct RegisterSoundDriver
{
static map<istring, CreateSoundDriverFn> *g_pRegistrees;
RegisterSoundDriver( const istring &sName, CreateSoundDriverFn pfn );
RegisterSoundDriver( const istring &sName, CreateRageDriverFn pfn );
};
// Can't use Create##name because many of these have -sw suffixes.
#define REGISTER_SOUND_DRIVER_CLASS2( name, x ) \
static RegisterSoundDriver register_##x( #name, CreateClass<RageSoundDriver_##x, RageSoundDriver> )
static RegisterSoundDriver register_##x( #name, CreateClass<RageSoundDriver_##x, RageDriver> )
#define REGISTER_SOUND_DRIVER_CLASS( name ) REGISTER_SOUND_DRIVER_CLASS2( name, name )