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>
29 //= OEnumerationByName
32 OEnumerationByName::OEnumerationByName(const staruno::Reference
<starcontainer::XNameAccess
>& _rxAccess
)
33 :m_aNames(_rxAccess
->getElementNames())
38 impl_startDisposeListening();
42 OEnumerationByName::OEnumerationByName(const staruno::Reference
<starcontainer::XNameAccess
>& _rxAccess
,
43 const staruno::Sequence
< OUString
>& _aNames
)
49 impl_startDisposeListening();
53 OEnumerationByName::~OEnumerationByName()
55 impl_stopDisposeListening();
59 sal_Bool SAL_CALL
OEnumerationByName::hasMoreElements( ) throw(staruno::RuntimeException
, std::exception
)
61 ::osl::ResettableMutexGuard
aLock(m_aLock
);
63 if (m_xAccess
.is() && m_aNames
.getLength() > m_nPos
)
68 impl_stopDisposeListening();
76 staruno::Any SAL_CALL
OEnumerationByName::nextElement( )
77 throw(starcontainer::NoSuchElementException
, starlang::WrappedTargetException
, staruno::RuntimeException
, std::exception
)
79 ::osl::ResettableMutexGuard
aLock(m_aLock
);
82 if (m_xAccess
.is() && m_nPos
< m_aNames
.getLength())
83 aRes
= m_xAccess
->getByName(m_aNames
.getConstArray()[m_nPos
++]);
85 if (m_xAccess
.is() && m_nPos
>= m_aNames
.getLength())
87 impl_stopDisposeListening();
91 if (!aRes
.hasValue()) //There are no more elements
92 throw starcontainer::NoSuchElementException();
98 void SAL_CALL
OEnumerationByName::disposing(const starlang::EventObject
& aEvent
)
99 throw(staruno::RuntimeException
, std::exception
)
101 ::osl::ResettableMutexGuard
aLock(m_aLock
);
103 if (aEvent
.Source
== m_xAccess
)
108 void OEnumerationByName::impl_startDisposeListening()
110 ::osl::ResettableMutexGuard
aLock(m_aLock
);
116 staruno::Reference
< starlang::XComponent
> xDisposable(m_xAccess
, staruno::UNO_QUERY
);
117 if (xDisposable
.is())
119 xDisposable
->addEventListener(this);
126 void OEnumerationByName::impl_stopDisposeListening()
128 ::osl::ResettableMutexGuard
aLock(m_aLock
);
134 staruno::Reference
< starlang::XComponent
> xDisposable(m_xAccess
, staruno::UNO_QUERY
);
135 if (xDisposable
.is())
137 xDisposable
->removeEventListener(this);
138 m_bListening
= false;
144 //= OEnumerationByIndex
147 OEnumerationByIndex::OEnumerationByIndex(const staruno::Reference
< starcontainer::XIndexAccess
>& _rxAccess
)
149 ,m_xAccess(_rxAccess
)
152 impl_startDisposeListening();
156 OEnumerationByIndex::~OEnumerationByIndex()
158 impl_stopDisposeListening();
162 sal_Bool SAL_CALL
OEnumerationByIndex::hasMoreElements( ) throw(staruno::RuntimeException
, std::exception
)
164 ::osl::ResettableMutexGuard
aLock(m_aLock
);
166 if (m_xAccess
.is() && m_xAccess
->getCount() > m_nPos
)
171 impl_stopDisposeListening();
179 staruno::Any SAL_CALL
OEnumerationByIndex::nextElement( )
180 throw(starcontainer::NoSuchElementException
, starlang::WrappedTargetException
, staruno::RuntimeException
, std::exception
)
182 ::osl::ResettableMutexGuard
aLock(m_aLock
);
187 aRes
= m_xAccess
->getByIndex(m_nPos
++);
188 if (m_nPos
>= m_xAccess
->getCount())
190 impl_stopDisposeListening();
195 if (!aRes
.hasValue())
196 throw starcontainer::NoSuchElementException();
201 void SAL_CALL
OEnumerationByIndex::disposing(const starlang::EventObject
& aEvent
)
202 throw(staruno::RuntimeException
, std::exception
)
204 ::osl::ResettableMutexGuard
aLock(m_aLock
);
206 if (aEvent
.Source
== m_xAccess
)
211 void OEnumerationByIndex::impl_startDisposeListening()
213 ::osl::ResettableMutexGuard
aLock(m_aLock
);
219 staruno::Reference
< starlang::XComponent
> xDisposable(m_xAccess
, staruno::UNO_QUERY
);
220 if (xDisposable
.is())
222 xDisposable
->addEventListener(this);
229 void OEnumerationByIndex::impl_stopDisposeListening()
231 ::osl::ResettableMutexGuard
aLock(m_aLock
);
237 staruno::Reference
< starlang::XComponent
> xDisposable(m_xAccess
, staruno::UNO_QUERY
);
238 if (xDisposable
.is())
240 xDisposable
->removeEventListener(this);
241 m_bListening
= false;
251 OAnyEnumeration::OAnyEnumeration(const staruno::Sequence
< staruno::Any
>& lItems
)
258 OAnyEnumeration::~OAnyEnumeration()
263 sal_Bool SAL_CALL
OAnyEnumeration::hasMoreElements( ) throw(staruno::RuntimeException
, std::exception
)
265 ::osl::ResettableMutexGuard
aLock(m_aLock
);
267 return (m_lItems
.getLength() > m_nPos
);
271 staruno::Any SAL_CALL
OAnyEnumeration::nextElement( )
272 throw(starcontainer::NoSuchElementException
, starlang::WrappedTargetException
, staruno::RuntimeException
, std::exception
)
274 if ( ! hasMoreElements())
275 throw starcontainer::NoSuchElementException();
277 ::osl::ResettableMutexGuard
aLock(m_aLock
);
278 sal_Int32 nPos
= m_nPos
;
280 return m_lItems
[nPos
];
284 } // namespace comphelper
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */