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 .
21 #include <config_options.h>
23 #include <osl/conditn.hxx>
24 #include <comphelper/interfacecontainer4.hxx>
25 #include "charttoolsdllapi.hxx"
27 namespace com::sun::star::document
{ class XStorageChangeListener
; }
28 namespace com::sun::star::lang
{ class XComponent
; }
29 namespace com::sun::star::lang
{ class XEventListener
; }
30 namespace com::sun::star::util
{ class CloseVetoException
; }
31 namespace com::sun::star::util
{ class XCloseListener
; }
32 namespace com::sun::star::util
{ class XCloseable
; }
33 namespace com::sun::star::util
{ class XModifyListener
; }
34 namespace com::sun::star::view
{ class XSelectionChangeListener
; }
39 class UNLESS_MERGELIBS(OOO_DLLPUBLIC_CHARTTOOLS
) LifeTimeManager
41 friend class LifeTimeGuard
;
43 LifeTimeManager( css::lang::XComponent
* pComponent
);
44 virtual ~LifeTimeManager();
46 bool impl_isDisposed( bool bAssert
=true );
47 /// @throws css::uno::RuntimeException
50 mutable std::mutex m_aAccessMutex
;
51 ::comphelper::OInterfaceContainerHelper4
<css::util::XCloseListener
> m_aCloseListeners
;
52 ::comphelper::OInterfaceContainerHelper4
<css::util::XModifyListener
> m_aModifyListeners
;
53 ::comphelper::OInterfaceContainerHelper4
<css::document::XStorageChangeListener
> m_aStorageChangeListeners
;
54 ::comphelper::OInterfaceContainerHelper4
<css::lang::XEventListener
> m_aEventListeners
;
55 ::comphelper::OInterfaceContainerHelper4
<css::view::XSelectionChangeListener
> m_aSelectionChangeListeners
;
58 SAL_DLLPRIVATE
virtual bool impl_canStartApiCall();
59 SAL_DLLPRIVATE
virtual void impl_apiCallCountReachedNull(std::unique_lock
<std::mutex
>& /*rGuard*/){}
61 SAL_DLLPRIVATE
void impl_registerApiCall(bool bLongLastingCall
);
62 SAL_DLLPRIVATE
void impl_unregisterApiCall(std::unique_lock
<std::mutex
>& rGuard
, bool bLongLastingCall
);
64 css::lang::XComponent
* m_pComponent
;
65 ::osl::Condition m_aNoAccessCountCondition
;
66 sal_Int32 m_nAccessCount
;
67 bool volatile m_bDisposed
;
68 bool volatile m_bInDispose
;
69 ::osl::Condition m_aNoLongLastingCallCountCondition
;
70 sal_Int32 m_nLongLastingCallCount
;
73 class CloseableLifeTimeManager final
: public LifeTimeManager
75 css::util::XCloseable
* m_pCloseable
;
77 ::osl::Condition m_aEndTryClosingCondition
;
78 bool volatile m_bClosed
;
79 bool volatile m_bInTryClose
;
80 //the ownership between model and controller is not clear at first
81 //each controller might consider him as owner of the model first
82 //at start the model is not considered as owner of itself
83 bool volatile m_bOwnership
;
86 CloseableLifeTimeManager( css::util::XCloseable
* pCloseable
87 , css::lang::XComponent
* pComponent
);
88 virtual ~CloseableLifeTimeManager() override
;
90 bool impl_isDisposedOrClosed( bool bAssert
=true );
91 /// @throws css::uno::Exception
92 bool g_close_startTryClose(bool bDeliverOwnership
);
93 /// @throws css::util::CloseVetoException
94 void g_close_isNeedToCancelLongLastingCalls( bool bDeliverOwnership
, css::util::CloseVetoException
const & ex
);
95 void g_close_endTryClose(bool bDeliverOwnership
);
96 void g_close_endTryClose_doClose();
97 /// @throws css::uno::RuntimeException
98 void g_addCloseListener( const css::uno::Reference
< css::util::XCloseListener
> & xListener
);
101 virtual bool impl_canStartApiCall() override
;
102 virtual void impl_apiCallCountReachedNull(std::unique_lock
<std::mutex
>& rGuard
) override
;
104 void impl_setOwnership( bool bDeliverOwnership
, bool bMyVeto
);
105 void impl_doClose(std::unique_lock
<std::mutex
>& rGuard
);
109 Use this Guard in your ApiCalls to protect access on resources
110 which will be released in dispose.
111 It's guaranteed that the release of resources only starts if your
112 guarded call has finished.
113 ! It's only partly guaranteed that this resources will not change during the call.
114 See the example for details.
116 This class is to be used as described in the example.
118 If this guard is used in all api calls of an XCloseable object
119 it's guaranteed that the closeable will close itself after finishing the last call
125 LifeTimeGuard aLifeTimeGuard(m_aLifeTimeManager);
127 //mutex is acquired; call is not registered
129 if(!aLifeTimeGuard.startApiCall())
130 return ; //behave as passive as possible, if disposed or closed
132 //mutex is acquired, call is registered
134 //you might access some private members here
135 //but then you need to protect access to these members always like this
136 //never call to the outside here
139 aLifeTimeGuard.clear(); //!!!
141 //Mutex is released, the running call is still registered
142 //this call will finish before the 'release-section' in dispose is allowed to start
145 //you might access some private members here guarded with your own mutex
146 //but release your mutex at the end of this block
149 //you can call to the outside (without holding the mutex) without becoming disposed
151 //End of method -> ~LifeTimeGuard
152 //-> call is unregistered
153 //-> this object might be disposed now
156 your XComponent::dispose method has to be implemented in the following way:
161 if( !m_aLifeTimeManager.dispose() )
164 //--release all resources and references
174 LifeTimeGuard( LifeTimeManager
& rManager
)
175 : m_guard( rManager
.m_aAccessMutex
)
176 , m_rManager(rManager
)
177 , m_bCallRegistered(false)
178 , m_bLongLastingCallRegistered(false)
182 bool startApiCall(bool bLongLastingCall
=false);
184 void clear() { m_guard
.unlock(); }
187 std::unique_lock
<std::mutex
> m_guard
;
188 LifeTimeManager
& m_rManager
;
189 bool m_bCallRegistered
;
190 bool m_bLongLastingCallRegistered
;
193 LifeTimeGuard( const LifeTimeGuard
& ) = delete;
194 LifeTimeGuard
& operator= ( const LifeTimeGuard
& ) = delete;
197 }//end namespace apphelper
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */