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>
22 #include <com/sun/star/container/XIndexAccess.hpp>
23 #include <com/sun/star/container/XNameAccess.hpp>
29 OEnumerationByName::OEnumerationByName(css::uno::Reference
<css::container::XNameAccess
> _xAccess
)
30 :m_aNames(_xAccess
->getElementNames())
35 impl_startDisposeListening();
39 OEnumerationByName::OEnumerationByName(css::uno::Reference
<css::container::XNameAccess
> _xAccess
,
40 std::vector
<OUString
> _aNames
)
41 :m_aNames(std::move(_aNames
))
42 ,m_xAccess(std::move(_xAccess
))
46 impl_startDisposeListening();
49 OEnumerationByName::~OEnumerationByName()
51 std::lock_guard
aLock(m_aLock
);
53 impl_stopDisposeListening();
57 sal_Bool SAL_CALL
OEnumerationByName::hasMoreElements( )
59 std::lock_guard
aLock(m_aLock
);
61 if (m_xAccess
.is() && getLength() > m_nPos
)
66 impl_stopDisposeListening();
74 css::uno::Any SAL_CALL
OEnumerationByName::nextElement( )
76 std::lock_guard
aLock(m_aLock
);
79 if (m_xAccess
.is() && m_nPos
< getLength())
80 aRes
= m_xAccess
->getByName(getElement(m_nPos
++));
82 if (m_xAccess
.is() && m_nPos
>= getLength())
84 impl_stopDisposeListening();
88 if (!aRes
.hasValue()) //There are no more elements
89 throw css::container::NoSuchElementException();
94 void SAL_CALL
OEnumerationByName::disposing(const css::lang::EventObject
& aEvent
)
96 std::lock_guard
aLock(m_aLock
);
98 if (aEvent
.Source
== m_xAccess
)
103 void OEnumerationByName::impl_startDisposeListening()
108 osl_atomic_increment(&m_refCount
);
109 css::uno::Reference
< css::lang::XComponent
> xDisposable(m_xAccess
, css::uno::UNO_QUERY
);
110 if (xDisposable
.is())
112 xDisposable
->addEventListener(this);
115 osl_atomic_decrement(&m_refCount
);
119 void OEnumerationByName::impl_stopDisposeListening()
124 osl_atomic_increment(&m_refCount
);
125 css::uno::Reference
< css::lang::XComponent
> xDisposable(m_xAccess
, css::uno::UNO_QUERY
);
126 if (xDisposable
.is())
128 xDisposable
->removeEventListener(this);
129 m_bListening
= false;
131 osl_atomic_decrement(&m_refCount
);
134 sal_Int32
OEnumerationByName::getLength() const
136 if (m_aNames
.index() == 0)
137 return std::get
<css::uno::Sequence
<OUString
>>(m_aNames
).getLength();
139 return std::get
<std::vector
<OUString
>>(m_aNames
).size();
142 const OUString
& OEnumerationByName::getElement(sal_Int32 nIndex
) const
144 if (m_aNames
.index() == 0)
145 return std::get
<css::uno::Sequence
<OUString
>>(m_aNames
).getConstArray()[nIndex
];
147 return std::get
<std::vector
<OUString
>>(m_aNames
)[nIndex
];
151 OEnumerationByIndex::OEnumerationByIndex(css::uno::Reference
< css::container::XIndexAccess
> _xAccess
)
152 :m_xAccess(std::move(_xAccess
))
156 impl_startDisposeListening();
160 OEnumerationByIndex::~OEnumerationByIndex()
162 std::lock_guard
aLock(m_aLock
);
164 impl_stopDisposeListening();
168 sal_Bool SAL_CALL
OEnumerationByIndex::hasMoreElements( )
170 std::lock_guard
aLock(m_aLock
);
172 if (m_xAccess
.is() && m_xAccess
->getCount() > m_nPos
)
177 impl_stopDisposeListening();
185 css::uno::Any SAL_CALL
OEnumerationByIndex::nextElement( )
187 std::lock_guard
aLock(m_aLock
);
190 if (m_xAccess
.is() && m_nPos
< m_xAccess
->getCount())
191 aRes
= m_xAccess
->getByIndex(m_nPos
++);
193 if (m_xAccess
.is() && m_nPos
>= m_xAccess
->getCount())
195 impl_stopDisposeListening();
199 if (!aRes
.hasValue())
200 throw css::container::NoSuchElementException();
205 void SAL_CALL
OEnumerationByIndex::disposing(const css::lang::EventObject
& aEvent
)
207 std::lock_guard
aLock(m_aLock
);
209 if (aEvent
.Source
== m_xAccess
)
214 void OEnumerationByIndex::impl_startDisposeListening()
219 osl_atomic_increment(&m_refCount
);
220 css::uno::Reference
< css::lang::XComponent
> xDisposable(m_xAccess
, css::uno::UNO_QUERY
);
221 if (xDisposable
.is())
223 xDisposable
->addEventListener(this);
226 osl_atomic_decrement(&m_refCount
);
230 void OEnumerationByIndex::impl_stopDisposeListening()
235 osl_atomic_increment(&m_refCount
);
236 css::uno::Reference
< css::lang::XComponent
> xDisposable(m_xAccess
, css::uno::UNO_QUERY
);
237 if (xDisposable
.is())
239 xDisposable
->removeEventListener(this);
240 m_bListening
= false;
242 osl_atomic_decrement(&m_refCount
);
245 OAnyEnumeration::OAnyEnumeration(const css::uno::Sequence
< css::uno::Any
>& lItems
)
252 OAnyEnumeration::~OAnyEnumeration()
257 sal_Bool SAL_CALL
OAnyEnumeration::hasMoreElements( )
259 std::lock_guard
aLock(m_aLock
);
261 return (m_lItems
.getLength() > m_nPos
);
265 css::uno::Any SAL_CALL
OAnyEnumeration::nextElement( )
267 if ( ! hasMoreElements())
268 throw css::container::NoSuchElementException();
270 std::lock_guard
aLock(m_aLock
);
271 sal_Int32 nPos
= m_nPos
;
273 return m_lItems
[nPos
];
277 } // namespace comphelper
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */