toolkit/source/awt/vclxtoolkit.cxx                     |  160 ++++------
 toolkit/source/controls/grid/sortablegriddatamodel.cxx |  252 +++++++++--------
 2 files changed, 211 insertions(+), 201 deletions(-)

New commits:
commit d4bcb8b4fb23a62e3b75a686263006c47ad8c04b
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Wed Mar 27 15:25:34 2024 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Sun Mar 31 13:03:24 2024 +0200

    convert SortableGridDataModel to comphelper::WeakComponentImplHelper
    
    Change-Id: If399c0fb6b82672edccbefa2b980737ccca98c30
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165546
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/toolkit/source/controls/grid/sortablegriddatamodel.cxx 
b/toolkit/source/controls/grid/sortablegriddatamodel.cxx
index 5eac49f47550..2d5056a8a0ab 100644
--- a/toolkit/source/controls/grid/sortablegriddatamodel.cxx
+++ b/toolkit/source/controls/grid/sortablegriddatamodel.cxx
@@ -30,11 +30,10 @@
 #include <com/sun/star/awt/grid/XGridDataListener.hpp>
 #include <com/sun/star/awt/grid/XSortableMutableGridDataModel.hpp>
 
-#include <cppuhelper/basemutex.hxx>
-#include <cppuhelper/compbase.hxx>
+#include <comphelper/compbase.hxx>
+#include <comphelper/interfacecontainer4.hxx>
 #include <cppuhelper/implbase1.hxx>
 #include <comphelper/anycompare.hxx>
-#include <comphelper/componentguard.hxx>
 #include <cppuhelper/supportsservice.hxx>
 #include <comphelper/diagnose_ex.hxx>
 #include <i18nlangtag/languagetag.hxx>
@@ -52,16 +51,14 @@ using namespace css::uno;
 namespace {
 
 class SortableGridDataModel;
-class MethodGuard;
 
-typedef ::cppu::WeakComponentImplHelper    <   
css::awt::grid::XSortableMutableGridDataModel
+typedef ::comphelper::WeakComponentImplHelper    <   
css::awt::grid::XSortableMutableGridDataModel
                                             ,   css::lang::XServiceInfo
                                             ,   css::lang::XInitialization
                                             >   SortableGridDataModel_Base;
 typedef ::cppu::ImplHelper1 <   css::awt::grid::XGridDataListener
                             >   SortableGridDataModel_PrivateBase;
-class SortableGridDataModel :public ::cppu::BaseMutex
-                            ,public SortableGridDataModel_Base
+class SortableGridDataModel :public SortableGridDataModel_Base
                             ,public SortableGridDataModel_PrivateBase
 {
 public:
@@ -103,7 +100,7 @@ public:
     virtual css::uno::Sequence< css::uno::Any > SAL_CALL getRowData( 
::sal_Int32 RowIndex ) override;
 
     // OComponentHelper
-    virtual void SAL_CALL disposing() override;
+    virtual void disposing(std::unique_lock<std::mutex>& rGuard) override;
 
     // XCloneable
     virtual css::uno::Reference< css::util::XCloneable > SAL_CALL createClone( 
 ) override;
@@ -139,7 +136,7 @@ private:
         @throws css::lang::IndexOutOfBoundsException
             if the given index does not denote a valid row
     */
-    ::sal_Int32 impl_getPrivateRowIndex_throw( ::sal_Int32 const 
i_publicRowIndex ) const;
+    ::sal_Int32 impl_getPrivateRowIndex_throw( std::unique_lock<std::mutex>& 
rGuard, ::sal_Int32 const i_publicRowIndex ) const;
 
     /** translates the given private row index to a public one
     */
@@ -155,7 +152,7 @@ private:
         Neither <member>m_currentSortColumn</member> nor 
<member>m_sortAscending</member> are touched by this method.
         Also, the given column index is not checked, this is the 
responsibility of the caller.
     */
-    bool    impl_reIndex_nothrow( ::sal_Int32 const i_columnIndex, bool const 
i_sortAscending );
+    bool    impl_reIndex_nothrow( std::unique_lock<std::mutex>& rGuard, 
::sal_Int32 const i_columnIndex, bool const i_sortAscending );
 
     /** translates the given event, obtained from our delegator, to a version 
which can be broadcasted to our own
         clients.
@@ -168,7 +165,7 @@ private:
     void    impl_broadcast(
                 void ( SAL_CALL 
css::awt::grid::XGridDataListener::*i_listenerMethod )( const 
css::awt::grid::GridDataEvent & ),
                 css::awt::grid::GridDataEvent const & i_publicEvent,
-                MethodGuard& i_instanceLock
+                std::unique_lock<std::mutex>& i_instanceLock
             );
 
     /** rebuilds our indexes, notifying row removal and row addition events
@@ -178,16 +175,22 @@ private:
 
         Only to be called when we're sorted.
     */
-    void    impl_rebuildIndexesAndNotify( MethodGuard& i_instanceLock );
+    void    impl_rebuildIndexesAndNotify( std::unique_lock<std::mutex>& 
i_instanceLock );
 
     /** removes the current sorting, and notifies a change of all data
     */
-    void    impl_removeColumnSort( MethodGuard& i_instanceLock );
+    void    impl_removeColumnSort( std::unique_lock<std::mutex>& 
i_instanceLock );
 
     /** removes the current sorting, without any broadcast
     */
     void    impl_removeColumnSort_noBroadcast();
 
+    void throwIfNotInitialized()
+    {
+        if (!isInitialized())
+            throw css::lang::NotInitializedException( OUString(), *this );
+    }
+
 private:
     css::uno::Reference< css::uno::XComponentContext >            m_xContext;
     bool                                                          
m_isInitialized;
@@ -197,17 +200,7 @@ private:
     bool                                                    m_sortAscending;
     ::std::vector< ::sal_Int32 >                                  
m_publicToPrivateRowIndex;
     ::std::vector< ::sal_Int32 >                                  
m_privateToPublicRowIndex;
-};
-
-class MethodGuard : public ::comphelper::ComponentGuard
-{
-public:
-    MethodGuard( SortableGridDataModel& i_component, ::cppu::OBroadcastHelper 
& i_broadcastHelper )
-        :comphelper::ComponentGuard( i_component, i_broadcastHelper )
-    {
-        if ( !i_component.isInitialized() )
-            throw css::lang::NotInitializedException( OUString(), i_component 
);
-    }
+    comphelper::OInterfaceContainerHelper4<XGridDataListener>            
m_GridListeners;
 };
 
 template< class STLCONTAINER >
@@ -217,9 +210,7 @@ void lcl_clear( STLCONTAINER& i_container )
 }
 
     SortableGridDataModel::SortableGridDataModel( Reference< XComponentContext 
> const & rxContext )
-        :SortableGridDataModel_Base( m_aMutex )
-        ,SortableGridDataModel_PrivateBase()
-        ,m_xContext( rxContext )
+        :m_xContext( rxContext )
         ,m_isInitialized( false )
         ,m_delegator()
         ,m_collator()
