2010-10-14 08:27:31 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-29 14:02:24 +01:00
|
|
|
/*
|
|
|
|
* 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 .
|
|
|
|
*/
|
2000-09-29 10:28:15 +00:00
|
|
|
|
|
|
|
#include <comphelper/enumhelper.hxx>
|
2004-01-28 11:46:55 +00:00
|
|
|
#include <com/sun/star/lang/XComponent.hpp>
|
2019-04-13 21:42:04 +02:00
|
|
|
#include <com/sun/star/container/XIndexAccess.hpp>
|
|
|
|
#include <com/sun/star/container/XNameAccess.hpp>
|
2022-05-26 09:30:35 +02:00
|
|
|
#include <utility>
|
2014-02-25 17:52:30 +01:00
|
|
|
|
2000-09-29 10:28:15 +00:00
|
|
|
namespace comphelper
|
|
|
|
{
|
|
|
|
|
2022-05-26 09:30:35 +02:00
|
|
|
OEnumerationByName::OEnumerationByName(css::uno::Reference<css::container::XNameAccess> _xAccess)
|
|
|
|
:m_aNames(_xAccess->getElementNames())
|
|
|
|
,m_xAccess(_xAccess)
|
2020-05-28 13:18:41 +02:00
|
|
|
,m_nPos(0)
|
2014-02-16 22:51:15 +01:00
|
|
|
,m_bListening(false)
|
2004-01-28 11:46:55 +00:00
|
|
|
{
|
|
|
|
impl_startDisposeListening();
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2022-11-10 19:01:49 +01:00
|
|
|
OEnumerationByName::OEnumerationByName(css::uno::Reference<css::container::XNameAccess> _xAccess,
|
2022-05-25 09:28:16 +02:00
|
|
|
std::vector<OUString> _aNames )
|
|
|
|
:m_aNames(std::move(_aNames))
|
2022-05-26 09:30:35 +02:00
|
|
|
,m_xAccess(std::move(_xAccess))
|
2020-05-28 13:18:41 +02:00
|
|
|
,m_nPos(0)
|
2014-02-16 22:51:15 +01:00
|
|
|
,m_bListening(false)
|
2004-01-28 11:46:55 +00:00
|
|
|
{
|
|
|
|
impl_startDisposeListening();
|
|
|
|
}
|
|
|
|
|
|
|
|
OEnumerationByName::~OEnumerationByName()
|
|
|
|
{
|
2021-10-24 19:41:36 +02:00
|
|
|
std::lock_guard aLock(m_aLock);
|
|
|
|
|
2004-01-28 11:46:55 +00:00
|
|
|
impl_stopDisposeListening();
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
sal_Bool SAL_CALL OEnumerationByName::hasMoreElements( )
|
2000-09-29 10:28:15 +00:00
|
|
|
{
|
2021-10-24 19:41:36 +02:00
|
|
|
std::lock_guard aLock(m_aLock);
|
2004-01-28 11:46:55 +00:00
|
|
|
|
2022-05-25 09:28:16 +02:00
|
|
|
if (m_xAccess.is() && getLength() > m_nPos)
|
2016-04-20 17:15:31 +02:00
|
|
|
return true;
|
2000-09-29 10:28:15 +00:00
|
|
|
|
2004-01-28 11:46:55 +00:00
|
|
|
if (m_xAccess.is())
|
|
|
|
{
|
|
|
|
impl_stopDisposeListening();
|
|
|
|
m_xAccess.clear();
|
|
|
|
}
|
|
|
|
|
2016-04-20 17:15:31 +02:00
|
|
|
return false;
|
2000-09-29 10:28:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2014-08-20 21:30:16 +02:00
|
|
|
css::uno::Any SAL_CALL OEnumerationByName::nextElement( )
|
2000-09-29 10:28:15 +00:00
|
|
|
{
|
2021-10-24 19:41:36 +02:00
|
|
|
std::lock_guard aLock(m_aLock);
|
2004-01-28 11:46:55 +00:00
|
|
|
|
2014-08-20 21:30:16 +02:00
|
|
|
css::uno::Any aRes;
|
2022-05-25 09:28:16 +02:00
|
|
|
if (m_xAccess.is() && m_nPos < getLength())
|
|
|
|
aRes = m_xAccess->getByName(getElement(m_nPos++));
|
2004-01-28 11:46:55 +00:00
|
|
|
|
2022-05-25 09:28:16 +02:00
|
|
|
if (m_xAccess.is() && m_nPos >= getLength())
|
2004-01-28 11:46:55 +00:00
|
|
|
{
|
|
|
|
impl_stopDisposeListening();
|
|
|
|
m_xAccess.clear();
|
|
|
|
}
|
2000-09-29 10:28:15 +00:00
|
|
|
|
2012-02-28 00:38:23 -05:00
|
|
|
if (!aRes.hasValue()) //There are no more elements
|
2014-08-20 21:42:06 +02:00
|
|
|
throw css::container::NoSuchElementException();
|
2000-09-29 10:28:15 +00:00
|
|
|
|
|
|
|
return aRes;
|
|
|
|
}
|
|
|
|
|
2014-08-20 21:32:19 +02:00
|
|
|
void SAL_CALL OEnumerationByName::disposing(const css::lang::EventObject& aEvent)
|
2004-01-28 11:46:55 +00:00
|
|
|
{
|
2021-10-24 19:41:36 +02:00
|
|
|
std::lock_guard aLock(m_aLock);
|
2004-01-28 11:46:55 +00:00
|
|
|
|
|
|
|
if (aEvent.Source == m_xAccess)
|
|
|
|
m_xAccess.clear();
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2004-01-28 11:46:55 +00:00
|
|
|
void OEnumerationByName::impl_startDisposeListening()
|
|
|
|
{
|
|
|
|
if (m_bListening)
|
|
|
|
return;
|
|
|
|
|
2019-04-07 16:00:31 +01:00
|
|
|
osl_atomic_increment(&m_refCount);
|
2014-08-20 21:32:19 +02:00
|
|
|
css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
|
2004-01-28 11:46:55 +00:00
|
|
|
if (xDisposable.is())
|
|
|
|
{
|
|
|
|
xDisposable->addEventListener(this);
|
2014-02-16 22:51:15 +01:00
|
|
|
m_bListening = true;
|
2004-01-28 11:46:55 +00:00
|
|
|
}
|
2019-04-07 16:00:31 +01:00
|
|
|
osl_atomic_decrement(&m_refCount);
|
2004-01-28 11:46:55 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2004-01-28 11:46:55 +00:00
|
|
|
void OEnumerationByName::impl_stopDisposeListening()
|
|
|
|
{
|
|
|
|
if (!m_bListening)
|
|
|
|
return;
|
|
|
|
|
2019-04-07 16:00:31 +01:00
|
|
|
osl_atomic_increment(&m_refCount);
|
2014-08-20 21:32:19 +02:00
|
|
|
css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
|
2004-01-28 11:46:55 +00:00
|
|
|
if (xDisposable.is())
|
|
|
|
{
|
|
|
|
xDisposable->removeEventListener(this);
|
2014-02-16 22:51:15 +01:00
|
|
|
m_bListening = false;
|
2004-01-28 11:46:55 +00:00
|
|
|
}
|
2019-04-07 16:00:31 +01:00
|
|
|
osl_atomic_decrement(&m_refCount);
|
2004-01-28 11:46:55 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 09:28:16 +02:00
|
|
|
sal_Int32 OEnumerationByName::getLength() const
|
|
|
|
{
|
|
|
|
if (m_aNames.index() == 0)
|
|
|
|
return std::get<css::uno::Sequence<OUString>>(m_aNames).getLength();
|
|
|
|
else
|
|
|
|
return std::get<std::vector<OUString>>(m_aNames).size();
|
|
|
|
}
|
|
|
|
|
|
|
|
const OUString& OEnumerationByName::getElement(sal_Int32 nIndex) const
|
|
|
|
{
|
|
|
|
if (m_aNames.index() == 0)
|
|
|
|
return std::get<css::uno::Sequence<OUString>>(m_aNames).getConstArray()[nIndex];
|
|
|
|
else
|
|
|
|
return std::get<std::vector<OUString>>(m_aNames)[nIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-26 09:30:35 +02:00
|
|
|
OEnumerationByIndex::OEnumerationByIndex(css::uno::Reference< css::container::XIndexAccess > _xAccess)
|
|
|
|
:m_xAccess(std::move(_xAccess))
|
2020-02-07 11:35:49 +02:00
|
|
|
,m_nPos(0)
|
2014-02-16 22:51:15 +01:00
|
|
|
,m_bListening(false)
|
2004-01-28 11:46:55 +00:00
|
|
|
{
|
|
|
|
impl_startDisposeListening();
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2004-01-28 11:46:55 +00:00
|
|
|
OEnumerationByIndex::~OEnumerationByIndex()
|
|
|
|
{
|
2021-10-24 19:41:36 +02:00
|
|
|
std::lock_guard aLock(m_aLock);
|
|
|
|
|
2004-01-28 11:46:55 +00:00
|
|
|
impl_stopDisposeListening();
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
sal_Bool SAL_CALL OEnumerationByIndex::hasMoreElements( )
|
2000-09-29 10:28:15 +00:00
|
|
|
{
|
2021-10-24 19:41:36 +02:00
|
|
|
std::lock_guard aLock(m_aLock);
|
2004-01-28 11:46:55 +00:00
|
|
|
|
2000-09-29 10:28:15 +00:00
|
|
|
if (m_xAccess.is() && m_xAccess->getCount() > m_nPos)
|
2016-04-20 17:15:31 +02:00
|
|
|
return true;
|
2004-01-28 11:46:55 +00:00
|
|
|
|
|
|
|
if (m_xAccess.is())
|
|
|
|
{
|
|
|
|
impl_stopDisposeListening();
|
|
|
|
m_xAccess.clear();
|
|
|
|
}
|
2000-09-29 10:28:15 +00:00
|
|
|
|
2016-04-20 17:15:31 +02:00
|
|
|
return false;
|
2000-09-29 10:28:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2014-08-20 21:30:16 +02:00
|
|
|
css::uno::Any SAL_CALL OEnumerationByIndex::nextElement( )
|
2000-09-29 10:28:15 +00:00
|
|
|
{
|
2021-10-24 19:41:36 +02:00
|
|
|
std::lock_guard aLock(m_aLock);
|
2004-01-28 11:46:55 +00:00
|
|
|
|
2014-08-20 21:30:16 +02:00
|
|
|
css::uno::Any aRes;
|
2017-07-10 17:13:49 +00:00
|
|
|
if (m_xAccess.is() && m_nPos < m_xAccess->getCount())
|
2000-09-29 10:28:15 +00:00
|
|
|
aRes = m_xAccess->getByIndex(m_nPos++);
|
2017-07-10 17:13:49 +00:00
|
|
|
|
|
|
|
if (m_xAccess.is() && m_nPos >= m_xAccess->getCount())
|
|
|
|
{
|
|
|
|
impl_stopDisposeListening();
|
|
|
|
m_xAccess.clear();
|
2000-09-29 10:28:15 +00:00
|
|
|
}
|
|
|
|
|
2012-02-28 00:38:23 -05:00
|
|
|
if (!aRes.hasValue())
|
2014-08-20 21:42:06 +02:00
|
|
|
throw css::container::NoSuchElementException();
|
2000-09-29 10:28:15 +00:00
|
|
|
return aRes;
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2014-08-20 21:32:19 +02:00
|
|
|
void SAL_CALL OEnumerationByIndex::disposing(const css::lang::EventObject& aEvent)
|
2004-01-28 11:46:55 +00:00
|
|
|
{
|
2021-10-24 19:41:36 +02:00
|
|
|
std::lock_guard aLock(m_aLock);
|
2004-01-28 11:46:55 +00:00
|
|
|
|
|
|
|
if (aEvent.Source == m_xAccess)
|
|
|
|
m_xAccess.clear();
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2004-01-28 11:46:55 +00:00
|
|
|
void OEnumerationByIndex::impl_startDisposeListening()
|
|
|
|
{
|
|
|
|
if (m_bListening)
|
|
|
|
return;
|
|
|
|
|
2019-04-07 16:00:31 +01:00
|
|
|
osl_atomic_increment(&m_refCount);
|
2014-08-20 21:32:19 +02:00
|
|
|
css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
|
2004-01-28 11:46:55 +00:00
|
|
|
if (xDisposable.is())
|
|
|
|
{
|
|
|
|
xDisposable->addEventListener(this);
|
2014-02-16 22:51:15 +01:00
|
|
|
m_bListening = true;
|
2004-01-28 11:46:55 +00:00
|
|
|
}
|
2019-04-07 16:00:31 +01:00
|
|
|
osl_atomic_decrement(&m_refCount);
|
2004-01-28 11:46:55 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2004-01-28 11:46:55 +00:00
|
|
|
void OEnumerationByIndex::impl_stopDisposeListening()
|
|
|
|
{
|
|
|
|
if (!m_bListening)
|
|
|
|
return;
|
|
|
|
|
2019-04-07 16:00:31 +01:00
|
|
|
osl_atomic_increment(&m_refCount);
|
2014-08-20 21:32:19 +02:00
|
|
|
css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
|
2004-01-28 11:46:55 +00:00
|
|
|
if (xDisposable.is())
|
|
|
|
{
|
|
|
|
xDisposable->removeEventListener(this);
|
2014-02-16 22:51:15 +01:00
|
|
|
m_bListening = false;
|
2004-01-28 11:46:55 +00:00
|
|
|
}
|
2019-04-07 16:00:31 +01:00
|
|
|
osl_atomic_decrement(&m_refCount);
|
2004-01-28 11:46:55 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 21:30:16 +02:00
|
|
|
OAnyEnumeration::OAnyEnumeration(const css::uno::Sequence< css::uno::Any >& lItems)
|
2007-04-16 15:58:44 +00:00
|
|
|
:m_nPos(0)
|
|
|
|
,m_lItems(lItems)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2007-04-16 15:58:44 +00:00
|
|
|
OAnyEnumeration::~OAnyEnumeration()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2017-01-26 12:28:58 +01:00
|
|
|
sal_Bool SAL_CALL OAnyEnumeration::hasMoreElements( )
|
2007-04-16 15:58:44 +00:00
|
|
|
{
|
2021-10-24 19:41:36 +02:00
|
|
|
std::lock_guard aLock(m_aLock);
|
2007-04-16 15:58:44 +00:00
|
|
|
|
|
|
|
return (m_lItems.getLength() > m_nPos);
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:20:15 +01:00
|
|
|
|
2014-08-20 21:30:16 +02:00
|
|
|
css::uno::Any SAL_CALL OAnyEnumeration::nextElement( )
|
2007-04-16 15:58:44 +00:00
|
|
|
{
|
|
|
|
if ( ! hasMoreElements())
|
2014-08-20 21:42:06 +02:00
|
|
|
throw css::container::NoSuchElementException();
|
2007-04-16 15:58:44 +00:00
|
|
|
|
2021-10-24 19:41:36 +02:00
|
|
|
std::lock_guard aLock(m_aLock);
|
2007-04-16 15:58:44 +00:00
|
|
|
sal_Int32 nPos = m_nPos;
|
|
|
|
++m_nPos;
|
|
|
|
return m_lItems[nPos];
|
|
|
|
}
|
|
|
|
|
2014-02-25 17:52:30 +01:00
|
|
|
|
2000-09-29 10:28:15 +00:00
|
|
|
} // namespace comphelper
|
2014-02-25 17:52:30 +01:00
|
|
|
|
2000-09-29 10:28:15 +00:00
|
|
|
|
2010-10-14 08:27:31 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|