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/SfxBroadcaster.hxx>
23 #include <sal/backtrace.hxx>
24 #include <sal/log.hxx>
31 // copy ctor of class SfxListener
33 SfxListener::SfxListener( const SfxListener
&rOther
)
34 : maBCs( rOther
.maBCs
)
36 for ( size_t n
= 0; n
< maBCs
.size(); ++n
)
38 maBCs
[n
]->AddListener(*this);
40 maCallStacks
.emplace( maBCs
[n
], sal::backtrace_get(10) );
45 // unregisters the SfxListener from its SfxBroadcasters
47 SfxListener::~SfxListener() COVERITY_NOEXCEPT_FALSE
49 // unregister at all remaining broadcasters
50 for ( size_t nPos
= 0; nPos
< maBCs
.size(); ++nPos
)
52 SfxBroadcaster
*pBC
= maBCs
[nPos
];
53 pBC
->RemoveListener(*this);
58 // unregisters a specific SfxBroadcaster
60 void SfxListener::RemoveBroadcaster_Impl( SfxBroadcaster
& rBroadcaster
)
62 auto it
= std::find( maBCs
.begin(), maBCs
.end(), &rBroadcaster
);
63 if (it
!= maBCs
.end()) {
66 maCallStacks
.erase( &rBroadcaster
);
74 Registers a specific SfxBroadcaster.
76 Some code uses duplicates as a kind of ref-counting thing i.e. they add and remove listeners
77 on different code paths, and they only really stop listening when the last EndListening() is called.
79 void SfxListener::StartListening(SfxBroadcaster
& rBroadcaster
, DuplicateHandling eDuplicateHanding
)
81 bool bListeningAlready
= IsListening( rBroadcaster
);
84 if (bListeningAlready
&& eDuplicateHanding
== DuplicateHandling::Unexpected
)
86 auto f
= maCallStacks
.find( &rBroadcaster
);
87 SAL_WARN("svl", "previous StartListening call came from: " << sal::backtrace_to_string(f
->second
.get()));
90 assert(!(bListeningAlready
&& eDuplicateHanding
== DuplicateHandling::Unexpected
) && "duplicate listener, try building with DBG_UTIL to find the other insert site.");
92 if (!bListeningAlready
|| eDuplicateHanding
!= DuplicateHandling::Prevent
)
94 rBroadcaster
.AddListener(*this);
95 maBCs
.push_back( &rBroadcaster
);
97 maCallStacks
.emplace( &rBroadcaster
, sal::backtrace_get(10) );
99 assert(IsListening(rBroadcaster
) && "StartListening failed");
103 // unregisters a specific SfxBroadcaster
105 void SfxListener::EndListening( SfxBroadcaster
& rBroadcaster
, bool bRemoveAllDuplicates
)
107 auto beginIt
= maBCs
.begin();
110 auto it
= std::find( beginIt
, maBCs
.end(), &rBroadcaster
);
111 if ( it
== maBCs
.end() )
115 rBroadcaster
.RemoveListener(*this);
116 beginIt
= maBCs
.erase( it
);
118 maCallStacks
.erase( &rBroadcaster
);
121 while ( bRemoveAllDuplicates
);
125 // unregisters all Broadcasters
127 void SfxListener::EndListeningAll()
129 std::vector
<SfxBroadcaster
*> aBroadcasters
;
130 std::swap(maBCs
, aBroadcasters
);
131 for (SfxBroadcaster
*pBC
: aBroadcasters
)
132 pBC
->RemoveListener(*this);
134 maCallStacks
.clear();
139 bool SfxListener::IsListening( SfxBroadcaster
& rBroadcaster
) const
141 return maBCs
.end() != std::find( maBCs
.begin(), maBCs
.end(), &rBroadcaster
);
144 sal_uInt16
SfxListener::GetBroadcasterCount() const
149 SfxBroadcaster
* SfxListener::GetBroadcasterJOE( sal_uInt16 nNo
) const
155 // base implementation of notification handler
157 void SfxListener::Notify( SfxBroadcaster
& rBroadcaster
, const SfxHint
& )
160 assert(IsListening(rBroadcaster
));
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */