LCOV - code coverage report
Current view: top level - unotools/source/misc - sharedunocomponent.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 33 43 76.7 %
Date: 2015-06-13 12:38:46 Functions: 9 12 75.0 %
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 <sal/config.h>
      21             : #include <osl/diagnose.h>
      22             : 
      23             : #include <unotools/sharedunocomponent.hxx>
      24             : #include <com/sun/star/lang/XComponent.hpp>
      25             : #include <com/sun/star/util/XCloseable.hpp>
      26             : #include <cppuhelper/implbase1.hxx>
      27             : #include <tools/debug.hxx>
      28             : 
      29             : namespace utl
      30             : {
      31             : 
      32             :     using ::com::sun::star::uno::XInterface;
      33             :     using ::com::sun::star::uno::Reference;
      34             :     using ::com::sun::star::uno::Exception;
      35             :     using ::com::sun::star::uno::UNO_QUERY;
      36             :     using ::com::sun::star::uno::RuntimeException;
      37             :     using ::com::sun::star::lang::XComponent;
      38             :     using ::com::sun::star::lang::EventObject;
      39             :     using ::com::sun::star::util::XCloseable;
      40             :     using ::com::sun::star::util::XCloseListener;
      41             :     using ::com::sun::star::util::CloseVetoException;
      42             : 
      43             :     //= DisposableComponent
      44             : 
      45         348 :     DisposableComponent::DisposableComponent( const Reference< XInterface >& _rxComponent )
      46         348 :         :m_xComponent( _rxComponent, UNO_QUERY )
      47             :     {
      48             :         DBG_ASSERT( m_xComponent.is() || !_rxComponent.is(), "DisposableComponent::DisposableComponent: should be an XComponent!" );
      49         348 :     }
      50             : 
      51         694 :     DisposableComponent::~DisposableComponent()
      52             :     {
      53         347 :         if ( m_xComponent.is() )
      54             :         {
      55             :             try
      56             :             {
      57         202 :                 m_xComponent->dispose();
      58             :             }
      59           0 :             catch( const Exception& )
      60             :             {
      61             :                 OSL_FAIL( "DisposableComponent::~DisposableComponent: caught an exception!" );
      62             :             }
      63         202 :             m_xComponent.clear();
      64             :         }
      65         347 :     }
      66             : 
      67             :     typedef ::cppu::WeakImplHelper1 <   XCloseListener
      68             :                                     >   CloseableComponentImpl_Base;
      69             :     class CloseableComponentImpl : public CloseableComponentImpl_Base
      70             :     {
      71             :     private:
      72             :         Reference< XCloseable > m_xCloseable;
      73             : 
      74             :         CloseableComponentImpl(const CloseableComponentImpl&) SAL_DELETED_FUNCTION;
      75             :         CloseableComponentImpl& operator=(const CloseableComponentImpl&) SAL_DELETED_FUNCTION;
      76             : 
      77             :     public:
      78             :         explicit CloseableComponentImpl( const Reference< XInterface >& _rxComponent );
      79             : 
      80             :         /** closes the component
      81             : 
      82             :             @nofail
      83             :         */
      84             :         void    nf_closeComponent();
      85             : 
      86             :     protected:
      87             :         virtual ~CloseableComponentImpl();
      88             : 
      89             :         // XCloseListener overridables
      90             :         virtual void SAL_CALL queryClosing( const EventObject& Source, sal_Bool GetsOwnership ) throw (CloseVetoException, RuntimeException, std::exception) SAL_OVERRIDE;
      91             :         virtual void SAL_CALL notifyClosing( const EventObject& Source ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      92             : 
      93             :         // XEventListener overridables
      94             :         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      95             : 
      96             :     private:
      97             :         /** starts or stops being a CloseListener at the component
      98             : 
      99             :             Only to be called upon construction of the instance, or when the component
     100             :             is to be closed.
     101             : 
     102             :         @nofail
     103             :         */
     104             :         void    impl_nf_switchListening( bool _bListen );
     105             :     };
     106             : 
     107           9 :     CloseableComponentImpl::CloseableComponentImpl( const Reference< XInterface >& _rxComponent )
     108           9 :         :m_xCloseable( _rxComponent, UNO_QUERY )
     109             :     {
     110             :         DBG_ASSERT( m_xCloseable.is() || !_rxComponent.is(), "CloseableComponentImpl::CloseableComponentImpl: component is not an XCloseable!" );
     111           9 :         impl_nf_switchListening( true );
     112           9 :     }
     113             : 
     114          27 :     CloseableComponentImpl::~CloseableComponentImpl()
     115             :     {
     116           9 :         nf_closeComponent();
     117          18 :     }
     118             : 
     119          18 :     void CloseableComponentImpl::nf_closeComponent()
     120             :     {
     121          18 :         if ( !m_xCloseable.is() )
     122             :             // nothing to do
     123          27 :             return;
     124             : 
     125             :         // stop listening
     126           9 :         impl_nf_switchListening( false );
     127             : 
     128             :         // close
     129             :         try
     130             :         {
     131           9 :             m_xCloseable->close( sal_True );
     132             :         }
     133           0 :         catch( const CloseVetoException& ) { /* fine */ }
     134           0 :         catch( const Exception& )
     135             :         {
     136             :             OSL_FAIL( "CloseableComponentImpl::nf_closeComponent: caught an unexpected exception!" );
     137             :         }
     138             : 
     139             :         // reset
     140           9 :         m_xCloseable.clear();
     141             :     }
     142             : 
     143          18 :     void CloseableComponentImpl::impl_nf_switchListening( bool _bListen )
     144             :     {
     145          18 :         if ( !m_xCloseable.is() )
     146          18 :             return;
     147             : 
     148             :         try
     149             :         {
     150          18 :             if ( _bListen )
     151           9 :                 m_xCloseable->addCloseListener( this );
     152             :             else
     153           9 :                 m_xCloseable->removeCloseListener( this );
     154             :         }
     155           0 :         catch( const Exception& )
     156             :         {
     157             :             OSL_FAIL( "CloseableComponentImpl::impl_nf_switchListening: caught an exception!" );
     158             :         }
     159             :     }
     160             : 
     161           0 :     void SAL_CALL CloseableComponentImpl::queryClosing( const EventObject&
     162             :     #ifdef DBG_UTIL
     163             :     Source
     164             :     #endif
     165             :     , sal_Bool /*GetsOwnership*/ ) throw (CloseVetoException, RuntimeException, std::exception)
     166             :     {
     167             :         // as long as we live, somebody wants to keep the object alive. So, veto the
     168             :         // closing
     169             :         DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::queryClosing: where did this come from?" );
     170           0 :         throw CloseVetoException();
     171             :     }
     172             : 
     173           0 :     void SAL_CALL CloseableComponentImpl::notifyClosing( const EventObject&
     174             :     #ifdef DBG_UTIL
     175             :     Source
     176             :     #endif
     177             :     ) throw (RuntimeException, std::exception)
     178             :     {
     179             :         DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::notifyClosing: where did this come from?" );
     180             : 
     181             :         // this should be unreachable: As long as we're a CloseListener, we veto the closing. If we're going
     182             :         // to close the component ourself, then we revoke ourself as listener *before* the close call. So,
     183             :         // if this here fires, something went definitely wrong.
     184             :         OSL_FAIL( "CloseableComponentImpl::notifyClosing: unreachable!" );
     185           0 :     }
     186             : 
     187           0 :     void SAL_CALL CloseableComponentImpl::disposing( const EventObject&
     188             :     #ifdef DBG_UTIL
     189             :     Source
     190             :     #endif
     191             :     ) throw (RuntimeException, std::exception)
     192             :     {
     193             :         DBG_ASSERT( Source.Source == m_xCloseable, "CloseableComponentImpl::disposing: where did this come from?" );
     194             :         OSL_FAIL( "CloseableComponentImpl::disposing: unreachable!" );
     195             :             // same reasoning for this assertion as in ->notifyClosing
     196           0 :     }
     197             : 
     198           9 :     CloseableComponent::CloseableComponent( const Reference< XInterface >& _rxComponent )
     199           9 :         :m_pImpl( new CloseableComponentImpl( _rxComponent ) )
     200             :     {
     201           9 :     }
     202             : 
     203          18 :     CloseableComponent::~CloseableComponent()
     204             :     {
     205             :         // close the component, deliver ownership to anybody who wants to veto the close
     206           9 :         m_pImpl->nf_closeComponent();
     207           9 :     }
     208             : 
     209             : }   // namespace utl
     210             : 
     211             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11