UnoControls/source/base/basecontainercontrol.cxx | 51 ++++++++++------------- 1 file changed, 24 insertions(+), 27 deletions(-)
New commits: commit 1d57a6f7fa3b3ea97ce481dc056a1261d269a502 Author: Mohamed Zaghloul <zaghloulmoham...@gmail.com> AuthorDate: Fri Mar 21 10:19:37 2025 +0200 Commit: Hossein <hoss...@libreoffice.org> CommitDate: Thu Mar 27 14:05:58 2025 +0100 tdf#145538 Replace loop with std::find_if Refactor 'removeControl' method for improved readability and performance while avoiding use of index-based-for-loop Change-Id: Ib89e34dc69ff9f6cdfc94e500463163cd2466245 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/183190 Tested-by: Jenkins Reviewed-by: Hossein <hoss...@libreoffice.org> diff --git a/UnoControls/source/base/basecontainercontrol.cxx b/UnoControls/source/base/basecontainercontrol.cxx index f78ed4552eac..120197f93ad1 100644 --- a/UnoControls/source/base/basecontainercontrol.cxx +++ b/UnoControls/source/base/basecontainercontrol.cxx @@ -190,40 +190,37 @@ void SAL_CALL BaseContainerControl::removeControl ( const Reference< XControl > // Ready for multithreading MutexGuard aGuard (m_aMutex); - size_t nControls = maControlInfoList.size(); + // Search for right control + auto it = std::find_if(maControlInfoList.begin(), maControlInfoList.end(), + [&rControl](const IMPL_ControlInfo& control) + { return rControl == control.xControl; }); - for ( size_t n = 0; n < nControls; n++ ) - { - // Search for right control - IMPL_ControlInfo* pControl = &maControlInfoList[ n ]; - if ( rControl == pControl->xControl ) - { - //.is it found ... remove listener from control - pControl->xControl->removeEventListener (static_cast< XEventListener* >( static_cast< XWindowListener* >( this ) )); - pControl->xControl->setContext ( Reference< XInterface > () ); + // if not found + if ( it == maControlInfoList.end() ) + return; - // ... free memory - maControlInfoList.erase(maControlInfoList.begin() + n); + // remove listener from control + it->xControl->removeEventListener (static_cast< XEventListener* >( static_cast< XWindowListener* >( this ) )); + it->xControl->setContext ( Reference< XInterface > () ); - // Send message to all other listener - comphelper::OInterfaceContainerHelper2 * pInterfaceContainer = m_aListeners.getContainer( cppu::UnoType<XContainerListener>::get()); + // ... free memory + maControlInfoList.erase(it); - if (pInterfaceContainer) - { - ContainerEvent aEvent; + // Send message to all other listener + comphelper::OInterfaceContainerHelper2 * pInterfaceContainer = m_aListeners.getContainer( cppu::UnoType<XContainerListener>::get()); - aEvent.Source = *this; - aEvent.Element <<= rControl; + if (pInterfaceContainer) + { + ContainerEvent aEvent; + + aEvent.Source = *this; + aEvent.Element <<= rControl; - comphelper::OInterfaceIteratorHelper2 aIterator (*pInterfaceContainer); + comphelper::OInterfaceIteratorHelper2 aIterator (*pInterfaceContainer); - while ( aIterator.hasMoreElements() ) - { - static_cast<XContainerListener*>(aIterator.next())->elementRemoved (aEvent); - } - } - // Break "for" ! - break; + while ( aIterator.hasMoreElements() ) + { + static_cast<XContainerListener*>(aIterator.next())->elementRemoved (aEvent); } } }