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 <assert.h>
21 :
22 : #include <tools/debug.hxx>
23 :
24 : #include <svl/hint.hxx>
25 : #include <svl/smplhint.hxx>
26 : #include <svl/lstner.hxx>
27 :
28 : #include <svl/brdcst.hxx>
29 : #include <algorithm>
30 :
31 96146 : TYPEINIT0(SfxBroadcaster);
32 :
33 :
34 : // broadcast immediately
35 :
36 850418 : void SfxBroadcaster::Broadcast( const SfxHint &rHint )
37 : {
38 : // notify all registered listeners exactly once
39 8127304 : for (size_t n = 0; n < m_Listeners.size(); ++n)
40 : {
41 7276886 : SfxListener *const pListener = m_Listeners[n];
42 7276886 : if (pListener) {
43 5238934 : pListener->Notify( *this, rHint );
44 : }
45 : }
46 850418 : }
47 :
48 : // unregister all listeners
49 :
50 593193 : SfxBroadcaster::~SfxBroadcaster()
51 : {
52 275813 : Broadcast( SfxSimpleHint(SFX_HINT_DYING) );
53 :
54 : // remove all still registered listeners
55 401472 : for (size_t nPos = 0; nPos < m_Listeners.size(); ++nPos)
56 : {
57 125659 : SfxListener *const pListener = m_Listeners[nPos];
58 125659 : if (pListener) {
59 18349 : pListener->RemoveBroadcaster_Impl(*this);
60 : }
61 : }
62 317380 : }
63 :
64 :
65 : // simple ctor of class SfxBroadcaster
66 :
67 310220 : SfxBroadcaster::SfxBroadcaster()
68 : {
69 310220 : }
70 :
71 :
72 : // copy ctor of class SfxBroadcaster
73 :
74 :
75 0 : SfxBroadcaster::SfxBroadcaster( const SfxBroadcaster &rBC )
76 : {
77 0 : for (size_t n = 0; n < rBC.m_Listeners.size(); ++n)
78 : {
79 0 : SfxListener *const pListener = rBC.m_Listeners[n];
80 0 : if (pListener) {
81 0 : pListener->StartListening( *this );
82 : }
83 : }
84 0 : }
85 :
86 :
87 : // add a new SfxListener to the list
88 :
89 449195 : void SfxBroadcaster::AddListener( SfxListener& rListener )
90 : {
91 10496514 : for (size_t i = 0; i < m_Listeners.size(); ++i)
92 : {
93 10332032 : if (!m_Listeners[i]) // removed by RemoveListener?
94 : {
95 284713 : m_Listeners[i] = &rListener;
96 733908 : return;
97 : }
98 : }
99 164482 : m_Listeners.push_back(&rListener);
100 : }
101 :
102 :
103 : // called, if no more listeners exists
104 :
105 58937 : void SfxBroadcaster::ListenersGone()
106 : {
107 58937 : }
108 :
109 :
110 : // forward a notification to all registered listeners
111 :
112 648716 : void SfxBroadcaster::Forward(SfxBroadcaster& rBC, const SfxHint& rHint)
113 : {
114 1178227 : for (size_t i = 0; i < m_Listeners.size(); ++i)
115 : {
116 529511 : SfxListener *const pListener = m_Listeners[i];
117 529511 : if (pListener) {
118 461445 : pListener->Notify( rBC, rHint );
119 : }
120 : }
121 648716 : }
122 :
123 :
124 : // remove one SfxListener from the list
125 :
126 395357 : void SfxBroadcaster::RemoveListener( SfxListener& rListener )
127 : {
128 : SfxListenerArr_Impl::iterator aIter = std::find(
129 395357 : m_Listeners.begin(), m_Listeners.end(), &rListener);
130 : assert(aIter != m_Listeners.end()); // "RemoveListener: Listener unknown"
131 : // DO NOT erase the listener, set the pointer to 0
132 : // because the current continuation may contain this->Broadcast
133 395357 : *aIter = 0;
134 :
135 395357 : if ( !HasListeners() )
136 58937 : ListenersGone();
137 395357 : }
138 :
139 396751 : bool SfxBroadcaster::HasListeners() const
140 : {
141 618209 : for (size_t i = 0; i < m_Listeners.size(); ++i)
142 : {
143 558055 : if (m_Listeners[i]) {
144 336597 : return true;
145 : }
146 : }
147 60154 : return false;
148 : }
149 :
150 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|