Simplify a bit
[LibreOffice.git] / comphelper / source / container / enumhelper.cxx
blob3390fef86444f7ab3c5b3abe5ae0c69692a2228b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <comphelper/enumhelper.hxx>
21 #include <com/sun/star/lang/XComponent.hpp>
22 #include <com/sun/star/container/XIndexAccess.hpp>
23 #include <com/sun/star/container/XNameAccess.hpp>
24 #include <utility>
26 namespace comphelper
29 OEnumerationByName::OEnumerationByName(css::uno::Reference<css::container::XNameAccess> _xAccess)
30 :m_aNames(_xAccess->getElementNames())
31 ,m_xAccess(_xAccess)
32 ,m_nPos(0)
33 ,m_bListening(false)
35 impl_startDisposeListening();
39 OEnumerationByName::OEnumerationByName(css::uno::Reference<css::container::XNameAccess> _xAccess,
40 std::vector<OUString> _aNames )
41 :m_aNames(std::move(_aNames))
42 ,m_xAccess(std::move(_xAccess))
43 ,m_nPos(0)
44 ,m_bListening(false)
46 impl_startDisposeListening();
49 OEnumerationByName::~OEnumerationByName()
51 std::lock_guard aLock(m_aLock);
53 impl_stopDisposeListening();
57 sal_Bool SAL_CALL OEnumerationByName::hasMoreElements( )
59 std::lock_guard aLock(m_aLock);
61 if (m_xAccess.is() && getLength() > m_nPos)
62 return true;
64 if (m_xAccess.is())
66 impl_stopDisposeListening();
67 m_xAccess.clear();
70 return false;
74 css::uno::Any SAL_CALL OEnumerationByName::nextElement( )
76 std::lock_guard aLock(m_aLock);
78 css::uno::Any aRes;
79 if (m_xAccess.is() && m_nPos < getLength())
80 aRes = m_xAccess->getByName(getElement(m_nPos++));
82 if (m_xAccess.is() && m_nPos >= getLength())
84 impl_stopDisposeListening();
85 m_xAccess.clear();
88 if (!aRes.hasValue()) //There are no more elements
89 throw css::container::NoSuchElementException();
91 return aRes;
94 void SAL_CALL OEnumerationByName::disposing(const css::lang::EventObject& aEvent)
96 std::lock_guard aLock(m_aLock);
98 if (aEvent.Source == m_xAccess)
99 m_xAccess.clear();
103 void OEnumerationByName::impl_startDisposeListening()
105 if (m_bListening)
106 return;
108 osl_atomic_increment(&m_refCount);
109 css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
110 if (xDisposable.is())
112 xDisposable->addEventListener(this);
113 m_bListening = true;
115 osl_atomic_decrement(&m_refCount);
119 void OEnumerationByName::impl_stopDisposeListening()
121 if (!m_bListening)
122 return;
124 osl_atomic_increment(&m_refCount);
125 css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
126 if (xDisposable.is())
128 xDisposable->removeEventListener(this);
129 m_bListening = false;
131 osl_atomic_decrement(&m_refCount);
134 sal_Int32 OEnumerationByName::getLength() const
136 if (m_aNames.index() == 0)
137 return std::get<css::uno::Sequence<OUString>>(m_aNames).getLength();
138 else
139 return std::get<std::vector<OUString>>(m_aNames).size();
142 const OUString& OEnumerationByName::getElement(sal_Int32 nIndex) const
144 if (m_aNames.index() == 0)
145 return std::get<css::uno::Sequence<OUString>>(m_aNames)[nIndex];
146 else
147 return std::get<std::vector<OUString>>(m_aNames)[nIndex];
151 OEnumerationByIndex::OEnumerationByIndex(css::uno::Reference< css::container::XIndexAccess > _xAccess)
152 :m_xAccess(std::move(_xAccess))
153 ,m_nPos(0)
154 ,m_bListening(false)
156 impl_startDisposeListening();
160 OEnumerationByIndex::~OEnumerationByIndex()
162 std::lock_guard aLock(m_aLock);
164 impl_stopDisposeListening();
168 sal_Bool SAL_CALL OEnumerationByIndex::hasMoreElements( )
170 std::lock_guard aLock(m_aLock);
172 if (m_xAccess.is() && m_xAccess->getCount() > m_nPos)
173 return true;
175 if (m_xAccess.is())
177 impl_stopDisposeListening();
178 m_xAccess.clear();
181 return false;
185 css::uno::Any SAL_CALL OEnumerationByIndex::nextElement( )
187 std::lock_guard aLock(m_aLock);
189 css::uno::Any aRes;
190 if (m_xAccess.is() && m_nPos < m_xAccess->getCount())
191 aRes = m_xAccess->getByIndex(m_nPos++);
193 if (m_xAccess.is() && m_nPos >= m_xAccess->getCount())
195 impl_stopDisposeListening();
196 m_xAccess.clear();
199 if (!aRes.hasValue())
200 throw css::container::NoSuchElementException();
201 return aRes;
205 void SAL_CALL OEnumerationByIndex::disposing(const css::lang::EventObject& aEvent)
207 std::lock_guard aLock(m_aLock);
209 if (aEvent.Source == m_xAccess)
210 m_xAccess.clear();
214 void OEnumerationByIndex::impl_startDisposeListening()
216 if (m_bListening)
217 return;
219 osl_atomic_increment(&m_refCount);
220 css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
221 if (xDisposable.is())
223 xDisposable->addEventListener(this);
224 m_bListening = true;
226 osl_atomic_decrement(&m_refCount);
230 void OEnumerationByIndex::impl_stopDisposeListening()
232 if (!m_bListening)
233 return;
235 osl_atomic_increment(&m_refCount);
236 css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
237 if (xDisposable.is())
239 xDisposable->removeEventListener(this);
240 m_bListening = false;
242 osl_atomic_decrement(&m_refCount);
245 OAnyEnumeration::OAnyEnumeration(const css::uno::Sequence< css::uno::Any >& lItems)
246 :m_nPos(0)
247 ,m_lItems(lItems)
252 OAnyEnumeration::~OAnyEnumeration()
257 sal_Bool SAL_CALL OAnyEnumeration::hasMoreElements( )
259 std::lock_guard aLock(m_aLock);
261 return (m_lItems.getLength() > m_nPos);
265 css::uno::Any SAL_CALL OAnyEnumeration::nextElement( )
267 if ( ! hasMoreElements())
268 throw css::container::NoSuchElementException();
270 std::lock_guard aLock(m_aLock);
271 sal_Int32 nPos = m_nPos;
272 ++m_nPos;
273 return m_lItems[nPos];
277 } // namespace comphelper
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */