1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_DBACCESS_SOURCE_SDBTOOLS_CONNECTION_CONNECTIONDEPENDENT_HXX
21 #define INCLUDED_DBACCESS_SOURCE_SDBTOOLS_CONNECTION_CONNECTIONDEPENDENT_HXX
23 #include <com/sun/star/sdbc/XConnection.hpp>
24 #include <com/sun/star/lang/DisposedException.hpp>
25 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <cppuhelper/weakref.hxx>
28 #include <osl/mutex.hxx>
33 // ConnectionDependentComponent
34 class ConnectionDependentComponent
37 mutable ::osl::Mutex m_aMutex
;
38 ::com::sun::star::uno::WeakReference
< ::com::sun::star::sdbc::XConnection
>
40 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>
43 /** a hard reference to the connection we're working for
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.
49 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>
53 ::osl::Mutex
& getMutex() const { return m_aMutex
; }
55 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>&
56 getContext() const { return m_aContext
; }
62 ConnectionDependentComponent( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> & _rContext
)
63 :m_aContext( _rContext
)
67 /** sets the connection we depend on.
69 To be called exactly once.
74 void setWeakConnection( const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _rxConnection
)
76 m_aConnection
= _rxConnection
;
79 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>&
80 getConnection() const { return m_xConnection
; }
84 friend struct GuardAccess
;
85 /** helper for granting exclusive access to various other methods
87 struct GuardAccess
{ friend class EntryGuard
; private: GuardAccess() { } };
89 ::osl::Mutex
& getMutex( GuardAccess
) const { return m_aMutex
; }
91 inline bool acquireConnection( GuardAccess
)
93 m_xConnection
= ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>(m_aConnection
);
94 return m_xConnection
.is();
96 inline void releaseConnection( GuardAccess
)
98 m_xConnection
.clear();
102 // ConnectionDependentComponent::EntryGuard
103 /** a class for guarding methods of a connection-dependent component
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
113 class ConnectionDependentComponent::EntryGuard
116 ::osl::MutexGuard m_aMutexGuard
;
117 ConnectionDependentComponent
& m_rComponent
;
120 EntryGuard( ConnectionDependentComponent
& _rComponent
)
121 :m_aMutexGuard( _rComponent
.getMutex( ConnectionDependentComponent::GuardAccess() ) )
122 ,m_rComponent( _rComponent
)
124 if ( !m_rComponent
.acquireConnection( ConnectionDependentComponent::GuardAccess() ) )
125 throw ::com::sun::star::lang::DisposedException();
130 m_rComponent
.releaseConnection( ConnectionDependentComponent::GuardAccess() );
134 } // namespace sdbtools
136 #endif // INCLUDED_DBACCESS_SOURCE_SDBTOOLS_CONNECTION_CONNECTIONDEPENDENT_HXX
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */