LCOV - code coverage report
Current view: top level - libreoffice/solver/unxlngi6.pro/inc/comphelper - scoped_disposing_ptr.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 47 55 85.5 %
Date: 2012-12-27 Functions: 33 59 55.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             :  * Version: MPL 1.1 / GPLv3+ / LGPLv3+
       4             :  *
       5             :  * The contents of this file are subject to the Mozilla Public License Version
       6             :  * 1.1 (the "License"); you may not use this file except in compliance with
       7             :  * the License or as specified alternatively below. You may obtain a copy of
       8             :  * the License at http://www.mozilla.org/MPL/
       9             :  *
      10             :  * Software distributed under the License is distributed on an "AS IS" basis,
      11             :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12             :  * for the specific language governing rights and limitations under the
      13             :  * License.
      14             :  *
      15             :  * Major Contributor(s):
      16             :  * Copyright (C) 2011 Red Hat, Inc., Caolán McNamara <caolanm@redhat.com>
      17             :  *  (initial developer)
      18             :  *
      19             :  * All Rights Reserved.
      20             :  *
      21             :  * For minor contributions see the git repository.
      22             :  *
      23             :  * Alternatively, the contents of this file may be used under the terms of
      24             :  * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
      25             :  * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
      26             :  * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
      27             :  * instead of those above.
      28             :  */
      29             : 
      30             : #ifndef _SCOPED_DISPOSING_PTR
      31             : #define _SCOPED_DISPOSING_PTR
      32             : 
      33             : #include <cppuhelper/implbase1.hxx>
      34             : #include <boost/utility.hpp>
      35             : #include <boost/scoped_ptr.hpp>
      36             : 
      37             : #include <com/sun/star/lang/XComponent.hpp>
      38             : #include <com/sun/star/frame/XDesktop.hpp>
      39             : 
      40             : // for locking SolarMutex: svapp + mutex
      41             : #include <vcl/svapp.hxx>
      42             : #include <osl/mutex.hxx>
      43             : 
      44             : namespace comphelper
      45             : {
      46             : //Similar to boost::scoped_ptr, except additionally releases the ptr on XComponent::disposing and/or XTerminateListener::notifyTermination if supported
      47             : template<class T> class scoped_disposing_ptr : private boost::noncopyable
      48             : {
      49             : private:
      50             :     boost::scoped_ptr<T> m_aItem;
      51             :     ::com::sun::star::uno::Reference< ::com::sun::star::frame::XTerminateListener> m_xTerminateListener;
      52             : public:
      53          35 :     scoped_disposing_ptr( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &rComponent, T * p = 0 )
      54          35 :         : m_aItem(p)
      55             :     {
      56          35 :         m_xTerminateListener = new TerminateListener(rComponent, *this);
      57          35 :     }
      58             : 
      59          95 :     virtual void reset(T * p = 0)
      60             :     {
      61          95 :         m_aItem.reset(p);
      62          95 :     }
      63             : 
      64             :     T & operator*() const
      65             :     {
      66             :         return *m_aItem;
      67             :     }
      68             : 
      69        7442 :     T * get() const
      70             :     {
      71        7442 :         return m_aItem.get();
      72             :     }
      73             : 
      74         502 :     T * operator->() const
      75             :     {
      76         502 :         return m_aItem.get();
      77             :     }
      78             : 
      79        7681 :     operator bool () const
      80             :     {
      81        7681 :         return m_aItem;
      82             :     }
      83             : 
      84          35 :     virtual ~scoped_disposing_ptr()
      85             :     {
      86          70 :         reset();
      87         105 :     }
      88             : private:
      89             :     class TerminateListener : public ::cppu::WeakImplHelper1< ::com::sun::star::frame::XTerminateListener >
      90             :     {
      91             :     private:
      92             :         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > m_xComponent;
      93             :         scoped_disposing_ptr<T>& m_rItem;
      94             :     public:
      95          35 :         TerminateListener(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &rComponent,
      96          35 :             scoped_disposing_ptr<T>& rItem) : m_xComponent(rComponent), m_rItem(rItem)
      97             :         {
      98          35 :             if (m_xComponent.is())
      99             :             {
     100          35 :                 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDesktop> xDesktop(m_xComponent, ::com::sun::star::uno::UNO_QUERY);
     101          35 :                 if (xDesktop.is())
     102          10 :                     xDesktop->addTerminateListener(this);
     103             :                 else
     104          25 :                     m_xComponent->addEventListener(this);
     105             :             }
     106          35 :         }
     107             : 
     108          70 :         ~TerminateListener()
     109             :         {
     110          35 :             if ( m_xComponent.is() )
     111             :             {
     112           0 :                 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDesktop> xDesktop(m_xComponent, ::com::sun::star::uno::UNO_QUERY);
     113           0 :                 if (xDesktop.is())
     114           0 :                     xDesktop->removeTerminateListener(this);
     115             :                 else
     116          35 :                     m_xComponent->removeEventListener(this);
     117             :             }
     118         140 :         }
     119             : 
     120             :     private:
     121             :         // XEventListener
     122          35 :         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& rEvt )
     123             :             throw (::com::sun::star::uno::RuntimeException)
     124             :         {
     125          35 :             bool shutDown = (rEvt.Source == m_xComponent);
     126             : 
     127          35 :             if (shutDown && m_xComponent.is())
     128             :             {
     129          35 :                 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDesktop> xDesktop(m_xComponent, ::com::sun::star::uno::UNO_QUERY);
     130          35 :                 if (xDesktop.is())
     131          10 :                     xDesktop->removeTerminateListener(this);
     132             :                 else
     133          25 :                     m_xComponent->removeEventListener(this);
     134          35 :                 m_xComponent.clear();
     135             :             }
     136             : 
     137          35 :             if (shutDown)
     138          35 :                m_rItem.reset();
     139          35 :         }
     140             : 
     141             :         // XTerminateListener
     142           0 :         virtual void SAL_CALL queryTermination( const ::com::sun::star::lang::EventObject& )
     143             :             throw(::com::sun::star::frame::TerminationVetoException,
     144             :                   ::com::sun::star::uno::RuntimeException)
     145             :         {
     146           0 :         }
     147             : 
     148           0 :         virtual void SAL_CALL notifyTermination( const ::com::sun::star::lang::EventObject& rEvt )
     149             :             throw (::com::sun::star::uno::RuntimeException)
     150             :         {
     151           0 :             disposing(rEvt);
     152           0 :         }
     153             :    };
     154             : };
     155             : 
     156             : //Something like an OutputDevice requires the SolarMutex to be taken before use
     157             : //for threadsafety. The user can ensure this, except in the case of its dtor
     158             : //being called from reset due to a terminate on the XComponent being called
     159             : //from an aribitrary thread
     160          28 : template<class T> class scoped_disposing_solar_mutex_reset_ptr
     161             :     : public scoped_disposing_ptr<T>
     162             : {
     163             : public:
     164          28 :     scoped_disposing_solar_mutex_reset_ptr( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &rComponent, T * p = 0 )
     165          28 :         : scoped_disposing_ptr<T>(rComponent, p)
     166             :     {
     167          28 :     }
     168             : 
     169          46 :     virtual void reset(T * p = 0)
     170             :     {
     171          46 :         SolarMutexGuard aGuard;
     172          46 :         scoped_disposing_ptr<T>::reset(p);
     173          46 :     }
     174             : };
     175             : 
     176             : }
     177             : 
     178             : #endif
     179             : 
     180             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10