UnoControls/inc/multiplexer.hxx         |   73 ++---------
 UnoControls/source/base/multiplexer.cxx |  196 ++++++++++----------------------
 2 files changed, 81 insertions(+), 188 deletions(-)

New commits:
commit 0f55739a75b418ff1b08abff2b8eaead2df8c997
Author:     Mike Kaganski <[email protected]>
AuthorDate: Fri Nov 8 21:12:47 2024 +0500
Commit:     Mike Kaganski <[email protected]>
CommitDate: Fri Nov 8 20:41:23 2024 +0100

    Simplify using WeakImplHelper and OMultiTypeInterfaceContainerHelperVar4
    
    Change-Id: Ib83b93d6055d3006eecdb25f1ccd7baf1251a40a
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176284
    Reviewed-by: Mike Kaganski <[email protected]>
    Tested-by: Jenkins

diff --git a/UnoControls/inc/multiplexer.hxx b/UnoControls/inc/multiplexer.hxx
index 58bd1563fb0a..095e316797e8 100644
--- a/UnoControls/inc/multiplexer.hxx
+++ b/UnoControls/inc/multiplexer.hxx
@@ -26,9 +26,9 @@
 #include <com/sun/star/awt/XTopWindowListener.hpp>
 #include <com/sun/star/awt/XMouseListener.hpp>
 #include <com/sun/star/awt/XFocusListener.hpp>
-#include <cppuhelper/weak.hxx>
+#include <comphelper/compbase.hxx>
 #include <cppuhelper/weakref.hxx>
-#include <comphelper/multicontainer2.hxx>
+#include <comphelper/multiinterfacecontainer4.hxx>
 
 namespace com::sun::star::awt { class XWindow; }
 namespace com::sun::star::awt { struct KeyEvent; }
@@ -38,14 +38,13 @@ namespace com::sun::star::awt { struct WindowEvent; }
 
 namespace unocontrols {
 
-class OMRCListenerMultiplexerHelper final : public css::awt::XFocusListener
-                                    , public css::awt::XWindowListener
-                                    , public css::awt::XKeyListener
-                                    , public css::awt::XMouseListener
-                                    , public css::awt::XMouseMotionListener
-                                    , public css::awt::XPaintListener
-                                    , public css::awt::XTopWindowListener
-                                    , public ::cppu::OWeakObject
+class OMRCListenerMultiplexerHelper final : public comphelper::WeakImplHelper< 
css::awt::XFocusListener
+                                                                             , 
css::awt::XWindowListener
+                                                                             , 
css::awt::XKeyListener
+                                                                             , 
css::awt::XMouseListener
+                                                                             , 
css::awt::XMouseMotionListener
+                                                                             , 
css::awt::XPaintListener
+                                                                             , 
css::awt::XTopWindowListener >
 {
 public:
 
@@ -59,51 +58,8 @@ public:
     OMRCListenerMultiplexerHelper(  const   css::uno::Reference< 
css::awt::XWindow >& xControl    ,
                                     const   css::uno::Reference< 
css::awt::XWindow >& xPeer       );
 
-    /**
-        @short      copy-constructor
-        @descr
-        @param      rCopyInstance   C++-Reference to instance to make copy 
from.
-    */
-
-    OMRCListenerMultiplexerHelper( const OMRCListenerMultiplexerHelper& 
aCopyInstance );
-
     virtual ~OMRCListenerMultiplexerHelper() override;
 
-    //  XInterface
-
-    /**
-        @short      give answer, if interface is supported
-        @descr      The interfaces are searched by type.
-
-        @seealso    XInterface
-
-        @param      "rType" is the type of searched interface.
-
-        @return     Any     information about found interface
-
-        @onerror    A RuntimeException is thrown.
-    */
-
-    virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type& aType 
) override;
-
-    /**
-        @short      increment refcount
-        @seealso    XInterface
-        @seealso    release()
-        @onerror    A RuntimeException is thrown.
-    */
-
-    virtual void SAL_CALL acquire() noexcept override;
-
-    /**
-        @short      decrement refcount
-        @seealso    XInterface
-        @seealso    acquire()
-        @onerror    A RuntimeException is thrown.
-    */
-
-    virtual void SAL_CALL release() noexcept override;
-
     OMRCListenerMultiplexerHelper& operator= ( const 
OMRCListenerMultiplexerHelper& aCopyInstance );
 
     //  container methods
@@ -126,14 +82,14 @@ public:
     */
 
     void advise(    const   css::uno::Type&                              aType 
      ,
-                    const   css::uno::Reference< css::uno::XInterface >&  
xListener   );
+                    const   css::uno::Reference< css::lang::XEventListener >&  
xListener   );
 
     /**
         @short      Remove the specified listener from the source.
     */
 
     void unadvise(  const   css::uno::Type&                              aType 
      ,
-                    const   css::uno::Reference< css::uno::XInterface >&  
xListener   );
+                    const   css::uno::Reference< css::lang::XEventListener >&  
xListener   );
 
     //  XEventListener
 
