From 2b9a167455fe4315534dd0cea85ff852bedab998 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 17 Dec 2006 07:06:53 +0000 Subject: [PATCH] add RageDriver to merge duplicate boilerplate code --- stepmania/src/arch/RageDriver.cpp | 47 ++++++++++++++++++++ stepmania/src/arch/RageDriver.h | 47 ++++++++++++++++++++ stepmania/src/arch/Sound/RageSoundDriver.cpp | 24 +++++----- stepmania/src/arch/Sound/RageSoundDriver.h | 9 ++-- 4 files changed, 108 insertions(+), 19 deletions(-) create mode 100644 stepmania/src/arch/RageDriver.cpp create mode 100644 stepmania/src/arch/RageDriver.h diff --git a/stepmania/src/arch/RageDriver.cpp b/stepmania/src/arch/RageDriver.cpp new file mode 100644 index 0000000000..c23fd658b8 --- /dev/null +++ b/stepmania/src/arch/RageDriver.cpp @@ -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; + + 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::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. + */ diff --git a/stepmania/src/arch/RageDriver.h b/stepmania/src/arch/RageDriver.h new file mode 100644 index 0000000000..c25797f5d2 --- /dev/null +++ b/stepmania/src/arch/RageDriver.h @@ -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 *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. + */ diff --git a/stepmania/src/arch/Sound/RageSoundDriver.cpp b/stepmania/src/arch/Sound/RageSoundDriver.cpp index 6590d16feb..cd9f4a7830 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver.cpp +++ b/stepmania/src/arch/Sound/RageSoundDriver.cpp @@ -4,14 +4,11 @@ #include "RageUtil.h" #include "Foreach.h" -map *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; - - 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::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( pDriver ); + ASSERT( pRet != NULL ); + const RString sError = pRet->Init(); - if( sError.empty() ) { LOG->Info( "Sound driver: %s", Driver->c_str() ); diff --git a/stepmania/src/arch/Sound/RageSoundDriver.h b/stepmania/src/arch/Sound/RageSoundDriver.h index 3f6a4263ea..ec2ea3fadd 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver.h +++ b/stepmania/src/arch/Sound/RageSoundDriver.h @@ -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 *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 ) + static RegisterSoundDriver register_##x( #name, CreateClass ) #define REGISTER_SOUND_DRIVER_CLASS( name ) REGISTER_SOUND_DRIVER_CLASS2( name, name )