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 //.........................................................................
36 //.........................................................................
38 //==================================================================
39 //= OEnumerationByName
40 //==================================================================
41 //------------------------------------------------------------------------------
42 OEnumerationByName::OEnumerationByName(const staruno::Reference
<starcontainer::XNameAccess
>& _rxAccess
)
43 :m_aNames(_rxAccess
->getElementNames())
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
)
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
)
78 impl_stopDisposeListening();
85 //------------------------------------------------------------------------------
86 staruno::Any SAL_CALL
OEnumerationByName::nextElement( )
87 throw(starcontainer::NoSuchElementException
, starlang::WrappedTargetException
, staruno::RuntimeException
)
89 ::osl::ResettableMutexGuard
aLock(m_aLock
);
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();
101 if (!aRes
.hasValue()) // es gibt kein Element mehr
102 throw starcontainer::NoSuchElementException();
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
)
117 //------------------------------------------------------------------------------
118 void OEnumerationByName::impl_startDisposeListening()
120 ::osl::ResettableMutexGuard
aLock(m_aLock
);
126 staruno::Reference
< starlang::XComponent
> xDisposable(m_xAccess
, staruno::UNO_QUERY
);
127 if (xDisposable
.is())
129 xDisposable
->addEventListener(this);
130 m_bListening
= sal_True
;
135 //------------------------------------------------------------------------------
136 void OEnumerationByName::impl_stopDisposeListening()
138 ::osl::ResettableMutexGuard
aLock(m_aLock
);
144 staruno::Reference
< starlang::XComponent
> xDisposable(m_xAccess
, staruno::UNO_QUERY
);
145 if (xDisposable
.is())
147 xDisposable
->removeEventListener(this);
148 m_bListening
= sal_False
;
153 //==================================================================
154 //= OEnumerationByIndex
155 //==================================================================
156 //------------------------------------------------------------------------------
157 OEnumerationByIndex::OEnumerationByIndex(const staruno::Reference
< starcontainer::XIndexAccess
>& _rxAccess
)
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
)
181 impl_stopDisposeListening();
188 //------------------------------------------------------------------------------
189 staruno::Any SAL_CALL
OEnumerationByIndex::nextElement( )
190 throw(starcontainer::NoSuchElementException
, starlang::WrappedTargetException
, staruno::RuntimeException
)
192 ::osl::ResettableMutexGuard
aLock(m_aLock
);
197 aRes
= m_xAccess
->getByIndex(m_nPos
++);
198 if (m_nPos
>= m_xAccess
->getCount())
200 impl_stopDisposeListening();
205 if (!aRes
.hasValue()) // es gibt kein Element mehr
206 throw starcontainer::NoSuchElementException();
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
)
220 //------------------------------------------------------------------------------
221 void OEnumerationByIndex::impl_startDisposeListening()
223 ::osl::ResettableMutexGuard
aLock(m_aLock
);
229 staruno::Reference
< starlang::XComponent
> xDisposable(m_xAccess
, staruno::UNO_QUERY
);
230 if (xDisposable
.is())
232 xDisposable
->addEventListener(this);
233 m_bListening
= sal_True
;
238 //------------------------------------------------------------------------------
239 void OEnumerationByIndex::impl_stopDisposeListening()
241 ::osl::ResettableMutexGuard
aLock(m_aLock
);
247 staruno::Reference
< starlang::XComponent
> xDisposable(m_xAccess
, staruno::UNO_QUERY
);
248 if (xDisposable
.is())
250 xDisposable
->removeEventListener(this);
251 m_bListening
= sal_False
;
256 //==================================================================
258 //==================================================================
260 //------------------------------------------------------------------------------
261 OAnyEnumeration::OAnyEnumeration(const staruno::Sequence
< staruno::Any
>& 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
;
290 return m_lItems
[nPos
];
293 //.........................................................................
294 } // namespace comphelper
295 //.........................................................................