LibreOffice Module comphelper (master) 1
enumhelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21#include <com/sun/star/lang/XComponent.hpp>
22#include <com/sun/star/container/XIndexAccess.hpp>
23#include <com/sun/star/container/XNameAccess.hpp>
24#include <utility>
25
26namespace comphelper
27{
28
29OEnumerationByName::OEnumerationByName(css::uno::Reference<css::container::XNameAccess> _xAccess)
30 :m_aNames(_xAccess->getElementNames())
31 ,m_xAccess(_xAccess)
32 ,m_nPos(0)
33 ,m_bListening(false)
34{
36}
37
38
39OEnumerationByName::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))
43 ,m_nPos(0)
44 ,m_bListening(false)
45{
47}
48
50{
51 std::lock_guard aLock(m_aLock);
52
54}
55
56
58{
59 std::lock_guard aLock(m_aLock);
60
61 if (m_xAccess.is() && getLength() > m_nPos)
62 return true;
63
64 if (m_xAccess.is())
65 {
67 m_xAccess.clear();
68 }
69
70 return false;
71}
72
73
74css::uno::Any SAL_CALL OEnumerationByName::nextElement( )
75{
76 std::lock_guard aLock(m_aLock);
77
78 css::uno::Any aRes;
79 if (m_xAccess.is() && m_nPos < getLength())
80 aRes = m_xAccess->getByName(getElement(m_nPos++));
81
82 if (m_xAccess.is() && m_nPos >= getLength())
83 {
85 m_xAccess.clear();
86 }
87
88 if (!aRes.hasValue()) //There are no more elements
89 throw css::container::NoSuchElementException();
90
91 return aRes;
92}
93
94void SAL_CALL OEnumerationByName::disposing(const css::lang::EventObject& aEvent)
95{
96 std::lock_guard aLock(m_aLock);
97
98 if (aEvent.Source == m_xAccess)
99 m_xAccess.clear();
100}
101
102
104{
105 if (m_bListening)
106 return;
107
108 osl_atomic_increment(&m_refCount);
109 css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
110 if (xDisposable.is())
111 {
112 xDisposable->addEventListener(this);
113 m_bListening = true;
114 }
115 osl_atomic_decrement(&m_refCount);
116}
117
118
120{
121 if (!m_bListening)
122 return;
123
124 osl_atomic_increment(&m_refCount);
125 css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
126 if (xDisposable.is())
127 {
128 xDisposable->removeEventListener(this);
129 m_bListening = false;
130 }
131 osl_atomic_decrement(&m_refCount);
132}
133
135{
136 if (m_aNames.index() == 0)
137 return std::get<css::uno::Sequence<OUString>>(m_aNames).getLength();
138 else
139 return std::get<std::vector<OUString>>(m_aNames).size();
140}
141
142const OUString& OEnumerationByName::getElement(sal_Int32 nIndex) const
143{
144 if (m_aNames.index() == 0)
145 return std::get<css::uno::Sequence<OUString>>(m_aNames).getConstArray()[nIndex];
146 else
147 return std::get<std::vector<OUString>>(m_aNames)[nIndex];
148}
149
150
151OEnumerationByIndex::OEnumerationByIndex(css::uno::Reference< css::container::XIndexAccess > _xAccess)
152 :m_xAccess(std::move(_xAccess))
153 ,m_nPos(0)
154 ,m_bListening(false)
155{
157}
158
159
161{
162 std::lock_guard aLock(m_aLock);
163
165}
166
167
169{
170 std::lock_guard aLock(m_aLock);
171
172 if (m_xAccess.is() && m_xAccess->getCount() > m_nPos)
173 return true;
174
175 if (m_xAccess.is())
176 {
178 m_xAccess.clear();
179 }
180
181 return false;
182}
183
184
185css::uno::Any SAL_CALL OEnumerationByIndex::nextElement( )
186{
187 std::lock_guard aLock(m_aLock);
188
189 css::uno::Any aRes;
190 if (m_xAccess.is() && m_nPos < m_xAccess->getCount())
191 aRes = m_xAccess->getByIndex(m_nPos++);
192
193 if (m_xAccess.is() && m_nPos >= m_xAccess->getCount())
194 {
196 m_xAccess.clear();
197 }
198
199 if (!aRes.hasValue())
200 throw css::container::NoSuchElementException();
201 return aRes;
202}
203
204
205void SAL_CALL OEnumerationByIndex::disposing(const css::lang::EventObject& aEvent)
206{
207 std::lock_guard aLock(m_aLock);
208
209 if (aEvent.Source == m_xAccess)
210 m_xAccess.clear();
211}
212
213
215{
216 if (m_bListening)
217 return;
218
219 osl_atomic_increment(&m_refCount);
220 css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
221 if (xDisposable.is())
222 {
223 xDisposable->addEventListener(this);
224 m_bListening = true;
225 }
226 osl_atomic_decrement(&m_refCount);
227}
228
229
231{
232 if (!m_bListening)
233 return;
234
235 osl_atomic_increment(&m_refCount);
236 css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
237 if (xDisposable.is())
238 {
239 xDisposable->removeEventListener(this);
240 m_bListening = false;
241 }
242 osl_atomic_decrement(&m_refCount);
243}
244
245OAnyEnumeration::OAnyEnumeration(const css::uno::Sequence< css::uno::Any >& lItems)
246 :m_nPos(0)
247 ,m_lItems(lItems)
248{
249}
250
251
253{
254}
255
256
258{
259 std::lock_guard aLock(m_aLock);
260
261 return (m_lItems.getLength() > m_nPos);
262}
263
264
265css::uno::Any SAL_CALL OAnyEnumeration::nextElement( )
266{
267 if ( ! hasMoreElements())
268 throw css::container::NoSuchElementException();
269
270 std::lock_guard aLock(m_aLock);
271 sal_Int32 nPos = m_nPos;
272 ++m_nPos;
273 return m_lItems[nPos];
274}
275
276
277} // namespace comphelper
278
279
280/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
OAnyEnumeration(const css::uno::Sequence< css::uno::Any > &lItems)
Definition: enumhelper.cxx:245
virtual ~OAnyEnumeration() override
Definition: enumhelper.cxx:252
virtual css::uno::Any SAL_CALL nextElement() override
Definition: enumhelper.cxx:265
virtual sal_Bool SAL_CALL hasMoreElements() override
Definition: enumhelper.cxx:257
css::uno::Sequence< css::uno::Any > m_lItems
Definition: enumhelper.hxx:106
virtual ~OEnumerationByIndex() override
Definition: enumhelper.cxx:160
css::uno::Reference< css::container::XIndexAccess > m_xAccess
Definition: enumhelper.hxx:75
virtual void SAL_CALL disposing(const css::lang::EventObject &aEvent) override
Definition: enumhelper.cxx:205
COMPHELPER_DLLPRIVATE void impl_startDisposeListening()
Definition: enumhelper.cxx:214
virtual sal_Bool SAL_CALL hasMoreElements() override
Definition: enumhelper.cxx:168
OEnumerationByIndex(css::uno::Reference< css::container::XIndexAccess > _xAccess)
Definition: enumhelper.cxx:151
virtual css::uno::Any SAL_CALL nextElement() override
Definition: enumhelper.cxx:185
COMPHELPER_DLLPRIVATE void impl_stopDisposeListening()
Definition: enumhelper.cxx:230
COMPHELPER_DLLPRIVATE void impl_stopDisposeListening()
Definition: enumhelper.cxx:119
virtual void SAL_CALL disposing(const css::lang::EventObject &aEvent) override
Definition: enumhelper.cxx:94
virtual ~OEnumerationByName() override
Definition: enumhelper.cxx:49
virtual sal_Bool SAL_CALL hasMoreElements() override
Definition: enumhelper.cxx:57
std::variant< css::uno::Sequence< OUString >, std::vector< OUString > > m_aNames
Definition: enumhelper.hxx:44
virtual css::uno::Any SAL_CALL nextElement() override
Definition: enumhelper.cxx:74
css::uno::Reference< css::container::XNameAccess > m_xAccess
Definition: enumhelper.hxx:45
COMPHELPER_DLLPRIVATE void impl_startDisposeListening()
Definition: enumhelper.cxx:103
OEnumerationByName(css::uno::Reference< css::container::XNameAccess > _xAccess)
Definition: enumhelper.cxx:29
const OUString & getElement(sal_Int32 nIndex) const
Definition: enumhelper.cxx:142
ULONG m_refCount
size_t m_nPos
sal_Int32 nIndex
sal_uInt16 nPos
size
unsigned char sal_Bool