@@ -232,10 +223,7 @@ void lcl_clear( STLCONTAINER& i_container )
 
 
     SortableGridDataModel::SortableGridDataModel( SortableGridDataModel const 
& i_copySource )
-        :cppu::BaseMutex()
-        ,SortableGridDataModel_Base( m_aMutex )
-        ,SortableGridDataModel_PrivateBase()
-        ,m_xContext( i_copySource.m_xContext )
+        :m_xContext( i_copySource.m_xContext )
         ,m_isInitialized( true )
         ,m_delegator()
         ,m_collator( i_copySource.m_collator )
@@ -252,11 +240,8 @@ void lcl_clear( STLCONTAINER& i_container )
 
     SortableGridDataModel::~SortableGridDataModel()
     {
-        if ( !rBHelper.bDisposed )
-        {
-            acquire();
-            dispose();
-        }
+        acquire();
+        dispose();
     }
 
 
@@ -302,7 +287,8 @@ void lcl_clear( STLCONTAINER& i_container )
 
     void SAL_CALL SortableGridDataModel::initialize( const Sequence< Any >& 
i_arguments )
     {
-        ::comphelper::ComponentGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard( m_aMutex );
+        throwIfDisposed(aGuard);
 
         if ( m_delegator.is() )
             throw AlreadyInitializedException( OUString(), *this );
@@ -346,27 +332,22 @@ void lcl_clear( STLCONTAINER& i_container )
 
 
     void SortableGridDataModel::impl_broadcast( void ( SAL_CALL 
XGridDataListener::*i_listenerMethod )( const GridDataEvent & ),
-            GridDataEvent const & i_publicEvent, MethodGuard& i_instanceLock )
+            GridDataEvent const & i_publicEvent, std::unique_lock<std::mutex>& 
i_instanceLock )
     {
-        ::cppu::OInterfaceContainerHelper* pListeners = rBHelper.getContainer( 
cppu::UnoType<XGridDataListener>::get() );
-        if ( pListeners == nullptr )
-            return;
-
-        i_instanceLock.clear();
-        pListeners->notifyEach( i_listenerMethod, i_publicEvent );
+        m_GridListeners.notifyEach( i_instanceLock, i_listenerMethod, 
i_publicEvent );
     }
 
 
     void SAL_CALL SortableGridDataModel::rowsInserted( const GridDataEvent& 
i_event )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
         if ( impl_isSorted_nothrow() )
         {
             // no infrastructure is in place currently to sort the new row to 
its proper location,
             // so we remove the sorting here.
             impl_removeColumnSort( aGuard );
-            aGuard.reset();
         }
 
         GridDataEvent const aEvent( impl_createPublicEvent( i_event ) );
@@ -382,7 +363,7 @@ void lcl_clear( STLCONTAINER& i_container )
         }
     }
 
-    void SortableGridDataModel::impl_rebuildIndexesAndNotify( MethodGuard& 
i_instanceLock )
+    void SortableGridDataModel::impl_rebuildIndexesAndNotify( 
std::unique_lock<std::mutex>& i_instanceLock )
     {
         OSL_PRECOND( impl_isSorted_nothrow(), 
"SortableGridDataModel::impl_rebuildIndexesAndNotify: illegal call!" );
 
@@ -391,7 +372,7 @@ void lcl_clear( STLCONTAINER& i_container )
         lcl_clear( m_privateToPublicRowIndex );
 
         // rebuild the index
-        if ( !impl_reIndex_nothrow( m_currentSortColumn, m_sortAscending ) )
+        if ( !impl_reIndex_nothrow( i_instanceLock, m_currentSortColumn, 
m_sortAscending ) )
         {
             impl_removeColumnSort( i_instanceLock );
             return;
@@ -400,7 +381,6 @@ void lcl_clear( STLCONTAINER& i_container )
         // broadcast an artificial event, saying that all rows have been 
removed
         GridDataEvent const aRemovalEvent( *this, -1, -1, -1, -1 );
         impl_broadcast( &XGridDataListener::rowsRemoved, aRemovalEvent, 
i_instanceLock );
-        i_instanceLock.reset();
 
         // broadcast an artificial event, saying that n rows have been added
         GridDataEvent const aAdditionEvent( *this, -1, -1, 0, 
m_delegator->getRowCount() - 1 );
@@ -410,7 +390,8 @@ void lcl_clear( STLCONTAINER& i_container )
 
     void SAL_CALL SortableGridDataModel::rowsRemoved( const GridDataEvent& 
i_event )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
         // if the data is not sorted, broadcast the event unchanged
         if ( !impl_isSorted_nothrow() )
@@ -470,7 +451,8 @@ void lcl_clear( STLCONTAINER& i_container )
 
     void SAL_CALL SortableGridDataModel::dataChanged( const GridDataEvent& 
i_event )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
         GridDataEvent const aEvent( impl_createPublicEvent( i_event ) );
         impl_broadcast( &XGridDataListener::dataChanged, aEvent, aGuard );
@@ -479,7 +461,8 @@ void lcl_clear( STLCONTAINER& i_container )
 
     void SAL_CALL SortableGridDataModel::rowHeadingChanged( const 
GridDataEvent& i_event )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
         GridDataEvent const aEvent( impl_createPublicEvent( i_event ) );
         impl_broadcast( &XGridDataListener::rowHeadingChanged, aEvent, aGuard 
);
@@ -527,9 +510,12 @@ void lcl_clear( STLCONTAINER& i_container )
         bool const                          m_sortAscending;
     };
 
-    bool SortableGridDataModel::impl_reIndex_nothrow( ::sal_Int32 const 
i_columnIndex, bool const i_sortAscending )
+    bool SortableGridDataModel::impl_reIndex_nothrow( 
std::unique_lock<std::mutex>& rGuard, ::sal_Int32 const i_columnIndex, bool 
const i_sortAscending )
     {
-        ::sal_Int32 const rowCount( getRowCount() );
+        Reference< XMutableGridDataModel > const delegator( m_delegator );
+        rGuard.unlock();
+        ::sal_Int32 const rowCount = delegator->getRowCount();
+        rGuard.lock();
         ::std::vector< ::sal_Int32 > aPublicToPrivate( rowCount );
 
         try
@@ -576,12 +562,17 @@ void lcl_clear( STLCONTAINER& i_container )
 
     void SAL_CALL SortableGridDataModel::sortByColumn( ::sal_Int32 
i_columnIndex, sal_Bool i_sortAscending )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
-        if ( ( i_columnIndex < 0 ) || ( i_columnIndex >= getColumnCount() ) )
+        Reference< XMutableGridDataModel > const delegator( m_delegator );
+        aGuard.unlock();
+        sal_Int32 nColumnCount = delegator->getColumnCount();
+        aGuard.lock();
+        if ( ( i_columnIndex < 0 ) || ( i_columnIndex >= nColumnCount ) )
             throw IndexOutOfBoundsException( OUString(), *this );
 
-        if ( !impl_reIndex_nothrow( i_columnIndex, i_sortAscending ) )
+        if ( !impl_reIndex_nothrow( aGuard, i_columnIndex, i_sortAscending ) )
             return;
 
         m_currentSortColumn = i_columnIndex;
@@ -605,7 +596,7 @@ void lcl_clear( STLCONTAINER& i_container )
     }
 
 
-    void SortableGridDataModel::impl_removeColumnSort( MethodGuard& 
i_instanceLock )
+    void SortableGridDataModel::impl_removeColumnSort( 
std::unique_lock<std::mutex>& i_instanceLock )
     {
         impl_removeColumnSort_noBroadcast();
         impl_broadcast(
@@ -618,14 +609,16 @@ void lcl_clear( STLCONTAINER& i_container )
 
     void SAL_CALL SortableGridDataModel::removeColumnSort(  )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
         impl_removeColumnSort( aGuard );
     }
 
 
     css::beans::Pair< ::sal_Int32, sal_Bool > SAL_CALL 
SortableGridDataModel::getCurrentSortOrder(  )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
         return css::beans::Pair< ::sal_Int32, sal_Bool >( m_currentSortColumn, 
m_sortAscending );
     }
@@ -633,213 +626,240 @@ void lcl_clear( STLCONTAINER& i_container )
 
     void SAL_CALL SortableGridDataModel::addRow( const Any& i_heading, const 
Sequence< Any >& i_data )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         delegator->addRow( i_heading, i_data );
     }
 
 
     void SAL_CALL SortableGridDataModel::addRows( const Sequence< Any >& 
i_headings, const Sequence< Sequence< Any > >& i_data )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         delegator->addRows( i_headings, i_data );
     }
 
 
     void SAL_CALL SortableGridDataModel::insertRow( ::sal_Int32 i_index, const 
Any& i_heading, const Sequence< Any >& i_data )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
-        ::sal_Int32 const rowIndex = i_index == getRowCount() ? i_index : 
impl_getPrivateRowIndex_throw( i_index );
+        Reference< XMutableGridDataModel > const delegator( m_delegator );
+        aGuard.unlock();
+        sal_Int32 nRowCount = delegator->getRowCount();
+        aGuard.lock();
+        
+        ::sal_Int32 const rowIndex = i_index == nRowCount ? i_index : 
impl_getPrivateRowIndex_throw( aGuard, i_index );
             // note that |RowCount| is a valid index in this method, but not 
for impl_getPrivateRowIndex_throw
 
-        Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         delegator->insertRow( rowIndex, i_heading, i_data );
     }
 
 
     void SAL_CALL SortableGridDataModel::insertRows( ::sal_Int32 i_index, 
const Sequence< Any>& i_headings, const Sequence< Sequence< Any > >& i_data )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
-        ::sal_Int32 const rowIndex = i_index == getRowCount() ? i_index : 
impl_getPrivateRowIndex_throw( i_index );
+        Reference< XMutableGridDataModel > const delegator( m_delegator );
+        aGuard.unlock();
+        sal_Int32 nRowCount = delegator->getRowCount();
+        aGuard.lock();
+
+        ::sal_Int32 const rowIndex = i_index == nRowCount ? i_index : 
impl_getPrivateRowIndex_throw( aGuard, i_index );
             // note that |RowCount| is a valid index in this method, but not 
for impl_getPrivateRowIndex_throw
 
-        Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         delegator->insertRows( rowIndex, i_headings, i_data );
     }
 
 
     void SAL_CALL SortableGridDataModel::removeRow( ::sal_Int32 i_rowIndex )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
-        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex 
);
+        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( aGuard, 
i_rowIndex );
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         delegator->removeRow( rowIndex );
     }
 
 
     void SAL_CALL SortableGridDataModel::removeAllRows(  )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         delegator->removeAllRows();
     }
 
 
     void SAL_CALL SortableGridDataModel::updateCellData( ::sal_Int32 
i_columnIndex, ::sal_Int32 i_rowIndex, const Any& i_value )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
-        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex 
);
+        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( aGuard, 
i_rowIndex );
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         delegator->updateCellData( i_columnIndex, rowIndex, i_value );
     }
 
 
     void SAL_CALL SortableGridDataModel::updateRowData( const Sequence< 
::sal_Int32 >& i_columnIndexes, ::sal_Int32 i_rowIndex, const Sequence< Any >& 
i_values )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
-        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex 
);
+        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( aGuard, 
i_rowIndex );
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         delegator->updateRowData( i_columnIndexes, rowIndex, i_values );
     }
 
 
     void SAL_CALL SortableGridDataModel::updateRowHeading( ::sal_Int32 
i_rowIndex, const Any& i_heading )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
-        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex 
);
+        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( aGuard, 
i_rowIndex );
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         delegator->updateRowHeading( rowIndex, i_heading );
     }
 
 
     void SAL_CALL SortableGridDataModel::updateCellToolTip( ::sal_Int32 
i_columnIndex, ::sal_Int32 i_rowIndex, const Any& i_value )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
-        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex 
);
+        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( aGuard, 
i_rowIndex );
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         delegator->updateCellToolTip( i_columnIndex, rowIndex, i_value );
     }
 
 
     void SAL_CALL SortableGridDataModel::updateRowToolTip( ::sal_Int32 
i_rowIndex, const Any& i_value )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
-        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex 
);
+        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( aGuard, 
i_rowIndex );
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         delegator->updateRowToolTip( rowIndex, i_value );
     }
 
 
     void SAL_CALL SortableGridDataModel::addGridDataListener( const Reference< 
XGridDataListener >& i_listener )
     {
-        rBHelper.addListener( cppu::UnoType<XGridDataListener>::get(), 
i_listener );
+        std::unique_lock aGuard(m_aMutex);
+        m_GridListeners.addInterface( aGuard, i_listener );
     }
 
 
     void SAL_CALL SortableGridDataModel::removeGridDataListener( const 
Reference< XGridDataListener >& i_listener )
     {
-        rBHelper.removeListener( cppu::UnoType<XGridDataListener>::get(), 
i_listener );
+        std::unique_lock aGuard(m_aMutex);
+        m_GridListeners.removeInterface( aGuard, i_listener );
     }
 
 
     ::sal_Int32 SAL_CALL SortableGridDataModel::getRowCount()
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         return delegator->getRowCount();
     }
 
 
     ::sal_Int32 SAL_CALL SortableGridDataModel::getColumnCount()
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         return delegator->getColumnCount();
     }
 
 
     Any SAL_CALL SortableGridDataModel::getCellData( ::sal_Int32 
i_columnIndex, ::sal_Int32 i_rowIndex )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
-        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex 
);
+        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( aGuard, 
i_rowIndex );
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         return delegator->getCellData( i_columnIndex, rowIndex );
     }
 
 
     Any SAL_CALL SortableGridDataModel::getCellToolTip( ::sal_Int32 
i_columnIndex, ::sal_Int32 i_rowIndex )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
-        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex 
);
+        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( aGuard, 
i_rowIndex );
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         return delegator->getCellToolTip( i_columnIndex, rowIndex );
     }
 
 
     Any SAL_CALL SortableGridDataModel::getRowHeading( ::sal_Int32 i_rowIndex )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
