Bump version to 4.3-4
[LibreOffice.git] / comphelper / source / container / enumhelper.cxx
blob343b27655a95a5d49032aa2e2ac4d19dbd4ac907
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>
24 namespace comphelper
29 //= OEnumerationByName
32 OEnumerationByName::OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess>& _rxAccess)
33 :m_aNames(_rxAccess->getElementNames())
34 ,m_nPos(0)
35 ,m_xAccess(_rxAccess)
36 ,m_bListening(false)
38 impl_startDisposeListening();
42 OEnumerationByName::OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess>& _rxAccess,
43 const staruno::Sequence< OUString >& _aNames )
44 :m_aNames(_aNames)
45 ,m_nPos(0)
46 ,m_xAccess(_rxAccess)
47 ,m_bListening(false)
49 impl_startDisposeListening();
53 OEnumerationByName::~OEnumerationByName()
55 impl_stopDisposeListening();
59 sal_Bool SAL_CALL OEnumerationByName::hasMoreElements( ) throw(staruno::RuntimeException, std::exception)
61 ::osl::ResettableMutexGuard aLock(m_aLock);
63 if (m_xAccess.is() && m_aNames.getLength() > m_nPos)
64 return sal_True;
66 if (m_xAccess.is())
68 impl_stopDisposeListening();
69 m_xAccess.clear();
72 return sal_False;
76 staruno::Any SAL_CALL OEnumerationByName::nextElement( )
77 throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException, std::exception)
79 ::osl::ResettableMutexGuard aLock(m_aLock);
81 staruno::Any aRes;
82 if (m_xAccess.is() && m_nPos < m_aNames.getLength())
83 aRes = m_xAccess->getByName(m_aNames.getConstArray()[m_nPos++]);
85 if (m_xAccess.is() && m_nPos >= m_aNames.getLength())
87 impl_stopDisposeListening();
88 m_xAccess.clear();
91 if (!aRes.hasValue()) //There are no more elements
92 throw starcontainer::NoSuchElementException();
94 return aRes;
98 void SAL_CALL OEnumerationByName::disposing(const starlang::EventObject& aEvent)
99 throw(staruno::RuntimeException, std::exception)
101 ::osl::ResettableMutexGuard aLock(m_aLock);
103 if (aEvent.Source == m_xAccess)
104 m_xAccess.clear();
108 void OEnumerationByName::impl_startDisposeListening()
110 ::osl::ResettableMutexGuard aLock(m_aLock);
112 if (m_bListening)
113 return;
115 ++m_refCount;
116 staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
117 if (xDisposable.is())
119 xDisposable->addEventListener(this);
120 m_bListening = true;
122 --m_refCount;
126 void OEnumerationByName::impl_stopDisposeListening()
128 ::osl::ResettableMutexGuard aLock(m_aLock);
130 if (!m_bListening)
131 return;
133 ++m_refCount;
134 staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
135 if (xDisposable.is())
137 xDisposable->removeEventListener(this);
138 m_bListening = false;
140 --m_refCount;
144 //= OEnumerationByIndex
147 OEnumerationByIndex::OEnumerationByIndex(const staruno::Reference< starcontainer::XIndexAccess >& _rxAccess)
148 :m_nPos(0)
149 ,m_xAccess(_rxAccess)
150 ,m_bListening(false)
152 impl_startDisposeListening();
156 OEnumerationByIndex::~OEnumerationByIndex()
158 impl_stopDisposeListening();
162 sal_Bool SAL_CALL OEnumerationByIndex::hasMoreElements( ) throw(staruno::RuntimeException, std::exception)
164 ::osl::ResettableMutexGuard aLock(m_aLock);
166 if (m_xAccess.is() && m_xAccess->getCount() > m_nPos)
167 return sal_True;
169 if (m_xAccess.is())
171 impl_stopDisposeListening();
172 m_xAccess.clear();
175 return sal_False;
179 staruno::Any SAL_CALL OEnumerationByIndex::nextElement( )
180 throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException, std::exception)
182 ::osl::ResettableMutexGuard aLock(m_aLock);
184 staruno::Any aRes;
185 if (m_xAccess.is())
187 aRes = m_xAccess->getByIndex(m_nPos++);
188 if (m_nPos >= m_xAccess->getCount())
190 impl_stopDisposeListening();
191 m_xAccess.clear();
195 if (!aRes.hasValue())
196 throw starcontainer::NoSuchElementException();
197 return aRes;
201 void SAL_CALL OEnumerationByIndex::disposing(const starlang::EventObject& aEvent)
202 throw(staruno::RuntimeException, std::exception)
204 ::osl::ResettableMutexGuard aLock(m_aLock);
206 if (aEvent.Source == m_xAccess)
207 m_xAccess.clear();
211 void OEnumerationByIndex::impl_startDisposeListening()
213 ::osl::ResettableMutexGuard aLock(m_aLock);
215 if (m_bListening)
216 return;
218 ++m_refCount;
219 staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
220 if (xDisposable.is())
222 xDisposable->addEventListener(this);
223 m_bListening = true;
225 --m_refCount;
229 void OEnumerationByIndex::impl_stopDisposeListening()
231 ::osl::ResettableMutexGuard aLock(m_aLock);
233 if (!m_bListening)
234 return;
236 ++m_refCount;
237 staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
238 if (xDisposable.is())
240 xDisposable->removeEventListener(this);
241 m_bListening = false;
243 --m_refCount;
247 //= OAnyEnumeration
251 OAnyEnumeration::OAnyEnumeration(const staruno::Sequence< staruno::Any >& lItems)
252 :m_nPos(0)
253 ,m_lItems(lItems)
258 OAnyEnumeration::~OAnyEnumeration()
263 sal_Bool SAL_CALL OAnyEnumeration::hasMoreElements( ) throw(staruno::RuntimeException, std::exception)
265 ::osl::ResettableMutexGuard aLock(m_aLock);
267 return (m_lItems.getLength() > m_nPos);
271 staruno::Any SAL_CALL OAnyEnumeration::nextElement( )
272 throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException, std::exception)
274 if ( ! hasMoreElements())
275 throw starcontainer::NoSuchElementException();
277 ::osl::ResettableMutexGuard aLock(m_aLock);
278 sal_Int32 nPos = m_nPos;
279 ++m_nPos;
280 return m_lItems[nPos];
284 } // namespace comphelper
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */