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

Generated by: LCOV version 1.11