-        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex 
);
+        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( aGuard, 
i_rowIndex );
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         return delegator->getRowHeading( rowIndex );
     }
 
 
     Sequence< Any > SAL_CALL SortableGridDataModel::getRowData( ::sal_Int32 
i_rowIndex )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
-        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( i_rowIndex 
);
+        ::sal_Int32 const rowIndex = impl_getPrivateRowIndex_throw( aGuard, 
i_rowIndex );
 
         Reference< XMutableGridDataModel > const delegator( m_delegator );
-        aGuard.clear();
+        aGuard.unlock();
         return delegator->getRowData( rowIndex );
     }
 
 
-    void SAL_CALL SortableGridDataModel::disposing()
+    void SortableGridDataModel::disposing(std::unique_lock<std::mutex>& 
/*rGuard*/)
     {
         m_currentSortColumn = -1;
 
@@ -860,7 +880,8 @@ void lcl_clear( STLCONTAINER& i_container )
 
     Reference< css::util::XCloneable > SAL_CALL 
SortableGridDataModel::createClone(  )
     {
-        MethodGuard aGuard( *this, rBHelper );
+        std::unique_lock aGuard(m_aMutex);
+        throwIfNotInitialized();
 
         return new SortableGridDataModel( *this );
     }
@@ -882,9 +903,12 @@ void lcl_clear( STLCONTAINER& i_container )
     }
 
 
-    ::sal_Int32 SortableGridDataModel::impl_getPrivateRowIndex_throw( 
::sal_Int32 const i_publicRowIndex ) const
+    ::sal_Int32 SortableGridDataModel::impl_getPrivateRowIndex_throw( 
std::unique_lock<std::mutex>& rGuard, ::sal_Int32 const i_publicRowIndex ) const
     {
-        if ( ( i_publicRowIndex < 0 ) || ( i_publicRowIndex >= 
m_delegator->getRowCount() ) )
+        rGuard.unlock();
+        sal_Int32 nRowCount = m_delegator->getRowCount();
+        rGuard.lock();
+        if ( ( i_publicRowIndex < 0 ) || ( i_publicRowIndex >= nRowCount ) )
             throw IndexOutOfBoundsException( OUString(), *const_cast< 
SortableGridDataModel* >( this ) );
 
         if ( !impl_isSorted_nothrow() )
commit 1fa0669e1790d4358fdac7d160c4bf57a5254425
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Sat Mar 30 20:25:56 2024 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Sun Mar 31 13:03:13 2024 +0200

    convert VCLXToolkit to comphelper::WeakComponentImplHelper
    
    drop the use of a mutex in methods that do not actually touch
    object-local state
    
    Change-Id: Idca1d1f8b25773f622189871da335012497cbf5e
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165578
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/toolkit/source/awt/vclxtoolkit.cxx 
b/toolkit/source/awt/vclxtoolkit.cxx
index d6d7e0986eb1..046e4f011b58 100644
--- a/toolkit/source/awt/vclxtoolkit.cxx
+++ b/toolkit/source/awt/vclxtoolkit.cxx
@@ -49,9 +49,8 @@
 #include <com/sun/star/awt/XToolkitExperimental.hpp>
 #include <com/sun/star/awt/XToolkitRobot.hpp>
 
-#include <cppuhelper/basemutex.hxx>
 #include <cppuhelper/bootstrap.hxx>
-#include <cppuhelper/compbase.hxx>
+#include <comphelper/compbase.hxx>
 #include <cppuhelper/supportsservice.hxx>
 #include <o3tl/safeint.hxx>
 #include <osl/conditn.hxx>
@@ -138,7 +137,7 @@
 #include <awt/vclxspinbutton.hxx>
 #include <tools/debug.hxx>
 #include <comphelper/diagnose_ex.hxx>
-#include <comphelper/interfacecontainer3.hxx>
+#include <comphelper/interfacecontainer4.hxx>
 #include <comphelper/processfactory.hxx>
 #include <comphelper/profilezone.hxx>
 
@@ -427,8 +426,7 @@ public:
     sal_Int32 m_nPauseMilliseconds;
 };
 
-class VCLXToolkit : public cppu::BaseMutex,
-                    public cppu::WeakComponentImplHelper<
+class VCLXToolkit : public comphelper::WeakComponentImplHelper<
                     css::awt::XToolkitExperimental,
                     css::awt::XToolkitRobot,
                     css::lang::XServiceInfo >
@@ -436,9 +434,9 @@ class VCLXToolkit : public cppu::BaseMutex,
     css::uno::Reference< css::datatransfer::clipboard::XClipboard > 
mxClipboard;
     css::uno::Reference< css::datatransfer::clipboard::XClipboard > 
mxSelection;
 
-    ::comphelper::OInterfaceContainerHelper3<css::awt::XTopWindowListener> 
m_aTopWindowListeners;
-    ::comphelper::OInterfaceContainerHelper3<css::awt::XKeyHandler> 
m_aKeyHandlers;
-    ::comphelper::OInterfaceContainerHelper3<css::awt::XFocusListener> 
m_aFocusListeners;
+    ::comphelper::OInterfaceContainerHelper4<css::awt::XTopWindowListener> 
m_aTopWindowListeners;
+    ::comphelper::OInterfaceContainerHelper4<css::awt::XKeyHandler> 
m_aKeyHandlers;
+    ::comphelper::OInterfaceContainerHelper4<css::awt::XFocusListener> 
m_aFocusListeners;
     ::Link<VclSimpleEvent&,void> m_aEventListenerLink;
     ::Link<VclWindowEvent&,bool> m_aKeyListenerLink;
     bool m_bEventListener;
@@ -458,13 +456,11 @@ class VCLXToolkit : public cppu::BaseMutex,
     void callFocusListeners(::VclSimpleEvent const * pEvent, bool bGained);
 
 protected:
-    ::osl::Mutex&   GetMutex() { return m_aMutex; }
-
-    virtual void SAL_CALL disposing() override;
+    virtual void disposing(std::unique_lock<std::mutex>& rGuard) override;
 
     static vcl::Window* ImplCreateWindow( rtl::Reference<VCLXWindow>* 
ppNewComp, const css::awt::WindowDescriptor& rDescriptor, vcl::Window* pParent,
                              WinBits nWinBits, MessBoxStyle nMessBoxStyle );
-    css::uno::Reference< css::awt::XWindowPeer > ImplCreateWindow( const 
css::awt::WindowDescriptor& Descriptor,
+    static css::uno::Reference< css::awt::XWindowPeer > ImplCreateWindow( 
const css::awt::WindowDescriptor& Descriptor,
                              MessBoxStyle nForceMessBoxStyle );
 
 public:
@@ -924,13 +920,6 @@ static void ToolkitWorkerFunction( void* pArgs )
 
 // constructor, which might initialize VCL
 VCLXToolkit::VCLXToolkit():
-    cppu::WeakComponentImplHelper<
-    css::awt::XToolkitExperimental,
-    css::awt::XToolkitRobot,
-    css::lang::XServiceInfo>( GetMutex() ),
-    m_aTopWindowListeners(rBHelper.rMutex),
-    m_aKeyHandlers(rBHelper.rMutex),
-    m_aFocusListeners(rBHelper.rMutex),
     m_aEventListenerLink(LINK(this, VCLXToolkit, eventListenerHandler)),
     m_aKeyListenerLink(LINK(this, VCLXToolkit, keyListenerHandler)),
     m_bEventListener(false),
@@ -948,9 +937,10 @@ VCLXToolkit::VCLXToolkit():
 #endif
 }
 
-void SAL_CALL VCLXToolkit::disposing()
+void VCLXToolkit::disposing(std::unique_lock<std::mutex>& rGuard)
 {
 #ifndef IOS
+    rGuard.unlock();
     {
         osl::Guard< osl::Mutex > aGuard( getInitMutex() );
         if( --nVCLToolkitInstanceCount == 0 )
@@ -963,6 +953,7 @@ void SAL_CALL VCLXToolkit::disposing()
             }
         }
     }
+    rGuard.lock();
 #endif
     if (m_bEventListener)
     {
@@ -976,9 +967,9 @@ void SAL_CALL VCLXToolkit::disposing()
     }
     css::lang::EventObject aEvent(
         getXWeak());
-    m_aTopWindowListeners.disposeAndClear(aEvent);
-    m_aKeyHandlers.disposeAndClear(aEvent);
-    m_aFocusListeners.disposeAndClear(aEvent);
+    m_aTopWindowListeners.disposeAndClear(rGuard, aEvent);
+    m_aKeyHandlers.disposeAndClear(rGuard, aEvent);
+    m_aFocusListeners.disposeAndClear(rGuard, aEvent);
 }
 
 
