LCOV - code coverage report
Current view: top level - unotools/source/misc - eventlisteneradapter.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 53 56 94.6 %
Date: 2014-11-03 Functions: 13 14 92.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <sal/config.h>
      21             : 
      22             : #include <vector>
      23             : 
      24             : #include <unotools/eventlisteneradapter.hxx>
      25             : #include <osl/diagnose.h>
      26             : #include <cppuhelper/implbase1.hxx>
      27             : 
      28             : namespace utl
      29             : {
      30             : 
      31             :     using namespace ::com::sun::star::uno;
      32             :     using namespace ::com::sun::star::lang;
      33             : 
      34             :     //= OEventListenerImpl
      35             : 
      36       49382 :     class OEventListenerImpl : public ::cppu::WeakImplHelper1< XEventListener >
      37             :     {
      38             :     protected:
      39             :         OEventListenerAdapter*          m_pAdapter;
      40             :         Reference< XEventListener >     m_xKeepMeAlive;
      41             :             // imagine an implementation of XComponent which holds it's listeners with a weak reference ...
      42             :             // would be very bad if we don't hold ourself
      43             :         Reference< XComponent >         m_xComponent;
      44             : 
      45             :     public:
      46             :         OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp );
      47             : 
      48             :         void                            dispose();
      49           2 :         const Reference< XComponent >&  getComponent() const { return m_xComponent; }
      50             : 
      51             :     protected:
      52             :         virtual void SAL_CALL disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      53             :     };
      54             : 
      55       28362 :     OEventListenerImpl::OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp )
      56       28362 :         :m_pAdapter(_pAdapter)
      57             :     {
      58             :         OSL_ENSURE(m_pAdapter, "OEventListenerImpl::OEventListenerImpl: invalid adapter!");
      59             :         // no checks of _rxComp !!
      60             :         // (OEventListenerAdapter is responsible for this)
      61             : 
      62             :         // just in case addEventListener throws an exception ... don't initialize m_xKeepMeAlive before this
      63             :         // is done
      64       28362 :         Reference< XEventListener > xMeMyselfAndI = this;
      65       28362 :         _rxComp->addEventListener(xMeMyselfAndI);
      66             : 
      67       28362 :         m_xComponent = _rxComp;
      68       28362 :         m_xKeepMeAlive = xMeMyselfAndI;
      69       28362 :     }
      70             : 
      71       24691 :     void OEventListenerImpl::dispose()
      72             :     {
      73       24691 :         if (m_xComponent.is())
      74             :         {
      75       14500 :             m_xComponent->removeEventListener(m_xKeepMeAlive);
      76       14500 :             m_xComponent.clear();
      77       14500 :             m_xKeepMeAlive.clear();
      78             :         }
      79       24691 :     }
      80             : 
      81       13833 :     void SAL_CALL OEventListenerImpl::disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception)
      82             :     {
      83       13833 :         Reference< XEventListener > xDeleteUponLeaving = m_xKeepMeAlive;
      84       13833 :         m_xKeepMeAlive.clear();
      85       13833 :         m_xComponent.clear();
      86             : 
      87       13833 :         m_pAdapter->_disposing(_rSource);
      88       13833 :     }
      89             : 
      90             :     //= OEventListenerAdapterImpl
      91             : 
      92       52574 :     struct OEventListenerAdapterImpl
      93             :     {
      94             :     public:
      95             :         ::std::vector< void* >  aListeners;
      96             :     };
      97             : 
      98             :     //= OEventListenerAdapter
      99             : 
     100       26573 :     OEventListenerAdapter::OEventListenerAdapter()
     101       26573 :         :m_pImpl(new OEventListenerAdapterImpl)
     102             :     {
     103       26573 :     }
     104             : 
     105       26001 :     OEventListenerAdapter::~OEventListenerAdapter()
     106             :     {
     107       26001 :         stopAllComponentListening( );
     108       26001 :         delete m_pImpl;
     109       26001 :         m_pImpl = NULL;
     110       26001 :     }
     111             : 
     112          26 :     void OEventListenerAdapter::stopComponentListening( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& _rxComp )
     113             :     {
     114          26 :         if ( m_pImpl->aListeners.empty() )
     115          50 :             return;
     116             : 
     117           2 :         ::std::vector< void* >::iterator dispose = m_pImpl->aListeners.begin();
     118           4 :         do
     119             :         {
     120           2 :             OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >( *dispose );
     121           2 :             if ( pListenerImpl->getComponent().get() == _rxComp.get() )
     122             :             {
     123           0 :                 pListenerImpl->dispose();
     124           0 :                 pListenerImpl->release();
     125           0 :                 dispose = m_pImpl->aListeners.erase( dispose );
     126             :             }
     127             :             else
     128           2 :                 ++dispose;
     129             :         }
     130           4 :         while ( dispose != m_pImpl->aListeners.end() );
     131             :     }
     132             : 
     133       37824 :     void OEventListenerAdapter::stopAllComponentListening(  )
     134             :     {
     135      187545 :         for (   ::std::vector< void* >::const_iterator aDisposeLoop = m_pImpl->aListeners.begin();
     136      125030 :                 aDisposeLoop != m_pImpl->aListeners.end();
     137             :                 ++aDisposeLoop
     138             :             )
     139             :         {
     140       24691 :             OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >(*aDisposeLoop);
     141       24691 :             pListenerImpl->dispose();
     142       24691 :             pListenerImpl->release();
     143             :         }
     144       37824 :         m_pImpl->aListeners.clear();
     145       37824 :     }
     146             : 
     147       28362 :     void OEventListenerAdapter::startComponentListening( const Reference< XComponent >& _rxComp )
     148             :     {
     149       28362 :         if (!_rxComp.is())
     150             :         {
     151             :             OSL_FAIL("OEventListenerAdapter::startComponentListening: invalid component!");
     152       28362 :             return;
     153             :         }
     154             : 
     155       28362 :         OEventListenerImpl* pListenerImpl = new OEventListenerImpl(this, _rxComp);
     156       28362 :         pListenerImpl->acquire();
     157       28362 :         m_pImpl->aListeners.push_back(pListenerImpl);
     158             :     }
     159             : 
     160             : }   // namespace utl
     161             : 
     162             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10