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   <hossein@libreoffice.org>
This commit is contained in:
Mohamed Zaghloul
2025-03-21 10:19:37 +02:00
committed by Hossein
parent 8bbba41d87
commit 1d57a6f7fa

View File

@@ -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++ )
// if not found
if ( it == maControlInfoList.end() )
return;
// remove listener from control
it->xControl->removeEventListener (static_cast< XEventListener* >( static_cast< XWindowListener* >( this ) ));
it->xControl->setContext ( Reference< XInterface > () );
// ... free memory
maControlInfoList.erase(it);
// Send message to all other listener
comphelper::OInterfaceContainerHelper2 * pInterfaceContainer = m_aListeners.getContainer( cppu::UnoType<XContainerListener>::get());
if (pInterfaceContainer)
{
// Search for right control
IMPL_ControlInfo* pControl = &maControlInfoList[ n ];
if ( rControl == pControl->xControl )
ContainerEvent aEvent;
aEvent.Source = *this;
aEvent.Element <<= rControl;
comphelper::OInterfaceIteratorHelper2 aIterator (*pInterfaceContainer);
while ( aIterator.hasMoreElements() )
{
//.is it found ... remove listener from control
pControl->xControl->removeEventListener (static_cast< XEventListener* >( static_cast< XWindowListener* >( this ) ));
pControl->xControl->setContext ( Reference< XInterface > () );
// ... free memory
maControlInfoList.erase(maControlInfoList.begin() + n);
// Send message to all other listener
comphelper::OInterfaceContainerHelper2 * pInterfaceContainer = m_aListeners.getContainer( cppu::UnoType<XContainerListener>::get());
if (pInterfaceContainer)
{
ContainerEvent aEvent;
aEvent.Source = *this;
aEvent.Element <<= rControl;
comphelper::OInterfaceIteratorHelper2 aIterator (*pInterfaceContainer);
while ( aIterator.hasMoreElements() )
{
static_cast<XContainerListener*>(aIterator.next())->elementRemoved (aEvent);
}
}
// Break "for" !
break;
static_cast<XContainerListener*>(aIterator.next())->elementRemoved (aEvent);
}
}
}