xref: /aoo41x/main/chart2/source/inc/LifeTime.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 #ifndef _LIFETIME_HXX
28 #define _LIFETIME_HXX
29 
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>
34 #endif
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"
41 
42 namespace apphelper
43 {
44 
45 class LifeTimeGuard;
46 class LifeTimeManager
47 {
48 friend class LifeTimeGuard;
49 protected:
50 	mutable ::osl::Mutex					m_aAccessMutex;
51 public:
52 OOO_DLLPUBLIC_CHARTTOOLS	LifeTimeManager( ::com::sun::star::lang::XComponent* pComponent, sal_Bool bLongLastingCallsCancelable = sal_False );
53 OOO_DLLPUBLIC_CHARTTOOLS	virtual ~LifeTimeManager();
54 
55 OOO_DLLPUBLIC_CHARTTOOLS	bool    	impl_isDisposed( bool bAssert=true );
56 OOO_DLLPUBLIC_CHARTTOOLS	sal_Bool	dispose() throw(::com::sun::star::uno::RuntimeException);
57 
58 public:
59 	::cppu::OMultiTypeInterfaceContainerHelper		m_aListenerContainer;
60 
61 protected:
62 	virtual sal_Bool	impl_canStartApiCall();
63 	virtual void		impl_apiCallCountReachedNull(){}
64 
65 	void		impl_registerApiCall(sal_Bool bLongLastingCall);
66 	void		impl_unregisterApiCall(sal_Bool bLongLastingCall);
67 
68 	void		impl_init();
69 
70 protected:
71 	::com::sun::star::lang::XComponent*		m_pComponent;
72 
73 	::osl::Condition		m_aNoAccessCountCondition;
74 	sal_Int32 volatile		m_nAccessCount;
75 
76 	sal_Bool volatile		m_bDisposed;
77 	sal_Bool volatile		m_bInDispose;
78 
79 	//
80 	sal_Bool				m_bLongLastingCallsCancelable;
81 	::osl::Condition		m_aNoLongLastingCallCountCondition;
82 	sal_Int32 volatile		m_nLongLastingCallCount;
83 };
84 
85 class CloseableLifeTimeManager : public LifeTimeManager
86 {
87 protected:
88 	::com::sun::star::util::XCloseable*			m_pCloseable;
89 
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;
101 
102 public:
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();
107 
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);
118 
119 protected:
120 	virtual sal_Bool	impl_canStartApiCall();
121 	virtual void		impl_apiCallCountReachedNull();
122 
123 	void		impl_setOwnership( sal_Bool bDeliverOwnership, sal_Bool bMyVeto );
124 	sal_Bool	impl_shouldCloseAtNextChance();
125 	void		impl_doClose();
126 
127 	void		impl_init()
128 	{
129 		m_bClosed = sal_False;
130 		m_bInTryClose = sal_False;
131 		m_bOwnership = sal_False;
132 		m_bOwnershipIsWellKnown = sal_False;
133 		m_aEndTryClosingCondition.set();
134 	}
135 };
136 
137 //-----------------------------------------------------------------
138 /*
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.
145 
146 This class is to be used as described in the example.
147 
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
150 if it should do so.
151 
152   ::ApiCall
153 {
154 	//hold no mutex!!!
155 	LifeTimeGuard aLifeTimeGuard(m_aLifeTimeManager);
156 
157 	//mutex is acquired; call is not registered
158 
159 	if(!aLifeTimeGuard.startApiCall())
160 		return ; //behave as passive as possible, if disposed or closed
161 
162 	//mutex is acquired, call is registered
163 	{
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
167 	}
168 
169 	aLifeTimeGuard.clear(); //!!!
170 
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
173 
174 	{
175 		//you might access some private members here guarded with your own mutex
176 		//but release your mutex at the end of this block
177 	}
178 
179 	//you can call to the outside (without holding the mutex) without becoming disposed
180 
181 	//End of method -> ~LifeTimeGuard
182 	//-> call is unregistered
183 	//-> this object might be disposed now
184 }
185 
186 your XComponent::dispose method has to be implemented in the following way:
187 
188 	::dispose()
189 {
190 	//hold no mutex!!!
191 	if( !m_aLifeTimeManager.dispose() )
192 		return;
193 
194 	//--release all resources and references
195 	//...
196 }
197 
198 */
199 //-----------------------------------------------------------------
200 
201 class OOO_DLLPUBLIC_CHARTTOOLS LifeTimeGuard
202 {
203 
204 public:
205 	LifeTimeGuard( LifeTimeManager& rManager )
206 		: m_guard( rManager.m_aAccessMutex )
207 		, m_rManager(rManager)
208 		, m_bCallRegistered(sal_False)
209 		, m_bLongLastingCallRegistered(sal_False)
210 	{
211 
212 	}
213 	sal_Bool startApiCall(sal_Bool bLongLastingCall=sal_False);
214 	~LifeTimeGuard();
215     void clear() { m_guard.clear(); }
216 
217 private:
218     osl::ClearableMutexGuard m_guard;
219 	LifeTimeManager&	m_rManager;
220 	sal_Bool			m_bCallRegistered;
221 	sal_Bool			m_bLongLastingCallRegistered;
222 
223 private:
224 	// these make no sense
225 	LifeTimeGuard( ::osl::Mutex& rMutex );
226 	LifeTimeGuard( const LifeTimeGuard& );
227     LifeTimeGuard& operator= ( const LifeTimeGuard& );
228 };
229 
230 
231 template<class T>
232 class NegativeGuard
233 {
234 protected:
235 	T * m_pT;
236 public:
237 
238 	NegativeGuard(T * pT) : m_pT(pT)
239 	{
240 		m_pT->release();
241 	}
242 
243 	NegativeGuard(T & t) : m_pT(&t)
244 	{
245 		m_pT->release();
246 	}
247 
248 	~NegativeGuard()
249 	{
250 		m_pT->acquire();
251 	}
252 };
253 
254 }//end namespace apphelper
255 #endif
256