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 .
19 #ifndef INCLUDED_CHART2_SOURCE_INC_LIFETIME_HXX
20 #define INCLUDED_CHART2_SOURCE_INC_LIFETIME_HXX
22 #include <osl/mutex.hxx>
23 #include <osl/conditn.hxx>
24 #include <cppuhelper/interfacecontainer.h>
25 #include "charttoolsdllapi.hxx"
27 namespace com
{ namespace sun
{ namespace star
{ namespace lang
{ class XComponent
; } } } }
28 namespace com
{ namespace sun
{ namespace star
{ namespace util
{ class CloseVetoException
; } } } }
29 namespace com
{ namespace sun
{ namespace star
{ namespace util
{ class XCloseListener
; } } } }
30 namespace com
{ namespace sun
{ namespace star
{ namespace util
{ class XCloseable
; } } } }
35 class OOO_DLLPUBLIC_CHARTTOOLS LifeTimeManager
37 friend class LifeTimeGuard
;
39 mutable ::osl::Mutex m_aAccessMutex
;
41 LifeTimeManager( css::lang::XComponent
* pComponent
);
42 virtual ~LifeTimeManager();
44 bool impl_isDisposed( bool bAssert
=true );
45 /// @throws css::uno::RuntimeException
49 ::cppu::OMultiTypeInterfaceContainerHelper m_aListenerContainer
;
52 SAL_DLLPRIVATE
virtual bool impl_canStartApiCall();
53 SAL_DLLPRIVATE
virtual void impl_apiCallCountReachedNull(){}
55 SAL_DLLPRIVATE
void impl_registerApiCall(bool bLongLastingCall
);
56 SAL_DLLPRIVATE
void impl_unregisterApiCall(bool bLongLastingCall
);
59 css::lang::XComponent
* m_pComponent
;
61 ::osl::Condition m_aNoAccessCountCondition
;
62 sal_Int32 m_nAccessCount
;
64 bool volatile m_bDisposed
;
65 bool volatile m_bInDispose
;
67 ::osl::Condition m_aNoLongLastingCallCountCondition
;
68 sal_Int32 m_nLongLastingCallCount
;
71 class CloseableLifeTimeManager final
: public LifeTimeManager
73 css::util::XCloseable
* m_pCloseable
;
75 ::osl::Condition m_aEndTryClosingCondition
;
76 bool volatile m_bClosed
;
77 bool volatile m_bInTryClose
;
78 //the ownership between model and controller is not clear at first
79 //each controller might consider him as owner of the model first
80 //at start the model is not considered as owner of itself
81 bool volatile m_bOwnership
;
84 OOO_DLLPUBLIC_CHARTTOOLS
CloseableLifeTimeManager( css::util::XCloseable
* pCloseable
85 , css::lang::XComponent
* pComponent
);
86 OOO_DLLPUBLIC_CHARTTOOLS
virtual ~CloseableLifeTimeManager() override
;
88 OOO_DLLPUBLIC_CHARTTOOLS
bool impl_isDisposedOrClosed( bool bAssert
=true );
89 /// @throws css::uno::Exception
90 OOO_DLLPUBLIC_CHARTTOOLS
bool g_close_startTryClose(bool bDeliverOwnership
);
91 /// @throws css::util::CloseVetoException
92 OOO_DLLPUBLIC_CHARTTOOLS
void g_close_isNeedToCancelLongLastingCalls( bool bDeliverOwnership
, css::util::CloseVetoException
const & ex
);
93 OOO_DLLPUBLIC_CHARTTOOLS
void g_close_endTryClose(bool bDeliverOwnership
);
94 OOO_DLLPUBLIC_CHARTTOOLS
void g_close_endTryClose_doClose();
95 /// @throws css::uno::RuntimeException
96 OOO_DLLPUBLIC_CHARTTOOLS
void g_addCloseListener( const css::uno::Reference
< css::util::XCloseListener
> & xListener
);
99 virtual bool impl_canStartApiCall() override
;
100 virtual void impl_apiCallCountReachedNull() override
;
102 void impl_setOwnership( bool bDeliverOwnership
, bool bMyVeto
);
107 Use this Guard in your ApiCalls to protect access on resources
108 which will be released in dispose.
109 It's guaranteed that the release of resources only starts if your
110 guarded call has finished.
111 ! It's only partly guaranteed that this resources will not change during the call.
112 See the example for details.
114 This class is to be used as described in the example.
116 If this guard is used in all api calls of an XCloseable object
117 it's guaranteed that the closeable will close itself after finishing the last call
123 LifeTimeGuard aLifeTimeGuard(m_aLifeTimeManager);
125 //mutex is acquired; call is not registered
127 if(!aLifeTimeGuard.startApiCall())
128 return ; //behave as passive as possible, if disposed or closed
130 //mutex is acquired, call is registered
132 //you might access some private members here
133 //but then you need to protect access to these members always like this
134 //never call to the outside here
137 aLifeTimeGuard.clear(); //!!!
139 //Mutex is released, the running call is still registered
140 //this call will finish before the 'release-section' in dispose is allowed to start
143 //you might access some private members here guarded with your own mutex
144 //but release your mutex at the end of this block
147 //you can call to the outside (without holding the mutex) without becoming disposed
149 //End of method -> ~LifeTimeGuard
150 //-> call is unregistered
151 //-> this object might be disposed now
154 your XComponent::dispose method has to be implemented in the following way:
159 if( !m_aLifeTimeManager.dispose() )
162 //--release all resources and references
168 class OOO_DLLPUBLIC_CHARTTOOLS LifeTimeGuard
172 LifeTimeGuard( LifeTimeManager
& rManager
)
173 : m_guard( rManager
.m_aAccessMutex
)
174 , m_rManager(rManager
)
175 , m_bCallRegistered(false)
176 , m_bLongLastingCallRegistered(false)
180 bool startApiCall(bool bLongLastingCall
=false);
182 void clear() { m_guard
.clear(); }
185 osl::ClearableMutexGuard m_guard
;
186 LifeTimeManager
& m_rManager
;
187 bool m_bCallRegistered
;
188 bool m_bLongLastingCallRegistered
;
191 LifeTimeGuard( const LifeTimeGuard
& ) = delete;
192 LifeTimeGuard
& operator= ( const LifeTimeGuard
& ) = delete;
196 class NegativeGuard final
201 NegativeGuard(T
& t
) : m_pT(&t
)
212 }//end namespace apphelper
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */