From bac085dc6d57db4b8de25a9a4076444bfc0fda61 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Tue, 17 Aug 2004 06:35:42 +0000 Subject: [PATCH] simplify --- stepmania/src/MemoryCardManager.cpp | 106 +++++++----------- .../src/arch/MemoryCard/MemoryCardDriver.cpp | 2 +- .../src/arch/MemoryCard/MemoryCardDriver.h | 19 +--- .../MemoryCardDriverThreaded_Linux.cpp | 37 ++---- 4 files changed, 52 insertions(+), 112 deletions(-) diff --git a/stepmania/src/MemoryCardManager.cpp b/stepmania/src/MemoryCardManager.cpp index 57aebaaaa8..e3fe73e8f9 100644 --- a/stepmania/src/MemoryCardManager.cpp +++ b/stepmania/src/MemoryCardManager.cpp @@ -47,16 +47,8 @@ void MemoryCardManager::Update( float fDelta ) // check for disconnects FOREACH( UsbStorageDevice, vOld, old ) { - bool bFoundMatch = false; - FOREACH( UsbStorageDevice, vNew, newd ) - { - if( old->IdsMatch(*newd) ) - { - bFoundMatch = true; - break; - } - } - if( !bFoundMatch ) + vector::iterator iter = find( vNew.begin(), vNew.end(), *old ); + if( iter == vNew.end() ) // card no longer present { LOG->Trace( "Disconnected bus %d port %d device %d path %s", old->iBus, old->iPort, old->iLevel, old->sOsMountDir.c_str() ); vDisconnects.push_back( *old ); @@ -66,16 +58,8 @@ void MemoryCardManager::Update( float fDelta ) // check for connects FOREACH( UsbStorageDevice, vNew, newd ) { - bool bFoundMatch = false; - FOREACH( UsbStorageDevice, vOld, old ) - { - if( old->IdsMatch(*newd) ) - { - bFoundMatch = true; - break; - } - } - if( !bFoundMatch ) + vector::iterator iter = find( vOld.begin(), vOld.end(), *newd ); + if( iter == vOld.end() ) // card wasn't present last update { LOG->Trace( "Connected bus %d port %d device %d path %s", newd->iBus, newd->iPort, newd->iLevel, newd->sOsMountDir.c_str() ); vDisconnects.push_back( *newd ); @@ -89,18 +73,16 @@ void MemoryCardManager::Update( float fDelta ) if( assigned_device.IsBlank() ) // not assigned a card continue; - FOREACH( UsbStorageDevice, vDisconnects, disconnect ) + vector::iterator iter = find( vDisconnects.begin(), vDisconnects.end(), assigned_device ); + if( iter != vDisconnects.end() ) { - if( disconnect->IdsMatch(assigned_device) ) - { - m_pDriver->Unmount(&assigned_device, MEM_CARD_MOUNT_POINT[p]); - - assigned_device.MakeBlank(); - m_soundDisconnect.Play(); - - if( PROFILEMAN->ProfileWasLoadedFromMemoryCard(p) ) - PROFILEMAN->UnloadProfile( p ); - } + m_pDriver->Unmount(&assigned_device, MEM_CARD_MOUNT_POINT[p]); + + assigned_device.MakeBlank(); + m_soundDisconnect.Play(); + + if( PROFILEMAN->ProfileWasLoadedFromMemoryCard(p) ) + PROFILEMAN->UnloadProfile( p ); } } @@ -112,15 +94,13 @@ void MemoryCardManager::Update( float fDelta ) if( assigned_device.IsBlank() ) // no card assigned to this player continue; - FOREACH( UsbStorageDevice, m_vStorageDevices, d ) + vector::iterator iter = find( m_vStorageDevices.begin(), m_vStorageDevices.end(), assigned_device ); + if( iter != m_vStorageDevices.end() ) { - if( d->IdsMatch(assigned_device) ) - { - // play write test error sound if write test failed since we last checked - if( assigned_device.bNeedsWriteTest && !d->bNeedsWriteTest && !d->bWriteTestSucceeded ) - m_soundError.Play(); - assigned_device = *d; - } + // play write test error sound if write test failed since we last checked + if( assigned_device.bNeedsWriteTest && !iter->bNeedsWriteTest && !iter->bWriteTestSucceeded ) + m_soundError.Play(); + assigned_device = *iter; } } @@ -136,7 +116,7 @@ void MemoryCardManager::Update( float fDelta ) FOREACH( UsbStorageDevice, vUnassignedDevices, d ) { - if( d->IdsMatch(assigned_device) ) + if( *d == assigned_device ) { vUnassignedDevices.erase( d ); break; @@ -297,26 +277,26 @@ bool IsAnyPlayerUsingMemoryCard() LuaFunction_NoArgs( IsAnyPlayerUsingMemoryCard, IsAnyPlayerUsingMemoryCard() ) /* -* (c) 2003-2004 Chris Danford -* 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. -*/ + * (c) 2003-2004 Chris Danford + * 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/MemoryCard/MemoryCardDriver.cpp b/stepmania/src/arch/MemoryCard/MemoryCardDriver.cpp index 0fa5c37921..7eb23e4ab2 100644 --- a/stepmania/src/arch/MemoryCard/MemoryCardDriver.cpp +++ b/stepmania/src/arch/MemoryCard/MemoryCardDriver.cpp @@ -4,7 +4,7 @@ #include "arch/arch_platform.h" -bool UsbStorageDevice::IdsMatch(const UsbStorageDevice& other) const +bool UsbStorageDevice::operator==(const UsbStorageDevice& other) const { // LOG->Trace( "Comparing %d %d %d %s %s to %d %d %d %s %s", // iBus, iPort, iLevel, sName.c_str(), sOsMountDir.c_str(), diff --git a/stepmania/src/arch/MemoryCard/MemoryCardDriver.h b/stepmania/src/arch/MemoryCard/MemoryCardDriver.h index 917c024ec9..aeeafe5f22 100644 --- a/stepmania/src/arch/MemoryCard/MemoryCardDriver.h +++ b/stepmania/src/arch/MemoryCard/MemoryCardDriver.h @@ -32,24 +32,7 @@ struct UsbStorageDevice bool IsBlank() { return sOsMountDir.empty(); } - bool operator==(const UsbStorageDevice& other) const - { -#define COMPARE(x) if( x != other.x ) return false; - COMPARE( iBus ); - COMPARE( iPort ); - COMPARE( iLevel ); - COMPARE( sSerial ); - COMPARE( iScsiIndex ); - COMPARE( sScsiDevice ); - COMPARE( sOsMountDir ); - COMPARE( sName ); - COMPARE( bNeedsWriteTest ); - COMPARE( bWriteTestSucceeded ); - COMPARE( sOsMountDir ); -#undef COMPARE - return true; - } - bool IdsMatch(const UsbStorageDevice& other) const; + bool operator==(const UsbStorageDevice& other) const; }; class MemoryCardDriver diff --git a/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Linux.cpp b/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Linux.cpp index f21fea33d5..3df00f69eb 100644 --- a/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Linux.cpp +++ b/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Linux.cpp @@ -223,29 +223,15 @@ void MemoryCardDriverThreaded_Linux::MountThreadDoOneUpdate() vector vDisconnects; FOREACH( UsbStorageDevice, vOld, old ) { - bool bMatch = false; - FOREACH( UsbStorageDevice, vNew, newd ) - { - if( old->IdsMatch(*newd) ) - { - bMatch = true; - break; - } - } - - if( !bMatch ) // didn't find + vector::iterator iter = find( vNew.begin(), vNew.end(), *old ); + if( iter == vNew.end() ) // didn't find { LOG->Trace( "Disconnected bus %d port %d level %d path %s", old->iBus, old->iPort, old->iLevel, old->sOsMountDir.c_str() ); vDisconnects.push_back( *old ); - FOREACH( UsbStorageDevice, m_vDevicesLastSeen, d ) - { - if( old->IdsMatch(*d) ) - { - m_vDevicesLastSeen.erase( d ); - break; - } - } + vector::iterator iter = find( m_vDevicesLastSeen.begin(), m_vDevicesLastSeen.end(), *old ); + ASSERT( iter != m_vDevicesLastSeen.end() ); + m_vDevicesLastSeen.erase( iter ); } } @@ -254,17 +240,8 @@ void MemoryCardDriverThreaded_Linux::MountThreadDoOneUpdate() vector vConnects; FOREACH( UsbStorageDevice, vNew, newd ) { - bool bMatch = false; - FOREACH( UsbStorageDevice, vOld, old ) - { - if( old->IdsMatch(*newd) ) - { - bMatch = true; - break; - } - } - - if( !bMatch ) // didn't find + vector::iterator iter = find( vOld.begin(), vOld.end(), *newd ); + if( iter == vOld.end() ) // didn't find { LOG->Trace( "Connected bus %d port %d level %d path %s", newd->iBus, newd->iPort, newd->iLevel, newd->sOsMountDir.c_str() ); vConnects.push_back( *newd );