LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/chart2/source/inc - ModifyListenerHelper.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 54 71 76.1 %
Date: 2013-07-09 Functions: 135 146 92.5 %
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             : #ifndef CHART2_MODIFYLISTENERHELPER_HXX
      20             : #define CHART2_MODIFYLISTENERHELPER_HXX
      21             : 
      22             : #include <cppuhelper/interfacecontainer.hxx>
      23             : #include <com/sun/star/util/XModifyListener.hpp>
      24             : #include <com/sun/star/util/XModifyBroadcaster.hpp>
      25             : #include <com/sun/star/util/XModifiable.hpp>
      26             : #include <com/sun/star/uno/XWeak.hpp>
      27             : #include <cppuhelper/implbase1.hxx>
      28             : #include <cppuhelper/compbase2.hxx>
      29             : #include <cppuhelper/weakref.hxx>
      30             : 
      31             : #include "MutexContainer.hxx"
      32             : #include "charttoolsdllapi.hxx"
      33             : 
      34             : #include <list>
      35             : #include <algorithm>
      36             : #include <functional>
      37             : #include <utility>
      38             : 
      39             : namespace chart
      40             : {
      41             : namespace ModifyListenerHelper
      42             : {
      43             : 
      44             : OOO_DLLPUBLIC_CHARTTOOLS ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > createModifyEventForwarder();
      45             : 
      46             : // ================================================================================
      47             : 
      48             : /** This helper class serves as forwarder of modify events.  It can be used
      49             :     whenever an object has to send modify events after it gets a modify event of
      50             :     one of its children.
      51             : 
      52             :     <p>The listeners are held as WeakReferences if they support XWeak.  Thus the
      53             :     life time of the listeners is independent of the broadcaster's lifetime in
      54             :     this case.</p>
      55             :  */
      56       45140 : class ModifyEventForwarder :
      57             :         public MutexContainer,
      58             :         public ::cppu::WeakComponentImplHelper2<
      59             :             ::com::sun::star::util::XModifyBroadcaster,
      60             :             ::com::sun::star::util::XModifyListener >
      61             : {
      62             : public:
      63             :     ModifyEventForwarder();
      64             : 
      65             :     void FireEvent( const ::com::sun::star::lang::EventObject & rEvent );
      66             : 
      67             :     void AddListener(
      68             :         const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener );
      69             :     void RemoveListener(
      70             :         const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener );
      71             : 
      72             : protected:
      73             :     // ____ XModifyBroadcaster ____
      74             :     virtual void SAL_CALL addModifyListener(
      75             :         const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener )
      76             :         throw (::com::sun::star::uno::RuntimeException);
      77             :     virtual void SAL_CALL removeModifyListener(
      78             :         const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& aListener )
      79             :         throw (::com::sun::star::uno::RuntimeException);
      80             : 
      81             :     // ____ XModifyListener ____
      82             :     virtual void SAL_CALL modified(
      83             :         const ::com::sun::star::lang::EventObject& aEvent )
      84             :         throw (::com::sun::star::uno::RuntimeException);
      85             : 
      86             :     // ____ XEventListener (base of XModifyListener) ____
      87             :     virtual void SAL_CALL disposing(
      88             :         const ::com::sun::star::lang::EventObject& Source )
      89             :         throw (::com::sun::star::uno::RuntimeException);
      90             : 
      91             :     // ____ WeakComponentImplHelperBase ____
      92             :     virtual void SAL_CALL disposing();
      93             : 
      94             : private:
      95             :     /// call disposing() at all listeners and remove all listeners
      96             :     void DisposeAndClear( const ::com::sun::star::uno::Reference<
      97             :                               ::com::sun::star::uno::XWeak > & xSource );
      98             : 
      99             : //     ::osl::Mutex & m_rMutex;
     100             :     ::cppu::OBroadcastHelper  m_aModifyListeners;
     101             : 
     102             :     typedef ::std::list<
     103             :             ::std::pair<
     104             :             ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XModifyListener >,
     105             :             ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > > >
     106             :         tListenerMap;
     107             : 
     108             :     tListenerMap m_aListenerMap;
     109             : };
     110             : 
     111             : // ================================================================================
     112             : 
     113             : namespace impl
     114             : {
     115             : 
     116             : template< class InterfaceRef >
     117       16284 : struct addListenerFunctor : public ::std::unary_function< InterfaceRef, void >
     118             : {
     119       13978 :     explicit addListenerFunctor( const ::com::sun::star::uno::Reference<
     120             :                                      ::com::sun::star::util::XModifyListener > & xListener ) :
     121       13978 :             m_xListener( xListener )
     122       13978 :     {}
     123             : 
     124       13966 :     void operator() ( const InterfaceRef & xObject )
     125             :     {
     126             :         ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
     127       13966 :               xBroadcaster( xObject, ::com::sun::star::uno::UNO_QUERY );
     128       13966 :         if( xBroadcaster.is() && m_xListener.is())
     129       13942 :             xBroadcaster->addModifyListener( m_xListener );
     130       13966 :     }
     131             : private:
     132             :     ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
     133             : };
     134             : 
     135             : template< class InterfaceRef >
     136       28783 : struct removeListenerFunctor : public ::std::unary_function< InterfaceRef, void >
     137             : {
     138       19795 :     explicit removeListenerFunctor( const ::com::sun::star::uno::Reference<
     139             :                                         ::com::sun::star::util::XModifyListener > & xListener ) :
     140       19795 :             m_xListener( xListener )
     141       19795 :     {}
     142             : 
     143       17451 :     void operator() ( const InterfaceRef & xObject )
     144             :     {
     145             :         ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
     146       17451 :               xBroadcaster( xObject, ::com::sun::star::uno::UNO_QUERY );
     147       17451 :         if( xBroadcaster.is() && m_xListener.is())
     148       13715 :             xBroadcaster->removeModifyListener( m_xListener );
     149       17451 :     }
     150             : private:
     151             :     ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
     152             : };
     153             : 
     154             : template< class Pair >
     155           0 : struct addListenerToMappedElementFunctor : public ::std::unary_function< Pair, void >
     156             : {
     157           0 :     explicit addListenerToMappedElementFunctor( const ::com::sun::star::uno::Reference<
     158             :                                                     ::com::sun::star::util::XModifyListener > & xListener ) :
     159           0 :             m_xListener( xListener )
     160           0 :     {}
     161             : 
     162           0 :     void operator() ( const Pair & aPair )
     163             :     {
     164             :         ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
     165           0 :               xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
     166           0 :         if( xBroadcaster.is() && m_xListener.is())
     167           0 :             xBroadcaster->addModifyListener( m_xListener );
     168           0 :     }
     169             : private:
     170             :     ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
     171             : };
     172             : 
     173             : template< class Pair >
     174        1134 : struct removeListenerFromMappedElementFunctor : public ::std::unary_function< Pair, void >
     175             : {
     176         378 :     explicit removeListenerFromMappedElementFunctor( const ::com::sun::star::uno::Reference<
     177             :                                                          ::com::sun::star::util::XModifyListener > & xListener ) :
     178         378 :             m_xListener( xListener )
     179         378 :     {}
     180             : 
     181          58 :     void operator() ( const Pair & aPair )
     182             :     {
     183             :         ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyBroadcaster >
     184          58 :               xBroadcaster( aPair.second, ::com::sun::star::uno::UNO_QUERY );
     185          58 :         if( xBroadcaster.is() && m_xListener.is())
     186          58 :             xBroadcaster->removeModifyListener( m_xListener );
     187          58 :     }
     188             : private:
     189             :     ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > m_xListener;
     190             : };
     191             : 
     192             : } //  namespace impl
     193             : 
     194             : // --------------------------------------------------------------------------------
     195             : 
     196             : 
     197             : template< class InterfaceRef >
     198       12825 : void addListener(
     199             :     const InterfaceRef & xObject,
     200             :     const ::com::sun::star::uno::Reference<
     201             :         ::com::sun::star::util::XModifyListener > & xListener )
     202             : {
     203       12825 :     if( xListener.is())
     204             :     {
     205       12825 :         impl::addListenerFunctor< InterfaceRef > aFunctor( xListener );
     206       12825 :         aFunctor( xObject );
     207             :     }
     208       12825 : }
     209             : 
     210             : template< class Container >
     211        1153 : void addListenerToAllElements(
     212             :     const Container & rContainer,
     213             :     const ::com::sun::star::uno::Reference<
     214             :         ::com::sun::star::util::XModifyListener > & xListener )
     215             : {
     216        1153 :     if( xListener.is())
     217        1153 :         ::std::for_each( rContainer.begin(), rContainer.end(),
     218             :                          impl::addListenerFunctor< typename Container::value_type >( xListener ));
     219        1153 : }
     220             : 
     221             : template< class Container >
     222           0 : void addListenerToAllMapElements(
     223             :     const Container & rContainer,
     224             :     const ::com::sun::star::uno::Reference<
     225             :         ::com::sun::star::util::XModifyListener > & xListener )
     226             : {
     227           0 :     if( xListener.is())
     228           0 :         ::std::for_each( rContainer.begin(), rContainer.end(),
     229             :                          impl::addListenerToMappedElementFunctor< typename Container::value_type >( xListener ));
     230           0 : }
     231             : 
     232             : template< typename T >
     233           0 : void addListenerToAllSequenceElements(
     234             :     const ::com::sun::star::uno::Sequence< T > & rSequence,
     235             :     const ::com::sun::star::uno::Reference<
     236             :         ::com::sun::star::util::XModifyListener > & xListener )
     237             : {
     238           0 :     if( xListener.is())
     239           0 :         ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
     240             :                          impl::addListenerFunctor< T >( xListener ));
     241           0 : }
     242             : 
     243             : template< class InterfaceRef >
     244       15301 : void removeListener(
     245             :     const InterfaceRef & xObject,
     246             :     const ::com::sun::star::uno::Reference<
     247             :         ::com::sun::star::util::XModifyListener > & xListener )
     248             : {
     249       15301 :     if( xListener.is())
     250             :     {
     251       15301 :         impl::removeListenerFunctor< InterfaceRef > aFunctor( xListener );
     252       15301 :         aFunctor( xObject );
     253             :     }
     254       15301 : }
     255             : 
     256             : template< class Container >
     257        4068 : void removeListenerFromAllElements(
     258             :     const Container & rContainer,
     259             :     const ::com::sun::star::uno::Reference<
     260             :         ::com::sun::star::util::XModifyListener > & xListener )
     261             : {
     262        4068 :     if( xListener.is())
     263        4068 :         ::std::for_each( rContainer.begin(), rContainer.end(),
     264             :                          impl::removeListenerFunctor< typename Container::value_type >( xListener ));
     265        4068 : }
     266             : 
     267             : template< class Container >
     268         378 : void removeListenerFromAllMapElements(
     269             :     const Container & rContainer,
     270             :     const ::com::sun::star::uno::Reference<
     271             :         ::com::sun::star::util::XModifyListener > & xListener )
     272             : {
     273         378 :     if( xListener.is())
     274         378 :         ::std::for_each( rContainer.begin(), rContainer.end(),
     275             :                          impl::removeListenerFromMappedElementFunctor< typename Container::value_type >( xListener ));
     276         378 : }
     277             : 
     278             : template< typename T >
     279         426 : void removeListenerFromAllSequenceElements(
     280             :     const ::com::sun::star::uno::Sequence< T > & rSequence,
     281             :     const ::com::sun::star::uno::Reference<
     282             :         ::com::sun::star::util::XModifyListener > & xListener )
     283             : {
     284         426 :     if( xListener.is())
     285         426 :         ::std::for_each( rSequence.getConstArray(), rSequence.getConstArray() + rSequence.getLength(),
     286             :                          impl::removeListenerFunctor< T >( xListener ));
     287         426 : }
     288             : 
     289             : } //  namespace ModifyListenerHelper
     290             : } //  namespace chart
     291             : 
     292             : // CHART2_MODIFYLISTENERHELPER_HXX
     293             : #endif
     294             : 
     295             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10