Branch data Line data Source code
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 : :
20 : : #include <comphelper/enumhelper.hxx>
21 : : #include <com/sun/star/lang/XComponent.hpp>
22 : :
23 : : //.........................................................................
24 : : namespace comphelper
25 : : {
26 : : //.........................................................................
27 : :
28 : : //==================================================================
29 : : //= OEnumerationByName
30 : : //==================================================================
31 : : //------------------------------------------------------------------------------
32 : 2 : OEnumerationByName::OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess>& _rxAccess)
33 [ + - ]: 2 : :m_aNames(_rxAccess->getElementNames())
34 : : ,m_nPos(0)
35 : : ,m_xAccess(_rxAccess)
36 [ + - ][ + - ]: 4 : ,m_bListening(sal_False)
37 : : {
38 [ + - ]: 2 : impl_startDisposeListening();
39 : 2 : }
40 : :
41 : : //------------------------------------------------------------------------------
42 : 7386 : OEnumerationByName::OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess>& _rxAccess,
43 : : const staruno::Sequence< ::rtl::OUString >& _aNames )
44 : : :m_aNames(_aNames)
45 : : ,m_nPos(0)
46 : : ,m_xAccess(_rxAccess)
47 [ + - ][ + - ]: 7386 : ,m_bListening(sal_False)
48 : : {
49 [ + - ]: 7386 : impl_startDisposeListening();
50 : 7386 : }
51 : :
52 : : //------------------------------------------------------------------------------
53 [ + - ][ + - ]: 7388 : OEnumerationByName::~OEnumerationByName()
54 : : {
55 [ + - ]: 7388 : impl_stopDisposeListening();
56 [ - + ]: 14776 : }
57 : :
58 : : //------------------------------------------------------------------------------
59 : 8293 : sal_Bool SAL_CALL OEnumerationByName::hasMoreElements( ) throw(staruno::RuntimeException)
60 : : {
61 [ + - ]: 8293 : ::osl::ResettableMutexGuard aLock(m_aLock);
62 : :
63 [ + + ][ + + ]: 8293 : if (m_xAccess.is() && m_aNames.getLength() > m_nPos)
[ + + ]
64 : 7000 : return sal_True;
65 : :
66 [ + + ]: 1293 : if (m_xAccess.is())
67 : : {
68 [ + - ]: 400 : impl_stopDisposeListening();
69 : 400 : m_xAccess.clear();
70 : : }
71 : :
72 [ + - ]: 8293 : return sal_False;
73 : : }
74 : :
75 : : //------------------------------------------------------------------------------
76 : 6519 : staruno::Any SAL_CALL OEnumerationByName::nextElement( )
77 : : throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
78 : : {
79 [ + - ]: 6519 : ::osl::ResettableMutexGuard aLock(m_aLock);
80 : :
81 : 6519 : staruno::Any aRes;
82 [ + - ][ + - ]: 6519 : if (m_xAccess.is() && m_nPos < m_aNames.getLength())
[ + - ]
83 [ + - ][ + - ]: 6519 : aRes = m_xAccess->getByName(m_aNames.getConstArray()[m_nPos++]);
84 : :
85 [ + - ][ + + ]: 6519 : if (m_xAccess.is() && m_nPos >= m_aNames.getLength())
[ + + ]
86 : : {
87 [ + - ]: 6490 : impl_stopDisposeListening();
88 : 6490 : m_xAccess.clear();
89 : : }
90 : :
91 [ - + ]: 6519 : if (!aRes.hasValue()) //There are no more elements
92 [ # # ]: 0 : throw starcontainer::NoSuchElementException();
93 : :
94 [ + - ]: 6519 : return aRes;
95 : : }
96 : :
97 : : //------------------------------------------------------------------------------
98 : 2 : void SAL_CALL OEnumerationByName::disposing(const starlang::EventObject& aEvent)
99 : : throw(staruno::RuntimeException)
100 : : {
101 [ + - ]: 2 : ::osl::ResettableMutexGuard aLock(m_aLock);
102 : :
103 [ + - ][ + - ]: 2 : if (aEvent.Source == m_xAccess)
104 [ + - ]: 2 : m_xAccess.clear();
105 : 2 : }
106 : :
107 : : //------------------------------------------------------------------------------
108 : 7388 : void OEnumerationByName::impl_startDisposeListening()
109 : : {
110 [ + - ]: 7388 : ::osl::ResettableMutexGuard aLock(m_aLock);
111 : :
112 [ - + ]: 7388 : if (m_bListening)
113 : 7388 : return;
114 : :
115 : 7388 : ++m_refCount;
116 [ + - ]: 7388 : staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
117 [ + + ]: 7388 : if (xDisposable.is())
118 : : {
119 [ + - ][ + - ]: 2 : xDisposable->addEventListener(this);
[ + - ]
120 : 2 : m_bListening = sal_True;
121 : : }
122 [ + - ][ + - ]: 7388 : --m_refCount;
123 : : }
124 : :
125 : : //------------------------------------------------------------------------------
126 : 14278 : void OEnumerationByName::impl_stopDisposeListening()
127 : : {
128 [ + - ]: 14278 : ::osl::ResettableMutexGuard aLock(m_aLock);
129 : :
130 [ + + ]: 14278 : if (!m_bListening)
131 : 14278 : return;
132 : :
133 : 2 : ++m_refCount;
134 [ + - ]: 2 : staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
135 [ - + ]: 2 : if (xDisposable.is())
136 : : {
137 [ # # ][ # # ]: 0 : xDisposable->removeEventListener(this);
[ # # ]
138 : 0 : m_bListening = sal_False;
139 : : }
140 [ + - ][ + + ]: 14278 : --m_refCount;
141 : : }
142 : :
143 : : //==================================================================
144 : : //= OEnumerationByIndex
145 : : //==================================================================
146 : : //------------------------------------------------------------------------------
147 : 2 : OEnumerationByIndex::OEnumerationByIndex(const staruno::Reference< starcontainer::XIndexAccess >& _rxAccess)
148 : : :m_nPos(0)
149 : : ,m_xAccess(_rxAccess)
150 [ + - ]: 2 : ,m_bListening(sal_False)
151 : : {
152 [ + - ]: 2 : impl_startDisposeListening();
153 : 2 : }
154 : :
155 : : //------------------------------------------------------------------------------
156 [ + - ]: 2 : OEnumerationByIndex::~OEnumerationByIndex()
157 : : {
158 [ + - ]: 2 : impl_stopDisposeListening();
159 [ - + ]: 4 : }
160 : :
161 : : //------------------------------------------------------------------------------
162 : 0 : sal_Bool SAL_CALL OEnumerationByIndex::hasMoreElements( ) throw(staruno::RuntimeException)
163 : : {
164 [ # # ]: 0 : ::osl::ResettableMutexGuard aLock(m_aLock);
165 : :
166 [ # # ][ # # ]: 0 : if (m_xAccess.is() && m_xAccess->getCount() > m_nPos)
[ # # ][ # # ]
[ # # ]
167 : 0 : return sal_True;
168 : :
169 [ # # ]: 0 : if (m_xAccess.is())
170 : : {
171 [ # # ]: 0 : impl_stopDisposeListening();
172 : 0 : m_xAccess.clear();
173 : : }
174 : :
175 [ # # ]: 0 : return sal_False;
176 : : }
177 : :
178 : : //------------------------------------------------------------------------------
179 : 0 : staruno::Any SAL_CALL OEnumerationByIndex::nextElement( )
180 : : throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
181 : : {
182 [ # # ]: 0 : ::osl::ResettableMutexGuard aLock(m_aLock);
183 : :
184 : 0 : staruno::Any aRes;
185 [ # # ]: 0 : if (m_xAccess.is())
186 : : {
187 [ # # ][ # # ]: 0 : aRes = m_xAccess->getByIndex(m_nPos++);
188 [ # # ][ # # ]: 0 : if (m_nPos >= m_xAccess->getCount())
[ # # ]
189 : : {
190 [ # # ]: 0 : impl_stopDisposeListening();
191 : 0 : m_xAccess.clear();
192 : : }
193 : : }
194 : :
195 [ # # ]: 0 : if (!aRes.hasValue())
196 [ # # ]: 0 : throw starcontainer::NoSuchElementException();
197 [ # # ]: 0 : return aRes;
198 : : }
199 : :
200 : : //------------------------------------------------------------------------------
201 : 2 : void SAL_CALL OEnumerationByIndex::disposing(const starlang::EventObject& aEvent)
202 : : throw(staruno::RuntimeException)
203 : : {
204 [ + - ]: 2 : ::osl::ResettableMutexGuard aLock(m_aLock);
205 : :
206 [ + - ][ + - ]: 2 : if (aEvent.Source == m_xAccess)
207 [ + - ]: 2 : m_xAccess.clear();
208 : 2 : }
209 : :
210 : : //------------------------------------------------------------------------------
211 : 2 : void OEnumerationByIndex::impl_startDisposeListening()
212 : : {
213 [ + - ]: 2 : ::osl::ResettableMutexGuard aLock(m_aLock);
214 : :
215 [ - + ]: 2 : if (m_bListening)
216 : 2 : return;
217 : :
218 : 2 : ++m_refCount;
219 [ + - ]: 2 : staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
220 [ + - ]: 2 : if (xDisposable.is())
221 : : {
222 [ + - ][ + - ]: 2 : xDisposable->addEventListener(this);
[ + - ]
223 : 2 : m_bListening = sal_True;
224 : : }
225 [ + - ][ + - ]: 2 : --m_refCount;
226 : : }
227 : :
228 : : //------------------------------------------------------------------------------
229 : 2 : void OEnumerationByIndex::impl_stopDisposeListening()
230 : : {
231 [ + - ]: 2 : ::osl::ResettableMutexGuard aLock(m_aLock);
232 : :
233 [ - + ]: 2 : if (!m_bListening)
234 : 2 : return;
235 : :
236 : 2 : ++m_refCount;
237 [ + - ]: 2 : staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
238 [ - + ]: 2 : if (xDisposable.is())
239 : : {
240 [ # # ][ # # ]: 0 : xDisposable->removeEventListener(this);
[ # # ]
241 : 0 : m_bListening = sal_False;
242 : : }
243 [ + - ][ + - ]: 2 : --m_refCount;
244 : : }
245 : :
246 : : //==================================================================
247 : : //= OAnyEnumeration
248 : : //==================================================================
249 : :
250 : : //------------------------------------------------------------------------------
251 : 6 : OAnyEnumeration::OAnyEnumeration(const staruno::Sequence< staruno::Any >& lItems)
252 : : :m_nPos(0)
253 [ + - ][ + - ]: 6 : ,m_lItems(lItems)
254 : : {
255 : 6 : }
256 : :
257 : : //------------------------------------------------------------------------------
258 [ + - ][ + - ]: 6 : OAnyEnumeration::~OAnyEnumeration()
259 : : {
260 [ - + ]: 12 : }
261 : :
262 : : //------------------------------------------------------------------------------
263 : 14 : sal_Bool SAL_CALL OAnyEnumeration::hasMoreElements( ) throw(staruno::RuntimeException)
264 : : {
265 [ + - ]: 14 : ::osl::ResettableMutexGuard aLock(m_aLock);
266 : :
267 [ + - ]: 14 : return (m_lItems.getLength() > m_nPos);
268 : : }
269 : :
270 : : //------------------------------------------------------------------------------
271 : 4 : staruno::Any SAL_CALL OAnyEnumeration::nextElement( )
272 : : throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
273 : : {
274 [ + - ][ - + ]: 4 : if ( ! hasMoreElements())
275 [ # # ]: 0 : throw starcontainer::NoSuchElementException();
276 : :
277 [ + - ]: 4 : ::osl::ResettableMutexGuard aLock(m_aLock);
278 : 4 : sal_Int32 nPos = m_nPos;
279 : 4 : ++m_nPos;
280 [ + - ][ + - ]: 4 : return m_lItems[nPos];
281 : : }
282 : :
283 : : //.........................................................................
284 : : } // namespace comphelper
285 : : //.........................................................................
286 : :
287 : :
288 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|