lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / svl / source / notify / lstner.cxx
blob6960b12bc1d8c52bb1c1c90131f9c40b251043ea
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <svl/lstner.hxx>
22 #include <svl/hint.hxx>
23 #include <svl/SfxBroadcaster.hxx>
24 #include <sal/backtrace.hxx>
25 #include <sal/log.hxx>
27 #include <algorithm>
28 #include <cassert>
29 #include <deque>
30 #include <memory>
31 #include <map>
33 typedef std::deque<SfxBroadcaster*> SfxBroadcasterArr_Impl;
35 struct SfxListener::Impl
37 SfxBroadcasterArr_Impl maBCs;
38 #ifdef DBG_UTIL
39 std::map<SfxBroadcaster*, std::unique_ptr<sal::BacktraceState>>
40 maCallStacks;
41 #endif
44 // simple ctor of class SfxListener
46 SfxListener::SfxListener() : mpImpl(new Impl)
50 // copy ctor of class SfxListener
52 SfxListener::SfxListener( const SfxListener &rListener ) : mpImpl(new Impl)
54 for ( size_t n = 0; n < rListener.mpImpl->maBCs.size(); ++n )
55 StartListening( *rListener.mpImpl->maBCs[n] );
58 // unregisters the SfxListener from its SfxBroadcasters
60 SfxListener::~SfxListener() COVERITY_NOEXCEPT_FALSE
62 // unregister at all remaining broadcasters
63 for ( size_t nPos = 0; nPos < mpImpl->maBCs.size(); ++nPos )
65 SfxBroadcaster *pBC = mpImpl->maBCs[nPos];
66 pBC->RemoveListener(*this);
71 // unregisters a specific SfxBroadcaster
73 void SfxListener::RemoveBroadcaster_Impl( SfxBroadcaster& rBroadcaster )
75 auto it = std::find( mpImpl->maBCs.begin(), mpImpl->maBCs.end(), &rBroadcaster );
76 if (it != mpImpl->maBCs.end()) {
77 mpImpl->maBCs.erase( it );
78 #ifdef DBG_UTIL
79 mpImpl->maCallStacks.erase( &rBroadcaster );
80 #endif
86 /**
87 Registers a specific SfxBroadcaster.
89 Some code uses duplicates as a kind of ref-counting thing i.e. they add and remove listeners
90 on different code paths, and they only really stop listening when the last EndListening() is called.
92 void SfxListener::StartListening(SfxBroadcaster& rBroadcaster, DuplicateHandling eDuplicateHanding)
94 bool bListeningAlready = IsListening( rBroadcaster );
96 #ifdef DBG_UTIL
97 if (bListeningAlready && eDuplicateHanding == DuplicateHandling::Unexpected)
99 auto f = mpImpl->maCallStacks.find( &rBroadcaster );
100 SAL_WARN("svl", "previous StartListening call came from: " << sal::backtrace_to_string(f->second.get()));
102 #endif
103 assert(!(bListeningAlready && eDuplicateHanding == DuplicateHandling::Unexpected) && "duplicate listener, try building with DBG_UTIL to find the other insert site.");
105 if (!bListeningAlready || eDuplicateHanding != DuplicateHandling::Prevent)
107 rBroadcaster.AddListener(*this);
108 mpImpl->maBCs.push_back( &rBroadcaster );
109 #ifdef DBG_UTIL
110 mpImpl->maCallStacks.emplace( &rBroadcaster, sal::backtrace_get(10) );
111 #endif
112 assert(IsListening(rBroadcaster) && "StartListening failed");
116 // unregisters a specific SfxBroadcaster
118 void SfxListener::EndListening( SfxBroadcaster& rBroadcaster, bool bRemoveAllDuplicates )
120 SfxBroadcasterArr_Impl::iterator beginIt = mpImpl->maBCs.begin();
123 SfxBroadcasterArr_Impl::iterator it = std::find( beginIt, mpImpl->maBCs.end(), &rBroadcaster );
124 if ( it == mpImpl->maBCs.end() )
126 break;
128 rBroadcaster.RemoveListener(*this);
129 beginIt = mpImpl->maBCs.erase( it );
130 #ifdef DBG_UTIL
131 mpImpl->maCallStacks.erase( &rBroadcaster );
132 #endif
134 while ( bRemoveAllDuplicates );
138 // unregisters all Broadcasters
140 void SfxListener::EndListeningAll()
142 // Attention: when optimizing this: respect side effects of RemoveListener!
143 while ( !mpImpl->maBCs.empty() )
145 SfxBroadcaster *pBC = mpImpl->maBCs.front();
146 pBC->RemoveListener(*this);
147 mpImpl->maBCs.pop_front();
149 #ifdef DBG_UTIL
150 mpImpl->maCallStacks.clear();
151 #endif
155 bool SfxListener::IsListening( SfxBroadcaster& rBroadcaster ) const
157 return mpImpl->maBCs.end() != std::find( mpImpl->maBCs.begin(), mpImpl->maBCs.end(), &rBroadcaster );
160 sal_uInt16 SfxListener::GetBroadcasterCount() const
162 return mpImpl->maBCs.size();
165 SfxBroadcaster* SfxListener::GetBroadcasterJOE( sal_uInt16 nNo ) const
167 return mpImpl->maBCs[nNo];
171 // base implementation of notification handler
173 void SfxListener::Notify( SfxBroadcaster& rBroadcaster, const SfxHint& )
175 (void) rBroadcaster;
176 assert(IsListening(rBroadcaster));
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */