LCOV - code coverage report
Current view: top level - solver/unxlngi6.pro/inc/unotools - sharedunocomponent.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 27 45 60.0 %
Date: 2012-08-25 Functions: 34 81 42.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 7 25 28.0 %

           Branch data     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                 :            : #ifndef UNOTOOLS_INC_SHAREDUNOCOMPONENT_HXX
      21                 :            : #define UNOTOOLS_INC_SHAREDUNOCOMPONENT_HXX
      22                 :            : 
      23                 :            : #include "unotoolsdllapi.h"
      24                 :            : 
      25                 :            : #include <boost/shared_ptr.hpp>
      26                 :            : #include <com/sun/star/uno/Reference.hxx>
      27                 :            : #include <rtl/ref.hxx>
      28                 :            : 
      29                 :            : namespace com { namespace sun { namespace star {
      30                 :            :     namespace lang {
      31                 :            :         class XComponent;
      32                 :            :     }
      33                 :            : } } }
      34                 :            : //............................................................................
      35                 :            : namespace utl
      36                 :            : {
      37                 :            : //............................................................................
      38                 :            : 
      39                 :            :     //========================================================================
      40                 :            :     //= DisposableComponent
      41                 :            :     //========================================================================
      42                 :            :     /** is a class which controls lifetime of an UNO component via ->XComponent::dispose
      43                 :            : 
      44                 :            :         You'll usually never use this class directly, but only as parameter for a
      45                 :            :         ->SharedUNOComponent class.
      46                 :            :     */
      47                 :            :     class UNOTOOLS_DLLPUBLIC DisposableComponent
      48                 :            :     {
      49                 :            :         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >
      50                 :            :             m_xComponent;
      51                 :            : 
      52                 :            :     public:
      53                 :            :         /** constructs a ->DisposableComponent instance
      54                 :            : 
      55                 :            :         @param _rxComponent
      56                 :            :             the component whose life time should be controlled by the instance. Must not be <NULL/>.
      57                 :            :         */
      58                 :            :         DisposableComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxComponent );
      59                 :            : 
      60                 :            :         /** disposes the component represented by the instance
      61                 :            : 
      62                 :            :             The component is queried for ->XComponent(which <em>must</em> be supported),
      63                 :            :             and ->XComponent::dispose is invoked. A failure of this invocation (e.g. a thrown
      64                 :            :             exception) is silenced in release builds, and reported in debug builds.
      65                 :            :         */
      66                 :            :         ~DisposableComponent();
      67                 :            : 
      68                 :            :     private:
      69                 :            :         DisposableComponent();                                          // never implemented
      70                 :            :         DisposableComponent( const DisposableComponent& );              // never implemented
      71                 :            :         DisposableComponent& operator=( const DisposableComponent& );   // never implemented
      72                 :            :     };
      73                 :            : 
      74                 :            :     //========================================================================
      75                 :            :     //= CloseableComponent
      76                 :            :     //========================================================================
      77                 :            :     class CloseableComponentImpl;
      78                 :            :     /** is a class which controls lifetime of an UNO component via ->XCloseable::close
      79                 :            : 
      80                 :            :         You'll usually never use this class directly, but only as parameter for a
      81                 :            :         ->SharedUNOComponent class.
      82                 :            :     */
      83                 :            :     class UNOTOOLS_DLLPUBLIC CloseableComponent
      84                 :            :     {
      85                 :            :     private:
      86                 :            :         /** Our IMPL class.
      87                 :            :         */
      88                 :            :         ::rtl::Reference< CloseableComponentImpl >  m_pImpl;
      89                 :            : 
      90                 :            :     public:
      91                 :            :         /** constructs a ->CloseableComponent instance
      92                 :            : 
      93                 :            :         @param _rxComponent
      94                 :            :             the component whose life time should be controlled by the instance. Must not be <NULL/>.
      95                 :            :         */
      96                 :            :         CloseableComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxComponent );
      97                 :            : 
      98                 :            :         /** destroys resources associated with this instance, and disposes the component
      99                 :            : 
     100                 :            :             The component is queried for ->XCloseable (which <em>must</em> be supported),
     101                 :            :             and ->XCloseable::close is invoked, with delivering the ownership.
     102                 :            :             If the invocation fails with a ->CloseVetoException, this is ignored, since in
     103                 :            :             this case the vetoing instance took the ownership.
     104                 :            : 
     105                 :            :             Any other failure will be reported in a debug version via assertion mechanisms,
     106                 :            :             and silenced in release builds.
     107                 :            :         */
     108                 :            :         ~CloseableComponent();
     109                 :            : 
     110                 :            :     private:
     111                 :            :         CloseableComponent();                                           // never implemented
     112                 :            :         CloseableComponent( const CloseableComponent& );                // never implemented
     113                 :            :         CloseableComponent& operator=( const CloseableComponent& );     // never implemented
     114                 :            :     };
     115                 :            : 
     116                 :            :     //========================================================================
     117                 :            :     //= SharedUNOComponent
     118                 :            :     //========================================================================
     119                 :            :     /** is a helper class for sharing ownership of a UNO component
     120                 :            : 
     121                 :            :         If you need to share an UNO component, which normally needs a dedicated owner,
     122                 :            :         and is lifetime controlled by an explicit disposal action (not necessarily ->XComponent::dispose,
     123                 :            :         but <em>any</em> explicit method call, after which the object is considered
     124                 :            :         to be disposed), between different classes, ->SharedUNOComponent is what you need.
     125                 :            : 
     126                 :            :         Instead of passing around a <code>Reference&lt; XFoo &gt;</code>, and bothering
     127                 :            :         with ownership and disposal, you just use a <code>SharedUNOComponent&lt; XFoo &gt;</code>.
     128                 :            :         This instance can be passed around, including copying, and in nearly all respects behaves
     129                 :            :         like the original <code>Reference&lt; XFoo &gt;</code>. However, when the last
     130                 :            :         ->SharedUNOComponent referencing a certain <code>Reference&lt; XFoo &gt;</code> dies, it
     131                 :            :         will automatically get rid of the object held by this reference.
     132                 :            : 
     133                 :            :     @param INTERFACE
     134                 :            :         the UNO interface type as which the component should be held
     135                 :            : 
     136                 :            :     @param COMPONENT_HOLDER
     137                 :            :         a class which can be used to represent and dispose a UNO component.
     138                 :            :         The class must support (maybe explicit only) construction from a
     139                 :            :         <code>Reference&lt; INTERFACE &gt;</code>, and destruction. Upon destruction,
     140                 :            :         the class must dispose (by any suitable means) the component instance it was
     141                 :            :         constructed with.
     142                 :            :     */
     143                 :            :     template < class INTERFACE, class COMPONENT = DisposableComponent >
     144                 :        962 :     class SharedUNOComponent
     145                 :            :     {
     146                 :            :     private:
     147                 :            :         typedef COMPONENT                           Component;
     148                 :            :         typedef ::boost::shared_ptr< Component >    ComponentPointer;
     149                 :            : 
     150                 :            :     private:
     151                 :            :         ComponentPointer                                m_pComponent;
     152                 :            :         ::com::sun::star::uno::Reference< INTERFACE >   m_xTypedComponent;
     153                 :            : 
     154                 :            :     public:
     155                 :            :         enum AssignmentMode
     156                 :            :         {
     157                 :            :             TakeOwnership,
     158                 :            :             NoTakeOwnership
     159                 :            :         };
     160                 :            : 
     161                 :            :     public:
     162                 :        962 :         inline  SharedUNOComponent()
     163                 :        962 :         {
     164                 :        962 :         }
     165                 :            : 
     166                 :          4 :         explicit inline  SharedUNOComponent( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode eMode = TakeOwnership )
     167                 :          4 :         {
     168         [ +  - ]:          4 :             reset( _rxComponent, eMode );
     169                 :          4 :         }
     170                 :            : 
     171                 :            : #ifndef EXCEPTIONS_OFF
     172                 :            :         inline SharedUNOComponent( const ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
     173                 :            :         {
     174                 :            :             set( _pInterface, _queryThrow );
     175                 :            :         }
     176                 :            : 
     177                 :          0 :         inline SharedUNOComponent( const ::com::sun::star::uno::BaseReference & _rRef, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
     178                 :          0 :         {
     179         [ #  # ]:          0 :             set( _rRef, _queryThrow );
     180                 :          0 :         }
     181                 :            : 
     182                 :            :         inline SharedUNOComponent( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
     183                 :            :         {
     184                 :            :             set( _rAny, _queryThrow );
     185                 :            :         }
     186                 :            : 
     187                 :            :         inline  SharedUNOComponent( const SharedUNOComponent& _rxComponent, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
     188                 :            :         {
     189                 :            :             set( _rxComponent, _setThrow );
     190                 :            :         }
     191                 :            : #endif
     192                 :            : 
     193                 :            : //        SharedUNOComponent& operator=( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent );
     194                 :            :         // this operator is not implemented by intention. There is no canonic ownership after this operatoer
     195                 :            :         // would hvae been applied: Should the SharedUNOComponent have the ownership of the component,
     196                 :            :         // or shouldn't it? Hard to guess, and probably wrong in 50 percent of all cases, anyway. So,
     197                 :            :         // instead of tempting clients of this class to use such a dangerous operator, we do
     198                 :            :         // not offer it at all. If you need to assign a Reference< INTERFACE > to your SharedUNOComponent,
     199                 :            :         // use the ->reset method.
     200                 :            : 
     201                 :            :         /** assigns a new component, and releases the old one
     202                 :            :         */
     203                 :            :         void reset( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode = TakeOwnership );
     204                 :            : 
     205                 :            :         inline bool set( ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_Query _query );
     206                 :            :         inline bool set( const ::com::sun::star::uno::BaseReference& _rRef, ::com::sun::star::uno::UnoReference_Query _query );
     207                 :            :         inline bool set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_Query _query );
     208                 :            : 
     209                 :            : #ifndef EXCEPTIONS_OFF
     210                 :            :         inline void set( const ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow );
     211                 :            :         inline void set( const ::com::sun::star::uno::BaseReference & _rRef, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow );
     212                 :            :         inline void set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow );
     213                 :            : 
     214                 :            :         inline void set( const INTERFACE* _pInterface, ::com::sun::star::uno::UnoReference_SetThrow _setThrow );
     215                 :            :         inline void set( const ::com::sun::star::uno::Reference< INTERFACE >& _rRef, ::com::sun::star::uno::UnoReference_SetThrow _setThrow );
     216                 :            :         inline void set( const SharedUNOComponent& _rComp, ::com::sun::star::uno::UnoReference_SetThrow _setThrow );
     217                 :            : #endif
     218                 :            : 
     219                 :            :         INTERFACE* SAL_CALL operator->() const;
     220                 :            : 
     221                 :        180 :         inline operator const ::com::sun::star::uno::Reference< INTERFACE >&() const
     222                 :            :         {
     223                 :        180 :             return m_xTypedComponent;
     224                 :            :         }
     225                 :            : 
     226                 :       2862 :         inline const ::com::sun::star::uno::Reference< INTERFACE >& getTyped() const
     227                 :            :         {
     228                 :       2862 :             return m_xTypedComponent;
     229                 :            :         }
     230                 :            : 
     231                 :        318 :         inline bool is() const
     232                 :            :         {
     233                 :        318 :             return m_xTypedComponent.is();
     234                 :            :         }
     235                 :            : 
     236                 :        574 :         inline void clear()
     237                 :            :         {
     238                 :        574 :             m_pComponent.reset();
     239                 :        574 :             m_xTypedComponent.clear();
     240                 :        574 :         }
     241                 :            :     };
     242                 :            : 
     243                 :            :     //-------------------------------------------------------------------------
     244                 :            :     template < class INTERFACE, class COMPONENT >
     245                 :         22 :     INTERFACE* SAL_CALL SharedUNOComponent< INTERFACE, COMPONENT >::operator->() const
     246                 :            :     {
     247                 :         22 :         return m_xTypedComponent.operator->();
     248                 :            :     }
     249                 :            : 
     250                 :            :     //-------------------------------------------------------------------------
     251                 :            :     // assignments
     252                 :            :     template < class INTERFACE, class COMPONENT >
     253                 :        550 :     void SharedUNOComponent< INTERFACE, COMPONENT >::reset( const ::com::sun::star::uno::Reference< INTERFACE >& _rxComponent, AssignmentMode _eMode )
     254                 :            :     {
     255         [ +  + ]:        550 :         m_pComponent.reset( _eMode == TakeOwnership ? new COMPONENT( _rxComponent ) : NULL );
              [ #  +  - ]
         [ +  - ][ +  - ]
         [ #  # ][ #  # ]
     256                 :        550 :         m_xTypedComponent = _rxComponent;
     257                 :        550 :     }
     258                 :            : 
     259                 :            :     //-------------------------------------------------------------------------
     260                 :            :     // comparison operators
     261                 :            :     template < class INTERFACE, class COMPONENT >
     262                 :            :     bool operator==( const ::com::sun::star::uno::Reference< INTERFACE >& _rLHS, const SharedUNOComponent< INTERFACE, COMPONENT >& _rRHS )
     263                 :            :     {
     264                 :            :         return _rLHS == _rRHS.getTyped();
     265                 :            :     }
     266                 :            : 
     267                 :            :     template < class INTERFACE, class COMPONENT >
     268                 :          0 :     bool operator==( const SharedUNOComponent< INTERFACE, COMPONENT >& _rLHS, const ::com::sun::star::uno::Reference< INTERFACE >& _rRHS )
     269                 :            :     {
     270                 :          0 :         return _rLHS.getTyped() == _rRHS;
     271                 :            :     }
     272                 :            : 
     273                 :            :     //-------------------------------------------------------------------------
     274                 :            :     // conversion to Any
     275                 :            :     template < class INTERFACE, class COMPONENT >
     276                 :            :     inline void SAL_CALL operator <<= ( ::com::sun::star::uno::Any & rAny, const SharedUNOComponent< INTERFACE, COMPONENT >& value ) SAL_THROW(())
     277                 :            :     {
     278                 :            :         rAny <<= value.getTyped();
     279                 :            :     }
     280                 :            : 
     281                 :            :     //-------------------------------------------------------------------------
     282                 :            :     template < class INTERFACE, class COMPONENT >
     283                 :          0 :     inline ::com::sun::star::uno::Any SAL_CALL makeAny( const SharedUNOComponent< INTERFACE, COMPONENT >& value ) SAL_THROW(())
     284                 :            :     {
     285                 :          0 :         return makeAny( value.getTyped() );
     286                 :            :     }
     287                 :            : 
     288                 :            : #ifndef EXCEPTIONS_OFF
     289                 :            :     //-------------------------------------------------------------------------
     290                 :            :     template < class INTERFACE, class COMPONENT >
     291                 :            :     void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
     292                 :            :     {
     293                 :            :         reset( ::com::sun::star::uno::Reference< INTERFACE >( _pInterface, _queryThrow ), TakeOwnership );
     294                 :            :     }
     295                 :            : 
     296                 :            :     //-------------------------------------------------------------------------
     297                 :            :     template < class INTERFACE, class COMPONENT >
     298                 :         48 :     void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::BaseReference & _rRef, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
     299                 :            :     {
     300         [ +  - ]:         48 :         reset( ::com::sun::star::uno::Reference< INTERFACE >( _rRef, _queryThrow ), TakeOwnership );
     301                 :         48 :     }
     302                 :            : 
     303                 :            :     //-------------------------------------------------------------------------
     304                 :            :     template < class INTERFACE, class COMPONENT >
     305                 :            :     void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_QueryThrow _queryThrow )
     306                 :            :     {
     307                 :            :         reset( ::com::sun::star::uno::Reference< INTERFACE >( _rAny, _queryThrow ), TakeOwnership );
     308                 :            :     }
     309                 :            : 
     310                 :            :     //-------------------------------------------------------------------------
     311                 :            :     template < class INTERFACE, class COMPONENT >
     312                 :            :     void SharedUNOComponent< INTERFACE, COMPONENT >::set( const INTERFACE* _pInterface, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
     313                 :            :     {
     314                 :            :         reset( ::com::sun::star::uno::Reference< INTERFACE >( _pInterface, _setThrow ), TakeOwnership );
     315                 :            :     }
     316                 :            : 
     317                 :            :     //-------------------------------------------------------------------------
     318                 :            :     template < class INTERFACE, class COMPONENT >
     319                 :          0 :     void SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::Reference< INTERFACE >& _rRef, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
     320                 :            :     {
     321 [ #  # ][ #  # ]:          0 :         reset( ::com::sun::star::uno::Reference< INTERFACE >( _rRef, _setThrow ), TakeOwnership );
     322                 :          0 :     }
     323                 :            : 
     324                 :            :     //-------------------------------------------------------------------------
     325                 :            :     template < class INTERFACE, class COMPONENT >
     326                 :          0 :     void SharedUNOComponent< INTERFACE, COMPONENT >::set( const SharedUNOComponent& _rComp, ::com::sun::star::uno::UnoReference_SetThrow _setThrow )
     327                 :            :     {
     328                 :          0 :         *this = _rComp;
     329                 :            :         // provoke an exception in case the component is NULL
     330                 :          0 :         m_xTypedComponent.set( m_xTypedComponent, _setThrow );
     331                 :          0 :     }
     332                 :            : #endif
     333                 :            : 
     334                 :            :     //-------------------------------------------------------------------------
     335                 :            :     template < class INTERFACE, class COMPONENT >
     336                 :            :     bool SharedUNOComponent< INTERFACE, COMPONENT >::set( ::com::sun::star::uno::XInterface* _pInterface, ::com::sun::star::uno::UnoReference_Query _query )
     337                 :            :     {
     338                 :            :         reset( ::com::sun::star::uno::Reference< INTERFACE >( _pInterface, _query ) );
     339                 :            :         return is();
     340                 :            :     }
     341                 :            : 
     342                 :            :     //-------------------------------------------------------------------------
     343                 :            :     template < class INTERFACE, class COMPONENT >
     344                 :          0 :     bool SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::BaseReference& _rRef, ::com::sun::star::uno::UnoReference_Query _query )
     345                 :            :     {
     346         [ #  # ]:          0 :         reset( ::com::sun::star::uno::Reference< INTERFACE >( _rRef, _query ) );
     347                 :          0 :         return is();
     348                 :            :     }
     349                 :            : 
     350                 :            :     //-------------------------------------------------------------------------
     351                 :            :     template < class INTERFACE, class COMPONENT >
     352                 :            :     bool SharedUNOComponent< INTERFACE, COMPONENT >::set( const ::com::sun::star::uno::Any& _rAny, ::com::sun::star::uno::UnoReference_Query _query )
     353                 :            :     {
     354                 :            :         reset( ::com::sun::star::uno::Reference< INTERFACE >( _rAny, _query ) );
     355                 :            :         return is();
     356                 :            :     }
     357                 :            : 
     358                 :            : //............................................................................
     359                 :            : }   // namespace utl
     360                 :            : //............................................................................
     361                 :            : 
     362                 :            : #endif // UNOTOOLS_INC_SHAREDUNOCOMPONENT_HXX
     363                 :            : 
     364                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10