LCOV - code coverage report
Current view: top level - libreoffice/cppuhelper/source - interfacecontainer.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 285 359 79.4 %
Date: 2012-12-17 Functions: 28 33 84.8 %
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 <cppuhelper/interfacecontainer.hxx>
      22             : #include <cppuhelper/queryinterface.hxx>
      23             : #include <cppuhelper/propshlp.hxx>
      24             : 
      25             : #include <osl/diagnose.h>
      26             : #include <osl/mutex.hxx>
      27             : 
      28             : #include <boost/unordered_map.hpp>
      29             : 
      30             : #include <com/sun/star/lang/XEventListener.hpp>
      31             : 
      32             : 
      33             : using namespace osl;
      34             : using namespace com::sun::star::uno;
      35             : using namespace com::sun::star::lang;
      36             : 
      37             : namespace cppu
      38             : {
      39             : /**
      40             :  * Reallocate the sequence.
      41             :  */
      42      135079 : static void realloc( Sequence< Reference< XInterface > > & rSeq, sal_Int32 nNewLen )
      43             :     SAL_THROW(())
      44             : {
      45      135079 :     rSeq.realloc( nNewLen );
      46      135079 : }
      47             : 
      48             : /**
      49             :  * Remove an element from an interface sequence.
      50             :  */
      51      194148 : static void sequenceRemoveElementAt( Sequence< Reference< XInterface > > & rSeq, sal_Int32 index )
      52             :     SAL_THROW(())
      53             : {
      54      194148 :     sal_Int32 nNewLen = rSeq.getLength() - 1;
      55             : 
      56      194148 :     Sequence< Reference< XInterface > > aDestSeq( rSeq.getLength() - 1 );
      57             :     // getArray on a const sequence is faster
      58      194148 :     const Reference< XInterface > * pSource = ((const Sequence< Reference< XInterface > > &)rSeq).getConstArray();
      59      194148 :     Reference< XInterface > * pDest = aDestSeq.getArray();
      60      194148 :     sal_Int32 i = 0;
      61      448771 :     for( ; i < index; i++ )
      62      254623 :         pDest[i] = pSource[i];
      63      378279 :     for( sal_Int32 j = i ; j < nNewLen; j++ )
      64      184131 :         pDest[j] = pSource[j+1];
      65      194148 :     rSeq = aDestSeq;
      66      194148 : }
      67             : 
      68             : #ifdef _MSC_VER
      69             : #pragma warning( disable: 4786 )
      70             : #endif
      71             : 
      72      239239 : OInterfaceIteratorHelper::OInterfaceIteratorHelper( OInterfaceContainerHelper & rCont_ )
      73             :     SAL_THROW(())
      74      239239 :     : rCont( rCont_ )
      75             : {
      76      239239 :     MutexGuard aGuard( rCont.rMutex );
      77      239239 :     if( rCont.bInUse )
      78             :         // worst case, two iterators at the same time
      79           6 :         rCont.copyAndResetInUse();
      80      239239 :     bIsList = rCont_.bIsList;
      81      239239 :     aData = rCont_.aData;
      82      239239 :     if( bIsList )
      83             :     {
      84       21234 :         rCont.bInUse = sal_True;
      85       21234 :         nRemain = aData.pAsSequence->getLength();
      86             :     }
      87      218005 :     else if( aData.pAsInterface )
      88             :     {
      89      130453 :         aData.pAsInterface->acquire();
      90      130453 :         nRemain = 1;
      91             :     }
      92             :     else
      93       87552 :         nRemain = 0;
      94      239239 : }
      95             : 
      96      239239 : OInterfaceIteratorHelper::~OInterfaceIteratorHelper() SAL_THROW(())
      97             : {
      98             :     sal_Bool bShared;
      99             :     {
     100      239239 :     MutexGuard aGuard( rCont.rMutex );
     101             :     // bResetInUse protect the iterator against recursion
     102      239239 :     bShared = aData.pAsSequence == rCont.aData.pAsSequence && rCont.bIsList;
     103      239239 :     if( bShared )
     104             :     {
     105             :         OSL_ENSURE( rCont.bInUse, "OInterfaceContainerHelper must be in use" );
     106       17269 :         rCont.bInUse = sal_False;
     107      239239 :     }
     108             :     }
     109             : 
     110      239239 :     if( !bShared )
     111             :     {
     112      221970 :         if( bIsList )
     113             :             // Sequence owned by the iterator
     114        3965 :             delete aData.pAsSequence;
     115      218005 :         else if( aData.pAsInterface )
     116             :             // Interface is acquired by the iterator
     117      130453 :             aData.pAsInterface->release();
     118             :     }
     119      239239 : }
     120             : 
     121      187248 : XInterface * OInterfaceIteratorHelper::next() SAL_THROW(())
     122             : {
     123      187248 :     if( nRemain )
     124             :     {
     125      187248 :         nRemain--;
     126      187248 :         if( bIsList )
     127             :             // typecase to const,so the getArray method is faster
     128       56795 :             return aData.pAsSequence->getConstArray()[nRemain].get();
     129      130453 :         else if( aData.pAsInterface )
     130      130453 :             return aData.pAsInterface;
     131             :     }
     132             :     // exception
     133           0 :     return 0;
     134             : }
     135             : 
     136           0 : void OInterfaceIteratorHelper::remove() SAL_THROW(())
     137             : {
     138           0 :     if( bIsList )
     139             :     {
     140             :         OSL_ASSERT( nRemain >= 0 &&
     141             :                     nRemain < aData.pAsSequence->getLength() );
     142           0 :         XInterface * p = aData.pAsSequence->getConstArray()[nRemain].get();
     143           0 :         rCont.removeInterface( * reinterpret_cast< const Reference< XInterface > * >( &p ) );
     144             :     }
     145             :     else
     146             :     {
     147             :         OSL_ASSERT( 0 == nRemain );
     148           0 :         rCont.removeInterface( * reinterpret_cast< const Reference< XInterface > * >(&aData.pAsInterface));
     149             :     }
     150           0 : }
     151             : 
     152      284344 : OInterfaceContainerHelper::OInterfaceContainerHelper( Mutex & rMutex_ ) SAL_THROW(())
     153             :     : rMutex( rMutex_ )
     154             :     , bInUse( sal_False )
     155      284344 :     , bIsList( sal_False )
     156             : {
     157      284344 : }
     158             : 
     159      181025 : OInterfaceContainerHelper::~OInterfaceContainerHelper() SAL_THROW(())
     160             : {
     161             :     OSL_ENSURE( !bInUse, "~OInterfaceContainerHelper but is in use" );
     162      181025 :     if( bIsList )
     163           0 :         delete aData.pAsSequence;
     164      181025 :     else if( aData.pAsInterface )
     165        1501 :         aData.pAsInterface->release();
     166      181025 : }
     167             : 
     168       46111 : sal_Int32 OInterfaceContainerHelper::getLength() const SAL_THROW(())
     169             : {
     170       46111 :     MutexGuard aGuard( rMutex );
     171       46111 :     if( bIsList )
     172        3447 :         return aData.pAsSequence->getLength();
     173       42664 :     else if( aData.pAsInterface )
     174        5730 :         return 1;
     175       36934 :     return 0;
     176             : }
     177             : 
     178        1395 : Sequence< Reference<XInterface> > OInterfaceContainerHelper::getElements() const SAL_THROW(())
     179             : {
     180        1395 :     MutexGuard aGuard( rMutex );
     181        1395 :     if( bIsList )
     182          62 :         return *aData.pAsSequence;
     183        1333 :     else if( aData.pAsInterface )
     184             :     {
     185        1279 :         Reference<XInterface> x( aData.pAsInterface );
     186        1279 :         return Sequence< Reference< XInterface > >( &x, 1 );
     187             :     }
     188          54 :     return Sequence< Reference< XInterface > >();
     189             : }
     190             : 
     191        2441 : void OInterfaceContainerHelper::copyAndResetInUse() SAL_THROW(())
     192             : {
     193             :     OSL_ENSURE( bInUse, "OInterfaceContainerHelper not in use" );
     194        2441 :     if( bInUse )
     195             :     {
     196             :         // this should be the worst case. If a iterator is active
     197             :         // and a new Listener is added.
     198        2441 :         if( bIsList )
     199        2441 :             aData.pAsSequence = new Sequence< Reference< XInterface > >( *aData.pAsSequence );
     200           0 :         else if( aData.pAsInterface )
     201           0 :             aData.pAsInterface->acquire();
     202             : 
     203        2441 :         bInUse = sal_False;
     204             :     }
     205        2441 : }
     206             : 
     207      386178 : sal_Int32 OInterfaceContainerHelper::addInterface( const Reference<XInterface> & rListener ) SAL_THROW(())
     208             : {
     209             :     OSL_ASSERT( rListener.is() );
     210      386178 :     MutexGuard aGuard( rMutex );
     211      386178 :     if( bInUse )
     212         478 :         copyAndResetInUse();
     213             : 
     214      386178 :     if( bIsList )
     215             :     {
     216      135079 :         sal_Int32 nLen = aData.pAsSequence->getLength();
     217      135079 :         realloc( *aData.pAsSequence, nLen +1 );
     218      135079 :         aData.pAsSequence->getArray()[ nLen ] = rListener;
     219      135079 :         return nLen +1;
     220             :     }
     221      251099 :     else if( aData.pAsInterface )
     222             :     {
     223       81625 :         Sequence< Reference< XInterface > > * pSeq = new Sequence< Reference< XInterface > >( 2 );
     224       81625 :         Reference<XInterface> * pArray = pSeq->getArray();
     225       81625 :         pArray[0] = aData.pAsInterface;
     226       81625 :         pArray[1] = rListener;
     227       81625 :         aData.pAsInterface->release();
     228       81625 :         aData.pAsSequence = pSeq;
     229       81625 :         bIsList = sal_True;
     230       81625 :         return 2;
     231             :     }
     232             :     else
     233             :     {
     234      169474 :         aData.pAsInterface = rListener.get();
     235      169474 :         if( rListener.is() )
     236      169474 :             rListener->acquire();
     237      169474 :         return 1;
     238      386178 :     }
     239             : }
     240             : 
     241      330203 : sal_Int32 OInterfaceContainerHelper::removeInterface( const Reference<XInterface> & rListener ) SAL_THROW(())
     242             : {
     243             :     OSL_ASSERT( rListener.is() );
     244      330203 :     MutexGuard aGuard( rMutex );
     245      330203 :     if( bInUse )
     246        1957 :         copyAndResetInUse();
     247             : 
     248      330203 :     if( bIsList )
     249             :     {
     250      194502 :         const Reference<XInterface> * pL = aData.pAsSequence->getConstArray();
     251      194502 :         sal_Int32 nLen = aData.pAsSequence->getLength();
     252             :         sal_Int32 i;
     253      452989 :         for( i = 0; i < nLen; i++ )
     254             :         {
     255             :             // It is not valid to compare the Pointer direkt, but is is is much
     256             :             // more faster.
     257      452635 :             if( pL[i].get() == rListener.get() )
     258             :             {
     259      194148 :                 sequenceRemoveElementAt( *aData.pAsSequence, i );
     260      194148 :                 break;
     261             :             }
     262             :         }
     263             : 
     264      194502 :         if( i == nLen )
     265             :         {
     266             :             // interface not found, use the correct compare method
     267        4218 :             for( i = 0; i < nLen; i++ )
     268             :             {
     269        3864 :                 if( pL[i] == rListener )
     270             :                 {
     271           0 :                     sequenceRemoveElementAt(*aData.pAsSequence, i );
     272           0 :                     break;
     273             :                 }
     274             :             }
     275             :         }
     276             : 
     277      194502 :         if( aData.pAsSequence->getLength() == 1 )
     278             :         {
     279       76149 :             XInterface * p = aData.pAsSequence->getConstArray()[0].get();
     280       76149 :             p->acquire();
     281       76149 :             delete aData.pAsSequence;
     282       76149 :             aData.pAsInterface = p;
     283       76149 :             bIsList = sal_False;
     284       76149 :             return 1;
     285             :         }
     286             :         else
     287      118353 :             return aData.pAsSequence->getLength();
     288             :     }
     289      135701 :     else if( aData.pAsInterface && Reference<XInterface>( aData.pAsInterface ) == rListener )
     290             :     {
     291      135265 :         aData.pAsInterface->release();
     292      135265 :         aData.pAsInterface = 0;
     293             :     }
     294      135701 :     return aData.pAsInterface ? 1 : 0;
     295             : }
     296             : 
     297       64718 : void OInterfaceContainerHelper::disposeAndClear( const EventObject & rEvt ) SAL_THROW(())
     298             : {
     299       64718 :     ClearableMutexGuard aGuard( rMutex );
     300       64718 :     OInterfaceIteratorHelper aIt( *this );
     301             :     // Release container, in case new entries come while disposing
     302             :     OSL_ENSURE( !bIsList || bInUse, "OInterfaceContainerHelper not in use" );
     303       64718 :     if( !bIsList && aData.pAsInterface )
     304        8508 :         aData.pAsInterface->release();
     305             :     // set the member to null, use the iterator to delete the values
     306       64718 :     aData.pAsInterface = NULL;
     307       64718 :     bIsList = sal_False;
     308       64718 :     bInUse = sal_False;
     309       64718 :     aGuard.clear();
     310      143806 :     while( aIt.hasMoreElements() )
     311             :     {
     312             :         try
     313             :         {
     314       14370 :             Reference<XEventListener > xLst( aIt.next(), UNO_QUERY );
     315       14370 :             if( xLst.is() )
     316       13822 :                 xLst->disposing( rEvt );
     317             :         }
     318           0 :         catch ( RuntimeException & )
     319             :         {
     320             :             // be robust, if e.g. a remote bridge has disposed already.
     321             :             // there is no way to delegate the error to the caller :o(.
     322             :         }
     323       64718 :     }
     324       64718 : }
     325             : 
     326             : 
     327           2 : void OInterfaceContainerHelper::clear() SAL_THROW(())
     328             : {
     329           2 :     ClearableMutexGuard aGuard( rMutex );
     330           2 :     OInterfaceIteratorHelper aIt( *this );
     331             :     // Release container, in case new entries come while disposing
     332             :     OSL_ENSURE( !bIsList || bInUse, "OInterfaceContainerHelper not in use" );
     333           2 :     if( !bIsList && aData.pAsInterface )
     334           0 :         aData.pAsInterface->release();
     335             :     // set the member to null, use the iterator to delete the values
     336           2 :     aData.pAsInterface = 0;
     337           2 :     bIsList = sal_False;
     338           2 :     bInUse = sal_False;
     339             :     // release mutex before aIt destructor call
     340           2 :     aGuard.clear();
     341           2 : }
     342             : 
     343             : // specialized class for type
     344             : 
     345             : typedef ::std::vector< std::pair < Type , void* > > t_type2ptr;
     346             : 
     347      136391 : OMultiTypeInterfaceContainerHelper::OMultiTypeInterfaceContainerHelper( Mutex & rMutex_ )
     348             :     SAL_THROW(())
     349      136391 :     : rMutex( rMutex_ )
     350             : {
     351      136391 :     m_pMap = new t_type2ptr();
     352      136391 : }
     353      104498 : OMultiTypeInterfaceContainerHelper::~OMultiTypeInterfaceContainerHelper()
     354             :     SAL_THROW(())
     355             : {
     356      104498 :     t_type2ptr * pMap = (t_type2ptr *)m_pMap;
     357      104498 :     t_type2ptr::iterator iter = pMap->begin();
     358      104498 :     t_type2ptr::iterator end = pMap->end();
     359             : 
     360      224158 :     while( iter != end )
     361             :     {
     362       15162 :         delete (OInterfaceContainerHelper*)(*iter).second;
     363       15162 :         (*iter).second = 0;
     364       15162 :         ++iter;
     365             :     }
     366      104498 :     delete pMap;
     367      104498 : }
     368           0 : Sequence< Type > OMultiTypeInterfaceContainerHelper::getContainedTypes() const
     369             :     SAL_THROW(())
     370             : {
     371           0 :     t_type2ptr * pMap = (t_type2ptr *)m_pMap;
     372             :     t_type2ptr::size_type nSize;
     373             : 
     374           0 :     ::osl::MutexGuard aGuard( rMutex );
     375           0 :     nSize = pMap->size();
     376           0 :     if( nSize )
     377             :     {
     378           0 :         ::com::sun::star::uno::Sequence< Type > aInterfaceTypes( nSize );
     379           0 :         Type * pArray = aInterfaceTypes.getArray();
     380             : 
     381           0 :         t_type2ptr::iterator iter = pMap->begin();
     382           0 :         t_type2ptr::iterator end = pMap->end();
     383             : 
     384           0 :         sal_Int32 i = 0;
     385           0 :         while( iter != end )
     386             :         {
     387             :             // are interfaces added to this container?
     388           0 :             if( ((OInterfaceContainerHelper*)(*iter).second)->getLength() )
     389             :                 // yes, put the type in the array
     390           0 :                 pArray[i++] = (*iter).first;
     391           0 :             ++iter;
     392             :         }
     393           0 :         if( (t_type2ptr::size_type)i != nSize ) {
     394             :             // may be empty container, reduce the sequence to the right size
     395           0 :             aInterfaceTypes = ::com::sun::star::uno::Sequence< Type >( pArray, i );
     396             :         }
     397           0 :         return aInterfaceTypes;
     398             :     }
     399           0 :     return ::com::sun::star::uno::Sequence< Type >();
     400             : }
     401             : 
     402      301818 : static t_type2ptr::iterator findType(t_type2ptr *pMap, const Type & rKey )
     403             : {
     404      301818 :     t_type2ptr::iterator iter = pMap->begin();
     405      301818 :     t_type2ptr::iterator end = pMap->end();
     406             : 
     407      719002 :     while( iter != end )
     408             :     {
     409      210438 :         if (iter->first == rKey)
     410       95072 :             break;
     411      115366 :         ++iter;
     412             :     }
     413      301818 :     return iter;
     414             : }
     415             : 
     416      247124 : OInterfaceContainerHelper * OMultiTypeInterfaceContainerHelper::getContainer( const Type & rKey ) const
     417             :     SAL_THROW(())
     418             : {
     419      247124 :     ::osl::MutexGuard aGuard( rMutex );
     420             : 
     421      247124 :     t_type2ptr * pMap = (t_type2ptr *)m_pMap;
     422      247124 :      t_type2ptr::iterator iter = findType( pMap, rKey );
     423      247124 :     if( iter != pMap->end() )
     424       66756 :             return (OInterfaceContainerHelper*) (*iter).second;
     425      180368 :     return 0;
     426             : }
     427       40630 : sal_Int32 OMultiTypeInterfaceContainerHelper::addInterface(
     428             :     const Type & rKey, const Reference< XInterface > & rListener )
     429             :     SAL_THROW(())
     430             : {
     431       40630 :     ::osl::MutexGuard aGuard( rMutex );
     432       40630 :     t_type2ptr * pMap = (t_type2ptr *)m_pMap;
     433       40630 :     t_type2ptr::iterator iter = findType( pMap, rKey );
     434       40630 :     if( iter == pMap->end() )
     435             :     {
     436       26378 :         OInterfaceContainerHelper * pLC = new OInterfaceContainerHelper( rMutex );
     437       26378 :         pMap->push_back(std::pair<Type, void*>(rKey, pLC));
     438       26378 :         return pLC->addInterface( rListener );
     439             :     }
     440             :     else
     441       14252 :         return ((OInterfaceContainerHelper*)(*iter).second)->addInterface( rListener );
     442             : }
     443       14064 : sal_Int32 OMultiTypeInterfaceContainerHelper::removeInterface(
     444             :     const Type & rKey, const Reference< XInterface > & rListener )
     445             :     SAL_THROW(())
     446             : {
     447       14064 :     ::osl::MutexGuard aGuard( rMutex );
     448             : 
     449             :     // search container with id nUik
     450       14064 :     t_type2ptr * pMap = (t_type2ptr *)m_pMap;
     451       14064 :     t_type2ptr::iterator iter = findType( pMap, rKey );
     452             :         // container found?
     453       14064 :     if( iter != pMap->end() )
     454       14064 :         return ((OInterfaceContainerHelper*)(*iter).second)->removeInterface( rListener );
     455             : 
     456             :     // no container with this id. Always return 0
     457           0 :     return 0;
     458             : }
     459       61001 : void OMultiTypeInterfaceContainerHelper::disposeAndClear( const EventObject & rEvt )
     460             :     SAL_THROW(())
     461             : {
     462       61001 :     t_type2ptr::size_type nSize = 0;
     463       61001 :     OInterfaceContainerHelper ** ppListenerContainers = NULL;
     464             :     {
     465       61001 :         ::osl::MutexGuard aGuard( rMutex );
     466       61001 :         t_type2ptr * pMap = (t_type2ptr *)m_pMap;
     467       61001 :         nSize = pMap->size();
     468       61001 :         if( nSize )
     469             :         {
     470             :             typedef OInterfaceContainerHelper* ppp;
     471        7336 :             ppListenerContainers = new ppp[nSize];
     472             :             //ppListenerContainers = new (ListenerContainer*)[nSize];
     473             : 
     474        7336 :             t_type2ptr::iterator iter = pMap->begin();
     475        7336 :             t_type2ptr::iterator end = pMap->end();
     476             : 
     477        7336 :             t_type2ptr::size_type i = 0;
     478       25160 :             while( iter != end )
     479             :             {
     480       10488 :                 ppListenerContainers[i++] = (OInterfaceContainerHelper*)(*iter).second;
     481       10488 :                 ++iter;
     482             :             }
     483       61001 :         }
     484             :     }
     485             : 
     486             :     // create a copy, because do not fire event in a guarded section
     487       71489 :     for( t_type2ptr::size_type i = 0;
     488             :             i < nSize; i++ )
     489             :     {
     490       10488 :         if( ppListenerContainers[i] )
     491       10488 :             ppListenerContainers[i]->disposeAndClear( rEvt );
     492             :     }
     493             : 
     494       61001 :     delete [] ppListenerContainers;
     495       61001 : }
     496           0 : void OMultiTypeInterfaceContainerHelper::clear()
     497             :     SAL_THROW(())
     498             : {
     499           0 :     ::osl::MutexGuard aGuard( rMutex );
     500           0 :     t_type2ptr * pMap = (t_type2ptr *)m_pMap;
     501           0 :     t_type2ptr::iterator iter = pMap->begin();
     502           0 :     t_type2ptr::iterator end = pMap->end();
     503             : 
     504           0 :     while( iter != end )
     505             :     {
     506           0 :         ((OInterfaceContainerHelper*)(*iter).second)->clear();
     507           0 :         ++iter;
     508           0 :     }
     509           0 : }
     510             : 
     511             : // specialized class for long
     512             : 
     513             : typedef ::std::vector< std::pair < sal_Int32 , void* > > t_long2ptr;
     514             : 
     515         672 : static t_long2ptr::iterator findLong(t_long2ptr *pMap, sal_Int32 nKey )
     516             : {
     517         672 :     t_long2ptr::iterator iter = pMap->begin();
     518         672 :     t_long2ptr::iterator end = pMap->end();
     519             : 
     520        2424 :     while( iter != end )
     521             :     {
     522        1330 :         if (iter->first == nKey)
     523         250 :             break;
     524        1080 :         ++iter;
     525             :     }
     526         672 :     return iter;
     527             : }
     528             : 
     529       30152 : OMultiTypeInterfaceContainerHelperInt32::OMultiTypeInterfaceContainerHelperInt32( Mutex & rMutex_ )
     530             :     SAL_THROW(())
     531             :     : m_pMap( NULL )
     532       30152 :     , rMutex( rMutex_ )
     533             : {
     534             :     // delay pMap allocation until necessary.
     535       30152 : }
     536       15244 : OMultiTypeInterfaceContainerHelperInt32::~OMultiTypeInterfaceContainerHelperInt32()
     537             :     SAL_THROW(())
     538             : {
     539       15244 :     if (!m_pMap)
     540             :         return;
     541             : 
     542          48 :     t_long2ptr * pMap = (t_long2ptr *)m_pMap;
     543          48 :     t_long2ptr::iterator iter = pMap->begin();
     544          48 :     t_long2ptr::iterator end = pMap->end();
     545             : 
     546         205 :     while( iter != end )
     547             :     {
     548         109 :         delete (OInterfaceContainerHelper*)(*iter).second;
     549         109 :         (*iter).second = 0;
     550         109 :         ++iter;
     551             :     }
     552          48 :     delete pMap;
     553       15244 : }
     554           0 : Sequence< sal_Int32 > OMultiTypeInterfaceContainerHelperInt32::getContainedTypes() const
     555             :     SAL_THROW(())
     556             : {
     557           0 :     t_long2ptr * pMap = (t_long2ptr *)m_pMap;
     558             :     t_long2ptr::size_type nSize;
     559             : 
     560           0 :     ::osl::MutexGuard aGuard( rMutex );
     561           0 :     nSize = pMap ? pMap->size() : 0;
     562           0 :     if( nSize )
     563             :     {
     564           0 :         ::com::sun::star::uno::Sequence< sal_Int32 > aInterfaceTypes( nSize );
     565           0 :         sal_Int32 * pArray = aInterfaceTypes.getArray();
     566             : 
     567           0 :         t_long2ptr::iterator iter = pMap->begin();
     568           0 :         t_long2ptr::iterator end = pMap->end();
     569             : 
     570           0 :         sal_Int32 i = 0;
     571           0 :         while( iter != end )
     572             :         {
     573             :             // are interfaces added to this container?
     574           0 :             if( ((OInterfaceContainerHelper*)(*iter).second)->getLength() )
     575             :                 // yes, put the type in the array
     576           0 :                 pArray[i++] = (*iter).first;
     577           0 :             ++iter;
     578             :         }
     579           0 :         if( (t_long2ptr::size_type)i != nSize ) {
     580             :             // may be empty container, reduce the sequence to the right size
     581           0 :             aInterfaceTypes = ::com::sun::star::uno::Sequence< sal_Int32 >( pArray, i );
     582             :         }
     583           0 :         return aInterfaceTypes;
     584             :     }
     585           0 :     return ::com::sun::star::uno::Sequence< sal_Int32 >();
     586             : }
     587        9464 : OInterfaceContainerHelper * OMultiTypeInterfaceContainerHelperInt32::getContainer( const sal_Int32 & rKey ) const
     588             :     SAL_THROW(())
     589             : {
     590        9464 :     ::osl::MutexGuard aGuard( rMutex );
     591             : 
     592        9464 :     if (!m_pMap)
     593        9160 :         return 0;
     594         304 :     t_long2ptr * pMap = (t_long2ptr *)m_pMap;
     595         304 :      t_long2ptr::iterator iter = findLong( pMap, rKey );
     596         304 :     if( iter != pMap->end() )
     597          71 :             return (OInterfaceContainerHelper*) (*iter).second;
     598         233 :     return 0;
     599             : }
     600         249 : sal_Int32 OMultiTypeInterfaceContainerHelperInt32::addInterface(
     601             :     const sal_Int32 & rKey, const Reference< XInterface > & rListener )
     602             :     SAL_THROW(())
     603             : {
     604         249 :     ::osl::MutexGuard aGuard( rMutex );
     605         249 :     if (!m_pMap)
     606          86 :         m_pMap = new t_long2ptr();
     607         249 :     t_long2ptr * pMap = (t_long2ptr *)m_pMap;
     608         249 :     t_long2ptr::iterator iter = findLong( pMap, rKey );
     609         249 :      if( iter == pMap->end() )
     610             :     {
     611         189 :         OInterfaceContainerHelper * pLC = new OInterfaceContainerHelper( rMutex );
     612         189 :         pMap->push_back(std::pair< sal_Int32, void* >(rKey, pLC));
     613         189 :         return pLC->addInterface( rListener );
     614             :     }
     615             :     else
     616          60 :         return ((OInterfaceContainerHelper*)(*iter).second)->addInterface( rListener );
     617             : }
     618         119 : sal_Int32 OMultiTypeInterfaceContainerHelperInt32::removeInterface(
     619             :     const sal_Int32 & rKey, const Reference< XInterface > & rListener )
     620             :     SAL_THROW(())
     621             : {
     622         119 :     ::osl::MutexGuard aGuard( rMutex );
     623             : 
     624         119 :     if (!m_pMap)
     625           0 :         return 0;
     626             :     // search container with id nUik
     627         119 :     t_long2ptr * pMap = (t_long2ptr *)m_pMap;
     628         119 :     t_long2ptr::iterator iter = findLong( pMap, rKey );
     629             :         // container found?
     630         119 :     if( iter != pMap->end() )
     631         119 :         return ((OInterfaceContainerHelper*)(*iter).second)->removeInterface( rListener );
     632             : 
     633             :     // no container with this id. Always return 0
     634           0 :     return 0;
     635             : }
     636         480 : void OMultiTypeInterfaceContainerHelperInt32::disposeAndClear( const EventObject & rEvt )
     637             :     SAL_THROW(())
     638             : {
     639         480 :     t_long2ptr::size_type nSize = 0;
     640         480 :     OInterfaceContainerHelper ** ppListenerContainers = NULL;
     641             :     {
     642         480 :         ::osl::MutexGuard aGuard( rMutex );
     643         480 :         if (!m_pMap)
     644         480 :             return;
     645             : 
     646          66 :         t_long2ptr * pMap = (t_long2ptr *)m_pMap;
     647          66 :         nSize = pMap->size();
     648          66 :         if( nSize )
     649             :         {
     650             :             typedef OInterfaceContainerHelper* ppp;
     651          66 :             ppListenerContainers = new ppp[nSize];
     652             : 
     653          66 :             t_long2ptr::iterator iter = pMap->begin();
     654          66 :             t_long2ptr::iterator end = pMap->end();
     655             : 
     656          66 :             t_long2ptr::size_type i = 0;
     657         267 :             while( iter != end )
     658             :             {
     659         135 :                 ppListenerContainers[i++] = (OInterfaceContainerHelper*)(*iter).second;
     660         135 :                 ++iter;
     661             :             }
     662         480 :         }
     663             :     }
     664             : 
     665             :     // create a copy, because do not fire event in a guarded section
     666         201 :     for( t_long2ptr::size_type i = 0;
     667             :             i < nSize; i++ )
     668             :     {
     669         135 :         if( ppListenerContainers[i] )
     670         135 :             ppListenerContainers[i]->disposeAndClear( rEvt );
     671             :     }
     672             : 
     673          66 :     delete [] ppListenerContainers;
     674             : }
     675           0 : void OMultiTypeInterfaceContainerHelperInt32::clear()
     676             :     SAL_THROW(())
     677             : {
     678           0 :     ::osl::MutexGuard aGuard( rMutex );
     679           0 :     if (!m_pMap)
     680           0 :         return;
     681           0 :     t_long2ptr * pMap = (t_long2ptr *)m_pMap;
     682           0 :     t_long2ptr::iterator iter = pMap->begin();
     683           0 :     t_long2ptr::iterator end = pMap->end();
     684             : 
     685           0 :     while( iter != end )
     686             :     {
     687           0 :         ((OInterfaceContainerHelper*)(*iter).second)->clear();
     688           0 :         ++iter;
     689           0 :     }
     690             : }
     691             : 
     692             : }
     693             : 
     694             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10