LCOV - code coverage report
Current view: top level - svl/source/notify - broadcast.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 75 0.0 %
Date: 2014-04-14 Functions: 0 12 0.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 <svl/broadcast.hxx>
      21             : #include <svl/listener.hxx>
      22             : #include <svl/smplhint.hxx>
      23             : 
      24           0 : void SvtBroadcaster::Normalize()
      25             : {
      26           0 :     if (!mbNormalized)
      27             :     {
      28           0 :         std::sort(maListeners.begin(), maListeners.end());
      29           0 :         ListenersType::iterator itUniqueEnd = std::unique(maListeners.begin(), maListeners.end());
      30           0 :         maListeners.erase(itUniqueEnd, maListeners.end());
      31           0 :         mbNormalized = true;
      32             :     }
      33             : 
      34           0 :     if (!mbDestNormalized)
      35             :     {
      36           0 :         std::sort(maDestructedListeners.begin(), maDestructedListeners.end());
      37           0 :         ListenersType::iterator itUniqueEnd = std::unique(maDestructedListeners.begin(), maDestructedListeners.end());
      38           0 :         maDestructedListeners.erase(itUniqueEnd, maDestructedListeners.end());
      39           0 :         mbDestNormalized = true;
      40             :     }
      41           0 : }
      42             : 
      43           0 : void SvtBroadcaster::Add( SvtListener* p )
      44             : {
      45           0 :     maListeners.push_back(p);
      46           0 :     mbNormalized = false;
      47           0 : }
      48             : 
      49           0 : void SvtBroadcaster::Remove( SvtListener* p )
      50             : {
      51           0 :     if (mbDisposing)
      52           0 :         return;
      53             : 
      54           0 :     if (mbAboutToDie)
      55             :     {
      56           0 :         maDestructedListeners.push_back(p);
      57           0 :         mbDestNormalized = false;
      58           0 :         return;
      59             :     }
      60             : 
      61           0 :     Normalize();
      62             :     std::pair<ListenersType::iterator,ListenersType::iterator> r =
      63           0 :         std::equal_range(maListeners.begin(), maListeners.end(), p);
      64             : 
      65           0 :     if (r.first != r.second)
      66           0 :         maListeners.erase(r.first, r.second);
      67           0 :     if (maListeners.empty())
      68           0 :         ListenersGone();
      69             : }
      70             : 
      71           0 : 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           0 : SvtBroadcaster::~SvtBroadcaster()
     101             : {
     102           0 :     mbDisposing = true;
     103           0 :     Broadcast( SfxSimpleHint(SFX_HINT_DYING) );
     104             : 
     105           0 :     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           0 :     ListenersType::iterator dest(maDestructedListeners.begin());
     111           0 :     for (ListenersType::iterator it(maListeners.begin()); it != maListeners.end(); ++it)
     112             :     {
     113             :         // skip the destructed ones
     114           0 :         while (dest != maDestructedListeners.end() && (*dest < *it))
     115           0 :             ++dest;
     116             : 
     117           0 :         if (dest == maDestructedListeners.end() || *dest != *it)
     118           0 :             (*it)->EndListening(*this);
     119             :     }
     120           0 : }
     121             : 
     122           0 : void SvtBroadcaster::Broadcast( const SfxHint &rHint )
     123             : {
     124           0 :     Normalize();
     125             : 
     126           0 :     ListenersType::iterator dest(maDestructedListeners.begin());
     127           0 :     ListenersType aListeners(maListeners); // this copy is important to avoid erasing entries while iterating
     128           0 :     for (ListenersType::iterator it(aListeners.begin()); it != aListeners.end(); ++it)
     129             :     {
     130             :         // skip the destructed ones
     131           0 :         while (dest != maDestructedListeners.end() && (*dest < *it))
     132           0 :             ++dest;
     133             : 
     134           0 :         if (dest == maDestructedListeners.end() || *dest != *it)
     135           0 :             (*it)->Notify(rHint);
     136           0 :     }
     137           0 : }
     138             : 
     139           0 : void SvtBroadcaster::ListenersGone() {}
     140             : 
     141           0 : SvtBroadcaster::ListenersType& SvtBroadcaster::GetAllListeners()
     142             : {
     143           0 :     return maListeners;
     144             : }
     145             : 
     146           0 : bool SvtBroadcaster::HasListeners() const
     147             : {
     148           0 :     return !maListeners.empty();
     149             : }
     150             : 
     151           0 : void SvtBroadcaster::PrepareForDestruction()
     152             : {
     153           0 :     mbAboutToDie = true;
     154           0 :     maDestructedListeners.reserve(maListeners.size());
     155           0 : }
     156             : 
     157             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10