LCOV - code coverage report
Current view: top level - comphelper/source/container - enumhelper.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 97 122 79.5 %
Date: 2014-04-11 Functions: 20 22 90.9 %
Legend: Lines: hit not hit

          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           1 : OEnumerationByName::OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess>& _rxAccess)
      33           1 :     :m_aNames(_rxAccess->getElementNames())
      34             :     ,m_nPos(0)
      35             :     ,m_xAccess(_rxAccess)
      36           2 :     ,m_bListening(false)
      37             : {
      38           1 :     impl_startDisposeListening();
      39           1 : }
      40             : 
      41             : 
      42       34578 : OEnumerationByName::OEnumerationByName(const staruno::Reference<starcontainer::XNameAccess>& _rxAccess,
      43             :                                        const staruno::Sequence< OUString >&           _aNames  )
      44             :     :m_aNames(_aNames)
      45             :     ,m_nPos(0)
      46             :     ,m_xAccess(_rxAccess)
      47       34578 :     ,m_bListening(false)
      48             : {
      49       34578 :     impl_startDisposeListening();
      50       34578 : }
      51             : 
      52             : 
      53      103737 : OEnumerationByName::~OEnumerationByName()
      54             : {
      55       34579 :     impl_stopDisposeListening();
      56       69158 : }
      57             : 
      58             : 
      59       35181 : sal_Bool SAL_CALL OEnumerationByName::hasMoreElements(  ) throw(staruno::RuntimeException, std::exception)
      60             : {
      61       35181 :     ::osl::ResettableMutexGuard aLock(m_aLock);
      62             : 
      63       35181 :     if (m_xAccess.is() && m_aNames.getLength() > m_nPos)
      64        7760 :         return sal_True;
      65             : 
      66       27421 :     if (m_xAccess.is())
      67             :     {
      68       26837 :         impl_stopDisposeListening();
      69       26837 :         m_xAccess.clear();
      70             :     }
      71             : 
      72       27421 :     return sal_False;
      73             : }
      74             : 
      75             : 
      76        6711 : staruno::Any SAL_CALL OEnumerationByName::nextElement(  )
      77             :         throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException, std::exception)
      78             : {
      79        6711 :     ::osl::ResettableMutexGuard aLock(m_aLock);
      80             : 
      81        6711 :     staruno::Any aRes;
      82        6711 :     if (m_xAccess.is() && m_nPos < m_aNames.getLength())
      83        6711 :         aRes = m_xAccess->getByName(m_aNames.getConstArray()[m_nPos++]);
      84             : 
      85        6711 :     if (m_xAccess.is() && m_nPos >= m_aNames.getLength())
      86             :     {
      87        6446 :         impl_stopDisposeListening();
      88        6446 :         m_xAccess.clear();
      89             :     }
      90             : 
      91        6711 :     if (!aRes.hasValue())       //There are no more elements
      92           0 :         throw starcontainer::NoSuchElementException();
      93             : 
      94        6711 :     return aRes;
      95             : }
      96             : 
      97             : 
      98           1 : void SAL_CALL OEnumerationByName::disposing(const starlang::EventObject& aEvent)
      99             :         throw(staruno::RuntimeException, std::exception)
     100             : {
     101           1 :     ::osl::ResettableMutexGuard aLock(m_aLock);
     102             : 
     103           1 :     if (aEvent.Source == m_xAccess)
     104           1 :         m_xAccess.clear();
     105           1 : }
     106             : 
     107             : 
     108       34579 : void OEnumerationByName::impl_startDisposeListening()
     109             : {
     110       34579 :     ::osl::ResettableMutexGuard aLock(m_aLock);
     111             : 
     112       34579 :     if (m_bListening)
     113       34579 :         return;
     114             : 
     115       34579 :     ++m_refCount;
     116       69158 :     staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
     117       34579 :     if (xDisposable.is())
     118             :     {
     119           1 :         xDisposable->addEventListener(this);
     120           1 :         m_bListening = true;
     121             :     }
     122       69158 :     --m_refCount;
     123             : }
     124             : 
     125             : 
     126       67862 : void OEnumerationByName::impl_stopDisposeListening()
     127             : {
     128       67862 :     ::osl::ResettableMutexGuard aLock(m_aLock);
     129             : 
     130       67862 :     if (!m_bListening)
     131      135723 :         return;
     132             : 
     133           1 :     ++m_refCount;
     134           2 :     staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
     135           1 :     if (xDisposable.is())
     136             :     {
     137           0 :         xDisposable->removeEventListener(this);
     138           0 :         m_bListening = false;
     139             :     }
     140           2 :     --m_refCount;
     141             : }
     142             : 
     143             : 
     144             : //= OEnumerationByIndex
     145             : 
     146             : 
     147           1 : OEnumerationByIndex::OEnumerationByIndex(const staruno::Reference< starcontainer::XIndexAccess >& _rxAccess)
     148             :     :m_nPos(0)
     149             :     ,m_xAccess(_rxAccess)
     150           1 :     ,m_bListening(false)
     151             : {
     152           1 :     impl_startDisposeListening();
     153           1 : }
     154             : 
     155             : 
     156           3 : OEnumerationByIndex::~OEnumerationByIndex()
     157             : {
     158           1 :     impl_stopDisposeListening();
     159           2 : }
     160             : 
     161             : 
     162           0 : sal_Bool SAL_CALL OEnumerationByIndex::hasMoreElements(  ) throw(staruno::RuntimeException, std::exception)
     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, std::exception)
     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           1 : void SAL_CALL OEnumerationByIndex::disposing(const starlang::EventObject& aEvent)
     202             :         throw(staruno::RuntimeException, std::exception)
     203             : {
     204           1 :     ::osl::ResettableMutexGuard aLock(m_aLock);
     205             : 
     206           1 :     if (aEvent.Source == m_xAccess)
     207           1 :         m_xAccess.clear();
     208           1 : }
     209             : 
     210             : 
     211           1 : void OEnumerationByIndex::impl_startDisposeListening()
     212             : {
     213           1 :     ::osl::ResettableMutexGuard aLock(m_aLock);
     214             : 
     215           1 :     if (m_bListening)
     216           1 :         return;
     217             : 
     218           1 :     ++m_refCount;
     219           2 :     staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
     220           1 :     if (xDisposable.is())
     221             :     {
     222           1 :         xDisposable->addEventListener(this);
     223           1 :         m_bListening = true;
     224             :     }
     225           2 :     --m_refCount;
     226             : }
     227             : 
     228             : 
     229           1 : void OEnumerationByIndex::impl_stopDisposeListening()
     230             : {
     231           1 :     ::osl::ResettableMutexGuard aLock(m_aLock);
     232             : 
     233           1 :     if (!m_bListening)
     234           1 :         return;
     235             : 
     236           1 :     ++m_refCount;
     237           2 :     staruno::Reference< starlang::XComponent > xDisposable(m_xAccess, staruno::UNO_QUERY);
     238           1 :     if (xDisposable.is())
     239             :     {
     240           0 :         xDisposable->removeEventListener(this);
     241           0 :         m_bListening = false;
     242             :     }
     243           2 :     --m_refCount;
     244             : }
     245             : 
     246             : 
     247             : //= OAnyEnumeration
     248             : 
     249             : 
     250             : 
     251          32 : OAnyEnumeration::OAnyEnumeration(const staruno::Sequence< staruno::Any >& lItems)
     252             :     :m_nPos(0)
     253          32 :     ,m_lItems(lItems)
     254             : {
     255          32 : }
     256             : 
     257             : 
     258          64 : OAnyEnumeration::~OAnyEnumeration()
     259             : {
     260          64 : }
     261             : 
     262             : 
     263          94 : sal_Bool SAL_CALL OAnyEnumeration::hasMoreElements(  ) throw(staruno::RuntimeException, std::exception)
     264             : {
     265          94 :     ::osl::ResettableMutexGuard aLock(m_aLock);
     266             : 
     267          94 :     return (m_lItems.getLength() > m_nPos);
     268             : }
     269             : 
     270             : 
     271          31 : staruno::Any SAL_CALL OAnyEnumeration::nextElement(  )
     272             :         throw(starcontainer::NoSuchElementException, starlang::WrappedTargetException, staruno::RuntimeException, std::exception)
     273             : {
     274          31 :     if ( ! hasMoreElements())
     275           0 :         throw starcontainer::NoSuchElementException();
     276             : 
     277          31 :     ::osl::ResettableMutexGuard aLock(m_aLock);
     278          31 :     sal_Int32 nPos = m_nPos;
     279          31 :     ++m_nPos;
     280          31 :     return m_lItems[nPos];
     281             : }
     282             : 
     283             : 
     284             : }   // namespace comphelper
     285             : 
     286             : 
     287             : 
     288             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10