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 .
22 #include <com/sun/star/sdbc/XConnection.hpp>
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <cppuhelper/weakref.hxx>
27 #include <osl/mutex.hxx>
33 // ConnectionDependentComponent
34 class ConnectionDependentComponent
37 mutable ::osl::Mutex m_aMutex
;
38 css::uno::WeakReference
< css::sdbc::XConnection
>
40 css::uno::Reference
< css::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 css::uno::Reference
< css::sdbc::XConnection
>
53 ::osl::Mutex
& getMutex() const { return m_aMutex
; }
55 const css::uno::Reference
< css::uno::XComponentContext
>&
56 getContext() const { return m_aContext
; }
62 explicit ConnectionDependentComponent( css::uno::Reference
< css::uno::XComponentContext
> _xContext
)
63 :m_aContext(std::move( _xContext
))
67 /** sets the connection we depend on.
69 To be called exactly once.
74 void setWeakConnection( const css::uno::Reference
< css::sdbc::XConnection
>& _rxConnection
)
76 m_aConnection
= _rxConnection
;
79 const css::uno::Reference
< css::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 bool acquireConnection( GuardAccess
)
93 m_xConnection
.set(m_aConnection
);
94 return m_xConnection
.is();
96 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 explicit EntryGuard( ConnectionDependentComponent
& _rComponent
)
121 :m_aMutexGuard( _rComponent
.getMutex( ConnectionDependentComponent::GuardAccess() ) )
122 ,m_rComponent( _rComponent
)
124 if ( !m_rComponent
.acquireConnection( ConnectionDependentComponent::GuardAccess() ) )
125 throw css::lang::DisposedException();
130 m_rComponent
.releaseConnection( ConnectionDependentComponent::GuardAccess() );
134 } // namespace sdbtools
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */