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