@@ -1008,8 +999,6 @@ css::uno::Reference< css::awt::XWindowPeer > 
VCLXToolkit::createWindow( const cs
 
 css::uno::Reference< css::awt::XDevice > 
VCLXToolkit::createScreenCompatibleDevice( sal_Int32 Width, sal_Int32 Height )
 {
-    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
-
     rtl::Reference<VCLXVirtualDevice> pVDev = new VCLXVirtualDevice;
 
     SolarMutexGuard aSolarGuard;
@@ -1023,8 +1012,6 @@ css::uno::Reference< css::awt::XDevice > 
VCLXToolkit::createScreenCompatibleDevi
 
 css::uno::Reference< css::awt::XRegion > VCLXToolkit::createRegion(  )
 {
-    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
-
     css::uno::Reference< css::awt::XRegion >  xRef = new VCLXRegion;
     return xRef;
 }
@@ -1838,12 +1825,11 @@ vcl::Window* VCLXToolkit::ImplCreateWindow( 
rtl::Reference<VCLXWindow>* ppNewCom
     return pNewWindow;
 }
 
+// static
 css::uno::Reference< css::awt::XWindowPeer > VCLXToolkit::ImplCreateWindow(
     const css::awt::WindowDescriptor& rDescriptor,
     MessBoxStyle nForceMessBoxStyle )
 {
-    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
-
     SolarMutexGuard aSolarGuard;
 
     css::uno::Reference< css::awt::XVclWindowPeer > xRef;
@@ -1913,8 +1899,6 @@ css::uno::Reference< css::awt::XWindowPeer > 
VCLXToolkit::ImplCreateWindow(
 
 css::uno::Sequence< css::uno::Reference< css::awt::XWindowPeer > > 
VCLXToolkit::createWindows( const css::uno::Sequence< 
css::awt::WindowDescriptor >& rDescriptors )
 {
-    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
-
     sal_uInt32 nComponents = rDescriptors.getLength();
     css::uno::Sequence< css::uno::Reference< css::awt::XWindowPeer > > aSeq( 
nComponents );
     for ( sal_uInt32 n = 0; n < nComponents; n++ )
@@ -2186,15 +2170,15 @@ void SAL_CALL VCLXToolkit::addTopWindowListener(
     css::uno::Reference< css::awt::XTopWindowListener > const & rListener)
 {
     OSL_ENSURE(rListener.is(), "Null rListener");
-    ::osl::ClearableMutexGuard aGuard(rBHelper.rMutex);
-    if (rBHelper.bDisposed || rBHelper.bInDispose)
+    std::unique_lock aGuard(m_aMutex);
+    if (m_bDisposed)
     {
-        aGuard.clear();
+        aGuard.unlock();
         rListener->disposing(
             css::lang::EventObject(
                 getXWeak()));
     }
-    else if (m_aTopWindowListeners.addInterface(rListener) == 1
+    else if (m_aTopWindowListeners.addInterface(aGuard, rListener) == 1
              && !m_bEventListener)
     {
         m_bEventListener = true;
@@ -2206,10 +2190,10 @@ void SAL_CALL VCLXToolkit::addTopWindowListener(
 void SAL_CALL VCLXToolkit::removeTopWindowListener(
     css::uno::Reference< css::awt::XTopWindowListener > const & rListener)
 {
-    ::osl::MutexGuard aGuard(rBHelper.rMutex);
-    if (!(rBHelper.bDisposed || rBHelper.bInDispose)
-        && m_aTopWindowListeners.removeInterface(rListener) == 0
-        && m_aFocusListeners.getLength() == 0 && m_bEventListener)
+    std::unique_lock aGuard(m_aMutex);
+    if (!m_bDisposed
+        && m_aTopWindowListeners.removeInterface(aGuard, rListener) == 0
+        && m_aFocusListeners.getLength(aGuard) == 0 && m_bEventListener)
     {
         ::Application::RemoveEventListener(m_aEventListenerLink);
         m_bEventListener = false;
@@ -2221,15 +2205,15 @@ void SAL_CALL VCLXToolkit::addKeyHandler(
     css::uno::Reference< css::awt::XKeyHandler > const & rHandler)
 {
     OSL_ENSURE(rHandler.is(), "Null rHandler");
-    ::osl::ClearableMutexGuard aGuard(rBHelper.rMutex);
-    if (rBHelper.bDisposed || rBHelper.bInDispose)
+    std::unique_lock aGuard(m_aMutex);
+    if (m_bDisposed)
     {
-        aGuard.clear();
+        aGuard.unlock();
         rHandler->disposing(
             css::lang::EventObject(
                 getXWeak()));
     }
-    else if (m_aKeyHandlers.addInterface(rHandler) == 1 && !m_bKeyListener)
+    else if (m_aKeyHandlers.addInterface(aGuard, rHandler) == 1 && 
!m_bKeyListener)
     {
         m_bKeyListener = true;
         ::Application::AddKeyListener(m_aKeyListenerLink);
@@ -2240,9 +2224,9 @@ void SAL_CALL VCLXToolkit::addKeyHandler(
 void SAL_CALL VCLXToolkit::removeKeyHandler(
     css::uno::Reference< css::awt::XKeyHandler > const & rHandler)
 {
-    ::osl::MutexGuard aGuard(rBHelper.rMutex);
-    if (!(rBHelper.bDisposed || rBHelper.bInDispose)
-        && m_aKeyHandlers.removeInterface(rHandler) == 0 && m_bKeyListener)
+    std::unique_lock aGuard(m_aMutex);
+    if (!m_bDisposed
+        && m_aKeyHandlers.removeInterface(aGuard, rHandler) == 0 && 
m_bKeyListener)
     {
         ::Application::RemoveKeyListener(m_aKeyListenerLink);
         m_bKeyListener = false;
@@ -2254,15 +2238,15 @@ void SAL_CALL VCLXToolkit::addFocusListener(
     css::uno::Reference< css::awt::XFocusListener > const & rListener)
 {
     OSL_ENSURE(rListener.is(), "Null rListener");
-    ::osl::ClearableMutexGuard aGuard(rBHelper.rMutex);
-    if (rBHelper.bDisposed || rBHelper.bInDispose)
+    std::unique_lock aGuard(m_aMutex);
+    if (m_bDisposed)
     {
-        aGuard.clear();
+        aGuard.unlock();
         rListener->disposing(
             css::lang::EventObject(
                 getXWeak()));
     }
-    else if (m_aFocusListeners.addInterface(rListener) == 1
+    else if (m_aFocusListeners.addInterface(aGuard, rListener) == 1
              && !m_bEventListener)
     {
         m_bEventListener = true;
@@ -2274,10 +2258,10 @@ void SAL_CALL VCLXToolkit::addFocusListener(
 void SAL_CALL VCLXToolkit::removeFocusListener(
     css::uno::Reference< css::awt::XFocusListener > const & rListener)
 {
-    ::osl::MutexGuard aGuard(rBHelper.rMutex);
-    if (!(rBHelper.bDisposed || rBHelper.bInDispose)
-        && m_aFocusListeners.removeInterface(rListener) == 0
-        && m_aTopWindowListeners.getLength() == 0 && m_bEventListener)
+    std::unique_lock aGuard(m_aMutex);
+    if (!m_bDisposed
+        && m_aFocusListeners.removeInterface(aGuard, rListener) == 0
+        && m_aTopWindowListeners.getLength(aGuard) == 0 && m_bEventListener)
     {
         ::Application::RemoveEventListener(m_aEventListenerLink);
         m_bEventListener = false;
@@ -2364,33 +2348,32 @@ void VCLXToolkit::callTopWindowListeners(
     if (!pWindow->IsTopWindow())
         return;
 
-    std::vector< css::uno::Reference< css::awt::XTopWindowListener > >
-          aListeners(m_aTopWindowListeners.getElements());
-    if (aListeners.empty())
+    std::unique_lock aGuard(m_aMutex);
+    if (m_aTopWindowListeners.getLength(aGuard) == 0)
         return;
 
     css::lang::EventObject aAwtEvent(
         static_cast< css::awt::XWindow * >(pWindow->GetWindowPeer()));
-    for (const css::uno::Reference<css::awt::XTopWindowListener> & xListener : 
aListeners)
-    {
-        try
-        {
-            (xListener.get()->*pFn)(aAwtEvent);
-        }
-        catch (const css::uno::RuntimeException &)
+    m_aTopWindowListeners.forEach(aGuard,
+        [&pFn, &aAwtEvent](const 
css::uno::Reference<css::awt::XTopWindowListener> & xListener)
         {
-            DBG_UNHANDLED_EXCEPTION("toolkit");
-        }
-    }
+            try
+            {
+                (xListener.get()->*pFn)(aAwtEvent);
+            }
+            catch (const css::uno::RuntimeException &)
+            {
+                DBG_UNHANDLED_EXCEPTION("toolkit");
+            }
+        });
 }
 
 bool VCLXToolkit::callKeyHandlers(::VclSimpleEvent const * pEvent,
                                   bool bPressed)
 {
-    std::vector< css::uno::Reference< css::awt::XKeyHandler > >
-          aHandlers(m_aKeyHandlers.getElements());
+    std::unique_lock aGuard(m_aMutex);
 
-    if (!aHandlers.empty())
+    if (m_aKeyHandlers.getLength(aGuard) != 0)
     {
         vcl::Window * pWindow = static_cast< ::VclWindowEvent const * 
>(pEvent)->GetWindow();
 
@@ -2410,12 +2393,15 @@ bool VCLXToolkit::callKeyHandlers(::VclSimpleEvent 
const * pEvent,
             pKeyEvent->GetKeyCode().GetCode(), pKeyEvent->GetCharCode(),
             sal::static_int_cast< sal_Int16 >(
                 pKeyEvent->GetKeyCode().GetFunction()));
-        for (const css::uno::Reference<css::awt::XKeyHandler> & xHandler : 
aHandlers)
+        comphelper::OInterfaceIteratorHelper4 aIt(aGuard, m_aKeyHandlers);
+        aGuard.unlock();
+        while (aIt.hasMoreElements())
         {
+            css::awt::XKeyHandler* pL = aIt.next().get();
             try
             {
-                if (bPressed ? xHandler->keyPressed(aAwtEvent)
-                             : xHandler->keyReleased(aAwtEvent))
+                if (bPressed ? pL->keyPressed(aAwtEvent)
+                             : pL->keyReleased(aAwtEvent))
                     return true;
             }
             catch (const css::uno::RuntimeException &)
@@ -2435,9 +2421,8 @@ void VCLXToolkit::callFocusListeners(::VclSimpleEvent 
const * pEvent,
     if (!pWindow->IsTopWindow())
         return;
 
-    std::vector< css::uno::Reference< css::awt::XFocusListener > >
-          aListeners(m_aFocusListeners.getElements());
-    if (aListeners.empty())
+    std::unique_lock aGuard(m_aMutex);
+    if (m_aFocusListeners.getLength(aGuard) == 0)
         return;
 
     // Ignore the interior of compound controls when determining the
@@ -2457,18 +2442,19 @@ void VCLXToolkit::callFocusListeners(::VclSimpleEvent 
const * pEvent,
         static_cast< css::awt::XWindow * >(pWindow->GetWindowPeer()),
         static_cast<sal_Int16>(pWindow->GetGetFocusFlags()),
         xNext, false);
-    for (const css::uno::Reference<css::awt::XFocusListener> & xListener : 
aListeners)
-    {
-        try
+    m_aFocusListeners.forEach(aGuard,
+        [bGained, &aAwtEvent] (const 
css::uno::Reference<css::awt::XFocusListener> & xListener)
         {
-            bGained ? xListener->focusGained(aAwtEvent)
-                : xListener->focusLost(aAwtEvent);
-        }
-        catch (const css::uno::RuntimeException &)
-        {
-            DBG_UNHANDLED_EXCEPTION("toolkit");
-        }
-    }
+            try
+            {
+                bGained ? xListener->focusGained(aAwtEvent)
+                    : xListener->focusLost(aAwtEvent);
+            }
+            catch (const css::uno::RuntimeException &)
+            {
+                DBG_UNHANDLED_EXCEPTION("toolkit");
+            }
+        });
 }
 
 // css::awt::XReschedule:

Reply via email to