1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
27 OEnumerationByName::OEnumerationByName(const css::uno::Reference
<css::container::XNameAccess
>& _rxAccess
)
28 :m_aNames(_rxAccess
->getElementNames())
33 impl_startDisposeListening();
37 OEnumerationByName::OEnumerationByName(const css::uno::Reference
<css::container::XNameAccess
>& _rxAccess
,
38 const css::uno::Sequence
< OUString
>& _aNames
)
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
)
63 impl_stopDisposeListening();
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
);
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();
86 if (!aRes
.hasValue()) //There are no more elements
87 throw css::container::NoSuchElementException();
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
)
103 void OEnumerationByName::impl_startDisposeListening()
105 ::osl::ResettableMutexGuard
aLock(m_aLock
);
111 css::uno::Reference
< css::lang::XComponent
> xDisposable(m_xAccess
, css::uno::UNO_QUERY
);
112 if (xDisposable
.is())
114 xDisposable
->addEventListener(this);
121 void OEnumerationByName::impl_stopDisposeListening()
123 ::osl::ResettableMutexGuard
aLock(m_aLock
);
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;
138 OEnumerationByIndex::OEnumerationByIndex(const css::uno::Reference
< css::container::XIndexAccess
>& _rxAccess
)
140 ,m_xAccess(_rxAccess
)
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
)
162 impl_stopDisposeListening();
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
);
178 aRes
= m_xAccess
->getByIndex(m_nPos
++);
179 if (m_nPos
>= m_xAccess
->getCount())
181 impl_stopDisposeListening();
186 if (!aRes
.hasValue())
187 throw css::container::NoSuchElementException();
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
)
202 void OEnumerationByIndex::impl_startDisposeListening()
204 ::osl::ResettableMutexGuard
aLock(m_aLock
);
210 css::uno::Reference
< css::lang::XComponent
> xDisposable(m_xAccess
, css::uno::UNO_QUERY
);
211 if (xDisposable
.is())
213 xDisposable
->addEventListener(this);
220 void OEnumerationByIndex::impl_stopDisposeListening()
222 ::osl::ResettableMutexGuard
aLock(m_aLock
);
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;
237 OAnyEnumeration::OAnyEnumeration(const css::uno::Sequence
< css::uno::Any
>& 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
;
266 return m_lItems
[nPos
];
270 } // namespace comphelper
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */