add RageDriver to merge duplicate boilerplate code
This commit is contained in:
@@ -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.
|
||||||
|
*/
|
||||||
@@ -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.
|
||||||
|
*/
|
||||||
@@ -4,14 +4,11 @@
|
|||||||
#include "RageUtil.h"
|
#include "RageUtil.h"
|
||||||
#include "Foreach.h"
|
#include "Foreach.h"
|
||||||
|
|
||||||
map<istring, CreateSoundDriverFn> *RegisterSoundDriver::g_pRegistrees;
|
static DriverList g_pRegistrees;
|
||||||
RegisterSoundDriver::RegisterSoundDriver( const istring &sName, CreateSoundDriverFn pfn )
|
|
||||||
{
|
|
||||||
if( g_pRegistrees == NULL )
|
|
||||||
g_pRegistrees = new map<istring, CreateSoundDriverFn>;
|
|
||||||
|
|
||||||
ASSERT( g_pRegistrees->find(sName) == g_pRegistrees->end() );
|
RegisterSoundDriver::RegisterSoundDriver( const istring &sName, CreateRageDriverFn pfn )
|
||||||
(*g_pRegistrees)[sName] = pfn;
|
{
|
||||||
|
g_pRegistrees.Add( sName, pfn );
|
||||||
}
|
}
|
||||||
|
|
||||||
RageSoundDriver *MakeRageSoundDriver( const RString &drivers )
|
RageSoundDriver *MakeRageSoundDriver( const RString &drivers )
|
||||||
@@ -21,18 +18,17 @@ RageSoundDriver *MakeRageSoundDriver( const RString &drivers )
|
|||||||
|
|
||||||
FOREACH_CONST( RString, DriversToTry, Driver )
|
FOREACH_CONST( RString, DriversToTry, Driver )
|
||||||
{
|
{
|
||||||
map<istring, CreateSoundDriverFn>::const_iterator iter = RegisterSoundDriver::g_pRegistrees->find( istring(*Driver) );
|
RageDriver *pDriver = g_pRegistrees.Create( *Driver );
|
||||||
|
if( pDriver == NULL )
|
||||||
if( iter == RegisterSoundDriver::g_pRegistrees->end() )
|
|
||||||
{
|
{
|
||||||
LOG->Trace( "Unknown sound driver: %s", Driver->c_str() );
|
LOG->Trace( "Unknown sound driver: %s", Driver->c_str() );
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
RageSoundDriver *pRet = (iter->second)();
|
RageSoundDriver *pRet = dynamic_cast<RageSoundDriver *>( pDriver );
|
||||||
DEBUG_ASSERT( pRet );
|
ASSERT( pRet != NULL );
|
||||||
const RString sError = pRet->Init();
|
|
||||||
|
|
||||||
|
const RString sError = pRet->Init();
|
||||||
if( sError.empty() )
|
if( sError.empty() )
|
||||||
{
|
{
|
||||||
LOG->Info( "Sound driver: %s", Driver->c_str() );
|
LOG->Info( "Sound driver: %s", Driver->c_str() );
|
||||||
|
|||||||
@@ -2,9 +2,10 @@
|
|||||||
#define RAGE_SOUND_DRIVER
|
#define RAGE_SOUND_DRIVER
|
||||||
|
|
||||||
#include "RageUtil.h"
|
#include "RageUtil.h"
|
||||||
|
#include "arch/RageDriver.h"
|
||||||
|
|
||||||
class RageSoundBase;
|
class RageSoundBase;
|
||||||
class RageSoundDriver
|
class RageSoundDriver: public RageDriver
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
friend class RageSoundManager;
|
friend class RageSoundManager;
|
||||||
@@ -50,15 +51,13 @@ public:
|
|||||||
virtual ~RageSoundDriver() { }
|
virtual ~RageSoundDriver() { }
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef RageSoundDriver *(*CreateSoundDriverFn)();
|
|
||||||
struct RegisterSoundDriver
|
struct RegisterSoundDriver
|
||||||
{
|
{
|
||||||
static map<istring, CreateSoundDriverFn> *g_pRegistrees;
|
RegisterSoundDriver( const istring &sName, CreateRageDriverFn pfn );
|
||||||
RegisterSoundDriver( const istring &sName, CreateSoundDriverFn pfn );
|
|
||||||
};
|
};
|
||||||
// Can't use Create##name because many of these have -sw suffixes.
|
// Can't use Create##name because many of these have -sw suffixes.
|
||||||
#define REGISTER_SOUND_DRIVER_CLASS2( name, x ) \
|
#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 )
|
#define REGISTER_SOUND_DRIVER_CLASS( name ) REGISTER_SOUND_DRIVER_CLASS2( name, name )
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user