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 <svl/hint.hxx>
23 : #include <svl/smplhint.hxx>
24 : #include <svl/lstner.hxx>
25 :
26 : #include <svl/SfxBroadcaster.hxx>
27 : #include <algorithm>
28 : #include <tools/debug.hxx>
29 :
30 367610 : TYPEINIT0(SfxBroadcaster);
31 :
32 : // broadcast immediately
33 :
34 2321580 : void SfxBroadcaster::Broadcast( const SfxHint &rHint )
35 : {
36 : // notify all registered listeners exactly once
37 19687942 : for (size_t n = 0; n < m_Listeners.size(); ++n)
38 : {
39 17366362 : SfxListener *const pListener = m_Listeners[n];
40 17366362 : if (pListener) {
41 12973293 : pListener->Notify( *this, rHint );
42 : }
43 : }
44 2321580 : }
45 :
46 : // unregister all listeners
47 :
48 1655252 : SfxBroadcaster::~SfxBroadcaster()
49 : {
50 725031 : Broadcast( SfxSimpleHint(SFX_HINT_DYING) );
51 :
52 : // remove all still registered listeners
53 1196558 : for (size_t nPos = 0; nPos < m_Listeners.size(); ++nPos)
54 : {
55 471527 : SfxListener *const pListener = m_Listeners[nPos];
56 471527 : if (pListener) {
57 54056 : pListener->RemoveBroadcaster_Impl(*this);
58 : }
59 : }
60 930221 : }
61 :
62 :
63 : // simple ctor of class SfxBroadcaster
64 :
65 741275 : SfxBroadcaster::SfxBroadcaster()
66 : {
67 741275 : }
68 :
69 :
70 : // copy ctor of class SfxBroadcaster
71 :
72 :
73 0 : SfxBroadcaster::SfxBroadcaster( const SfxBroadcaster &rBC )
74 : {
75 0 : for (size_t n = 0; n < rBC.m_Listeners.size(); ++n)
76 : {
77 0 : SfxListener *const pListener = rBC.m_Listeners[n];
78 0 : if (pListener) {
79 0 : pListener->StartListening( *this );
80 : }
81 : }
82 0 : }
83 :
84 :
85 : // add a new SfxListener to the list
86 :
87 1033249 : void SfxBroadcaster::AddListener( SfxListener& rListener )
88 : {
89 : DBG_TESTSOLARMUTEX();
90 1033249 : if (m_RemovedPositions.empty()) {
91 487284 : m_Listeners.push_back(&rListener);
92 : }
93 : else {
94 545965 : size_t targetPosition = m_RemovedPositions.back();
95 545965 : m_RemovedPositions.pop_back();
96 : assert(m_Listeners[targetPosition] == NULL);
97 545965 : m_Listeners[targetPosition] = &rListener;
98 : }
99 1033249 : }
100 :
101 :
102 : // called, if no more listeners exists
103 :
104 258628 : void SfxBroadcaster::ListenersGone()
105 : {
106 258628 : }
107 :
108 :
109 : // forward a notification to all registered listeners
110 :
111 2126880 : void SfxBroadcaster::Forward(SfxBroadcaster& rBC, const SfxHint& rHint)
112 : {
113 3924922 : for (size_t i = 0; i < m_Listeners.size(); ++i)
114 : {
115 1798042 : SfxListener *const pListener = m_Listeners[i];
116 1798042 : if (pListener) {
117 1710578 : pListener->Notify( rBC, rHint );
118 : }
119 : }
120 2126880 : }
121 :
122 :
123 : // remove one SfxListener from the list
124 :
125 964671 : void SfxBroadcaster::RemoveListener( SfxListener& rListener )
126 : {
127 : DBG_TESTSOLARMUTEX();
128 : SfxListenerArr_Impl::iterator aIter = std::find(
129 964671 : 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 964671 : *aIter = 0;
134 964671 : size_t positionOfRemovedElement = std::distance(m_Listeners.begin(), aIter);
135 964671 : m_RemovedPositions.push_back(positionOfRemovedElement);
136 :
137 964671 : if ( !HasListeners() )
138 258628 : ListenersGone();
139 964671 : }
140 :
141 968962 : bool SfxBroadcaster::HasListeners() const
142 : {
143 968962 : return (GetListenerCount() != 0);
144 : }
145 :
146 968974 : size_t SfxBroadcaster::GetListenerCount() const
147 : {
148 : assert(m_Listeners.size() >= m_RemovedPositions.size());
149 968974 : return m_Listeners.size() - m_RemovedPositions.size();
150 : }
151 :
152 :
153 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|