@@ -217,14 +173,15 @@ private:
     void impl_unadviseFromPeer( const   css::uno::Reference< css::awt::XWindow 
>& xPeer   ,
                                 const   css::uno::Type&                        
  aType   );
 
+    template <class Interface, typename Event>
+    void Multiplex(void (SAL_CALL Interface::*method)(const Event&), const 
Event& event);
+
 //  private variables
 
 private:
-    ::osl::Mutex                                m_aMutex;
     css::uno::Reference< css::awt::XWindow >      m_xPeer;   /// The source of 
the events. Normally this is the peer object.
     css::uno::WeakReference< css::awt::XWindow >  m_xControl;
-    comphelper::OMultiTypeInterfaceContainerHelper2  m_aListenerHolder;
-
+    comphelper::OMultiTypeInterfaceContainerHelperVar4<css::uno::Type, 
css::lang::XEventListener> m_aListenerHolder;
 };
 
 }
diff --git a/UnoControls/source/base/multiplexer.cxx 
b/UnoControls/source/base/multiplexer.cxx
index 0f0f34b68660..a51cbbd36fc3 100644
--- a/UnoControls/source/base/multiplexer.cxx
+++ b/UnoControls/source/base/multiplexer.cxx
@@ -33,34 +33,38 @@ using namespace ::com::sun::star::lang;
 
 namespace unocontrols {
 
-//  macros
-
-#define MULTIPLEX( INTERFACE, METHOD, EVENTTYP, EVENT )                        
                                                     \
-                                                                               
                                                     \
-    /* First get all interfaces from container with right type.*/              
                                                     \
-    comphelper::OInterfaceContainerHelper2* pContainer = 
m_aListenerHolder.getContainer( cppu::UnoType<INTERFACE>::get() );  \
-    /* Do the follow only, if elements in container exist.*/                   
                                                     \
-    if( !pContainer )                                                          
                                              \
-        return;                                                                
                                                               \
-    comphelper::OInterfaceIteratorHelper2 aIterator( *pContainer );            
                                                              \
-    EVENTTYP aLocalEvent = EVENT;                                              
                                                 \
-    /* Remark: The control is the event source not the peer.*/                 
                                                 \
-    /*         We must change the source of the event.      */                 
                                                 \
-    aLocalEvent.Source = m_xControl;                                           
                                                \
-    /* Is the control not destroyed? */                                        
                                                 \
-    if( !aLocalEvent.Source )                                                  
                                             \
-        return;                                                                
                                                           \
-    if( !aIterator.hasMoreElements() )                                         
                                              \
-        return;                                                                
                                             \
-    INTERFACE * pListener = static_cast<INTERFACE *>(aIterator.next());        
                                         \
-    try                                                                        
                                         \
-    {                                                                          
                                         \
-        pListener->METHOD( aLocalEvent );                                      
                                         \
-    }                                                                          
                                         \
-    catch(const RuntimeException& )                                            
                                         \
-    {                                                                          
                                         \
-        /* Ignore all system exceptions from the listener! */                  
                                         \
+template <class Interface, typename Event>
+void OMRCListenerMultiplexerHelper::Multiplex(void (SAL_CALL 
Interface::*method)(const Event&),
+                                              const Event& event)
+{
+    std::unique_lock aGuard(m_aMutex);
+    /* First get all interfaces from container with right type.*/
+    auto* pContainer = m_aListenerHolder.getContainer(aGuard, 
cppu::UnoType<Interface>::get());
+    /* Do the follow only, if elements in container exist.*/
+    if (!pContainer)
+        return;
+    comphelper::OInterfaceIteratorHelper4 aIterator(aGuard, *pContainer);
+    Event aLocalEvent = event;
+    /* Remark: The control is the event source not the peer.*/
+    /*         We must change the source of the event.      */
+    aLocalEvent.Source = m_xControl;
+    aGuard.unlock();
+    /* Is the control not destroyed? */
+    if (!aLocalEvent.Source)
+        return;
+    if (!aIterator.hasMoreElements())
+        return;
+    auto* pListener = aIterator.next().get();
+    assert(dynamic_cast<Interface*>(pListener));
+    try
+    {
+        (static_cast<Interface*>(pListener)->*method)(aLocalEvent);
     }
+    catch (const RuntimeException&)
+    {
+        /* Ignore all system exceptions from the listener! */
+    }
+}
 
 //  construct/destruct
 
@@ -68,20 +72,6 @@ 
OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper(   const   Referenc
                                                                 const   
Reference< XWindow >&   xPeer       )
     : m_xPeer           ( xPeer     )
     , m_xControl        ( xControl  )
-    , m_aListenerHolder ( m_aMutex  )
-{
-}
-
-OMRCListenerMultiplexerHelper::OMRCListenerMultiplexerHelper( const 
OMRCListenerMultiplexerHelper& aCopyInstance )
-    : XFocusListener()
-    , XWindowListener()
-    , XKeyListener()
-    , XMouseListener()
-    , XMouseMotionListener()
-    , XPaintListener()
-    , XTopWindowListener()
-    , OWeakObject(aCopyInstance)
-    , m_aListenerHolder ( m_aMutex  )
 {
 }
 
@@ -89,74 +79,18 @@ 
OMRCListenerMultiplexerHelper::~OMRCListenerMultiplexerHelper()
 {
 }
 
-//  XInterface
-
-Any SAL_CALL OMRCListenerMultiplexerHelper::queryInterface( const Type& rType )
-{
-    // Attention:
-    //  Don't use mutex or guard in this method!!! Is a method of XInterface.
-
-    // Ask for my own supported interfaces ...
-    // Attention: XTypeProvider and XInterface are supported by 
WeakComponentImplHelper!
-    Any aReturn ( ::cppu::queryInterface(   rType                              
             ,
-                                            static_cast< XWindowListener*      
 > ( this )  ,
-                                            static_cast< XKeyListener*         
 > ( this )  ,
-                                            static_cast< XFocusListener*       
 > ( this )  ,
-                                            static_cast< XMouseListener*       
 > ( this )  ,
-                                            static_cast< XMouseMotionListener* 
 > ( this )  ,
-                                            static_cast< XPaintListener*       
 > ( this )  ,
-                                            static_cast< XTopWindowListener*   
 > ( this )  ,
-                                            static_cast< XTopWindowListener*   
 > ( this )
-                                        )
-                );
-
-    // If searched interface supported by this class ...
-    if ( aReturn.hasValue() )
-    {
-        // ... return this information.
-        return aReturn;
-    }
-    else
-    {
-        // Else; ... ask baseclass for interfaces!
-        return OWeakObject::queryInterface( rType );
-    }
-}
-
-//  XInterface
-
-void SAL_CALL OMRCListenerMultiplexerHelper::acquire() noexcept
-{
-    // Attention:
-    //  Don't use mutex or guard in this method!!! Is a method of XInterface.
-
-    // Forward to baseclass
-    OWeakObject::acquire();
-}
-
-//  XInterface
-
-void SAL_CALL OMRCListenerMultiplexerHelper::release() noexcept
-{
-    // Attention:
-    //  Don't use mutex or guard in this method!!! Is a method of XInterface.
-
-    // Forward to baseclass
-    OWeakObject::release();
-}
-
 //  container method
 
 void OMRCListenerMultiplexerHelper::setPeer( const Reference< XWindow >& xPeer 
)
 {
-    MutexGuard aGuard( m_aMutex );
+    std::unique_lock aGuard(m_aMutex);
     if( m_xPeer == xPeer )
         return;
 
     if( m_xPeer.is() )
     {
         // get all types from the listener added to the peer
-        const std::vector< Type > aContainedTypes = 
m_aListenerHolder.getContainedTypes();
+        const std::vector< Type > aContainedTypes = 
m_aListenerHolder.getContainedTypes(aGuard);
         // loop over all listener types and remove the listeners from the peer
         for( const auto& rContainedType : aContainedTypes )
             impl_unadviseFromPeer( m_xPeer, rContainedType );
@@ -165,7 +99,7 @@ void OMRCListenerMultiplexerHelper::setPeer( const 
Reference< XWindow >& xPeer )
     if( m_xPeer.is() )
     {
         // get all types from the listener added to the peer
-        const std::vector< Type > aContainedTypes = 
m_aListenerHolder.getContainedTypes();
+        const std::vector< Type > aContainedTypes = 
m_aListenerHolder.getContainedTypes(aGuard);
         // loop over all listener types and add the listeners to the peer
         for( const auto& rContainedType : aContainedTypes )
             impl_adviseToPeer( m_xPeer, rContainedType );
@@ -176,18 +110,20 @@ void OMRCListenerMultiplexerHelper::setPeer( const 
Reference< XWindow >& xPeer )
 
 void OMRCListenerMultiplexerHelper::disposeAndClear()
 {
+    std::unique_lock aGuard(m_aMutex);
     EventObject aEvent;
     aEvent.Source = m_xControl;
-    m_aListenerHolder.disposeAndClear( aEvent );
+    m_aListenerHolder.disposeAndClear(aGuard, aEvent);
 }
 
 //  container method
 
 void OMRCListenerMultiplexerHelper::advise( const   Type&                      
 aType       ,
-                                            const   Reference< XInterface >&   
 xListener   )
+                                            const   Reference< XEventListener 
>&    xListener   )
 {
-    MutexGuard aGuard( m_aMutex );
-    if( m_aListenerHolder.addInterface( aType, xListener ) == 1 )
+    assert(xListener && xListener->queryInterface(aType).getValue());
+    std::unique_lock aGuard(m_aMutex);
+    if (m_aListenerHolder.addInterface(aGuard, aType, xListener) == 1)
     {
         // the first listener is added
         if( m_xPeer.is() )
@@ -200,10 +136,10 @@ void OMRCListenerMultiplexerHelper::advise( const   Type&
 //  container method
 
 void OMRCListenerMultiplexerHelper::unadvise(   const   Type&                  
     aType       ,
-                                                const   Reference< XInterface 
>&    xListener   )
+                                                const   Reference< 
XEventListener >&    xListener   )
 {
-    MutexGuard aGuard( m_aMutex );
-    if( m_aListenerHolder.removeInterface( aType, xListener ) == 0 )
+    std::unique_lock aGuard(m_aMutex);
+    if (m_aListenerHolder.removeInterface(aGuard, aType, xListener) == 0)
     {
         // the last listener is removed
         if ( m_xPeer.is() )
@@ -217,7 +153,7 @@ void OMRCListenerMultiplexerHelper::unadvise(   const   
Type&
 
 void SAL_CALL OMRCListenerMultiplexerHelper::disposing( const EventObject& 
/*aSource*/ )
 {
-    MutexGuard aGuard( m_aMutex );
+    std::unique_lock aGuard(m_aMutex);
     // peer is disposed, clear the reference
     m_xPeer.clear();
 }
@@ -226,154 +162,154 @@ void SAL_CALL OMRCListenerMultiplexerHelper::disposing( 
const EventObject& /*aSo
 
 void OMRCListenerMultiplexerHelper::focusGained(const FocusEvent& aEvent )
 {
-    MULTIPLEX( XFocusListener, focusGained, FocusEvent, aEvent )
+    Multiplex(&XFocusListener::focusGained, aEvent);
 }
 
 //  XFcousListener
 
 void OMRCListenerMultiplexerHelper::focusLost(const FocusEvent& aEvent )
 {
-    MULTIPLEX( XFocusListener, focusLost, FocusEvent, aEvent )
+    Multiplex(&XFocusListener::focusLost, aEvent);
 }
 
 //  XWindowListener
 
 void OMRCListenerMultiplexerHelper::windowResized(const WindowEvent& aEvent )
 {
-    MULTIPLEX( XWindowListener, windowResized, WindowEvent, aEvent )
+    Multiplex(&XWindowListener::windowResized, aEvent);
 }
 
 //  XWindowListener
 
 void OMRCListenerMultiplexerHelper::windowMoved(const WindowEvent& aEvent )
 {
-    MULTIPLEX( XWindowListener, windowMoved, WindowEvent, aEvent )
+    Multiplex(&XWindowListener::windowMoved, aEvent);
 }
 
 //  XWindowListener
 
 void OMRCListenerMultiplexerHelper::windowShown(const EventObject& aEvent )
 {
-    MULTIPLEX( XWindowListener, windowShown, EventObject, aEvent )
+    Multiplex(&XWindowListener::windowShown, aEvent);
 }
 
 //  XWindowListener
 
 void OMRCListenerMultiplexerHelper::windowHidden(const EventObject& aEvent )
 {
-    MULTIPLEX( XWindowListener, windowHidden, EventObject, aEvent )
+    Multiplex(&XWindowListener::windowHidden, aEvent);
 }
 
 //  XKeyListener
 
 void OMRCListenerMultiplexerHelper::keyPressed(const KeyEvent& aEvent)
 {
-    MULTIPLEX( XKeyListener, keyPressed, KeyEvent, aEvent )
+    Multiplex(&XKeyListener::keyPressed, aEvent);
 }
 
 //  XKeyListener
 
 void OMRCListenerMultiplexerHelper::keyReleased(const KeyEvent& aEvent)
 {
-    MULTIPLEX( XKeyListener, keyReleased, KeyEvent, aEvent )
+    Multiplex(&XKeyListener::keyReleased, aEvent);
 }
 
 //  XMouseListener
 
 void OMRCListenerMultiplexerHelper::mousePressed(const MouseEvent& aEvent)
 {
-    MULTIPLEX( XMouseListener, mousePressed, MouseEvent, aEvent )
+    Multiplex(&XMouseListener::mousePressed, aEvent);
 }
 
 //  XMouseListener
 
 void OMRCListenerMultiplexerHelper::mouseReleased(const MouseEvent& aEvent)
 {
-    MULTIPLEX( XMouseListener, mouseReleased, MouseEvent, aEvent )
+    Multiplex(&XMouseListener::mouseReleased, aEvent);
 }
 
 //  XMouseListener
 
 void OMRCListenerMultiplexerHelper::mouseEntered(const MouseEvent& aEvent)
 {
-    MULTIPLEX( XMouseListener, mouseEntered, MouseEvent, aEvent )
+    Multiplex(&XMouseListener::mouseEntered, aEvent);
 }
 
 //  XMouseListener
 
 void OMRCListenerMultiplexerHelper::mouseExited(const MouseEvent& aEvent)
 {
-    MULTIPLEX( XMouseListener, mouseExited, MouseEvent, aEvent )
+    Multiplex(&XMouseListener::mouseExited, aEvent);
 }
 
 //  XMouseMotionListener
 
 void OMRCListenerMultiplexerHelper::mouseDragged(const MouseEvent& aEvent)
 {
-    MULTIPLEX( XMouseMotionListener, mouseDragged, MouseEvent, aEvent )
+    Multiplex(&XMouseMotionListener::mouseDragged, aEvent);
 }
 
 //  XMouseMotionListener
 
 void OMRCListenerMultiplexerHelper::mouseMoved(const MouseEvent& aEvent)
 {
-    MULTIPLEX( XMouseMotionListener, mouseMoved, MouseEvent, aEvent )
+    Multiplex(&XMouseMotionListener::mouseMoved, aEvent);
 }
 
 //  XPaintListener
 
 void OMRCListenerMultiplexerHelper::windowPaint(const PaintEvent& aEvent)
 {
-    MULTIPLEX( XPaintListener, windowPaint, PaintEvent, aEvent )
+    Multiplex(&XPaintListener::windowPaint, aEvent);
 }
 
 //  XTopWindowListener
 
 void OMRCListenerMultiplexerHelper::windowOpened(const EventObject& aEvent)
 {
-    MULTIPLEX( XTopWindowListener, windowOpened, EventObject, aEvent )
+    Multiplex(&XTopWindowListener::windowOpened, aEvent);
 }
 
 //  XTopWindowListener
 
 void OMRCListenerMultiplexerHelper::windowClosing( const EventObject& aEvent )
 {
-    MULTIPLEX( XTopWindowListener, windowClosing, EventObject, aEvent )
+    Multiplex(&XTopWindowListener::windowClosing, aEvent);
 }
 
 //  XTopWindowListener
 
 void OMRCListenerMultiplexerHelper::windowClosed( const EventObject& aEvent )
 {
-    MULTIPLEX( XTopWindowListener, windowClosed, EventObject, aEvent )
+    Multiplex(&XTopWindowListener::windowClosed, aEvent);
 }
 
 //  XTopWindowListener
 
 void OMRCListenerMultiplexerHelper::windowMinimized( const EventObject& aEvent 
)
 {
-    MULTIPLEX( XTopWindowListener, windowMinimized, EventObject, aEvent )
+    Multiplex(&XTopWindowListener::windowMinimized, aEvent);
 }
 
 //  XTopWindowListener
 
 void OMRCListenerMultiplexerHelper::windowNormalized( const EventObject& 
aEvent )
 {
-    MULTIPLEX( XTopWindowListener, windowNormalized, EventObject, aEvent )
+    Multiplex(&XTopWindowListener::windowNormalized, aEvent);
 }
 
 //  XTopWindowListener
 
 void OMRCListenerMultiplexerHelper::windowActivated( const EventObject& aEvent 
)
 {
-    MULTIPLEX( XTopWindowListener, windowActivated, EventObject, aEvent )
+    Multiplex(&XTopWindowListener::windowActivated, aEvent);
 }
 
 //  XTopWindowListener
 
 void OMRCListenerMultiplexerHelper::windowDeactivated( const EventObject& 
aEvent )
 {
-    MULTIPLEX( XTopWindowListener, windowDeactivated, EventObject, aEvent )
+    Multiplex(&XTopWindowListener::windowDeactivated, aEvent);
 }
 
 //  protected method

Reply via email to