LCOV - code coverage report
Current view: top level - comphelper/source/misc - numberedcollection.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 72 92 78.3 %
Date: 2014-11-03 Functions: 10 11 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             : 
      21             : #include <comphelper/numberedcollection.hxx>
      22             : 
      23             : #include <com/sun/star/frame/UntitledNumbersConst.hpp>
      24             : 
      25             : 
      26             : namespace comphelper{
      27             : 
      28             : static const char ERRMSG_INVALID_COMPONENT_PARAM[] = "NULL as component reference not allowed.";
      29             : 
      30             : 
      31        5831 : NumberedCollection::NumberedCollection()
      32             :     : ::cppu::BaseMutex ()
      33             :     , m_sUntitledPrefix ()
      34             :     , m_lComponents     ()
      35        5831 :     , m_xOwner          ()
      36             : {
      37        5831 : }
      38             : 
      39             : 
      40       11416 : NumberedCollection::~NumberedCollection()
      41             : {
      42       11416 : }
      43             : 
      44             : 
      45        5831 : void NumberedCollection::setOwner(const css::uno::Reference< css::uno::XInterface >& xOwner)
      46             : {
      47             :     // SYNCHRONIZED ->
      48        5831 :     ::osl::ResettableMutexGuard aLock(m_aMutex);
      49             : 
      50        5831 :         m_xOwner = xOwner;
      51             : 
      52             :     // <- SYNCHRONIZED
      53        5831 : }
      54             : 
      55             : 
      56        5815 : void NumberedCollection::setUntitledPrefix(const OUString& sPrefix)
      57             : {
      58             :     // SYNCHRONIZED ->
      59        5815 :     ::osl::ResettableMutexGuard aLock(m_aMutex);
      60             : 
      61        5815 :         m_sUntitledPrefix = sPrefix;
      62             : 
      63             :     // <- SYNCHRONIZED
      64        5815 : }
      65             : 
      66             : 
      67        7638 : ::sal_Int32 SAL_CALL NumberedCollection::leaseNumber(const css::uno::Reference< css::uno::XInterface >& xComponent)
      68             :     throw (css::lang::IllegalArgumentException,
      69             :            css::uno::RuntimeException, std::exception         )
      70             : {
      71             :     // SYNCHRONIZED ->
      72        7638 :     ::osl::ResettableMutexGuard aLock(m_aMutex);
      73             : 
      74        7638 :         if ( ! xComponent.is ())
      75           0 :             throw css::lang::IllegalArgumentException (OUString(ERRMSG_INVALID_COMPONENT_PARAM), m_xOwner.get(), 1);
      76             : 
      77        7638 :         sal_IntPtr pComponent = reinterpret_cast<sal_IntPtr>( xComponent.get() );
      78        7638 :         TNumberedItemHash::const_iterator pIt = m_lComponents.find (pComponent);
      79             : 
      80             :         // a) component already exists - return it's number directly
      81        7638 :         if (pIt != m_lComponents.end())
      82           0 :             return pIt->second.nNumber;
      83             : 
      84             :         // b) component must be added new to this container
      85             : 
      86             :         // b1) collection is full - no further components possible
      87             :         //     -> return INVALID_NUMBER
      88        7638 :         ::sal_Int32 nFreeNumber = impl_searchFreeNumber();
      89        7638 :         if (nFreeNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER)
      90           0 :             return css::frame::UntitledNumbersConst::INVALID_NUMBER;
      91             : 
      92             :         // b2) add component to collection and return its number
      93       15276 :         TNumberedItem aItem;
      94        7638 :         aItem.xItem   = css::uno::WeakReference< css::uno::XInterface >(xComponent);
      95        7638 :         aItem.nNumber = nFreeNumber;
      96        7638 :         m_lComponents[pComponent] = aItem;
      97             : 
      98       15276 :         return nFreeNumber;
      99             : 
     100             :     // <- SYNCHRONIZED
     101             : }
     102             : 
     103             : 
     104        2095 : void SAL_CALL NumberedCollection::releaseNumber(::sal_Int32 nNumber)
     105             :     throw (css::lang::IllegalArgumentException,
     106             :            css::uno::RuntimeException, std::exception         )
     107             : {
     108             :     // SYNCHRONIZED ->
     109        2095 :     ::osl::ResettableMutexGuard aLock(m_aMutex);
     110             : 
     111        2095 :     if (nNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER)
     112           0 :         throw css::lang::IllegalArgumentException ("Special valkud INVALID_NUMBER not allowed as input parameter.", m_xOwner.get(), 1);
     113             : 
     114        4190 :     TDeadItemList               lDeadItems;
     115        2095 :     TNumberedItemHash::iterator pComponent;
     116             : 
     117        6981 :     for (  pComponent  = m_lComponents.begin ();
     118        4654 :            pComponent != m_lComponents.end   ();
     119             :          ++pComponent                          )
     120             :     {
     121        2327 :         const TNumberedItem&                              rItem = pComponent->second;
     122        2327 :         const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
     123             : 
     124        2327 :         if ( ! xItem.is ())
     125             :         {
     126           0 :             lDeadItems.push_back(pComponent->first);
     127           0 :             continue;
     128             :         }
     129             : 
     130        2327 :         if (rItem.nNumber == nNumber)
     131             :         {
     132        2095 :             m_lComponents.erase (pComponent);
     133        2095 :             break;
     134             :         }
     135         232 :     }
     136             : 
     137        4190 :     impl_cleanUpDeadItems(m_lComponents, lDeadItems);
     138             : 
     139             :     // <- SYNCHRONIZED
     140        2095 : }
     141             : 
     142             : 
     143           0 : void SAL_CALL NumberedCollection::releaseNumberForComponent(const css::uno::Reference< css::uno::XInterface >& xComponent)
     144             :     throw (css::lang::IllegalArgumentException,
     145             :            css::uno::RuntimeException, std::exception         )
     146             : {
     147             :     // SYNCHRONIZED ->
     148           0 :     ::osl::ResettableMutexGuard aLock(m_aMutex);
     149             : 
     150           0 :         if ( ! xComponent.is ())
     151           0 :             throw css::lang::IllegalArgumentException (OUString(ERRMSG_INVALID_COMPONENT_PARAM), m_xOwner.get(), 1);
     152             : 
     153           0 :         sal_IntPtr pComponent = reinterpret_cast<sal_IntPtr>( xComponent.get() );
     154           0 :         TNumberedItemHash::iterator pIt = m_lComponents.find (pComponent);
     155             : 
     156             :         // a) component exists and will be removed
     157           0 :         if (pIt != m_lComponents.end())
     158           0 :             m_lComponents.erase(pIt);
     159             : 
     160             :         // else
     161             :         // b) component does not exists - nothing todo here (ignore request!)
     162             : 
     163             :     // <- SYNCHRONIZED
     164           0 : }
     165             : 
     166             : 
     167       11060 : OUString SAL_CALL NumberedCollection::getUntitledPrefix()
     168             :     throw (css::uno::RuntimeException, std::exception)
     169             : {
     170             :     // SYNCHRONIZED ->
     171       11060 :     ::osl::ResettableMutexGuard aLock(m_aMutex);
     172             : 
     173       11060 :         return m_sUntitledPrefix;
     174             : 
     175             :     // <- SYNCHRONIZED
     176             : }
     177             : 
     178             : 
     179             : /** create an ordered list of all possible numbers ...
     180             :     e.g. {1,2,3,...,N} Max size of these list will be
     181             :     current size of component list + 1 .
     182             : 
     183             :     "+1" ... because in case all numbers in range 1..n
     184             :     are in use we need a new number n+1 :-)
     185             : 
     186             :     Every item which is already used as unique number
     187             :     will be removed. At the end a list of e.g. {3,6,...,M}
     188             :     exists where the first item represent the lowest free
     189             :     number (in this example 3).
     190             :  */
     191        7638 : ::sal_Int32 NumberedCollection::impl_searchFreeNumber ()
     192             : {
     193             :     // create ordered list of all possible numbers.
     194        7638 :     ::std::vector< ::sal_Int32 > lPossibleNumbers;
     195        7638 :     ::sal_Int32                  c = (::sal_Int32)m_lComponents.size ();
     196        7638 :     ::sal_Int32                  i = 1;
     197             : 
     198             :     // c can't be less then 0 ... otherwise hash.size() has an error :-)
     199             :     // But we need at least n+1 numbers here.
     200        7638 :     c += 1;
     201             : 
     202       17060 :     for (i=1; i<=c; ++i)
     203        9422 :         lPossibleNumbers.push_back (i);
     204             : 
     205             :     // SYNCHRONIZED ->
     206       15276 :     ::osl::ResettableMutexGuard aLock(m_aMutex);
     207             : 
     208       15276 :         TDeadItemList                     lDeadItems;
     209        7638 :         TNumberedItemHash::const_iterator pComponent;
     210             : 
     211       28266 :         for (  pComponent  = m_lComponents.begin ();
     212       18844 :                pComponent != m_lComponents.end   ();
     213             :              ++pComponent                          )
     214             :         {
     215        1784 :             const TNumberedItem&                              rItem = pComponent->second;
     216        1784 :             const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
     217             : 
     218        1784 :             if ( ! xItem.is ())
     219             :             {
     220           0 :                 lDeadItems.push_back(pComponent->first);
     221           0 :                 continue;
     222             :             }
     223             : 
     224        1784 :             ::std::vector< ::sal_Int32 >::iterator pPossible = ::std::find(lPossibleNumbers.begin (), lPossibleNumbers.end (), rItem.nNumber);
     225        1784 :             if (pPossible != lPossibleNumbers.end ())
     226        1784 :                 lPossibleNumbers.erase (pPossible);
     227        1784 :         }
     228             : 
     229        7638 :         impl_cleanUpDeadItems(m_lComponents, lDeadItems);
     230             : 
     231             :         // a) non free numbers ... return INVALID_NUMBER
     232        7638 :         if (lPossibleNumbers.size () < 1)
     233           0 :             return css::frame::UntitledNumbersConst::INVALID_NUMBER;
     234             : 
     235             :         // b) return first free number
     236       15276 :         return *(lPossibleNumbers.begin ());
     237             : 
     238             :     // <- SYNCHRONIZED
     239             : }
     240             : 
     241        9733 : void NumberedCollection::impl_cleanUpDeadItems (      TNumberedItemHash& lItems    ,
     242             :                                                 const TDeadItemList&     lDeadItems)
     243             : {
     244        9733 :     TDeadItemList::const_iterator pIt;
     245             : 
     246       29199 :     for (  pIt  = lDeadItems.begin ();
     247       19466 :            pIt != lDeadItems.end   ();
     248             :          ++pIt                       )
     249             :     {
     250           0 :         const long& rDeadItem = *pIt;
     251           0 :         lItems.erase(rDeadItem);
     252             :     }
     253        9733 : }
     254             : 
     255             : } // namespace comphelper
     256             : 
     257             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10