bump product version to 6.3.0.0.beta1
[LibreOffice.git] / svl / source / notify / lstner.cxx
blobdcd380c86dec2ccaafe628c764a7c970760aea0c
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 <vector>
30 #include <memory>
31 #include <map>
33 struct SfxListener::Impl
35 std::vector<SfxBroadcaster*> maBCs;
36 #ifdef DBG_UTIL
37 std::map<SfxBroadcaster*, std::unique_ptr<sal::BacktraceState>>
38 maCallStacks;
39 #endif
42 // simple ctor of class SfxListener
44 SfxListener::SfxListener() : mpImpl(new Impl)
48 // copy ctor of class SfxListener
50 SfxListener::SfxListener( const SfxListener &rListener ) : mpImpl(new Impl)
52 for ( size_t n = 0; n < rListener.mpImpl->maBCs.size(); ++n )
53 StartListening( *rListener.mpImpl->maBCs[n] );
56 // unregisters the SfxListener from its SfxBroadcasters
58 SfxListener::~SfxListener() COVERITY_NOEXCEPT_FALSE
60 // unregister at all remaining broadcasters
61 for ( size_t nPos = 0; nPos < mpImpl->maBCs.size(); ++nPos )
63 SfxBroadcaster *pBC = mpImpl->maBCs[nPos];
64 pBC->RemoveListener(*this);
69 // unregisters a specific SfxBroadcaster
71 void SfxListener::RemoveBroadcaster_Impl( SfxBroadcaster& rBroadcaster )
73 auto it = std::find( mpImpl->maBCs.begin(), mpImpl->maBCs.end(), &rBroadcaster );
74 if (it != mpImpl->maBCs.end()) {
75 mpImpl->maBCs.erase( it );
76 #ifdef DBG_UTIL
77 mpImpl->maCallStacks.erase( &rBroadcaster );
78 #endif
84 /**
85 Registers a specific SfxBroadcaster.
87 Some code uses duplicates as a kind of ref-counting thing i.e. they add and remove listeners
88 on different code paths, and they only really stop listening when the last EndListening() is called.
90 void SfxListener::StartListening(SfxBroadcaster& rBroadcaster, DuplicateHandling eDuplicateHanding)
92 bool bListeningAlready = IsListening( rBroadcaster );
94 #ifdef DBG_UTIL
95 if (bListeningAlready && eDuplicateHanding == DuplicateHandling::Unexpected)
97 auto f = mpImpl->maCallStacks.find( &rBroadcaster );
98 SAL_WARN("svl", "previous StartListening call came from: " << sal::backtrace_to_string(f->second.get()));
100 #endif
101 assert(!(bListeningAlready && eDuplicateHanding == DuplicateHandling::Unexpected) && "duplicate listener, try building with DBG_UTIL to find the other insert site.");
103 if (!bListeningAlready || eDuplicateHanding != DuplicateHandling::Prevent)
105 rBroadcaster.AddListener(*this);
106 mpImpl->maBCs.push_back( &rBroadcaster );
107 #ifdef DBG_UTIL
108 mpImpl->maCallStacks.emplace( &rBroadcaster, sal::backtrace_get(10) );
109 #endif
110 assert(IsListening(rBroadcaster) && "StartListening failed");
114 // unregisters a specific SfxBroadcaster
116 void SfxListener::EndListening( SfxBroadcaster& rBroadcaster, bool bRemoveAllDuplicates )
118 auto beginIt = mpImpl->maBCs.begin();
121 auto it = std::find( beginIt, mpImpl->maBCs.end(), &rBroadcaster );
122 if ( it == mpImpl->maBCs.end() )
124 break;
126 rBroadcaster.RemoveListener(*this);
127 beginIt = mpImpl->maBCs.erase( it );
128 #ifdef DBG_UTIL
129 mpImpl->maCallStacks.erase( &rBroadcaster );
130 #endif
132 while ( bRemoveAllDuplicates );
136 // unregisters all Broadcasters
138 void SfxListener::EndListeningAll()
140 std::vector<SfxBroadcaster*> aBroadcasters;
141 std::swap(mpImpl->maBCs, aBroadcasters);
142 for (SfxBroadcaster *pBC : aBroadcasters)
143 pBC->RemoveListener(*this);
144 #ifdef DBG_UTIL
145 mpImpl->maCallStacks.clear();
146 #endif
150 bool SfxListener::IsListening( SfxBroadcaster& rBroadcaster ) const
152 return mpImpl->maBCs.end() != std::find( mpImpl->maBCs.begin(), mpImpl->maBCs.end(), &rBroadcaster );
155 sal_uInt16 SfxListener::GetBroadcasterCount() const
157 return mpImpl->maBCs.size();
160 SfxBroadcaster* SfxListener::GetBroadcasterJOE( sal_uInt16 nNo ) const
162 return mpImpl->maBCs[nNo];
166 // base implementation of notification handler
168 void SfxListener::Notify( SfxBroadcaster& rBroadcaster, const SfxHint& )
170 (void) rBroadcaster;
171 assert(IsListening(rBroadcaster));
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */