From e79db65efb48f2f4e82945fc850bc38c8923e22d Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Wed, 25 May 2022 09:28:16 +0200 Subject: allow comphelper::OEnumerationByName to use a vector too for names so we can skip some allocation. Use a std::variant to preserve existing functionality Change-Id: If01ebb04f7895fd52fa3f5d90648868fd38dc39e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134929 Tested-by: Jenkins Reviewed-by: Noel Grandin --- comphelper/source/container/enumhelper.cxx | 31 ++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'comphelper/source') diff --git a/comphelper/source/container/enumhelper.cxx b/comphelper/source/container/enumhelper.cxx index 2487d3adf234..b5fe864c8820 100644 --- a/comphelper/source/container/enumhelper.cxx +++ b/comphelper/source/container/enumhelper.cxx @@ -36,8 +36,8 @@ OEnumerationByName::OEnumerationByName(const css::uno::Reference& _rxAccess, - const css::uno::Sequence< OUString >& _aNames ) - :m_aNames(_aNames) + std::vector _aNames ) + :m_aNames(std::move(_aNames)) ,m_xAccess(_rxAccess) ,m_nPos(0) ,m_bListening(false) @@ -45,7 +45,6 @@ OEnumerationByName::OEnumerationByName(const css::uno::Reference m_nPos) + if (m_xAccess.is() && getLength() > m_nPos) return true; if (m_xAccess.is()) @@ -76,10 +75,10 @@ css::uno::Any SAL_CALL OEnumerationByName::nextElement( ) std::lock_guard aLock(m_aLock); css::uno::Any aRes; - if (m_xAccess.is() && m_nPos < m_aNames.getLength()) - aRes = m_xAccess->getByName(m_aNames.getConstArray()[m_nPos++]); + if (m_xAccess.is() && m_nPos < getLength()) + aRes = m_xAccess->getByName(getElement(m_nPos++)); - if (m_xAccess.is() && m_nPos >= m_aNames.getLength()) + if (m_xAccess.is() && m_nPos >= getLength()) { impl_stopDisposeListening(); m_xAccess.clear(); @@ -91,7 +90,6 @@ css::uno::Any SAL_CALL OEnumerationByName::nextElement( ) return aRes; } - void SAL_CALL OEnumerationByName::disposing(const css::lang::EventObject& aEvent) { std::lock_guard aLock(m_aLock); @@ -132,6 +130,23 @@ void OEnumerationByName::impl_stopDisposeListening() osl_atomic_decrement(&m_refCount); } +sal_Int32 OEnumerationByName::getLength() const +{ + if (m_aNames.index() == 0) + return std::get>(m_aNames).getLength(); + else + return std::get>(m_aNames).size(); +} + +const OUString& OEnumerationByName::getElement(sal_Int32 nIndex) const +{ + if (m_aNames.index() == 0) + return std::get>(m_aNames).getConstArray()[nIndex]; + else + return std::get>(m_aNames)[nIndex]; +} + + OEnumerationByIndex::OEnumerationByIndex(const css::uno::Reference< css::container::XIndexAccess >& _rxAccess) :m_xAccess(_rxAccess) ,m_nPos(0) -- cgit