LCOV - code coverage report
Current view: top level - svl/source/notify - broadcast.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 61 75 81.3 %
Date: 2014-04-11 Functions: 11 12 91.7 %
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/broadcast.hxx>
      21             : #include <svl/listener.hxx>
      22             : #include <svl/smplhint.hxx>
      23             : 
      24       17962 : void SvtBroadcaster::Normalize()
      25             : {
      26       17962 :     if (!mbNormalized)
      27             :     {
      28        9369 :         std::sort(maListeners.begin(), maListeners.end());
      29        9369 :         ListenersType::iterator itUniqueEnd = std::unique(maListeners.begin(), maListeners.end());
      30        9369 :         maListeners.erase(itUniqueEnd, maListeners.end());
      31        9369 :         mbNormalized = true;
      32             :     }
      33             : 
      34       17962 :     if (!mbDestNormalized)
      35             :     {
      36        6711 :         std::sort(maDestructedListeners.begin(), maDestructedListeners.end());
      37        6711 :         ListenersType::iterator itUniqueEnd = std::unique(maDestructedListeners.begin(), maDestructedListeners.end());
      38        6711 :         maDestructedListeners.erase(itUniqueEnd, maDestructedListeners.end());
      39        6711 :         mbDestNormalized = true;
      40             :     }
      41       17962 : }
      42             : 
      43       12829 : void SvtBroadcaster::Add( SvtListener* p )
      44             : {
      45       12829 :     maListeners.push_back(p);
      46       12829 :     mbNormalized = false;
      47       12829 : }
      48             : 
      49       12409 : void SvtBroadcaster::Remove( SvtListener* p )
      50             : {
      51       12409 :     if (mbDisposing)
      52       11946 :         return;
      53             : 
      54        9035 :     if (mbAboutToDie)
      55             :     {
      56        5198 :         maDestructedListeners.push_back(p);
      57        5198 :         mbDestNormalized = false;
      58        5198 :         return;
      59             :     }
      60             : 
      61        3837 :     Normalize();
      62             :     std::pair<ListenersType::iterator,ListenersType::iterator> r =
      63        3837 :         std::equal_range(maListeners.begin(), maListeners.end(), p);
      64             : 
      65        3837 :     if (r.first != r.second)
      66        3837 :         maListeners.erase(r.first, r.second);
      67        3837 :     if (maListeners.empty())
      68        1722 :         ListenersGone();
      69             : }
      70             : 
      71        7849 : SvtBroadcaster::SvtBroadcaster() : mbAboutToDie(false), mbDisposing(false), mbNormalized(false), mbDestNormalized(false) {}
      72             : 
      73           0 : SvtBroadcaster::SvtBroadcaster( const SvtBroadcaster &rBC ) :
      74             :     maListeners(rBC.maListeners), maDestructedListeners(rBC.maDestructedListeners),
      75             :     mbAboutToDie(rBC.mbAboutToDie), mbDisposing(false),
      76           0 :     mbNormalized(rBC.mbNormalized), mbDestNormalized(rBC.mbDestNormalized)
      77             : {
      78           0 :     if (mbAboutToDie)
      79           0 :         Normalize();
      80             : 
      81           0 :     ListenersType::iterator dest(maDestructedListeners.begin());
      82           0 :     for (ListenersType::iterator it(maListeners.begin()); it != maListeners.end(); ++it)
      83             :     {
      84           0 :         bool bStart = true;
      85             : 
      86           0 :         if (mbAboutToDie)
      87             :         {
      88             :             // skip the destructed ones
      89           0 :             while (dest != maDestructedListeners.end() && (*dest < *it))
      90           0 :                 ++dest;
      91             : 
      92           0 :             bStart = (dest == maDestructedListeners.end() || *dest != *it);
      93             :         }
      94             : 
      95           0 :         if (bStart)
      96           0 :             (*it)->StartListening(*this);
      97             :     }
      98           0 : }
      99             : 
     100       17508 : SvtBroadcaster::~SvtBroadcaster()
     101             : {
     102        6606 :     mbDisposing = true;
     103        6606 :     Broadcast( SfxSimpleHint(SFX_HINT_DYING) );
     104             : 
     105        6606 :     Normalize();
     106             : 
     107             :     // now when both lists are sorted, we can linearly unregister all
     108             :     // listeners, with the exception of those that already asked to be removed
     109             :     // during their own destruction
     110        6606 :     ListenersType::iterator dest(maDestructedListeners.begin());
     111       15178 :     for (ListenersType::iterator it(maListeners.begin()); it != maListeners.end(); ++it)
     112             :     {
     113             :         // skip the destructed ones
     114       18633 :         while (dest != maDestructedListeners.end() && (*dest < *it))
     115        1489 :             ++dest;
     116             : 
     117        8572 :         if (dest == maDestructedListeners.end() || *dest != *it)
     118        3374 :             (*it)->EndListening(*this);
     119             :     }
     120       10902 : }
     121             : 
     122        7519 : void SvtBroadcaster::Broadcast( const SfxHint &rHint )
     123             : {
     124        7519 :     Normalize();
     125             : 
     126        7519 :     ListenersType::iterator dest(maDestructedListeners.begin());
     127        7519 :     ListenersType aListeners(maListeners); // this copy is important to avoid erasing entries while iterating
     128       58936 :     for (ListenersType::iterator it(aListeners.begin()); it != aListeners.end(); ++it)
     129             :     {
     130             :         // skip the destructed ones
     131      104323 :         while (dest != maDestructedListeners.end() && (*dest < *it))
     132        1489 :             ++dest;
     133             : 
     134       51417 :         if (dest == maDestructedListeners.end() || *dest != *it)
     135       46219 :             (*it)->Notify(rHint);
     136        7519 :     }
     137        7519 : }
     138             : 
     139        1719 : void SvtBroadcaster::ListenersGone() {}
     140             : 
     141           5 : SvtBroadcaster::ListenersType& SvtBroadcaster::GetAllListeners()
     142             : {
     143           5 :     return maListeners;
     144             : }
     145             : 
     146        1281 : bool SvtBroadcaster::HasListeners() const
     147             : {
     148        1281 :     return !maListeners.empty();
     149             : }
     150             : 
     151        3906 : void SvtBroadcaster::PrepareForDestruction()
     152             : {
     153        3906 :     mbAboutToDie = true;
     154        3906 :     maDestructedListeners.reserve(maListeners.size());
     155        3906 : }
     156             : 
     157             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10