diff options
author | Noel Grandin <noelgrandin@gmail.com> | 2021-12-01 18:18:47 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2021-12-03 07:37:28 +0100 |
commit | cd96af570352ff19b1f1a3d006750237eba8ca5c (patch) | |
tree | 8e500c9d37dc43adc2714d5e3a204487a881623f | |
parent | 6e334850104b2fe5d325328e64e08c190027f8d9 (diff) |
create OMultiTypeInterfaceContainerHelperVar3 template
to supercede OMultiTypeInterfaceContainerHelperVar2 and add
(a) type-safety
(b) skip the need for doing UNO_QUERY when firing events
Change-Id: Iaeb2421da5d92d2d4fd4feb45ab5019ca85d0d7a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/126192
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r-- | dbaccess/source/ui/browser/sbagrid.cxx | 12 | ||||
-rw-r--r-- | dbaccess/source/ui/inc/sbagrid.hxx | 6 | ||||
-rw-r--r-- | include/comphelper/multiinterfacecontainer3.hxx | 224 |
3 files changed, 234 insertions, 8 deletions
diff --git a/dbaccess/source/ui/browser/sbagrid.cxx b/dbaccess/source/ui/browser/sbagrid.cxx index 16980f05bfe5..5c8ab2095d60 100644 --- a/dbaccess/source/ui/browser/sbagrid.cxx +++ b/dbaccess/source/ui/browser/sbagrid.cxx @@ -280,13 +280,14 @@ void SbaXGridPeer::NotifyStatusChanged(const css::util::URL& _rUrl, const Refere xControl->statusChanged(aEvt); else { - ::comphelper::OInterfaceContainerHelper2 * pIter = m_aStatusListeners.getContainer(_rUrl); + ::comphelper::OInterfaceContainerHelper3<css::frame::XStatusListener> * pIter + = m_aStatusListeners.getContainer(_rUrl); if (pIter) { - ::comphelper::OInterfaceIteratorHelper2 aListIter(*pIter); + ::comphelper::OInterfaceIteratorHelper3 aListIter(*pIter); while (aListIter.hasMoreElements()) - static_cast< css::frame::XStatusListener*>(aListIter.next())->statusChanged(aEvt); + aListIter.next()->statusChanged(aEvt); } } } @@ -445,7 +446,8 @@ void SAL_CALL SbaXGridPeer::dispatch(const URL& aURL, const Sequence< PropertyVa void SAL_CALL SbaXGridPeer::addStatusListener(const Reference< css::frame::XStatusListener > & xControl, const css::util::URL& aURL) { - ::comphelper::OInterfaceContainerHelper2* pCont = m_aStatusListeners.getContainer(aURL); + ::comphelper::OInterfaceContainerHelper3< css::frame::XStatusListener >* pCont + = m_aStatusListeners.getContainer(aURL); if (!pCont) m_aStatusListeners.addInterface(aURL,xControl); else @@ -455,7 +457,7 @@ void SAL_CALL SbaXGridPeer::addStatusListener(const Reference< css::frame::XStat void SAL_CALL SbaXGridPeer::removeStatusListener(const Reference< css::frame::XStatusListener > & xControl, const css::util::URL& aURL) { - ::comphelper::OInterfaceContainerHelper2* pCont = m_aStatusListeners.getContainer(aURL); + ::comphelper::OInterfaceContainerHelper3< css::frame::XStatusListener >* pCont = m_aStatusListeners.getContainer(aURL); if ( pCont ) pCont->removeInterface(xControl); } diff --git a/dbaccess/source/ui/inc/sbagrid.hxx b/dbaccess/source/ui/inc/sbagrid.hxx index fceb8d84f7ad..0d22f6dceec1 100644 --- a/dbaccess/source/ui/inc/sbagrid.hxx +++ b/dbaccess/source/ui/inc/sbagrid.hxx @@ -25,7 +25,7 @@ #include <com/sun/star/frame/XDispatch.hpp> #include <com/sun/star/util/URL.hpp> -#include <comphelper/multiinterfacecontainer2.hxx> +#include <comphelper/multiinterfacecontainer3.hxx> #include <comphelper/servicehelper.hxx> #include <comphelper/uno3.hxx> #include "sbamultiplex.hxx" @@ -93,8 +93,8 @@ namespace dbaui :public FmXGridPeer ,public css::frame::XDispatch { - comphelper::OMultiTypeInterfaceContainerHelperVar2< - css::util::URL, void, SbaURLCompare> m_aStatusListeners; + comphelper::OMultiTypeInterfaceContainerHelperVar3< css::frame::XStatusListener, + css::util::URL, SbaURLCompare> m_aStatusListeners; public: SbaXGridPeer(const css::uno::Reference< css::uno::XComponentContext >&); diff --git a/include/comphelper/multiinterfacecontainer3.hxx b/include/comphelper/multiinterfacecontainer3.hxx new file mode 100644 index 000000000000..a9abdd5f77b0 --- /dev/null +++ b/include/comphelper/multiinterfacecontainer3.hxx @@ -0,0 +1,224 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#pragma once + +#include <sal/config.h> + +#include <com/sun/star/lang/EventObject.hpp> +#include <com/sun/star/lang/DisposedException.hpp> +#include <comphelper/comphelperdllapi.h> +#include <comphelper/interfacecontainer3.hxx> +#include <memory> +#include <vector> + +namespace com::sun::star::uno +{ +class XInterface; +} +namespace osl +{ +class Mutex; +} + +/** */ //for docpp +namespace comphelper +{ +/** + A helper class to store interface references of different types. + This is a copy of the similar class at include/cppuhelper/interfacecontainer.h, + but now uses the improved comphelper::InterfaceContainer3. + + @see OInterfaceIteratorHelper3 + @see OInterfaceContainerHelper3 + */ +template <class listener, class key, class equalImpl = std::equal_to<key>> +class OMultiTypeInterfaceContainerHelperVar3 +{ +public: + /** + Create a container of interface containers. + + @param rMutex the mutex to protect multi thread access. + The lifetime must be longer than the lifetime + of this object. + */ + inline OMultiTypeInterfaceContainerHelperVar3(::osl::Mutex& rMutex_) + : rMutex(rMutex_) + { + } + + /** + Return all id's under which at least one interface is added. + */ + inline std::vector<key> getContainedTypes() const + { + ::osl::MutexGuard aGuard(rMutex); + std::vector<key> aInterfaceTypes; + aInterfaceTypes.reserve(m_aMap.size()); + for (const auto& rPair : m_aMap) + // are interfaces added to this container? + if (rPair.second->getLength()) + // yes, put the type in the array + aInterfaceTypes.push_back(rPair.first); + return aInterfaceTypes; + } + + inline bool hasContainedTypes() const + { + ::osl::MutexGuard aGuard(rMutex); + for (const auto& rPair : m_aMap) + // are interfaces added to this container? + if (rPair.second->getLength()) + return true; + return false; + } + + /** + Return the container created under this key. + The InterfaceContainerHelper exists until the whole MultiTypeContainer is destroyed. + @return the container created under this key. If the container + was not created, null was returned. + */ + inline OInterfaceContainerHelper3<listener>* getContainer(const key& rKey) const + { + ::osl::MutexGuard aGuard(rMutex); + + auto iter = find(rKey); + if (iter != m_aMap.end()) + return (*iter).second.get(); + return nullptr; + } + + /** Inserts an element into the container with the specified key. + The position is not specified, thus it is not specified in which order events are fired. + + @attention + If you add the same interface more than once, then it will be added to the elements list + more than once and thus if you want to remove that interface from the list, you have to call + removeInterface() the same number of times. + In the latter case, you will also get events fired more than once (if the interface is a + listener interface). + + @param rKey + the id of the container + @param r + interface to be added; it is allowed, to insert null or + the same interface more than once + @return + the new count of elements in the container + */ + inline sal_Int32 addInterface(const key& rKey, const css::uno::Reference<listener>& rListener) + { + ::osl::MutexGuard aGuard(rMutex); + auto iter = find(rKey); + if (iter == m_aMap.end()) + { + auto pLC = new OInterfaceContainerHelper3<listener>(rMutex); + m_aMap.emplace_back(rKey, pLC); + return pLC->addInterface(rListener); + } + else + return (*iter).second->addInterface(rListener); + } + + /** Removes an element from the container with the specified key. + It uses interface equality to remove the interface. + + @param rKey + the id of the container + @param rxIFace + interface to be removed + @return + the new count of elements in the container + */ + inline sal_Int32 removeInterface(const key& rKey, + const css::uno::Reference<listener>& rListener) + { + ::osl::MutexGuard aGuard(rMutex); + + // search container with id nUik + auto iter = find(rKey); + // container found? + if (iter != m_aMap.end()) + return (*iter).second->removeInterface(rListener); + + // no container with this id. Always return 0 + return 0; + } + + /** + Call disposing on all references in the container, that + support XEventListener. Then clears the container. + @param rEvt the event object which is passed during disposing() call + */ + inline void disposeAndClear(const css::lang::EventObject& rEvt) + { + // create a copy, because do not fire event in a guarded section + InterfaceMap tempMap; + { + ::osl::MutexGuard aGuard(rMutex); + tempMap = std::move(m_aMap); + } + + for (auto& rPair : tempMap) + rPair.second->disposeAndClear(rEvt); + } + + /** + Remove all elements of all containers. Does not delete the container. + */ + inline void clear() + { + ::osl::MutexGuard aGuard(rMutex); + + for (const auto& rPair : m_aMap) + rPair.second->clear(); + } + + typedef key keyType; + +private: + typedef ::std::vector<std::pair<key, std::unique_ptr<OInterfaceContainerHelper3<listener>>>> + InterfaceMap; + InterfaceMap m_aMap; + ::osl::Mutex& rMutex; + + typename InterfaceMap::const_iterator find(const key& rKey) const + { + auto iter = m_aMap.begin(); + auto end = m_aMap.end(); + + while (iter != end) + { + equalImpl equal; + if (equal(iter->first, rKey)) + break; + ++iter; + } + return iter; + } + + OMultiTypeInterfaceContainerHelperVar3(const OMultiTypeInterfaceContainerHelperVar3&) = delete; + OMultiTypeInterfaceContainerHelperVar3& operator=(const OMultiTypeInterfaceContainerHelperVar3&) + = delete; +}; + +} // namespace comphelper + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |