1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: enumhelper.cxx,v $
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 //.........................................................................
39 //.........................................................................
41 //==================================================================
42 //= OEnumerationByName
43 //==================================================================
44 //------------------------------------------------------------------------------
45 OEnumerationByName::OEnumerationByName(const staruno::Reference
<starcontainer::XNameAccess
>& _rxAccess
)
46 :m_aNames(_rxAccess
->getElementNames())
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
)
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
)
81 impl_stopDisposeListening();
88 //------------------------------------------------------------------------------
89 staruno::Any SAL_CALL
OEnumerationByName::nextElement( )
90 throw(starcontainer::NoSuchElementException
, starlang::WrappedTargetException
, staruno::RuntimeException
)
92 ::osl::ResettableMutexGuard
aLock(m_aLock
);
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();
104 if (!aRes
.hasValue()) // es gibt kein Element mehr
105 throw starcontainer::NoSuchElementException();
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
)
120 //------------------------------------------------------------------------------
121 void OEnumerationByName::impl_startDisposeListening()
123 ::osl::ResettableMutexGuard
aLock(m_aLock
);
129 staruno::Reference
< starlang::XComponent
> xDisposable(m_xAccess
, staruno::UNO_QUERY
);
130 if (xDisposable
.is())
132 xDisposable
->addEventListener(this);
133 m_bListening
= sal_True
;
138 //------------------------------------------------------------------------------
139 void OEnumerationByName::impl_stopDisposeListening()
141 ::osl::ResettableMutexGuard
aLock(m_aLock
);
147 staruno::Reference
< starlang::XComponent
> xDisposable(m_xAccess
, staruno::UNO_QUERY
);
148 if (xDisposable
.is())
150 xDisposable
->removeEventListener(this);
151 m_bListening
= sal_False
;
156 //==================================================================
157 //= OEnumerationByIndex
158 //==================================================================
159 //------------------------------------------------------------------------------
160 OEnumerationByIndex::OEnumerationByIndex(const staruno::Reference
< starcontainer::XIndexAccess
>& _rxAccess
)
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
)
184 impl_stopDisposeListening();
191 //------------------------------------------------------------------------------
192 staruno::Any SAL_CALL
OEnumerationByIndex::nextElement( )
193 throw(starcontainer::NoSuchElementException
, starlang::WrappedTargetException
, staruno::RuntimeException
)
195 ::osl::ResettableMutexGuard
aLock(m_aLock
);
200 aRes
= m_xAccess
->getByIndex(m_nPos
++);
201 if (m_nPos
>= m_xAccess
->getCount())
203 impl_stopDisposeListening();
208 if (!aRes
.hasValue()) // es gibt kein Element mehr
209 throw starcontainer::NoSuchElementException();
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
)
223 //------------------------------------------------------------------------------
224 void OEnumerationByIndex::impl_startDisposeListening()
226 ::osl::ResettableMutexGuard
aLock(m_aLock
);
232 staruno::Reference
< starlang::XComponent
> xDisposable(m_xAccess
, staruno::UNO_QUERY
);
233 if (xDisposable
.is())
235 xDisposable
->addEventListener(this);
236 m_bListening
= sal_True
;
241 //------------------------------------------------------------------------------
242 void OEnumerationByIndex::impl_stopDisposeListening()
244 ::osl::ResettableMutexGuard
aLock(m_aLock
);
250 staruno::Reference
< starlang::XComponent
> xDisposable(m_xAccess
, staruno::UNO_QUERY
);
251 if (xDisposable
.is())
253 xDisposable
->removeEventListener(this);
254 m_bListening
= sal_False
;
259 //==================================================================
261 //==================================================================
263 //------------------------------------------------------------------------------
264 OAnyEnumeration::OAnyEnumeration(const staruno::Sequence
< staruno::Any
>& 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
;
293 return m_lItems
[nPos
];
296 //.........................................................................
297 } // namespace comphelper
298 //.........................................................................