LCOV - code coverage report
Current view: top level - unotools/source/misc - eventlisteneradapter.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 53 56 94.6 %
Date: 2014-04-11 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       17862 :     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           1 :         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       10285 :     OEventListenerImpl::OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp )
      56       10285 :         :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       10285 :         Reference< XEventListener > xMeMyselfAndI = this;
      65       10285 :         _rxComp->addEventListener(xMeMyselfAndI);
      66             : 
      67       10285 :         m_xComponent = _rxComp;
      68       10285 :         m_xKeepMeAlive = xMeMyselfAndI;
      69       10285 :     }
      70             : 
      71        8931 :     void OEventListenerImpl::dispose()
      72             :     {
      73        8931 :         if (m_xComponent.is())
      74             :         {
      75        5194 :             m_xComponent->removeEventListener(m_xKeepMeAlive);
      76        5194 :             m_xComponent.clear();
      77        5194 :             m_xKeepMeAlive.clear();
      78             :         }
      79        8931 :     }
      80             : 
      81        5052 :     void SAL_CALL OEventListenerImpl::disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception)
      82             :     {
      83        5052 :         Reference< XEventListener > xDeleteUponLeaving = m_xKeepMeAlive;
      84        5052 :         m_xKeepMeAlive.clear();
      85        5052 :         m_xComponent.clear();
      86             : 
      87        5052 :         m_pAdapter->_disposing(_rSource);
      88        5052 :     }
      89             : 
      90             :     //= OEventListenerAdapterImpl
      91             : 
      92       16464 :     struct OEventListenerAdapterImpl
      93             :     {
      94             :     public:
      95             :         ::std::vector< void* >  aListeners;
      96             :     };
      97             : 
      98             :     //= OEventListenerAdapter
      99             : 
     100        9733 :     OEventListenerAdapter::OEventListenerAdapter()
     101        9733 :         :m_pImpl(new OEventListenerAdapterImpl)
     102             :     {
     103        9733 :     }
     104             : 
     105        6731 :     OEventListenerAdapter::~OEventListenerAdapter()
     106             :     {
     107        6731 :         stopAllComponentListening( );
     108        6731 :         delete m_pImpl;
     109        6731 :         m_pImpl = NULL;
     110        6731 :     }
     111             : 
     112           3 :     void OEventListenerAdapter::stopComponentListening( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& _rxComp )
     113             :     {
     114           3 :         if ( m_pImpl->aListeners.empty() )
     115           5 :             return;
     116             : 
     117           1 :         ::std::vector< void* >::iterator dispose = m_pImpl->aListeners.begin();
     118           2 :         do
     119             :         {
     120           1 :             OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >( *dispose );
     121           1 :             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           1 :                 ++dispose;
     129             :         }
     130           2 :         while ( dispose != m_pImpl->aListeners.end() );
     131             :     }
     132             : 
     133       10959 :     void OEventListenerAdapter::stopAllComponentListening(  )
     134             :     {
     135       59670 :         for (   ::std::vector< void* >::const_iterator aDisposeLoop = m_pImpl->aListeners.begin();
     136       39780 :                 aDisposeLoop != m_pImpl->aListeners.end();
     137             :                 ++aDisposeLoop
     138             :             )
     139             :         {
     140        8931 :             OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >(*aDisposeLoop);
     141        8931 :             pListenerImpl->dispose();
     142        8931 :             pListenerImpl->release();
     143             :         }
     144       10959 :         m_pImpl->aListeners.clear();
     145       10959 :     }
     146             : 
     147       10285 :     void OEventListenerAdapter::startComponentListening( const Reference< XComponent >& _rxComp )
     148             :     {
     149       10285 :         if (!_rxComp.is())
     150             :         {
     151             :             OSL_FAIL("OEventListenerAdapter::startComponentListening: invalid component!");
     152       10285 :             return;
     153             :         }
     154             : 
     155       10285 :         OEventListenerImpl* pListenerImpl = new OEventListenerImpl(this, _rxComp);
     156       10285 :         pListenerImpl->acquire();
     157       10285 :         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