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 0 : TYPEINIT0(SfxBroadcaster);
32 :
33 :
34 : // broadcast immediately
35 :
36 0 : void SfxBroadcaster::Broadcast( const SfxHint &rHint )
37 : {
38 : // notify all registered listeners exactly once
39 0 : for (size_t n = 0; n < m_Listeners.size(); ++n)
40 : {
41 0 : SfxListener *const pListener = m_Listeners[n];
42 0 : if (pListener) {
43 0 : pListener->Notify( *this, rHint );
44 : }
45 : }
46 0 : }
47 :
48 : // unregister all listeners
49 :
50 0 : SfxBroadcaster::~SfxBroadcaster()
51 : {
52 0 : Broadcast( SfxSimpleHint(SFX_HINT_DYING) );
53 :
54 : // remove all still registered listeners
55 0 : for (size_t nPos = 0; nPos < m_Listeners.size(); ++nPos)
56 : {
57 0 : SfxListener *const pListener = m_Listeners[nPos];
58 0 : if (pListener) {
59 0 : pListener->RemoveBroadcaster_Impl(*this);
60 : }
61 : }
62 0 : }
63 :
64 :
65 : // simple ctor of class SfxBroadcaster
66 :
67 0 : SfxBroadcaster::SfxBroadcaster()
68 : {
69 0 : }
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 0 : void SfxBroadcaster::AddListener( SfxListener& rListener )
90 : {
91 0 : for (size_t i = 0; i < m_Listeners.size(); ++i)
92 : {
93 0 : if (!m_Listeners[i]) // removed by RemoveListener?
94 : {
95 0 : m_Listeners[i] = &rListener;
96 0 : return;
97 : }
98 : }
99 0 : m_Listeners.push_back(&rListener);
100 : }
101 :
102 :
103 : // called, if no more listeners exists
104 :
105 0 : void SfxBroadcaster::ListenersGone()
106 : {
107 0 : }
108 :
109 :
110 : // forward a notification to all registered listeners
111 :
112 0 : void SfxBroadcaster::Forward(SfxBroadcaster& rBC, const SfxHint& rHint)
113 : {
114 0 : for (size_t i = 0; i < m_Listeners.size(); ++i)
115 : {
116 0 : SfxListener *const pListener = m_Listeners[i];
117 0 : if (pListener) {
118 0 : pListener->Notify( rBC, rHint );
119 : }
120 : }
121 0 : }
122 :
123 :
124 : // remove one SfxListener from the list
125 :
126 0 : void SfxBroadcaster::RemoveListener( SfxListener& rListener )
127 : {
128 : SfxListenerArr_Impl::iterator aIter = std::find(
129 0 : 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 0 : *aIter = 0;
134 :
135 0 : if ( !HasListeners() )
136 0 : ListenersGone();
137 0 : }
138 :
139 0 : bool SfxBroadcaster::HasListeners() const
140 : {
141 0 : for (size_t i = 0; i < m_Listeners.size(); ++i)
142 : {
143 0 : if (m_Listeners[i]) {
144 0 : return true;
145 : }
146 : }
147 0 : return false;
148 : }
149 :
150 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|