diff options
author | Tobias Lippert <drtl@fastmail.fm> | 2014-07-18 15:36:30 +0200 |
---|---|---|
committer | Noel Grandin <noelgrandin@gmail.com> | 2014-07-22 06:26:06 +0000 |
commit | 798379313dca8de97e431ef2fe68129aaa1dcf04 (patch) | |
tree | dd9e0a372e2fa1f59608a125c04faecb416bf9a6 /svl/source | |
parent | 312926823dc6da7e87eb60c98ba084f14f0aa676 (diff) |
fdo#76754 Speed up registration of new listeners to SfxBroadcaster
Also change behavior for the GetListenerCount() method which now
returns the count of listeners.
The previous behavior is available in method GetSizeOfVector().
Change-Id: I5b03fa55a309f4ff5aea5e8830c137786fc07e89
Reviewed-on: https://gerrit.libreoffice.org/10344
Reviewed-by: Noel Grandin <noelgrandin@gmail.com>
Tested-by: Noel Grandin <noelgrandin@gmail.com>
Diffstat (limited to 'svl/source')
-rw-r--r-- | svl/source/notify/SfxBroadcaster.cxx | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/svl/source/notify/SfxBroadcaster.cxx b/svl/source/notify/SfxBroadcaster.cxx index 2fe79033b3b7..3aa493d19a84 100644 --- a/svl/source/notify/SfxBroadcaster.cxx +++ b/svl/source/notify/SfxBroadcaster.cxx @@ -88,15 +88,15 @@ SfxBroadcaster::SfxBroadcaster( const SfxBroadcaster &rBC ) void SfxBroadcaster::AddListener( SfxListener& rListener ) { - for (size_t i = 0; i < m_Listeners.size(); ++i) - { - if (!m_Listeners[i]) // removed by RemoveListener? - { - m_Listeners[i] = &rListener; - return; - } + if (m_RemovedPositions.empty()) { + m_Listeners.push_back(&rListener); + } + else { + size_t targetPosition = m_RemovedPositions.back(); + m_RemovedPositions.pop_back(); + assert(m_Listeners[targetPosition] == NULL); + m_Listeners[targetPosition] = &rListener; } - m_Listeners.push_back(&rListener); } @@ -131,6 +131,8 @@ void SfxBroadcaster::RemoveListener( SfxListener& rListener ) // DO NOT erase the listener, set the pointer to 0 // because the current continuation may contain this->Broadcast *aIter = 0; + size_t positionOfRemovedElement = std::distance(m_Listeners.begin(), aIter); + m_RemovedPositions.push_back(positionOfRemovedElement); if ( !HasListeners() ) ListenersGone(); @@ -138,13 +140,14 @@ void SfxBroadcaster::RemoveListener( SfxListener& rListener ) bool SfxBroadcaster::HasListeners() const { - for (size_t i = 0; i < m_Listeners.size(); ++i) - { - if (m_Listeners[i]) { - return true; - } - } - return false; + return (GetListenerCount() != 0); +} + +size_t SfxBroadcaster::GetListenerCount() const +{ + assert(m_Listeners.size() >= m_RemovedPositions.size()); + return m_Listeners.size() - m_RemovedPositions.size(); } + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |