update emoji autocorrect entries from po-files
[LibreOffice.git] / comphelper / source / container / enumhelper.cxx
blob7a7b07725584831201d48f86844f3d4e0b414d2d
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
27 OEnumerationByName::OEnumerationByName(const css::uno::Reference<css::container::XNameAccess>& _rxAccess)
28 :m_aNames(_rxAccess->getElementNames())
29 ,m_nPos(0)
30 ,m_xAccess(_rxAccess)
31 ,m_bListening(false)
33 impl_startDisposeListening();
37 OEnumerationByName::OEnumerationByName(const css::uno::Reference<css::container::XNameAccess>& _rxAccess,
38 const css::uno::Sequence< OUString >& _aNames )
39 :m_aNames(_aNames)
40 ,m_nPos(0)
41 ,m_xAccess(_rxAccess)
42 ,m_bListening(false)
44 impl_startDisposeListening();
48 OEnumerationByName::~OEnumerationByName()
50 impl_stopDisposeListening();
54 sal_Bool SAL_CALL OEnumerationByName::hasMoreElements( ) throw(css::uno::RuntimeException, std::exception)
56 ::osl::ResettableMutexGuard aLock(m_aLock);
58 if (m_xAccess.is() && m_aNames.getLength() > m_nPos)
59 return sal_True;
61 if (m_xAccess.is())
63 impl_stopDisposeListening();
64 m_xAccess.clear();
67 return sal_False;
71 css::uno::Any SAL_CALL OEnumerationByName::nextElement( )
72 throw(css::container::NoSuchElementException, css::lang::WrappedTargetException, css::uno::RuntimeException, std::exception)
74 ::osl::ResettableMutexGuard aLock(m_aLock);
76 css::uno::Any aRes;
77 if (m_xAccess.is() && m_nPos < m_aNames.getLength())
78 aRes = m_xAccess->getByName(m_aNames.getConstArray()[m_nPos++]);
80 if (m_xAccess.is() && m_nPos >= m_aNames.getLength())
82 impl_stopDisposeListening();
83 m_xAccess.clear();
86 if (!aRes.hasValue()) //There are no more elements
87 throw css::container::NoSuchElementException();
89 return aRes;
93 void SAL_CALL OEnumerationByName::disposing(const css::lang::EventObject& aEvent)
94 throw(css::uno::RuntimeException, std::exception)
96 ::osl::ResettableMutexGuard aLock(m_aLock);
98 if (aEvent.Source == m_xAccess)
99 m_xAccess.clear();
103 void OEnumerationByName::impl_startDisposeListening()
105 ::osl::ResettableMutexGuard aLock(m_aLock);
107 if (m_bListening)
108 return;
110 ++m_refCount;
111 css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
112 if (xDisposable.is())
114 xDisposable->addEventListener(this);
115 m_bListening = true;
117 --m_refCount;
121 void OEnumerationByName::impl_stopDisposeListening()
123 ::osl::ResettableMutexGuard aLock(m_aLock);
125 if (!m_bListening)
126 return;
128 ++m_refCount;
129 css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
130 if (xDisposable.is())
132 xDisposable->removeEventListener(this);
133 m_bListening = false;
135 --m_refCount;
138 OEnumerationByIndex::OEnumerationByIndex(const css::uno::Reference< css::container::XIndexAccess >& _rxAccess)
139 :m_nPos(0)
140 ,m_xAccess(_rxAccess)
141 ,m_bListening(false)
143 impl_startDisposeListening();
147 OEnumerationByIndex::~OEnumerationByIndex()
149 impl_stopDisposeListening();
153 sal_Bool SAL_CALL OEnumerationByIndex::hasMoreElements( ) throw(css::uno::RuntimeException, std::exception)
155 ::osl::ResettableMutexGuard aLock(m_aLock);
157 if (m_xAccess.is() && m_xAccess->getCount() > m_nPos)
158 return sal_True;
160 if (m_xAccess.is())
162 impl_stopDisposeListening();
163 m_xAccess.clear();
166 return sal_False;
170 css::uno::Any SAL_CALL OEnumerationByIndex::nextElement( )
171 throw(css::container::NoSuchElementException, css::lang::WrappedTargetException, css::uno::RuntimeException, std::exception)
173 ::osl::ResettableMutexGuard aLock(m_aLock);
175 css::uno::Any aRes;
176 if (m_xAccess.is())
178 aRes = m_xAccess->getByIndex(m_nPos++);
179 if (m_nPos >= m_xAccess->getCount())
181 impl_stopDisposeListening();
182 m_xAccess.clear();
186 if (!aRes.hasValue())
187 throw css::container::NoSuchElementException();
188 return aRes;
192 void SAL_CALL OEnumerationByIndex::disposing(const css::lang::EventObject& aEvent)
193 throw(css::uno::RuntimeException, std::exception)
195 ::osl::ResettableMutexGuard aLock(m_aLock);
197 if (aEvent.Source == m_xAccess)
198 m_xAccess.clear();
202 void OEnumerationByIndex::impl_startDisposeListening()
204 ::osl::ResettableMutexGuard aLock(m_aLock);
206 if (m_bListening)
207 return;
209 ++m_refCount;
210 css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
211 if (xDisposable.is())
213 xDisposable->addEventListener(this);
214 m_bListening = true;
216 --m_refCount;
220 void OEnumerationByIndex::impl_stopDisposeListening()
222 ::osl::ResettableMutexGuard aLock(m_aLock);
224 if (!m_bListening)
225 return;
227 ++m_refCount;
228 css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
229 if (xDisposable.is())
231 xDisposable->removeEventListener(this);
232 m_bListening = false;
234 --m_refCount;
237 OAnyEnumeration::OAnyEnumeration(const css::uno::Sequence< css::uno::Any >& lItems)
238 :m_nPos(0)
239 ,m_lItems(lItems)
244 OAnyEnumeration::~OAnyEnumeration()
249 sal_Bool SAL_CALL OAnyEnumeration::hasMoreElements( ) throw(css::uno::RuntimeException, std::exception)
251 ::osl::ResettableMutexGuard aLock(m_aLock);
253 return (m_lItems.getLength() > m_nPos);
257 css::uno::Any SAL_CALL OAnyEnumeration::nextElement( )
258 throw(css::container::NoSuchElementException, css::lang::WrappedTargetException, css::uno::RuntimeException, std::exception)
260 if ( ! hasMoreElements())
261 throw css::container::NoSuchElementException();
263 ::osl::ResettableMutexGuard aLock(m_aLock);
264 sal_Int32 nPos = m_nPos;
265 ++m_nPos;
266 return m_lItems[nPos];
270 } // namespace comphelper
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */