LCOV - code coverage report
Current view: top level - include/comphelper - scoped_disposing_ptr.hxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 51 55 92.7 %
Date: 2014-04-11 Functions: 22 42 52.4 %
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             : 
      10             : #ifndef _SCOPED_DISPOSING_PTR
      11             : #define _SCOPED_DISPOSING_PTR
      12             : 
      13             : #include <cppuhelper/implbase1.hxx>
      14             : #include <boost/utility.hpp>
      15             : #include <boost/scoped_ptr.hpp>
      16             : 
      17             : #include <com/sun/star/lang/XComponent.hpp>
      18             : #include <com/sun/star/frame/XDesktop.hpp>
      19             : 
      20             : // for locking SolarMutex: svapp + mutex
      21             : #include <vcl/svapp.hxx>
      22             : #include <osl/mutex.hxx>
      23             : 
      24             : namespace comphelper
      25             : {
      26             : //Similar to boost::scoped_ptr, except additionally releases the ptr on XComponent::disposing and/or XTerminateListener::notifyTermination if supported
      27             : template<class T> class scoped_disposing_ptr : private boost::noncopyable
      28             : {
      29             : private:
      30             :     boost::scoped_ptr<T> m_aItem;
      31             :     ::com::sun::star::uno::Reference< ::com::sun::star::frame::XTerminateListener> m_xTerminateListener;
      32             : public:
      33          71 :     scoped_disposing_ptr( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &rComponent, T * p = 0 )
      34          71 :         : m_aItem(p)
      35             :     {
      36          71 :         m_xTerminateListener = new TerminateListener(rComponent, *this);
      37          71 :     }
      38             : 
      39         182 :     virtual void reset(T * p = 0)
      40             :     {
      41         182 :         m_aItem.reset(p);
      42         182 :     }
      43             : 
      44             :     T & operator*() const
      45             :     {
      46             :         return *m_aItem;
      47             :     }
      48             : 
      49          21 :     T * get() const
      50             :     {
      51          21 :         return m_aItem.get();
      52             :     }
      53             : 
      54       34108 :     T * operator->() const
      55             :     {
      56       34108 :         return m_aItem.get();
      57             :     }
      58             : 
      59       17054 :     operator bool () const
      60             :     {
      61       17054 :         return static_cast< bool >(m_aItem);
      62             :     }
      63             : 
      64          71 :     virtual ~scoped_disposing_ptr()
      65             :     {
      66          71 :         reset();
      67         142 :     }
      68             : private:
      69             :     class TerminateListener : public ::cppu::WeakImplHelper1< ::com::sun::star::frame::XTerminateListener >
      70             :     {
      71             :     private:
      72             :         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > m_xComponent;
      73             :         scoped_disposing_ptr<T>& m_rItem;
      74             :     public:
      75          71 :         TerminateListener(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &rComponent,
      76          71 :             scoped_disposing_ptr<T>& rItem) : m_xComponent(rComponent), m_rItem(rItem)
      77             :         {
      78          71 :             if (m_xComponent.is())
      79             :             {
      80          71 :                 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDesktop> xDesktop(m_xComponent, ::com::sun::star::uno::UNO_QUERY);
      81          71 :                 if (xDesktop.is())
      82          37 :                     xDesktop->addTerminateListener(this);
      83             :                 else
      84          34 :                     m_xComponent->addEventListener(this);
      85             :             }
      86          71 :         }
      87             : 
      88         140 :         virtual ~TerminateListener()
      89             :         {
      90          70 :             if ( m_xComponent.is() )
      91             :             {
      92           0 :                 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDesktop> xDesktop(m_xComponent, ::com::sun::star::uno::UNO_QUERY);
      93           0 :                 if (xDesktop.is())
      94           0 :                     xDesktop->removeTerminateListener(this);
      95             :                 else
      96           0 :                     m_xComponent->removeEventListener(this);
      97             :             }
      98         210 :         }
      99             : 
     100             :     private:
     101             :         // XEventListener
     102          70 :         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& rEvt )
     103             :             throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     104             :         {
     105          70 :             bool shutDown = (rEvt.Source == m_xComponent);
     106             : 
     107          70 :             if (shutDown && m_xComponent.is())
     108             :             {
     109          70 :                 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDesktop> xDesktop(m_xComponent, ::com::sun::star::uno::UNO_QUERY);
     110          70 :                 if (xDesktop.is())
     111          36 :                     xDesktop->removeTerminateListener(this);
     112             :                 else
     113          34 :                     m_xComponent->removeEventListener(this);
     114          70 :                 m_xComponent.clear();
     115             :             }
     116             : 
     117          70 :             if (shutDown)
     118          70 :                m_rItem.reset();
     119          70 :         }
     120             : 
     121             :         // XTerminateListener
     122          18 :         virtual void SAL_CALL queryTermination( const ::com::sun::star::lang::EventObject& )
     123             :             throw(::com::sun::star::frame::TerminationVetoException,
     124             :                   ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     125             :         {
     126          18 :         }
     127             : 
     128          18 :         virtual void SAL_CALL notifyTermination( const ::com::sun::star::lang::EventObject& rEvt )
     129             :             throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     130             :         {
     131          18 :             disposing(rEvt);
     132          18 :         }
     133             :    };
     134             : };
     135             : 
     136             : //Something like an OutputDevice requires the SolarMutex to be taken before use
     137             : //for threadsafety. The user can ensure this, except in the case of its dtor
     138             : //being called from reset due to a terminate on the XComponent being called
     139             : //from an aribitrary thread
     140          37 : template<class T> class scoped_disposing_solar_mutex_reset_ptr
     141             :     : public scoped_disposing_ptr<T>
     142             : {
     143             : public:
     144          37 :     scoped_disposing_solar_mutex_reset_ptr( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &rComponent, T * p = 0 )
     145          37 :         : scoped_disposing_ptr<T>(rComponent, p)
     146             :     {
     147          37 :     }
     148             : 
     149          36 :     virtual void reset(T * p = 0)
     150             :     {
     151          36 :         SolarMutexGuard aGuard;
     152          36 :         scoped_disposing_ptr<T>::reset(p);
     153          36 :     }
     154             : };
     155             : 
     156             : }
     157             : 
     158             : #endif
     159             : 
     160             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10