update dev300-m58
[ooovba.git] / comphelper / source / container / enumhelper.cxx
blob3c952b9f7dbc4be6c0de8e51202437311153f3ce
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: enumhelper.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <comphelper/enumhelper.hxx>
34 #include <com/sun/star/lang/XComponent.hpp>
36 //.........................................................................
37 namespace comphelper
39 //.........................................................................
41 //==================================================================
42 //= OEnumerationByName
43 //==================================================================
44 //------------------------------------------------------------------------------
45 OEnumerationByName::OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess>& _rxAccess)
46 :m_aNames(_rxAccess->getElementNames())
47 ,m_nPos(0)
48 ,m_xAccess(_rxAccess)
49 ,m_bListening(sal_False)
51 impl_startDisposeListening();
54 //------------------------------------------------------------------------------
55 OEnumerationByName::OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess>& _rxAccess,
56 const staruno::Sequence< ::rtl::OUString >& _aNames )
57 :m_aNames(_aNames)
58 ,m_nPos(0)
59 ,m_xAccess(_rxAccess)
60 ,m_bListening(sal_False)
62 impl_startDisposeListening();
65 //------------------------------------------------------------------------------
66 OEnumerationByName::~OEnumerationByName()
68 impl_stopDisposeListening();
71 //------------------------------------------------------------------------------
72 sal_Bool SAL_CALL OEnumerationByName::hasMoreElements( ) throw(staruno::RuntimeException)
74 ::osl::ResettableMutexGuard aLock(m_aLock);
76 if (m_xAccess.is() && m_aNames.getLength() > m_nPos)
77 return sal_True;
79 if (m_xAccess.is())
81 impl_stopDisposeListening();
82 m_xAccess.clear();
85 return sal_False;
88 //------------------------------------------------------------------------------
89 staruno::Any SAL_CALL OEnumerationByName::nextElement( )
90 throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
92 ::osl::ResettableMutexGuard aLock(m_aLock);
94 staruno::Any aRes;
95 if (m_xAccess.is() && m_nPos < m_aNames.getLength())
96 aRes = m_xAccess->getByName(m_aNames.getConstArray()[m_nPos++]);
98 if (m_xAccess.is() && m_nPos >= m_aNames.getLength())
100 impl_stopDisposeListening();
101 m_xAccess.clear();
104 if (!aRes.hasValue()) // es gibt kein Element mehr
105 throw starcontainer::NoSuchElementException();
107 return aRes;
110 //------------------------------------------------------------------------------
111 void SAL_CALL OEnumerationByName::disposing(const starlang::EventObject& aEvent)
112 throw(staruno::RuntimeException)
114 ::osl::ResettableMutexGuard aLock(m_aLock);
116 if (aEvent.Source == m_xAccess)
117 m_xAccess.clear();
120 //------------------------------------------------------------------------------
121 void OEnumerationByName::impl_startDisposeListening()
123 ::osl::ResettableMutexGuard aLock(m_aLock);
125 if (m_bListening)
126 return;
128 ++m_refCount;
129 staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
130 if (xDisposable.is())
132 xDisposable->addEventListener(this);
133 m_bListening = sal_True;
135 --m_refCount;
138 //------------------------------------------------------------------------------
139 void OEnumerationByName::impl_stopDisposeListening()
141 ::osl::ResettableMutexGuard aLock(m_aLock);
143 if (!m_bListening)
144 return;
146 ++m_refCount;
147 staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
148 if (xDisposable.is())
150 xDisposable->removeEventListener(this);
151 m_bListening = sal_False;
153 --m_refCount;
156 //==================================================================
157 //= OEnumerationByIndex
158 //==================================================================
159 //------------------------------------------------------------------------------
160 OEnumerationByIndex::OEnumerationByIndex(const staruno::Reference< starcontainer::XIndexAccess >& _rxAccess)
161 :m_nPos(0)
162 ,m_xAccess(_rxAccess)
163 ,m_bListening(sal_False)
165 impl_startDisposeListening();
168 //------------------------------------------------------------------------------
169 OEnumerationByIndex::~OEnumerationByIndex()
171 impl_stopDisposeListening();
174 //------------------------------------------------------------------------------
175 sal_Bool SAL_CALL OEnumerationByIndex::hasMoreElements( ) throw(staruno::RuntimeException)
177 ::osl::ResettableMutexGuard aLock(m_aLock);
179 if (m_xAccess.is() && m_xAccess->getCount() > m_nPos)
180 return sal_True;
182 if (m_xAccess.is())
184 impl_stopDisposeListening();
185 m_xAccess.clear();
188 return sal_False;
191 //------------------------------------------------------------------------------
192 staruno::Any SAL_CALL OEnumerationByIndex::nextElement( )
193 throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
195 ::osl::ResettableMutexGuard aLock(m_aLock);
197 staruno::Any aRes;
198 if (m_xAccess.is())
200 aRes = m_xAccess->getByIndex(m_nPos++);
201 if (m_nPos >= m_xAccess->getCount())
203 impl_stopDisposeListening();
204 m_xAccess.clear();
208 if (!aRes.hasValue()) // es gibt kein Element mehr
209 throw starcontainer::NoSuchElementException();
210 return aRes;
213 //------------------------------------------------------------------------------
214 void SAL_CALL OEnumerationByIndex::disposing(const starlang::EventObject& aEvent)
215 throw(staruno::RuntimeException)
217 ::osl::ResettableMutexGuard aLock(m_aLock);
219 if (aEvent.Source == m_xAccess)
220 m_xAccess.clear();
223 //------------------------------------------------------------------------------
224 void OEnumerationByIndex::impl_startDisposeListening()
226 ::osl::ResettableMutexGuard aLock(m_aLock);
228 if (m_bListening)
229 return;
231 ++m_refCount;
232 staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
233 if (xDisposable.is())
235 xDisposable->addEventListener(this);
236 m_bListening = sal_True;
238 --m_refCount;
241 //------------------------------------------------------------------------------
242 void OEnumerationByIndex::impl_stopDisposeListening()
244 ::osl::ResettableMutexGuard aLock(m_aLock);
246 if (!m_bListening)
247 return;
249 ++m_refCount;
250 staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
251 if (xDisposable.is())
253 xDisposable->removeEventListener(this);
254 m_bListening = sal_False;
256 --m_refCount;
259 //==================================================================
260 //= OAnyEnumeration
261 //==================================================================
263 //------------------------------------------------------------------------------
264 OAnyEnumeration::OAnyEnumeration(const staruno::Sequence< staruno::Any >& lItems)
265 :m_nPos(0)
266 ,m_lItems(lItems)
270 //------------------------------------------------------------------------------
271 OAnyEnumeration::~OAnyEnumeration()
275 //------------------------------------------------------------------------------
276 sal_Bool SAL_CALL OAnyEnumeration::hasMoreElements( ) throw(staruno::RuntimeException)
278 ::osl::ResettableMutexGuard aLock(m_aLock);
280 return (m_lItems.getLength() > m_nPos);
283 //------------------------------------------------------------------------------
284 staruno::Any SAL_CALL OAnyEnumeration::nextElement( )
285 throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
287 if ( ! hasMoreElements())
288 throw starcontainer::NoSuchElementException();
290 ::osl::ResettableMutexGuard aLock(m_aLock);
291 sal_Int32 nPos = m_nPos;
292 ++m_nPos;
293 return m_lItems[nPos];
296 //.........................................................................
297 } // namespace comphelper
298 //.........................................................................