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 
25 namespace comphelper
26 {
27 
28 OEnumerationByName::OEnumerationByName(const css::uno::Reference<css::container::XNameAccess>& _rxAccess)
29  :m_aNames(_rxAccess->getElementNames())
30  ,m_xAccess(_rxAccess)
31  ,m_nPos(0)
32  ,m_bListening(false)
33 {
35 }
36 
37 
38 OEnumerationByName::OEnumerationByName(const css::uno::Reference<css::container::XNameAccess>& _rxAccess,
39  const css::uno::Sequence< OUString >& _aNames )
40  :m_aNames(_aNames)
41  ,m_xAccess(_rxAccess)
42  ,m_nPos(0)
43  ,m_bListening(false)
44 {
46 }
47 
48 
50 {
52 }
53 
54 
56 {
57  osl::MutexGuard aLock(m_aLock);
58 
59  if (m_xAccess.is() && m_aNames.getLength() > m_nPos)
60  return true;
61 
62  if (m_xAccess.is())
63  {
65  m_xAccess.clear();
66  }
67 
68  return false;
69 }
70 
71 
72 css::uno::Any SAL_CALL OEnumerationByName::nextElement( )
73 {
74  osl::MutexGuard aLock(m_aLock);
75 
76  css::uno::Any aRes;
77  if (m_xAccess.is() && m_nPos < m_aNames.getLength())
78  aRes = m_xAccess->getByName(m_aNames.getConstArray()[m_nPos++]);
79 
80  if (m_xAccess.is() && m_nPos >= m_aNames.getLength())
81  {
83  m_xAccess.clear();
84  }
85 
86  if (!aRes.hasValue()) //There are no more elements
87  throw css::container::NoSuchElementException();
88 
89  return aRes;
90 }
91 
92 
93 void SAL_CALL OEnumerationByName::disposing(const css::lang::EventObject& aEvent)
94 {
95  osl::MutexGuard aLock(m_aLock);
96 
97  if (aEvent.Source == m_xAccess)
98  m_xAccess.clear();
99 }
100 
101 
103 {
104  osl::MutexGuard aLock(m_aLock);
105 
106  if (m_bListening)
107  return;
108 
109  osl_atomic_increment(&m_refCount);
110  css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
111  if (xDisposable.is())
112  {
113  xDisposable->addEventListener(this);
114  m_bListening = true;
115  }
116  osl_atomic_decrement(&m_refCount);
117 }
118 
119 
121 {
122  osl::MutexGuard aLock(m_aLock);
123 
124  if (!m_bListening)
125  return;
126 
127  osl_atomic_increment(&m_refCount);
128  css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
129  if (xDisposable.is())
130  {
131  xDisposable->removeEventListener(this);
132  m_bListening = false;
133  }
134  osl_atomic_decrement(&m_refCount);
135 }
136 
137 OEnumerationByIndex::OEnumerationByIndex(const css::uno::Reference< css::container::XIndexAccess >& _rxAccess)
138  :m_xAccess(_rxAccess)
139  ,m_nPos(0)
140  ,m_bListening(false)
141 {
143 }
144 
145 
147 {
149 }
150 
151 
153 {
154  osl::MutexGuard aLock(m_aLock);
155 
156  if (m_xAccess.is() && m_xAccess->getCount() > m_nPos)
157  return true;
158 
159  if (m_xAccess.is())
160  {
162  m_xAccess.clear();
163  }
164 
165  return false;
166 }
167 
168 
169 css::uno::Any SAL_CALL OEnumerationByIndex::nextElement( )
170 {
171  osl::MutexGuard aLock(m_aLock);
172 
173  css::uno::Any aRes;
174  if (m_xAccess.is() && m_nPos < m_xAccess->getCount())
175  aRes = m_xAccess->getByIndex(m_nPos++);
176 
177  if (m_xAccess.is() && m_nPos >= m_xAccess->getCount())
178  {
180  m_xAccess.clear();
181  }
182 
183  if (!aRes.hasValue())
184  throw css::container::NoSuchElementException();
185  return aRes;
186 }
187 
188 
189 void SAL_CALL OEnumerationByIndex::disposing(const css::lang::EventObject& aEvent)
190 {
191  osl::MutexGuard aLock(m_aLock);
192 
193  if (aEvent.Source == m_xAccess)
194  m_xAccess.clear();
195 }
196 
197 
199 {
200  osl::MutexGuard aLock(m_aLock);
201 
202  if (m_bListening)
203  return;
204 
205  osl_atomic_increment(&m_refCount);
206  css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
207  if (xDisposable.is())
208  {
209  xDisposable->addEventListener(this);
210  m_bListening = true;
211  }
212  osl_atomic_decrement(&m_refCount);
213 }
214 
215 
217 {
218  osl::MutexGuard aLock(m_aLock);
219 
220  if (!m_bListening)
221  return;
222 
223  osl_atomic_increment(&m_refCount);
224  css::uno::Reference< css::lang::XComponent > xDisposable(m_xAccess, css::uno::UNO_QUERY);
225  if (xDisposable.is())
226  {
227  xDisposable->removeEventListener(this);
228  m_bListening = false;
229  }
230  osl_atomic_decrement(&m_refCount);
231 }
232 
233 OAnyEnumeration::OAnyEnumeration(const css::uno::Sequence< css::uno::Any >& lItems)
234  :m_nPos(0)
235  ,m_lItems(lItems)
236 {
237 }
238 
239 
241 {
242 }
243 
244 
246 {
247  osl::MutexGuard aLock(m_aLock);
248 
249  return (m_lItems.getLength() > m_nPos);
250 }
251 
252 
253 css::uno::Any SAL_CALL OAnyEnumeration::nextElement( )
254 {
255  if ( ! hasMoreElements())
256  throw css::container::NoSuchElementException();
257 
258  osl::MutexGuard aLock(m_aLock);
259  sal_Int32 nPos = m_nPos;
260  ++m_nPos;
261  return m_lItems[nPos];
262 }
263 
264 
265 } // namespace comphelper
266 
267 
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
size_t m_nPos
ULONG m_refCount
virtual void SAL_CALL disposing(const css::lang::EventObject &aEvent) override
Definition: enumhelper.cxx:93
virtual ~OEnumerationByIndex() override
Definition: enumhelper.cxx:146
virtual css::uno::Any SAL_CALL nextElement() override
Definition: enumhelper.cxx:169
virtual css::uno::Any SAL_CALL nextElement() override
Definition: enumhelper.cxx:253
virtual ~OEnumerationByName() override
Definition: enumhelper.cxx:49
virtual void SAL_CALL disposing(const css::lang::EventObject &aEvent) override
Definition: enumhelper.cxx:189
COMPHELPER_DLLPRIVATE void impl_startDisposeListening()
Definition: enumhelper.cxx:102
Mutex aLock
virtual css::uno::Any SAL_CALL nextElement() override
Definition: enumhelper.cxx:72
unsigned char sal_Bool
COMPHELPER_DLLPRIVATE void impl_stopDisposeListening()
Definition: enumhelper.cxx:216
COMPHELPER_DLLPRIVATE void impl_startDisposeListening()
Definition: enumhelper.cxx:198
OEnumerationByIndex(const css::uno::Reference< css::container::XIndexAccess > &_rxAccess)
Definition: enumhelper.cxx:137
css::uno::Reference< css::container::XNameAccess > m_xAccess
Definition: enumhelper.hxx:49
OAnyEnumeration(const css::uno::Sequence< css::uno::Any > &lItems)
Definition: enumhelper.cxx:233
css::uno::Sequence< css::uno::Any > m_lItems
Definition: enumhelper.hxx:106
virtual sal_Bool SAL_CALL hasMoreElements() override
Definition: enumhelper.cxx:55
COMPHELPER_DLLPRIVATE void impl_stopDisposeListening()
Definition: enumhelper.cxx:120
virtual sal_Bool SAL_CALL hasMoreElements() override
Definition: enumhelper.cxx:152
css::uno::Sequence< OUString > const m_aNames
Definition: enumhelper.hxx:48
virtual ~OAnyEnumeration() override
Definition: enumhelper.cxx:240
css::uno::Reference< css::container::XIndexAccess > m_xAccess
Definition: enumhelper.hxx:76
OEnumerationByName(const css::uno::Reference< css::container::XNameAccess > &_rxAccess)
Definition: enumhelper.cxx:28
virtual sal_Bool SAL_CALL hasMoreElements() override
Definition: enumhelper.cxx:245
AnyEventRef aEvent
sal_uInt16 nPos