LCOV - code coverage report
Current view: top level - dbaccess/source/sdbtools/connection - connectiondependent.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 27 28 96.4 %
Date: 2015-06-13 12:38:46 Functions: 12 12 100.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             : #ifndef INCLUDED_DBACCESS_SOURCE_SDBTOOLS_CONNECTION_CONNECTIONDEPENDENT_HXX
      21             : #define INCLUDED_DBACCESS_SOURCE_SDBTOOLS_CONNECTION_CONNECTIONDEPENDENT_HXX
      22             : 
      23             : #include <com/sun/star/sdbc/XConnection.hpp>
      24             : #include <com/sun/star/lang/DisposedException.hpp>
      25             : #include <com/sun/star/uno/XComponentContext.hpp>
      26             : 
      27             : #include <cppuhelper/weakref.hxx>
      28             : #include <osl/mutex.hxx>
      29             : 
      30             : namespace sdbtools
      31             : {
      32             : 
      33             :     // ConnectionDependentComponent
      34          50 :     class ConnectionDependentComponent
      35             :     {
      36             :     private:
      37             :         mutable ::osl::Mutex    m_aMutex;
      38             :         ::com::sun::star::uno::WeakReference< ::com::sun::star::sdbc::XConnection >
      39             :                                 m_aConnection;
      40             :         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >
      41             :                                 m_aContext;
      42             : 
      43             :         /** a hard reference to the connection we're working for
      44             : 
      45             :             This member is only valid as long as a EntryGuard is on the stack.
      46             :             The guard will, in its constructor, set the member, and reset it in its destructor.
      47             :             This ensures that the connection is only held hard when it's needed, and weak otherwise.
      48             :         */
      49             :         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >
      50             :                                 m_xConnection;
      51             : 
      52             :     protected:
      53          25 :         ::osl::Mutex&   getMutex() const { return m_aMutex; }
      54             : 
      55             :         const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >&
      56          75 :                         getContext() const { return m_aContext; }
      57             : 
      58             :     protected:
      59             :         class EntryGuard;
      60             : 
      61             :     protected:
      62          50 :         ConnectionDependentComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > & _rContext )
      63          50 :             :m_aContext( _rContext )
      64             :         {
      65          50 :         }
      66             : 
      67             :         /** sets the connection we depend on.
      68             : 
      69             :             To be called exactly once.
      70             : 
      71             :             @param  _rxConnection
      72             :                 the connection to set
      73             :         */
      74          50 :         void    setWeakConnection( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection )
      75             :         {
      76          50 :             m_aConnection = _rxConnection;
      77          50 :         }
      78             : 
      79             :         const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >&
      80          75 :                 getConnection() const { return m_xConnection; }
      81             : 
      82             :     public:
      83             :     struct GuardAccess;
      84             :     friend struct GuardAccess;
      85             :         /** helper for granting exclusive access to various other methods
      86             :         */
      87         150 :         struct GuardAccess { friend class EntryGuard; private: GuardAccess() { } };
      88             : 
      89          50 :         ::osl::Mutex&   getMutex( GuardAccess ) const { return m_aMutex; }
      90             : 
      91          50 :         inline bool acquireConnection( GuardAccess )
      92             :         {
      93          50 :             m_xConnection = ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >(m_aConnection);
      94          50 :             return m_xConnection.is();
      95             :         }
      96          50 :         inline void releaseConnection( GuardAccess )
      97             :         {
      98          50 :             m_xConnection.clear();
      99          50 :         }
     100             :     };
     101             : 
     102             :     // ConnectionDependentComponent::EntryGuard
     103             :     /** a class for guarding methods of a connection-dependent component
     104             : 
     105             :         This class serves multiple purposes:
     106             :         <ul><li>It ensures multi-threading safety by guarding the component's mutex
     107             :                 as long as it lives.</li>
     108             :             <li>It ensures that the component's connection is alive. The constructor
     109             :                 throws a DisposedException if no hard reference to the connection can
     110             :                 be obtained.</li>
     111             :         </ul>
     112             :     */
     113             :     class ConnectionDependentComponent::EntryGuard
     114             :     {
     115             :     private:
     116             :         ::osl::MutexGuard               m_aMutexGuard;
     117             :         ConnectionDependentComponent&   m_rComponent;
     118             : 
     119             :     public:
     120          50 :         EntryGuard( ConnectionDependentComponent& _rComponent )
     121         100 :             :m_aMutexGuard( _rComponent.getMutex( ConnectionDependentComponent::GuardAccess() ) )
     122         100 :             ,m_rComponent( _rComponent )
     123             :         {
     124          50 :             if ( !m_rComponent.acquireConnection( ConnectionDependentComponent::GuardAccess() ) )
     125           0 :                 throw ::com::sun::star::lang::DisposedException();
     126          50 :         }
     127             : 
     128          50 :         ~EntryGuard()
     129          50 :         {
     130          50 :             m_rComponent.releaseConnection( ConnectionDependentComponent::GuardAccess() );
     131          50 :         }
     132             :     };
     133             : 
     134             : } // namespace sdbtools
     135             : 
     136             : #endif // INCLUDED_DBACCESS_SOURCE_SDBTOOLS_CONNECTION_CONNECTIONDEPENDENT_HXX
     137             : 
     138             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11