LCOV - code coverage report
Current view: top level - cppuhelper/source - component.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 70 84 83.3 %
Date: 2014-04-11 Functions: 11 12 91.7 %
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 <rtl/string.hxx>
      21             : #include <osl/diagnose.h>
      22             : #include <cppuhelper/component.hxx>
      23             : #include <cppuhelper/queryinterface.hxx>
      24             : #include <cppuhelper/typeprovider.hxx>
      25             : #include "com/sun/star/uno/RuntimeException.hpp"
      26             : 
      27             : using namespace osl;
      28             : using namespace com::sun::star;
      29             : using namespace com::sun::star::uno;
      30             : using namespace com::sun::star::lang;
      31             : 
      32             : using ::rtl::OUString;
      33             : using ::rtl::OString;
      34             : using ::rtl::OUStringToOString;
      35             : 
      36             : namespace cppu
      37             : {
      38             : 
      39             : 
      40             : //  class OComponentHelper
      41             : 
      42             : 
      43       13364 : OComponentHelper::OComponentHelper( Mutex & rMutex ) SAL_THROW(())
      44       13364 :     : rBHelper( rMutex )
      45             : {
      46       13364 : }
      47        9444 : OComponentHelper::~OComponentHelper() SAL_THROW( (RuntimeException) )
      48             : {
      49        9444 : }
      50             : 
      51      184706 : Any OComponentHelper::queryInterface( Type const & rType ) throw (RuntimeException, std::exception)
      52             : {
      53      184706 :     return OWeakAggObject::queryInterface( rType );
      54             : }
      55      145553 : Any OComponentHelper::queryAggregation( Type const & rType ) throw (RuntimeException, std::exception)
      56             : {
      57      145553 :     if (rType == ::getCppuType( (Reference< lang::XComponent > const *)0 ))
      58             :     {
      59        2707 :         void * p = static_cast< lang::XComponent * >( this );
      60        2707 :         return Any( &p, rType );
      61             :     }
      62      142846 :     else if (rType == ::getCppuType( (Reference< lang::XTypeProvider > const *)0 ))
      63             :     {
      64         203 :         void * p = static_cast< lang::XTypeProvider * >( this );
      65         203 :         return Any( &p, rType );
      66             :     }
      67      142643 :     return OWeakAggObject::queryAggregation( rType );
      68             : }
      69     1588969 : void OComponentHelper::acquire() throw ()
      70             : {
      71     1588969 :     OWeakAggObject::acquire();
      72     1588976 : }
      73             : 
      74     1552870 : void OComponentHelper::release() throw()
      75             : {
      76     1552870 :     Reference<XInterface > x( xDelegator );
      77     1552870 :     if (! x.is())
      78             :     {
      79     1502490 :         if (osl_atomic_decrement( &m_refCount ) == 0)
      80             :         {
      81       15278 :             if (! rBHelper.bDisposed)
      82             :             {
      83             :                 // *before* again incrementing our ref count, ensure that our weak connection point
      84             :                 // will not create references to us anymore (via XAdapter::queryAdapted)
      85        6981 :                 disposeWeakConnectionPoint();
      86             : 
      87        6981 :                 Reference<XInterface > xHoldAlive( *this );
      88             :                 // First dispose
      89             :                 try
      90             :                 {
      91        6981 :                     dispose();
      92             :                 }
      93           0 :                 catch (::com::sun::star::uno::RuntimeException & exc)
      94             :                 {
      95             :                     // release should not throw exceptions
      96             : #if OSL_DEBUG_LEVEL > 0
      97             :                     OString msg( OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
      98             :                     OSL_FAIL( msg.getStr() );
      99             : #else
     100             :                     (void) exc; // avoid warning about unused variable
     101             : #endif
     102             :                 }
     103             : 
     104             :                 // only the alive ref holds the object
     105             :                 OSL_ASSERT( m_refCount == 1 );
     106             :                 // destroy the object if xHoldAlive decrement the refcount to 0
     107     1559851 :                 return;
     108             :             }
     109             :         }
     110             :         // restore the reference count
     111     1495509 :         osl_atomic_increment( &m_refCount );
     112             :     }
     113     1545889 :     OWeakAggObject::release();
     114             : }
     115             : 
     116         160 : Sequence< Type > OComponentHelper::getTypes() throw (RuntimeException, std::exception)
     117             : {
     118             :     static OTypeCollection * s_pTypes = 0;
     119         160 :     if (! s_pTypes)
     120             :     {
     121           4 :         MutexGuard aGuard( Mutex::getGlobalMutex() );
     122           4 :         if (! s_pTypes)
     123             :         {
     124             :             static OTypeCollection s_aTypes(
     125           4 :                 ::getCppuType( (const Reference< lang::XComponent > *)0 ),
     126           4 :                 ::getCppuType( (const Reference< lang::XTypeProvider > *)0 ),
     127           4 :                 ::getCppuType( (const Reference< XAggregation > *)0 ),
     128           8 :                 ::getCppuType( (const Reference< XWeak > *)0 ) );
     129           4 :             s_pTypes = &s_aTypes;
     130           4 :         }
     131             :     }
     132         160 :     return s_pTypes->getTypes();
     133             : }
     134             : 
     135             : // XComponent
     136        7402 : void OComponentHelper::disposing()
     137             : {
     138        7402 : }
     139             : 
     140             : // XComponent
     141        8500 : void OComponentHelper::dispose()
     142             :     throw(::com::sun::star::uno::RuntimeException, std::exception)
     143             : {
     144             :     // An frequently programming error is to release the last
     145             :     // reference to this object in the disposing message.
     146             :     // Make it rubust, hold a self Reference.
     147        8500 :     Reference<XComponent > xSelf( this );
     148             : 
     149             :     // Guard dispose against multible threading
     150             :     // Remark: It is an error to call dispose more than once
     151        8500 :     bool bDoDispose = false;
     152             :     {
     153        8500 :     MutexGuard aGuard( rBHelper.rMutex );
     154        8500 :     if( !rBHelper.bDisposed && !rBHelper.bInDispose )
     155             :     {
     156             :         // only one call go into this section
     157        8487 :         rBHelper.bInDispose = sal_True;
     158        8487 :         bDoDispose = true;
     159        8500 :     }
     160             :     }
     161             : 
     162             :     // Do not hold the mutex because we are broadcasting
     163        8500 :     if( bDoDispose )
     164             :     {
     165             :         // Create an event with this as sender
     166             :         try
     167             :         {
     168             :             try
     169             :             {
     170             :                 Reference<XInterface > xSource(
     171        8487 :                     Reference<XInterface >::query( (XComponent *)this ) );
     172       16974 :                 EventObject aEvt;
     173        8487 :                 aEvt.Source = xSource;
     174             :                 // inform all listeners to release this object
     175             :                 // The listener container are automaticly cleared
     176        8487 :                 rBHelper.aLC.disposeAndClear( aEvt );
     177             :                 // notify subclasses to do their dispose
     178       16974 :                 disposing();
     179             :             }
     180           0 :             catch (...)
     181             :             {
     182           0 :                 MutexGuard aGuard( rBHelper.rMutex );
     183             :                 // bDispose and bInDisposing must be set in this order:
     184           0 :                 rBHelper.bDisposed = sal_True;
     185           0 :                 rBHelper.bInDispose = sal_False;
     186           0 :                 throw;
     187             :             }
     188        8487 :             MutexGuard aGuard( rBHelper.rMutex );
     189             :             // bDispose and bInDisposing must be set in this order:
     190        8487 :             rBHelper.bDisposed = sal_True;
     191        8487 :             rBHelper.bInDispose = sal_False;
     192             :         }
     193           0 :         catch (RuntimeException &)
     194             :         {
     195           0 :             throw;
     196             :         }
     197           0 :         catch (Exception & exc)
     198             :         {
     199             :             throw RuntimeException(
     200             :                 OUString(
     201           0 :                               "unexpected UNO exception caught: ") +
     202           0 :                 exc.Message, Reference< XInterface >() );
     203             :         }
     204             :     }
     205             :     else
     206             :     {
     207             :         // in a multithreaded environment, it can't be avoided
     208             :         // that dispose is called twice.
     209             :         // However this condition is traced, because it MAY indicate an error.
     210             :         OSL_TRACE( "OComponentHelper::dispose() - dispose called twice" );
     211        8500 :     }
     212        8500 : }
     213             : 
     214             : // XComponent
     215        1447 : void OComponentHelper::addEventListener(
     216             :     const Reference<XEventListener > & rxListener )
     217             :     throw(::com::sun::star::uno::RuntimeException, std::exception)
     218             : {
     219        1447 :     ClearableMutexGuard aGuard( rBHelper.rMutex );
     220        1447 :     if (rBHelper.bDisposed || rBHelper.bInDispose)
     221             :     {
     222           0 :         aGuard.clear();
     223           0 :         Reference< XInterface > x( (XComponent *)this, UNO_QUERY );
     224           0 :         rxListener->disposing( EventObject( x ) );
     225             :     }
     226             :     else
     227             :     {
     228        1447 :         rBHelper.addListener( ::getCppuType( &rxListener ) , rxListener );
     229        1447 :     }
     230        1447 : }
     231             : 
     232             : // XComponent
     233         979 : void OComponentHelper::removeEventListener(
     234             :     const Reference<XEventListener > & rxListener )
     235             :     throw(::com::sun::star::uno::RuntimeException, std::exception)
     236             : {
     237         979 :     rBHelper.removeListener( ::getCppuType( &rxListener ) , rxListener );
     238         979 : }
     239             : 
     240             : }
     241             : 
     242             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10