summaryrefslogtreecommitdiff
path: root/comphelper
diff options
context:
space:
mode:
authorNoel Grandin <noelgrandin@gmail.com>2021-08-07 09:24:46 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2021-08-07 21:34:33 +0200
commit86abf3a682b424dc0fcbccf030f5a0b9bfb81d8c (patch)
treeba40de884231f604bbd8eedb610834d92afb86ae /comphelper
parent2cd1442f83d41af1f2b663ccb8bfab6a954009f1 (diff)
create comphelper::OMultiTypeInterfaceContainerHelper2 and use it
based on OInterfaceContainerHelper2 which is considerably faster than the original OInterfaceContainerHelper Change-Id: I9c8b6d0e5382018824bf7188a26343703abf2d51 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/120161 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'comphelper')
-rw-r--r--comphelper/source/container/interfacecontainer2.cxx116
1 files changed, 116 insertions, 0 deletions
diff --git a/comphelper/source/container/interfacecontainer2.cxx b/comphelper/source/container/interfacecontainer2.cxx
index 1524600affd0..151f298d19c0 100644
--- a/comphelper/source/container/interfacecontainer2.cxx
+++ b/comphelper/source/container/interfacecontainer2.cxx
@@ -19,6 +19,7 @@
#include <comphelper/interfacecontainer2.hxx>
+#include <comphelper/multicontainer2.hxx>
#include <osl/diagnose.h>
#include <osl/mutex.hxx>
@@ -285,6 +286,121 @@ void OInterfaceContainerHelper2::clear()
}
+
+// specialized class for type
+
+OMultiTypeInterfaceContainerHelper2::OMultiTypeInterfaceContainerHelper2( Mutex & rMutex_ )
+ : rMutex( rMutex_ )
+{
+}
+
+OMultiTypeInterfaceContainerHelper2::~OMultiTypeInterfaceContainerHelper2()
+{
+}
+
+std::vector< css::uno::Type > OMultiTypeInterfaceContainerHelper2::getContainedTypes() const
+{
+ ::osl::MutexGuard aGuard( rMutex );
+ std::vector< Type > aInterfaceTypes;
+ aInterfaceTypes.reserve( m_aMap.size() );
+ for (const auto& rItem : m_aMap)
+ {
+ // are interfaces added to this container?
+ if( rItem.second->getLength() )
+ // yes, put the type in the array
+ aInterfaceTypes.push_back(rItem.first);
+ }
+ return aInterfaceTypes;
+}
+
+OMultiTypeInterfaceContainerHelper2::t_type2ptr::iterator OMultiTypeInterfaceContainerHelper2::findType(const Type & rKey )
+{
+ return std::find_if(m_aMap.begin(), m_aMap.end(),
+ [&rKey](const t_type2ptr::value_type& rItem) { return rItem.first == rKey; });
+}
+
+OMultiTypeInterfaceContainerHelper2::t_type2ptr::const_iterator OMultiTypeInterfaceContainerHelper2::findType(const Type & rKey ) const
+{
+ return std::find_if(m_aMap.begin(), m_aMap.end(),
+ [&rKey](const t_type2ptr::value_type& rItem) { return rItem.first == rKey; });
+}
+
+OInterfaceContainerHelper2 * OMultiTypeInterfaceContainerHelper2::getContainer( const Type & rKey ) const
+{
+ ::osl::MutexGuard aGuard( rMutex );
+
+ auto iter = findType( rKey );
+ if( iter != m_aMap.end() )
+ return (*iter).second.get();
+ return nullptr;
+}
+
+sal_Int32 OMultiTypeInterfaceContainerHelper2::addInterface(
+ const Type & rKey, const Reference< XInterface > & rListener )
+{
+ ::osl::MutexGuard aGuard( rMutex );
+ auto iter = findType( rKey );
+ if( iter == m_aMap.end() )
+ {
+ OInterfaceContainerHelper2 * pLC = new OInterfaceContainerHelper2( rMutex );
+ m_aMap.emplace_back(rKey, pLC);
+ return pLC->addInterface( rListener );
+ }
+ return (*iter).second->addInterface( rListener );
+}
+
+sal_Int32 OMultiTypeInterfaceContainerHelper2::removeInterface(
+ const Type & rKey, const Reference< XInterface > & rListener )
+{
+ ::osl::MutexGuard aGuard( rMutex );
+
+ // search container with id nUik
+ auto iter = findType( rKey );
+ // container found?
+ if( iter != m_aMap.end() )
+ return (*iter).second->removeInterface( rListener );
+
+ // no container with this id. Always return 0
+ return 0;
+}
+
+void OMultiTypeInterfaceContainerHelper2::disposeAndClear( const EventObject & rEvt )
+{
+ t_type2ptr::size_type nSize = 0;
+ std::unique_ptr<OInterfaceContainerHelper2 *[]> ppListenerContainers;
+ {
+ ::osl::MutexGuard aGuard( rMutex );
+ nSize = m_aMap.size();
+ if( nSize )
+ {
+ typedef OInterfaceContainerHelper2* ppp;
+ ppListenerContainers.reset(new ppp[nSize]);
+
+ t_type2ptr::size_type i = 0;
+ for (const auto& rItem : m_aMap)
+ {
+ ppListenerContainers[i++] = rItem.second.get();
+ }
+ }
+ }
+
+ // create a copy, because do not fire event in a guarded section
+ for( t_type2ptr::size_type i = 0; i < nSize; i++ )
+ {
+ if( ppListenerContainers[i] )
+ ppListenerContainers[i]->disposeAndClear( rEvt );
+ }
+}
+
+void OMultiTypeInterfaceContainerHelper2::clear()
+{
+ ::osl::MutexGuard aGuard( rMutex );
+
+ for (auto& rItem : m_aMap)
+ rItem.second->clear();
+}
+
+
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */