xref: /trunk/main/comphelper/source/container/enumhelper.cxx (revision 2aed523360a57b9d104375c3b972acc26c7f89ff)
1dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5dde7d3faSAndrew Rist  * distributed with this work for additional information
6dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18dde7d3faSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20dde7d3faSAndrew Rist  *************************************************************/
21dde7d3faSAndrew Rist 
22dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir #include <comphelper/enumhelper.hxx>
27cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir //.........................................................................
30cdf0e10cSrcweir namespace comphelper
31cdf0e10cSrcweir {
32cdf0e10cSrcweir //.........................................................................
33cdf0e10cSrcweir 
34cdf0e10cSrcweir //==================================================================
35cdf0e10cSrcweir //= OEnumerationByName
36cdf0e10cSrcweir //==================================================================
37cdf0e10cSrcweir //------------------------------------------------------------------------------
OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess> & _rxAccess)38cdf0e10cSrcweir OEnumerationByName::OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess>& _rxAccess)
39cdf0e10cSrcweir     :m_aNames(_rxAccess->getElementNames())
40cdf0e10cSrcweir     ,m_nPos(0)
41cdf0e10cSrcweir     ,m_xAccess(_rxAccess)
42cdf0e10cSrcweir     ,m_bListening(sal_False)
43cdf0e10cSrcweir {
44cdf0e10cSrcweir     impl_startDisposeListening();
45cdf0e10cSrcweir }
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //------------------------------------------------------------------------------
OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess> & _rxAccess,const staruno::Sequence<::rtl::OUString> & _aNames)48cdf0e10cSrcweir OEnumerationByName::OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess>& _rxAccess,
49cdf0e10cSrcweir                                        const staruno::Sequence< ::rtl::OUString >&           _aNames  )
50cdf0e10cSrcweir     :m_aNames(_aNames)
51cdf0e10cSrcweir     ,m_nPos(0)
52cdf0e10cSrcweir     ,m_xAccess(_rxAccess)
53cdf0e10cSrcweir     ,m_bListening(sal_False)
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     impl_startDisposeListening();
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir //------------------------------------------------------------------------------
~OEnumerationByName()59cdf0e10cSrcweir OEnumerationByName::~OEnumerationByName()
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     impl_stopDisposeListening();
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir //------------------------------------------------------------------------------
hasMoreElements()65cdf0e10cSrcweir sal_Bool SAL_CALL OEnumerationByName::hasMoreElements(  ) throw(staruno::RuntimeException)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     if (m_xAccess.is() && m_aNames.getLength() > m_nPos)
70cdf0e10cSrcweir         return sal_True;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     if (m_xAccess.is())
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir         impl_stopDisposeListening();
75cdf0e10cSrcweir         m_xAccess.clear();
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     return sal_False;
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir //------------------------------------------------------------------------------
nextElement()82cdf0e10cSrcweir staruno::Any SAL_CALL OEnumerationByName::nextElement(  )
83cdf0e10cSrcweir         throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
84cdf0e10cSrcweir {
85cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     staruno::Any aRes;
88cdf0e10cSrcweir     if (m_xAccess.is() && m_nPos < m_aNames.getLength())
89cdf0e10cSrcweir         aRes = m_xAccess->getByName(m_aNames.getConstArray()[m_nPos++]);
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     if (m_xAccess.is() && m_nPos >= m_aNames.getLength())
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         impl_stopDisposeListening();
94cdf0e10cSrcweir         m_xAccess.clear();
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     if (!aRes.hasValue())       // es gibt kein Element mehr
98cdf0e10cSrcweir         throw starcontainer::NoSuchElementException();
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     return aRes;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir //------------------------------------------------------------------------------
disposing(const starlang::EventObject & aEvent)104cdf0e10cSrcweir void SAL_CALL OEnumerationByName::disposing(const starlang::EventObject& aEvent)
105cdf0e10cSrcweir         throw(staruno::RuntimeException)
106cdf0e10cSrcweir {
107cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     if (aEvent.Source == m_xAccess)
110cdf0e10cSrcweir         m_xAccess.clear();
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir //------------------------------------------------------------------------------
impl_startDisposeListening()114cdf0e10cSrcweir void OEnumerationByName::impl_startDisposeListening()
115cdf0e10cSrcweir {
116cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     if (m_bListening)
119cdf0e10cSrcweir         return;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     ++m_refCount;
122cdf0e10cSrcweir     staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
123cdf0e10cSrcweir     if (xDisposable.is())
124cdf0e10cSrcweir     {
125cdf0e10cSrcweir         xDisposable->addEventListener(this);
126cdf0e10cSrcweir         m_bListening = sal_True;
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir     --m_refCount;
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir //------------------------------------------------------------------------------
impl_stopDisposeListening()132cdf0e10cSrcweir void OEnumerationByName::impl_stopDisposeListening()
133cdf0e10cSrcweir {
134cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     if (!m_bListening)
137cdf0e10cSrcweir         return;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     ++m_refCount;
140cdf0e10cSrcweir     staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
141cdf0e10cSrcweir     if (xDisposable.is())
142cdf0e10cSrcweir     {
143cdf0e10cSrcweir         xDisposable->removeEventListener(this);
144cdf0e10cSrcweir         m_bListening = sal_False;
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir     --m_refCount;
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir //==================================================================
150cdf0e10cSrcweir //= OEnumerationByIndex
151cdf0e10cSrcweir //==================================================================
152cdf0e10cSrcweir //------------------------------------------------------------------------------
OEnumerationByIndex(const staruno::Reference<starcontainer::XIndexAccess> & _rxAccess)153cdf0e10cSrcweir OEnumerationByIndex::OEnumerationByIndex(const staruno::Reference< starcontainer::XIndexAccess >& _rxAccess)
154cdf0e10cSrcweir     :m_nPos(0)
155cdf0e10cSrcweir     ,m_xAccess(_rxAccess)
156cdf0e10cSrcweir     ,m_bListening(sal_False)
157cdf0e10cSrcweir {
158cdf0e10cSrcweir     impl_startDisposeListening();
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir //------------------------------------------------------------------------------
~OEnumerationByIndex()162cdf0e10cSrcweir OEnumerationByIndex::~OEnumerationByIndex()
163cdf0e10cSrcweir {
164cdf0e10cSrcweir     impl_stopDisposeListening();
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir //------------------------------------------------------------------------------
hasMoreElements()168cdf0e10cSrcweir sal_Bool SAL_CALL OEnumerationByIndex::hasMoreElements(  ) throw(staruno::RuntimeException)
169cdf0e10cSrcweir {
170cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     if (m_xAccess.is() && m_xAccess->getCount() > m_nPos)
173cdf0e10cSrcweir         return sal_True;
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     if (m_xAccess.is())
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         impl_stopDisposeListening();
178cdf0e10cSrcweir         m_xAccess.clear();
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     return sal_False;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir //------------------------------------------------------------------------------
nextElement()185cdf0e10cSrcweir staruno::Any SAL_CALL OEnumerationByIndex::nextElement(  )
186cdf0e10cSrcweir         throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
187cdf0e10cSrcweir {
188cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     staruno::Any aRes;
191*a144be1aSDamjan Jovanovic     if (m_xAccess.is() && m_nPos < m_xAccess->getCount())
192cdf0e10cSrcweir         aRes = m_xAccess->getByIndex(m_nPos++);
193*a144be1aSDamjan Jovanovic 
194*a144be1aSDamjan Jovanovic     if (m_xAccess.is() && m_nPos >= m_xAccess->getCount())
195cdf0e10cSrcweir     {
196cdf0e10cSrcweir         impl_stopDisposeListening();
197cdf0e10cSrcweir         m_xAccess.clear();
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     if (!aRes.hasValue())       // es gibt kein Element mehr
201cdf0e10cSrcweir         throw starcontainer::NoSuchElementException();
202cdf0e10cSrcweir     return aRes;
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir //------------------------------------------------------------------------------
disposing(const starlang::EventObject & aEvent)206cdf0e10cSrcweir void SAL_CALL OEnumerationByIndex::disposing(const starlang::EventObject& aEvent)
207cdf0e10cSrcweir         throw(staruno::RuntimeException)
208cdf0e10cSrcweir {
209cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
210cdf0e10cSrcweir 
211cdf0e10cSrcweir     if (aEvent.Source == m_xAccess)
212cdf0e10cSrcweir         m_xAccess.clear();
213cdf0e10cSrcweir }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir //------------------------------------------------------------------------------
impl_startDisposeListening()216cdf0e10cSrcweir void OEnumerationByIndex::impl_startDisposeListening()
217cdf0e10cSrcweir {
218cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
219cdf0e10cSrcweir 
220cdf0e10cSrcweir     if (m_bListening)
221cdf0e10cSrcweir         return;
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     ++m_refCount;
224cdf0e10cSrcweir     staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
225cdf0e10cSrcweir     if (xDisposable.is())
226cdf0e10cSrcweir     {
227cdf0e10cSrcweir         xDisposable->addEventListener(this);
228cdf0e10cSrcweir         m_bListening = sal_True;
229cdf0e10cSrcweir     }
230cdf0e10cSrcweir     --m_refCount;
231cdf0e10cSrcweir }
232cdf0e10cSrcweir 
233cdf0e10cSrcweir //------------------------------------------------------------------------------
impl_stopDisposeListening()234cdf0e10cSrcweir void OEnumerationByIndex::impl_stopDisposeListening()
235cdf0e10cSrcweir {
236cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     if (!m_bListening)
239cdf0e10cSrcweir         return;
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     ++m_refCount;
242cdf0e10cSrcweir     staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
243cdf0e10cSrcweir     if (xDisposable.is())
244cdf0e10cSrcweir     {
245cdf0e10cSrcweir         xDisposable->removeEventListener(this);
246cdf0e10cSrcweir         m_bListening = sal_False;
247cdf0e10cSrcweir     }
248cdf0e10cSrcweir     --m_refCount;
249cdf0e10cSrcweir }
250cdf0e10cSrcweir 
251cdf0e10cSrcweir //==================================================================
252cdf0e10cSrcweir //= OAnyEnumeration
253cdf0e10cSrcweir //==================================================================
254cdf0e10cSrcweir 
255cdf0e10cSrcweir //------------------------------------------------------------------------------
OAnyEnumeration(const staruno::Sequence<staruno::Any> & lItems)256cdf0e10cSrcweir OAnyEnumeration::OAnyEnumeration(const staruno::Sequence< staruno::Any >& lItems)
257cdf0e10cSrcweir     :m_nPos(0)
258cdf0e10cSrcweir     ,m_lItems(lItems)
259cdf0e10cSrcweir {
260cdf0e10cSrcweir }
261cdf0e10cSrcweir 
262cdf0e10cSrcweir //------------------------------------------------------------------------------
~OAnyEnumeration()263cdf0e10cSrcweir OAnyEnumeration::~OAnyEnumeration()
264cdf0e10cSrcweir {
265cdf0e10cSrcweir }
266cdf0e10cSrcweir 
267cdf0e10cSrcweir //------------------------------------------------------------------------------
hasMoreElements()268cdf0e10cSrcweir sal_Bool SAL_CALL OAnyEnumeration::hasMoreElements(  ) throw(staruno::RuntimeException)
269cdf0e10cSrcweir {
270cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
271cdf0e10cSrcweir 
272cdf0e10cSrcweir     return (m_lItems.getLength() > m_nPos);
273cdf0e10cSrcweir }
274cdf0e10cSrcweir 
275cdf0e10cSrcweir //------------------------------------------------------------------------------
nextElement()276cdf0e10cSrcweir staruno::Any SAL_CALL OAnyEnumeration::nextElement(  )
277cdf0e10cSrcweir         throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException)
278cdf0e10cSrcweir {
279cdf0e10cSrcweir     if ( ! hasMoreElements())
280cdf0e10cSrcweir         throw starcontainer::NoSuchElementException();
281cdf0e10cSrcweir 
282cdf0e10cSrcweir     ::osl::ResettableMutexGuard aLock(m_aLock);
283cdf0e10cSrcweir     sal_Int32 nPos = m_nPos;
284cdf0e10cSrcweir     ++m_nPos;
285cdf0e10cSrcweir     return m_lItems[nPos];
286cdf0e10cSrcweir }
287cdf0e10cSrcweir 
288cdf0e10cSrcweir //.........................................................................
289cdf0e10cSrcweir }   // namespace comphelper
290cdf0e10cSrcweir //.........................................................................
291