1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: LifeTime.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 #include <osl/mutex.hxx>
34 #include <osl/conditn.hxx>
35 #ifndef _COM_SUN_STAR_UNO_EXCEPTION_HDL_
36 #include <com/sun/star/uno/Exception.hdl>
38 #include <cppuhelper/interfacecontainer.hxx>
39 #include <com/sun/star/util/XCloseListener.hpp>
40 #include <com/sun/star/util/XCloseable.hpp>
41 #include <com/sun/star/lang/XComponent.hpp>
42 #include <cppuhelper/weakref.hxx>
43 #include "charttoolsdllapi.hxx"
51 friend class LifeTimeGuard
;
53 mutable ::osl::Mutex m_aAccessMutex
;
55 OOO_DLLPUBLIC_CHARTTOOLS
LifeTimeManager( ::com::sun::star::lang::XComponent
* pComponent
, sal_Bool bLongLastingCallsCancelable
= sal_False
);
56 OOO_DLLPUBLIC_CHARTTOOLS
virtual ~LifeTimeManager();
58 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool
impl_isDisposed();
59 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool
dispose() throw(::com::sun::star::uno::RuntimeException
);
62 ::cppu::OMultiTypeInterfaceContainerHelper m_aListenerContainer
;
65 virtual sal_Bool
impl_canStartApiCall();
66 virtual void impl_apiCallCountReachedNull(){}
68 void impl_registerApiCall(sal_Bool bLongLastingCall
);
69 void impl_unregisterApiCall(sal_Bool bLongLastingCall
);
74 ::com::sun::star::lang::XComponent
* m_pComponent
;
76 ::osl::Condition m_aNoAccessCountCondition
;
77 sal_Int32
volatile m_nAccessCount
;
79 sal_Bool
volatile m_bDisposed
;
80 sal_Bool
volatile m_bInDispose
;
83 sal_Bool m_bLongLastingCallsCancelable
;
84 ::osl::Condition m_aNoLongLastingCallCountCondition
;
85 sal_Int32
volatile m_nLongLastingCallCount
;
88 class CloseableLifeTimeManager
: public LifeTimeManager
91 ::com::sun::star::util::XCloseable
* m_pCloseable
;
93 ::osl::Condition m_aEndTryClosingCondition
;
94 sal_Bool
volatile m_bClosed
;
95 sal_Bool
volatile m_bInTryClose
;
96 //the ownership between model and controller is not clear at first
97 //each controller might consider him as owner of the model first
98 //at start the model is not considered as owner of itself
99 sal_Bool
volatile m_bOwnership
;
100 //with a XCloseable::close call and during XCloseListener::queryClosing
101 //the ownership can be regulated more explicit,
102 //if so the ownership is considered to be well known
103 sal_Bool
volatile m_bOwnershipIsWellKnown
;
106 OOO_DLLPUBLIC_CHARTTOOLS
CloseableLifeTimeManager( ::com::sun::star::util::XCloseable
* pCloseable
107 , ::com::sun::star::lang::XComponent
* pComponent
108 , sal_Bool bLongLastingCallsCancelable
= sal_False
);
109 OOO_DLLPUBLIC_CHARTTOOLS
virtual ~CloseableLifeTimeManager();
111 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool
impl_isDisposedOrClosed();
112 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool
g_close_startTryClose(sal_Bool bDeliverOwnership
)
113 throw ( ::com::sun::star::uno::Exception
);
114 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool
g_close_isNeedToCancelLongLastingCalls( sal_Bool bDeliverOwnership
, ::com::sun::star::util::CloseVetoException
& ex
)
115 throw ( ::com::sun::star::util::CloseVetoException
);
116 OOO_DLLPUBLIC_CHARTTOOLS
void g_close_endTryClose(sal_Bool bDeliverOwnership
, sal_Bool bMyVeto
);
117 OOO_DLLPUBLIC_CHARTTOOLS
void g_close_endTryClose_doClose();
118 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool
g_addCloseListener( const ::com::sun::star::uno::Reference
<
119 ::com::sun::star::util::XCloseListener
> & xListener
)
120 throw(::com::sun::star::uno::RuntimeException
);
123 virtual sal_Bool
impl_canStartApiCall();
124 virtual void impl_apiCallCountReachedNull();
126 void impl_setOwnership( sal_Bool bDeliverOwnership
, sal_Bool bMyVeto
);
127 sal_Bool
impl_shouldCloseAtNextChance();
132 m_bClosed
= sal_False
;
133 m_bInTryClose
= sal_False
;
134 m_bOwnership
= sal_False
;
135 m_bOwnershipIsWellKnown
= sal_False
;
136 m_aEndTryClosingCondition
.set();
140 //-----------------------------------------------------------------
142 Use this Guard in your apicalls to protect access on resources
143 which will be released in dispose.
144 It's guarantied, that the release of resources only starts if your
145 guarded call has finished.
146 ! It's only partly guaranteed that this resources will not change during the call.
147 See the example for details.
149 This class is to be used as described in the example.
151 If this guard is used in all api calls of an XCloseable object
152 it's guarantied, that the closeable will close itself after finishing the last call
158 LifeTimeGuard aLifeTimeGuard(m_aLifeTimeManager);
160 //mutex is acquired; call is not registered
162 if(!aLifeTimeGuard.startApiCall())
163 return ; //behave as passive as possible, if disposed or closed
165 //mutex is acquired, call is registered
167 //you might access some private members here
168 //but than you need to protect access to these members always like this
169 //never call to the outside here
172 aLifeTimeGuard.clear(); //!!!
174 //Mutex is released, the running call is still registered
175 //this call will finish before the 'release-section' in dispose is allowed to start
178 //you might access some private members here guarded with your own mutex
179 //but release your mutex at the end of this block
182 //you can call to the outside (without holding the mutex) without becoming disposed
184 //End of method -> ~LifeTimeGuard
185 //-> call is unregistered
186 //-> this object might be disposed now
189 your XComponent::dispose method has to be implemented in the following way:
194 if( !m_aLifeTimeManager.dispose() )
197 //--release all resources and references
202 //-----------------------------------------------------------------
204 class OOO_DLLPUBLIC_CHARTTOOLS LifeTimeGuard
208 LifeTimeGuard( LifeTimeManager
& rManager
)
209 : m_guard( rManager
.m_aAccessMutex
)
210 , m_rManager(rManager
)
211 , m_bCallRegistered(sal_False
)
212 , m_bLongLastingCallRegistered(sal_False
)
216 sal_Bool
startApiCall(sal_Bool bLongLastingCall
=sal_False
);
218 void clear() { m_guard
.clear(); }
221 osl::ClearableMutexGuard m_guard
;
222 LifeTimeManager
& m_rManager
;
223 sal_Bool m_bCallRegistered
;
224 sal_Bool m_bLongLastingCallRegistered
;
227 // these make no sense
228 LifeTimeGuard( ::osl::Mutex
& rMutex
);
229 LifeTimeGuard( const LifeTimeGuard
& );
230 LifeTimeGuard
& operator= ( const LifeTimeGuard
& );
241 NegativeGuard(T
* pT
) : m_pT(pT
)
246 NegativeGuard(T
& t
) : m_pT(&t
)
257 }//end namespace apphelper