From d2ab30cc21a3d63dd5b228a1b9392eff7db0be4c Mon Sep 17 00:00:00 2001 From: Kohei Yoshida Date: Thu, 4 Aug 2011 22:21:31 -0400 Subject: Prefer std::for_each to manual looping. --- .../source/eventattachermgr/eventattachermgr.cxx | 71 ++++++++++------------ 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/comphelper/source/eventattachermgr/eventattachermgr.cxx b/comphelper/source/eventattachermgr/eventattachermgr.cxx index d1b27fae04..6736740494 100644 --- a/comphelper/source/eventattachermgr/eventattachermgr.cxx +++ b/comphelper/source/eventattachermgr/eventattachermgr.cxx @@ -29,8 +29,6 @@ // MARKER(update_precomp.py): autogen include statement, do not remove #include "precompiled_comphelper.hxx" -#include - #if defined( UNX ) #include #endif @@ -57,6 +55,9 @@ #include #include +#include +#include + using namespace com::sun::star::uno; using namespace com::sun::star::io; using namespace com::sun::star::lang; @@ -457,38 +458,36 @@ Reference< XIdlReflection > ImplEventAttacherManager::getReflection() throw( Exc return aIt; } -//----------------------------------------------------------------------------- -void detachAll_Impl -( - ImplEventAttacherManager * pMgr, - sal_Int32 nIdx, - ::std::deque< AttachedObject_Impl > & rList -) +namespace { + +class DetachObject : public std::unary_function { - ::std::deque< AttachedObject_Impl >::iterator aObjIt = rList.begin(); - ::std::deque< AttachedObject_Impl >::iterator aObjEnd = rList.end(); - while( aObjIt != aObjEnd ) + ImplEventAttacherManager& mrMgr; + sal_Int32 mnIdx; +public: + DetachObject(ImplEventAttacherManager& rMgr, sal_Int32 nIdx) : + mrMgr(rMgr), mnIdx(nIdx) {} + + void operator() (AttachedObject_Impl& rObj) { - pMgr->detach( nIdx, (*aObjIt).xTarget ); - ++aObjIt; + mrMgr.detach(mnIdx, rObj.xTarget); } -} +}; -//----------------------------------------------------------------------------- -void attachAll_Impl -( - ImplEventAttacherManager * pMgr, - sal_Int32 nIdx, - ::std::deque< AttachedObject_Impl > & rList -) +class AttachObject : public std::unary_function { - ::std::deque< AttachedObject_Impl >::iterator aObjIt = rList.begin(); - ::std::deque< AttachedObject_Impl >::iterator aObjEnd = rList.end(); - while( aObjIt != aObjEnd ) + ImplEventAttacherManager& mrMgr; + sal_Int32 mnIdx; +public: + AttachObject(ImplEventAttacherManager& rMgr, sal_Int32 nIdx) : + mrMgr(rMgr), mnIdx(nIdx) {} + + void operator() (AttachedObject_Impl& rObj) { - pMgr->attach( nIdx, (*aObjIt).xTarget, (*aObjIt).aHelper ); - ++aObjIt; + mrMgr.attach(mnIdx, rObj.xTarget, rObj.aHelper); } +}; + } //----------------------------------------------------------------------------- @@ -559,14 +558,14 @@ void SAL_CALL ImplEventAttacherManager::registerScriptEvents ::std::deque::iterator aIt = implCheckIndex( nIndex ); ::std::deque< AttachedObject_Impl > aList = (*aIt).aObjList; - detachAll_Impl( this, nIndex, aList ); + ::std::for_each(aList.begin(), aList.end(), DetachObject(*this, nIndex)); const ScriptEventDescriptor* pArray = ScriptEvents.getConstArray(); sal_Int32 nLen = ScriptEvents.getLength(); for( sal_Int32 i = 0 ; i < nLen ; i++ ) registerScriptEvent( nIndex, pArray[ i ] ); - attachAll_Impl( this, nIndex, aList ); + ::std::for_each(aList.begin(), aList.end(), AttachObject(*this, nIndex)); } //----------------------------------------------------------------------------- @@ -584,7 +583,7 @@ void SAL_CALL ImplEventAttacherManager::revokeScriptEvent ::std::deque::iterator aIt = implCheckIndex( nIndex ); ::std::deque< AttachedObject_Impl > aList = (*aIt).aObjList; - detachAll_Impl( this, nIndex, aList ); + ::std::for_each(aList.begin(), aList.end(), DetachObject(*this, nIndex)); OUString aLstType = ListenerType; const sal_Unicode * pLastDot = aLstType.getStr(); @@ -630,7 +629,7 @@ void SAL_CALL ImplEventAttacherManager::revokeScriptEvent } } #endif - attachAll_Impl( this, nIndex, aList ); + ::std::for_each(aList.begin(), aList.end(), AttachObject(*this, nIndex)); } //----------------------------------------------------------------------------- @@ -641,13 +640,13 @@ void SAL_CALL ImplEventAttacherManager::revokeScriptEvents(sal_Int32 nIndex ) ::std::deque::iterator aIt = implCheckIndex( nIndex ); ::std::deque< AttachedObject_Impl > aList = (*aIt).aObjList; - detachAll_Impl( this, nIndex, aList ); + ::std::for_each(aList.begin(), aList.end(), DetachObject(*this, nIndex)); #ifdef DEQUE_OK (*aIt).aEventList = ::std::deque< ScriptEventDescriptor >(); #else (*aIt).aEventList.realloc( 0 ); #endif - attachAll_Impl( this, nIndex, aList ); + ::std::for_each(aList.begin(), aList.end(), AttachObject(*this, nIndex)); } //----------------------------------------------------------------------------- @@ -658,10 +657,6 @@ void SAL_CALL ImplEventAttacherManager::insertEntry(sal_Int32 nIndex) if( nIndex < 0 ) throw IllegalArgumentException(); -// ::std::deque::iterator aIt = aIndex.begin(); -// while( nIndex-- ) -// aIt++; - if ( static_cast< ::std::deque< AttacherIndex_Impl >::size_type>(nIndex) >= aIndex.size() ) aIndex.resize(nIndex+1); @@ -677,7 +672,7 @@ void SAL_CALL ImplEventAttacherManager::removeEntry(sal_Int32 nIndex) ::std::deque::iterator aIt = implCheckIndex( nIndex ); ::std::deque< AttachedObject_Impl > aList = (*aIt).aObjList; - detachAll_Impl( this, nIndex, aList ); + ::std::for_each(aList.begin(), aList.end(), DetachObject(*this, nIndex)); aIndex.erase( aIt ); } -- cgit v1.2.3