1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
33 struct SfxListener::Impl
35 std::vector
<SfxBroadcaster
*> maBCs
;
37 std::map
<SfxBroadcaster
*, std::unique_ptr
<sal::BacktraceState
>>
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
);
77 mpImpl
->maCallStacks
.erase( &rBroadcaster
);
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
);
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()));
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
);
108 mpImpl
->maCallStacks
.emplace( &rBroadcaster
, sal::backtrace_get(10) );
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() )
126 rBroadcaster
.RemoveListener(*this);
127 beginIt
= mpImpl
->maBCs
.erase( it
);
129 mpImpl
->maCallStacks
.erase( &rBroadcaster
);
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);
145 mpImpl
->maCallStacks
.clear();
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
& )
171 assert(IsListening(rBroadcaster
));
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */