xref: /AOO41X/main/chart2/source/inc/LifeTime.hxx (revision de7b3f825c374d9f7270d1282e9d92876f197200)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _LIFETIME_HXX
24 #define _LIFETIME_HXX
25 
26 #include <osl/mutex.hxx>
27 #include <osl/conditn.hxx>
28 #ifndef _COM_SUN_STAR_UNO_EXCEPTION_HDL_
29 #include <com/sun/star/uno/Exception.hdl>
30 #endif
31 #include <cppuhelper/interfacecontainer.hxx>
32 #include <com/sun/star/util/XCloseListener.hpp>
33 #include <com/sun/star/util/XCloseable.hpp>
34 #include <com/sun/star/lang/XComponent.hpp>
35 #include <cppuhelper/weakref.hxx>
36 #include "charttoolsdllapi.hxx"
37 
38 namespace apphelper
39 {
40 
41 class LifeTimeGuard;
42 class LifeTimeManager
43 {
44 friend class LifeTimeGuard;
45 protected:
46     mutable ::osl::Mutex                    m_aAccessMutex;
47 public:
48 OOO_DLLPUBLIC_CHARTTOOLS    LifeTimeManager( ::com::sun::star::lang::XComponent* pComponent, sal_Bool bLongLastingCallsCancelable = sal_False );
49 OOO_DLLPUBLIC_CHARTTOOLS    virtual ~LifeTimeManager();
50 
51 OOO_DLLPUBLIC_CHARTTOOLS    bool        impl_isDisposed( bool bAssert=true );
52 OOO_DLLPUBLIC_CHARTTOOLS    sal_Bool    dispose() throw(::com::sun::star::uno::RuntimeException);
53 
54 public:
55     ::cppu::OMultiTypeInterfaceContainerHelper      m_aListenerContainer;
56 
57 protected:
58     virtual sal_Bool    impl_canStartApiCall();
impl_apiCallCountReachedNull()59     virtual void        impl_apiCallCountReachedNull(){}
60 
61     void        impl_registerApiCall(sal_Bool bLongLastingCall);
62     void        impl_unregisterApiCall(sal_Bool bLongLastingCall);
63 
64     void        impl_init();
65 
66 protected:
67     ::com::sun::star::lang::XComponent*     m_pComponent;
68 
69     ::osl::Condition        m_aNoAccessCountCondition;
70     sal_Int32 volatile      m_nAccessCount;
71 
72     sal_Bool volatile       m_bDisposed;
73     sal_Bool volatile       m_bInDispose;
74 
75     //
76     sal_Bool                m_bLongLastingCallsCancelable;
77     ::osl::Condition        m_aNoLongLastingCallCountCondition;
78     sal_Int32 volatile      m_nLongLastingCallCount;
79 };
80 
81 class CloseableLifeTimeManager : public LifeTimeManager
82 {
83 protected:
84     ::com::sun::star::util::XCloseable*         m_pCloseable;
85 
86     ::osl::Condition        m_aEndTryClosingCondition;
87     sal_Bool volatile       m_bClosed;
88     sal_Bool volatile       m_bInTryClose;
89     //the ownership between model and controller is not clear at first
90     //each controller might consider him as owner of the model first
91     //at start the model is not considered as owner of itself
92     sal_Bool volatile       m_bOwnership;
93     //with a XCloseable::close call and during XCloseListener::queryClosing
94     //the ownership can be regulated more explicit,
95     //if so the ownership is considered to be well known
96     sal_Bool volatile       m_bOwnershipIsWellKnown;
97 
98 public:
99 OOO_DLLPUBLIC_CHARTTOOLS    CloseableLifeTimeManager( ::com::sun::star::util::XCloseable* pCloseable
100         , ::com::sun::star::lang::XComponent* pComponent
101         , sal_Bool bLongLastingCallsCancelable = sal_False );
102 OOO_DLLPUBLIC_CHARTTOOLS    virtual ~CloseableLifeTimeManager();
103 
104 OOO_DLLPUBLIC_CHARTTOOLS    bool        impl_isDisposedOrClosed( bool bAssert=true );
105 OOO_DLLPUBLIC_CHARTTOOLS    sal_Bool    g_close_startTryClose(sal_Bool bDeliverOwnership)
106                     throw ( ::com::sun::star::uno::Exception );
107 OOO_DLLPUBLIC_CHARTTOOLS    sal_Bool    g_close_isNeedToCancelLongLastingCalls( sal_Bool bDeliverOwnership, ::com::sun::star::util::CloseVetoException& ex )
108                     throw ( ::com::sun::star::util::CloseVetoException );
109 OOO_DLLPUBLIC_CHARTTOOLS    void        g_close_endTryClose(sal_Bool bDeliverOwnership, sal_Bool bMyVeto );
110 OOO_DLLPUBLIC_CHARTTOOLS    void        g_close_endTryClose_doClose();
111 OOO_DLLPUBLIC_CHARTTOOLS    sal_Bool    g_addCloseListener( const ::com::sun::star::uno::Reference<
112                     ::com::sun::star::util::XCloseListener > & xListener )
113                     throw(::com::sun::star::uno::RuntimeException);
114 
115 protected:
116     virtual sal_Bool    impl_canStartApiCall();
117     virtual void        impl_apiCallCountReachedNull();
118 
119     void        impl_setOwnership( sal_Bool bDeliverOwnership, sal_Bool bMyVeto );
120     sal_Bool    impl_shouldCloseAtNextChance();
121     void        impl_doClose();
122 
impl_init()123     void        impl_init()
124     {
125         m_bClosed = sal_False;
126         m_bInTryClose = sal_False;
127         m_bOwnership = sal_False;
128         m_bOwnershipIsWellKnown = sal_False;
129         m_aEndTryClosingCondition.set();
130     }
131 };
132 
133 //-----------------------------------------------------------------
134 /*
135 Use this Guard in your apicalls to protect access on resources
136 which will be released in dispose.
137 It's guarantied, that the release of resources only starts if your
138 guarded call has finished.
139 ! It's only partly guaranteed that this resources will not change during the call.
140 See the example for details.
141 
142 This class is to be used as described in the example.
143 
144 If this guard is used in all api calls of an XCloseable object
145 it's guarantied, that the closeable will close itself after finishing the last call
146 if it should do so.
147 
148   ::ApiCall
149 {
150     //hold no mutex!!!
151     LifeTimeGuard aLifeTimeGuard(m_aLifeTimeManager);
152 
153     //mutex is acquired; call is not registered
154 
155     if(!aLifeTimeGuard.startApiCall())
156         return ; //behave as passive as possible, if disposed or closed
157 
158     //mutex is acquired, call is registered
159     {
160         //you might access some private members here
161         //but than you need to protect access to these members always like this
162         //never call to the outside here
163     }
164 
165     aLifeTimeGuard.clear(); //!!!
166 
167     //Mutex is released, the running call is still registered
168     //this call will finish before the 'release-section' in dispose is allowed to start
169 
170     {
171         //you might access some private members here guarded with your own mutex
172         //but release your mutex at the end of this block
173     }
174 
175     //you can call to the outside (without holding the mutex) without becoming disposed
176 
177     //End of method -> ~LifeTimeGuard
178     //-> call is unregistered
179     //-> this object might be disposed now
180 }
181 
182 your XComponent::dispose method has to be implemented in the following way:
183 
184     ::dispose()
185 {
186     //hold no mutex!!!
187     if( !m_aLifeTimeManager.dispose() )
188         return;
189 
190     //--release all resources and references
191     //...
192 }
193 
194 */
195 //-----------------------------------------------------------------
196 
197 class OOO_DLLPUBLIC_CHARTTOOLS LifeTimeGuard
198 {
199 
200 public:
LifeTimeGuard(LifeTimeManager & rManager)201     LifeTimeGuard( LifeTimeManager& rManager )
202         : m_guard( rManager.m_aAccessMutex )
203         , m_rManager(rManager)
204         , m_bCallRegistered(sal_False)
205         , m_bLongLastingCallRegistered(sal_False)
206     {
207 
208     }
209     sal_Bool startApiCall(sal_Bool bLongLastingCall=sal_False);
210     ~LifeTimeGuard();
clear()211     void clear() { m_guard.clear(); }
212 
213 private:
214     osl::ClearableMutexGuard m_guard;
215     LifeTimeManager&    m_rManager;
216     sal_Bool            m_bCallRegistered;
217     sal_Bool            m_bLongLastingCallRegistered;
218 
219 private:
220     // these make no sense
221     LifeTimeGuard( ::osl::Mutex& rMutex );
222     LifeTimeGuard( const LifeTimeGuard& );
223     LifeTimeGuard& operator= ( const LifeTimeGuard& );
224 };
225 
226 
227 template<class T>
228 class NegativeGuard
229 {
230 protected:
231     T * m_pT;
232 public:
233 
NegativeGuard(T * pT)234     NegativeGuard(T * pT) : m_pT(pT)
235     {
236         m_pT->release();
237     }
238 
NegativeGuard(T & t)239     NegativeGuard(T & t) : m_pT(&t)
240     {
241         m_pT->release();
242     }
243 
~NegativeGuard()244     ~NegativeGuard()
245     {
246         m_pT->acquire();
247     }
248 };
249 
250 }//end namespace apphelper
251 #endif
252