LCOV - code coverage report
Current view: top level - svl/source/notify - SfxBroadcaster.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 45 51 88.2 %
Date: 2015-06-13 12:38:46 Functions: 16 19 84.2 %
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 <svl/SfxBroadcaster.hxx>
      21             : 
      22             : #include <svl/hint.hxx>
      23             : #include <svl/smplhint.hxx>
      24             : #include <svl/lstner.hxx>
      25             : #include <tools/debug.hxx>
      26             : 
      27             : #include <algorithm>
      28             : #include <cassert>
      29             : #include <vector>
      30             : 
      31      290397 : TYPEINIT0(SfxBroadcaster);
      32             : 
      33             : typedef std::vector<SfxListener*> SfxListenerArr_Impl;
      34             : 
      35      994228 : struct SfxBroadcaster::Impl
      36             : {
      37             :     /** Contains the positions of removed listeners. */
      38             :     std::vector<size_t>     m_RemovedPositions;
      39             :     SfxListenerArr_Impl     m_Listeners;
      40             : };
      41             : 
      42             : // broadcast immediately
      43             : 
      44     1574585 : void SfxBroadcaster::Broadcast( const SfxHint &rHint )
      45             : {
      46             :     // notify all registered listeners exactly once
      47    16349200 :     for (size_t i = 0; i < mpImpl->m_Listeners.size(); ++i)
      48             :     {
      49    14774615 :         SfxListener *const pListener = mpImpl->m_Listeners[i];
      50    14774615 :         if (pListener)
      51    10356865 :             pListener->Notify( *this, rHint );
      52             :     }
      53     1574585 : }
      54             : 
      55             : // unregister all listeners
      56             : 
      57      604997 : SfxBroadcaster::~SfxBroadcaster()
      58             : {
      59      486727 :     Broadcast( SfxSimpleHint(SFX_HINT_DYING) );
      60             : 
      61             :     // remove all still registered listeners
      62      783307 :     for (size_t i = 0; i < mpImpl->m_Listeners.size(); ++i)
      63             :     {
      64      296580 :         SfxListener *const pListener = mpImpl->m_Listeners[i];
      65      296580 :         if (pListener)
      66       37858 :             pListener->RemoveBroadcaster_Impl(*this);
      67             :     }
      68             : 
      69      486727 :     delete mpImpl;
      70      604997 : }
      71             : 
      72             : 
      73             : // simple ctor of class SfxBroadcaster
      74             : 
      75      507501 : SfxBroadcaster::SfxBroadcaster() : mpImpl(new Impl)
      76             : {
      77      507501 : }
      78             : 
      79             : 
      80             : // copy ctor of class SfxBroadcaster
      81             : 
      82             : 
      83           0 : SfxBroadcaster::SfxBroadcaster( const SfxBroadcaster &rBC ) : mpImpl(new Impl)
      84             : {
      85           0 :     for (size_t i = 0; i < rBC.mpImpl->m_Listeners.size(); ++i)
      86             :     {
      87           0 :         SfxListener *const pListener = rBC.mpImpl->m_Listeners[i];
      88           0 :         if (pListener)
      89           0 :             pListener->StartListening( *this );
      90             :     }
      91           0 : }
      92             : 
      93             : 
      94             : // add a new SfxListener to the list
      95             : 
      96      804221 : void SfxBroadcaster::AddListener( SfxListener& rListener )
      97             : {
      98             :     DBG_TESTSOLARMUTEX();
      99      804221 :     if (mpImpl->m_RemovedPositions.empty())
     100             :     {
     101      307886 :         mpImpl->m_Listeners.push_back(&rListener);
     102             :     }
     103             :     else
     104             :     {
     105      496335 :         size_t targetPosition = mpImpl->m_RemovedPositions.back();
     106      496335 :         mpImpl->m_RemovedPositions.pop_back();
     107             :         assert(mpImpl->m_Listeners[targetPosition] == NULL);
     108      496335 :         mpImpl->m_Listeners[targetPosition] = &rListener;
     109             :     }
     110      804221 : }
     111             : 
     112             : 
     113             : // forward a notification to all registered listeners
     114             : 
     115     1566363 : void SfxBroadcaster::Forward(SfxBroadcaster& rBC, const SfxHint& rHint)
     116             : {
     117     2934165 :     for (size_t i = 0; i < mpImpl->m_Listeners.size(); ++i)
     118             :     {
     119     1367802 :         SfxListener *const pListener = mpImpl->m_Listeners[i];
     120     1367802 :         if (pListener)
     121     1270437 :             pListener->Notify( rBC, rHint );
     122             :     }
     123     1566363 : }
     124             : 
     125             : 
     126             : // remove one SfxListener from the list
     127             : 
     128      756009 : void SfxBroadcaster::RemoveListener( SfxListener& rListener )
     129             : {
     130             :     DBG_TESTSOLARMUTEX();
     131             :     SfxListenerArr_Impl::iterator aIter = std::find(
     132      756009 :             mpImpl->m_Listeners.begin(), mpImpl->m_Listeners.end(), &rListener);
     133             :     assert(aIter != mpImpl->m_Listeners.end()); // "RemoveListener: Listener unknown"
     134             :     // DO NOT erase the listener, set the pointer to 0
     135             :     // because the current continuation may contain this->Broadcast
     136      756009 :     *aIter = 0;
     137      756009 :     size_t positionOfRemovedElement = std::distance(mpImpl->m_Listeners.begin(), aIter);
     138      756009 :     mpImpl->m_RemovedPositions.push_back(positionOfRemovedElement);
     139      756009 : }
     140             : 
     141        2365 : bool SfxBroadcaster::HasListeners() const
     142             : {
     143        2365 :     return (GetListenerCount() != 0);
     144             : }
     145             : 
     146        2371 : size_t SfxBroadcaster::GetListenerCount() const
     147             : {
     148             :     assert(mpImpl->m_Listeners.size() >= mpImpl->m_RemovedPositions.size());
     149        2371 :     return mpImpl->m_Listeners.size() - mpImpl->m_RemovedPositions.size();
     150             : }
     151             : 
     152      134965 : size_t SfxBroadcaster::GetSizeOfVector() const
     153             : {
     154      134965 :     return mpImpl->m_Listeners.size();
     155             : }
     156             : 
     157     2146019 : SfxListener* SfxBroadcaster::GetListener( size_t nNo ) const
     158             : {
     159     2146019 :     return mpImpl->m_Listeners[nNo];
     160             : }
     161             : 
     162             : 
     163             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11