1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 #include <osl/mutex.hxx>
31 #include <osl/conditn.hxx>
32 #ifndef _COM_SUN_STAR_UNO_EXCEPTION_HDL_
33 #include <com/sun/star/uno/Exception.hdl>
35 #include <cppuhelper/interfacecontainer.hxx>
36 #include <com/sun/star/util/XCloseListener.hpp>
37 #include <com/sun/star/util/XCloseable.hpp>
38 #include <com/sun/star/lang/XComponent.hpp>
39 #include <cppuhelper/weakref.hxx>
40 #include "charttoolsdllapi.hxx"
48 friend class LifeTimeGuard
;
50 mutable ::osl::Mutex m_aAccessMutex
;
52 OOO_DLLPUBLIC_CHARTTOOLS
LifeTimeManager( ::com::sun::star::lang::XComponent
* pComponent
, sal_Bool bLongLastingCallsCancelable
= sal_False
);
53 OOO_DLLPUBLIC_CHARTTOOLS
virtual ~LifeTimeManager();
55 OOO_DLLPUBLIC_CHARTTOOLS
bool impl_isDisposed( bool bAssert
=true );
56 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool
dispose() throw(::com::sun::star::uno::RuntimeException
);
59 ::cppu::OMultiTypeInterfaceContainerHelper m_aListenerContainer
;
62 virtual sal_Bool
impl_canStartApiCall();
63 virtual void impl_apiCallCountReachedNull(){}
65 void impl_registerApiCall(sal_Bool bLongLastingCall
);
66 void impl_unregisterApiCall(sal_Bool bLongLastingCall
);
71 ::com::sun::star::lang::XComponent
* m_pComponent
;
73 ::osl::Condition m_aNoAccessCountCondition
;
74 sal_Int32
volatile m_nAccessCount
;
76 sal_Bool
volatile m_bDisposed
;
77 sal_Bool
volatile m_bInDispose
;
80 sal_Bool m_bLongLastingCallsCancelable
;
81 ::osl::Condition m_aNoLongLastingCallCountCondition
;
82 sal_Int32
volatile m_nLongLastingCallCount
;
85 class CloseableLifeTimeManager
: public LifeTimeManager
88 ::com::sun::star::util::XCloseable
* m_pCloseable
;
90 ::osl::Condition m_aEndTryClosingCondition
;
91 sal_Bool
volatile m_bClosed
;
92 sal_Bool
volatile m_bInTryClose
;
93 //the ownership between model and controller is not clear at first
94 //each controller might consider him as owner of the model first
95 //at start the model is not considered as owner of itself
96 sal_Bool
volatile m_bOwnership
;
97 //with a XCloseable::close call and during XCloseListener::queryClosing
98 //the ownership can be regulated more explicit,
99 //if so the ownership is considered to be well known
100 sal_Bool
volatile m_bOwnershipIsWellKnown
;
103 OOO_DLLPUBLIC_CHARTTOOLS
CloseableLifeTimeManager( ::com::sun::star::util::XCloseable
* pCloseable
104 , ::com::sun::star::lang::XComponent
* pComponent
105 , sal_Bool bLongLastingCallsCancelable
= sal_False
);
106 OOO_DLLPUBLIC_CHARTTOOLS
virtual ~CloseableLifeTimeManager();
108 OOO_DLLPUBLIC_CHARTTOOLS
bool impl_isDisposedOrClosed( bool bAssert
=true );
109 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool
g_close_startTryClose(sal_Bool bDeliverOwnership
)
110 throw ( ::com::sun::star::uno::Exception
);
111 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool
g_close_isNeedToCancelLongLastingCalls( sal_Bool bDeliverOwnership
, ::com::sun::star::util::CloseVetoException
& ex
)
112 throw ( ::com::sun::star::util::CloseVetoException
);
113 OOO_DLLPUBLIC_CHARTTOOLS
void g_close_endTryClose(sal_Bool bDeliverOwnership
, sal_Bool bMyVeto
);
114 OOO_DLLPUBLIC_CHARTTOOLS
void g_close_endTryClose_doClose();
115 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool
g_addCloseListener( const ::com::sun::star::uno::Reference
<
116 ::com::sun::star::util::XCloseListener
> & xListener
)
117 throw(::com::sun::star::uno::RuntimeException
);
120 virtual sal_Bool
impl_canStartApiCall();
121 virtual void impl_apiCallCountReachedNull();
123 void impl_setOwnership( sal_Bool bDeliverOwnership
, sal_Bool bMyVeto
);
124 sal_Bool
impl_shouldCloseAtNextChance();
129 m_bClosed
= sal_False
;
130 m_bInTryClose
= sal_False
;
131 m_bOwnership
= sal_False
;
132 m_bOwnershipIsWellKnown
= sal_False
;
133 m_aEndTryClosingCondition
.set();
137 //-----------------------------------------------------------------
139 Use this Guard in your apicalls to protect access on resources
140 which will be released in dispose.
141 It's guarantied, that the release of resources only starts if your
142 guarded call has finished.
143 ! It's only partly guaranteed that this resources will not change during the call.
144 See the example for details.
146 This class is to be used as described in the example.
148 If this guard is used in all api calls of an XCloseable object
149 it's guarantied, that the closeable will close itself after finishing the last call
155 LifeTimeGuard aLifeTimeGuard(m_aLifeTimeManager);
157 //mutex is acquired; call is not registered
159 if(!aLifeTimeGuard.startApiCall())
160 return ; //behave as passive as possible, if disposed or closed
162 //mutex is acquired, call is registered
164 //you might access some private members here
165 //but than you need to protect access to these members always like this
166 //never call to the outside here
169 aLifeTimeGuard.clear(); //!!!
171 //Mutex is released, the running call is still registered
172 //this call will finish before the 'release-section' in dispose is allowed to start
175 //you might access some private members here guarded with your own mutex
176 //but release your mutex at the end of this block
179 //you can call to the outside (without holding the mutex) without becoming disposed
181 //End of method -> ~LifeTimeGuard
182 //-> call is unregistered
183 //-> this object might be disposed now
186 your XComponent::dispose method has to be implemented in the following way:
191 if( !m_aLifeTimeManager.dispose() )
194 //--release all resources and references
199 //-----------------------------------------------------------------
201 class OOO_DLLPUBLIC_CHARTTOOLS LifeTimeGuard
205 LifeTimeGuard( LifeTimeManager
& rManager
)
206 : m_guard( rManager
.m_aAccessMutex
)
207 , m_rManager(rManager
)
208 , m_bCallRegistered(sal_False
)
209 , m_bLongLastingCallRegistered(sal_False
)
213 sal_Bool
startApiCall(sal_Bool bLongLastingCall
=sal_False
);
215 void clear() { m_guard
.clear(); }
218 osl::ClearableMutexGuard m_guard
;
219 LifeTimeManager
& m_rManager
;
220 sal_Bool m_bCallRegistered
;
221 sal_Bool m_bLongLastingCallRegistered
;
224 // these make no sense
225 LifeTimeGuard( ::osl::Mutex
& rMutex
);
226 LifeTimeGuard( const LifeTimeGuard
& );
227 LifeTimeGuard
& operator= ( const LifeTimeGuard
& );
238 NegativeGuard(T
* pT
) : m_pT(pT
)
243 NegativeGuard(T
& t
) : m_pT(&t
)
254 }//end namespace apphelper