merged tag ooo/OOO330_m14
[LibreOffice.git] / comphelper / source / container / enumhelper.cxx
blob646bffa9810fc3128bd7af3f8ccff62d7287a939
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/enumhelper.hxx>
31 #include <com/sun/star/lang/XComponent.hpp>
33 //.........................................................................
34 namespace comphelper
36 //.........................................................................
38 //==================================================================
39 //= OEnumerationByName
40 //==================================================================
41 //------------------------------------------------------------------------------
42 OEnumerationByName::OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess>& _rxAccess)
43 :m_aNames(_rxAccess->getElementNames())
44 ,m_nPos(0)
45 ,m_xAccess(_rxAccess)
46 ,m_bListening(sal_False)
48 impl_startDisposeListening();
51 //------------------------------------------------------------------------------
52 OEnumerationByName::OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess>& _rxAccess,
53 const staruno::Sequence< ::rtl::OUString >& _aNames )
54 :m_aNames(_aNames)
55 ,m_nPos(0)
56 ,m_xAccess(_rxAccess)
57 ,m_bListening(sal_False)
59 impl_startDisposeListening();
62 //------------------------------------------------------------------------------
63 OEnumerationByName::~OEnumerationByName()
65 impl_stopDisposeListening();
68 //------------------------------------------------------------------------------
69 sal_Bool SAL_CALL OEnumerationByName::hasMoreElements( ) throw(staruno::RuntimeException)
71 ::osl::ResettableMutexGuard aLock(m_aLock);
73 if (m_xAccess.is() && m_aNames.getLength() > m_nPos)
74 return sal_True;
76 if (m_xAccess.is())
78 impl_stopDisposeListening();
79 m_xAccess.clear();
82 return sal_False;
85 //------------------------------------------------------------------------------
86 staruno::Any SAL_CALL OEnumerationByName::nextElement( )
87 throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
89 ::osl::ResettableMutexGuard aLock(m_aLock);
91 staruno::Any aRes;
92 if (m_xAccess.is() && m_nPos < m_aNames.getLength())
93 aRes = m_xAccess->getByName(m_aNames.getConstArray()[m_nPos++]);
95 if (m_xAccess.is() && m_nPos >= m_aNames.getLength())
97 impl_stopDisposeListening();
98 m_xAccess.clear();
101 if (!aRes.hasValue()) // es gibt kein Element mehr
102 throw starcontainer::NoSuchElementException();
104 return aRes;
107 //------------------------------------------------------------------------------
108 void SAL_CALL OEnumerationByName::disposing(const starlang::EventObject& aEvent)
109 throw(staruno::RuntimeException)
111 ::osl::ResettableMutexGuard aLock(m_aLock);
113 if (aEvent.Source == m_xAccess)
114 m_xAccess.clear();
117 //------------------------------------------------------------------------------
118 void OEnumerationByName::impl_startDisposeListening()
120 ::osl::ResettableMutexGuard aLock(m_aLock);
122 if (m_bListening)
123 return;
125 ++m_refCount;
126 staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
127 if (xDisposable.is())
129 xDisposable->addEventListener(this);
130 m_bListening = sal_True;
132 --m_refCount;
135 //------------------------------------------------------------------------------
136 void OEnumerationByName::impl_stopDisposeListening()
138 ::osl::ResettableMutexGuard aLock(m_aLock);
140 if (!m_bListening)
141 return;
143 ++m_refCount;
144 staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
145 if (xDisposable.is())
147 xDisposable->removeEventListener(this);
148 m_bListening = sal_False;
150 --m_refCount;
153 //==================================================================
154 //= OEnumerationByIndex
155 //==================================================================
156 //------------------------------------------------------------------------------
157 OEnumerationByIndex::OEnumerationByIndex(const staruno::Reference< starcontainer::XIndexAccess >& _rxAccess)
158 :m_nPos(0)
159 ,m_xAccess(_rxAccess)
160 ,m_bListening(sal_False)
162 impl_startDisposeListening();
165 //------------------------------------------------------------------------------
166 OEnumerationByIndex::~OEnumerationByIndex()
168 impl_stopDisposeListening();
171 //------------------------------------------------------------------------------
172 sal_Bool SAL_CALL OEnumerationByIndex::hasMoreElements( ) throw(staruno::RuntimeException)
174 ::osl::ResettableMutexGuard aLock(m_aLock);
176 if (m_xAccess.is() && m_xAccess->getCount() > m_nPos)
177 return sal_True;
179 if (m_xAccess.is())
181 impl_stopDisposeListening();
182 m_xAccess.clear();
185 return sal_False;
188 //------------------------------------------------------------------------------
189 staruno::Any SAL_CALL OEnumerationByIndex::nextElement( )
190 throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
192 ::osl::ResettableMutexGuard aLock(m_aLock);
194 staruno::Any aRes;
195 if (m_xAccess.is())
197 aRes = m_xAccess->getByIndex(m_nPos++);
198 if (m_nPos >= m_xAccess->getCount())
200 impl_stopDisposeListening();
201 m_xAccess.clear();
205 if (!aRes.hasValue()) // es gibt kein Element mehr
206 throw starcontainer::NoSuchElementException();
207 return aRes;
210 //------------------------------------------------------------------------------
211 void SAL_CALL OEnumerationByIndex::disposing(const starlang::EventObject& aEvent)
212 throw(staruno::RuntimeException)
214 ::osl::ResettableMutexGuard aLock(m_aLock);
216 if (aEvent.Source == m_xAccess)
217 m_xAccess.clear();
220 //------------------------------------------------------------------------------
221 void OEnumerationByIndex::impl_startDisposeListening()
223 ::osl::ResettableMutexGuard aLock(m_aLock);
225 if (m_bListening)
226 return;
228 ++m_refCount;
229 staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
230 if (xDisposable.is())
232 xDisposable->addEventListener(this);
233 m_bListening = sal_True;
235 --m_refCount;
238 //------------------------------------------------------------------------------
239 void OEnumerationByIndex::impl_stopDisposeListening()
241 ::osl::ResettableMutexGuard aLock(m_aLock);
243 if (!m_bListening)
244 return;
246 ++m_refCount;
247 staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
248 if (xDisposable.is())
250 xDisposable->removeEventListener(this);
251 m_bListening = sal_False;
253 --m_refCount;
256 //==================================================================
257 //= OAnyEnumeration
258 //==================================================================
260 //------------------------------------------------------------------------------
261 OAnyEnumeration::OAnyEnumeration(const staruno::Sequence< staruno::Any >& lItems)
262 :m_nPos(0)
263 ,m_lItems(lItems)
267 //------------------------------------------------------------------------------
268 OAnyEnumeration::~OAnyEnumeration()
272 //------------------------------------------------------------------------------
273 sal_Bool SAL_CALL OAnyEnumeration::hasMoreElements( ) throw(staruno::RuntimeException)
275 ::osl::ResettableMutexGuard aLock(m_aLock);
277 return (m_lItems.getLength() > m_nPos);
280 //------------------------------------------------------------------------------
281 staruno::Any SAL_CALL OAnyEnumeration::nextElement( )
282 throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
284 if ( ! hasMoreElements())
285 throw starcontainer::NoSuchElementException();
287 ::osl::ResettableMutexGuard aLock(m_aLock);
288 sal_Int32 nPos = m_nPos;
289 ++m_nPos;
290 return m_lItems[nPos];
293 //.........................................................................
294 } // namespace comphelper
295 //.........................................................................