LCOV - code coverage report
Current view: top level - chart2/source/tools - ModifyListenerHelper.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 65 69 94.2 %
Date: 2015-06-13 12:38:46 Functions: 16 16 100.0 %
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 "ModifyListenerHelper.hxx"
      21             : #include "WeakListenerAdapter.hxx"
      22             : #include "macros.hxx"
      23             : 
      24             : #include <cppuhelper/interfacecontainer.hxx>
      25             : 
      26             : #include <com/sun/star/frame/XModel.hpp>
      27             : 
      28             : using namespace ::com::sun::star;
      29             : 
      30             : using ::com::sun::star::uno::Reference;
      31             : using ::com::sun::star::uno::Sequence;
      32             : 
      33             : namespace
      34             : {
      35             : 
      36      258934 : void lcl_fireModifyEvent(
      37             :     ::cppu::OBroadcastHelper & rBroadcastHelper,
      38             :     const Reference< uno::XWeak > & xEventSource,
      39             :     const lang::EventObject * pEvent )
      40             : {
      41             :     ::cppu::OInterfaceContainerHelper * pCntHlp = rBroadcastHelper.getContainer(
      42      258934 :         cppu::UnoType<util::XModifyListener>::get());
      43      258934 :     if( pCntHlp )
      44             :     {
      45       48077 :         lang::EventObject aEventToSend;
      46       48077 :         if( pEvent )
      47       48077 :             aEventToSend = *pEvent;
      48             :         else
      49           0 :             aEventToSend.Source.set( xEventSource );
      50             :         OSL_ENSURE( aEventToSend.Source.is(), "Sending event without source" );
      51             : 
      52       96154 :         ::cppu::OInterfaceIteratorHelper aIt( *pCntHlp );
      53             : 
      54      144230 :         while( aIt.hasMoreElements())
      55             :         {
      56       48076 :             Reference< util::XModifyListener > xModListener( aIt.next(), uno::UNO_QUERY );
      57       48076 :             if( xModListener.is())
      58       48076 :                 xModListener->modified( aEventToSend );
      59       96153 :         }
      60             :     }
      61      258934 : }
      62             : 
      63       65415 : struct lcl_weakReferenceToSame : public ::std::unary_function<
      64             :         ::std::pair<
      65             :             ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XModifyListener >,
      66             :             ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > >,
      67             :         bool >
      68             : {
      69       21805 :     explicit lcl_weakReferenceToSame( const Reference< util::XModifyListener > & xModListener ) :
      70       21805 :             m_xHardRef( xModListener )
      71       21805 :     {}
      72             : 
      73       21805 :     bool operator() ( const argument_type & xElem )
      74             :     {
      75       21805 :         Reference< util::XModifyListener > xWeakAsHard( xElem.first );
      76       21805 :         if( xWeakAsHard.is())
      77       21805 :             return (xWeakAsHard == m_xHardRef);
      78           0 :         return false;
      79             :     }
      80             : 
      81             : private:
      82             :     Reference< util::XModifyListener > m_xHardRef;
      83             : };
      84             : 
      85             : } //  anonymous namespace
      86             : 
      87             : namespace chart
      88             : {
      89             : namespace ModifyListenerHelper
      90             : {
      91             : 
      92      114215 : uno::Reference< util::XModifyListener > createModifyEventForwarder()
      93             : {
      94      114215 :     return new ModifyEventForwarder();
      95             : }
      96             : 
      97      114255 : ModifyEventForwarder::ModifyEventForwarder() :
      98             :         ::cppu::WeakComponentImplHelper2<
      99             :         ::com::sun::star::util::XModifyBroadcaster,
     100             :         ::com::sun::star::util::XModifyListener >( m_aMutex ),
     101      114255 :         m_aModifyListeners( m_aMutex )
     102             : {
     103      114255 : }
     104             : 
     105      258934 : void ModifyEventForwarder::FireEvent( const lang::EventObject & rEvent )
     106             : {
     107      258934 :     lcl_fireModifyEvent( m_aModifyListeners, Reference< uno::XWeak >(), & rEvent );
     108      258934 : }
     109             : 
     110       22692 : void ModifyEventForwarder::AddListener( const Reference< util::XModifyListener >& aListener )
     111             : {
     112             :     try
     113             :     {
     114       22692 :         Reference< util::XModifyListener > xListenerToAdd( aListener );
     115             : 
     116       45384 :         Reference< uno::XWeak > xWeak( aListener, uno::UNO_QUERY );
     117       22692 :         if( xWeak.is())
     118             :         {
     119             :             // remember the helper class for later remove
     120       22692 :             uno::WeakReference< util::XModifyListener > xWeakRef( aListener );
     121       22692 :             xListenerToAdd.set( new WeakModifyListenerAdapter( xWeakRef ));
     122       22692 :             m_aListenerMap.push_back( tListenerMap::value_type( xWeakRef, xListenerToAdd ));
     123             :         }
     124             : 
     125       45384 :         m_aModifyListeners.addListener( cppu::UnoType<decltype(xListenerToAdd)>::get(), xListenerToAdd );
     126             :     }
     127           0 :     catch( const uno::Exception & ex )
     128             :     {
     129             :         ASSERT_EXCEPTION( ex );
     130             :     }
     131       22692 : }
     132             : 
     133       21805 : void ModifyEventForwarder::RemoveListener( const Reference< util::XModifyListener >& aListener )
     134             : {
     135             :     try
     136             :     {
     137             :         // look up fitting helper class that has been added
     138       21805 :         Reference< util::XModifyListener > xListenerToRemove( aListener );
     139             :         tListenerMap::iterator aIt(
     140       21805 :             ::std::find_if( m_aListenerMap.begin(), m_aListenerMap.end(), lcl_weakReferenceToSame( aListener )));
     141       21805 :         if( aIt != m_aListenerMap.end())
     142             :         {
     143       21805 :             xListenerToRemove.set( (*aIt).second );
     144             :             // map entry is no longer needed
     145       21805 :             m_aListenerMap.erase( aIt );
     146             :         }
     147             : 
     148       21805 :         m_aModifyListeners.removeListener( cppu::UnoType<decltype(aListener)>::get(), xListenerToRemove );
     149             :     }
     150           0 :     catch( const uno::Exception & ex )
     151             :     {
     152             :         ASSERT_EXCEPTION( ex );
     153             :     }
     154       21805 : }
     155             : 
     156      113891 : void ModifyEventForwarder::DisposeAndClear( const Reference< uno::XWeak > & xSource )
     157             : {
     158             :     ::cppu::OInterfaceContainerHelper * pCntHlp = m_aModifyListeners.getContainer(
     159      113891 :         cppu::UnoType<util::XModifyListener>::get());
     160      113891 :     if( pCntHlp )
     161       21882 :         pCntHlp->disposeAndClear( lang::EventObject( xSource ) );
     162      113891 : }
     163             : 
     164             : // ____ XModifyBroadcaster ____
     165       22692 : void SAL_CALL ModifyEventForwarder::addModifyListener( const Reference< util::XModifyListener >& aListener )
     166             :     throw (uno::RuntimeException, std::exception)
     167             : {
     168       22692 :     AddListener( aListener );
     169       22692 : }
     170             : 
     171       21805 : void SAL_CALL ModifyEventForwarder::removeModifyListener( const Reference< util::XModifyListener >& aListener )
     172             :     throw (uno::RuntimeException, std::exception)
     173             : {
     174       21805 :     RemoveListener( aListener );
     175       21805 : }
     176             : 
     177             : // ____ XModifyListener ____
     178      258934 : void SAL_CALL ModifyEventForwarder::modified( const lang::EventObject& aEvent )
     179             :     throw (uno::RuntimeException, std::exception)
     180             : {
     181      258934 :     FireEvent( aEvent );
     182      258934 : }
     183             : 
     184             : // ____ XEventListener (base of XModifyListener) ____
     185          30 : void SAL_CALL ModifyEventForwarder::disposing( const lang::EventObject& /* Source */ )
     186             :     throw (uno::RuntimeException, std::exception)
     187             : {
     188             :     // nothing
     189          30 : }
     190             : 
     191             : // ____ WeakComponentImplHelperBase ____
     192      113891 : void SAL_CALL ModifyEventForwarder::disposing()
     193             : {
     194             :     // dispose was called at this
     195      113891 :     DisposeAndClear( this );
     196      113891 : }
     197             : 
     198             : } //  namespace ModifyListenerHelper
     199             : } //  namespace chart
     200             : 
     201             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11