fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / chart2 / source / inc / LifeTime.hxx
blob63c9bdadf0c435620c733c5c90a4c19104b6ee68
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <com/sun/star/uno/Exception.hpp>
25 #include <cppuhelper/interfacecontainer.hxx>
26 #include <com/sun/star/util/XCloseListener.hpp>
27 #include <com/sun/star/util/XCloseable.hpp>
28 #include <com/sun/star/lang/XComponent.hpp>
29 #include <cppuhelper/weakref.hxx>
30 #include "charttoolsdllapi.hxx"
32 namespace apphelper
35 class LifeTimeGuard;
36 class OOO_DLLPUBLIC_CHARTTOOLS LifeTimeManager
38 friend class LifeTimeGuard;
39 protected:
40 mutable ::osl::Mutex m_aAccessMutex;
41 public:
42 LifeTimeManager( ::com::sun::star::lang::XComponent* pComponent, bool bLongLastingCallsCancelable = false );
43 virtual ~LifeTimeManager();
45 bool impl_isDisposed( bool bAssert=true );
46 bool dispose() throw(::com::sun::star::uno::RuntimeException);
48 public:
49 ::cppu::OMultiTypeInterfaceContainerHelper m_aListenerContainer;
51 protected:
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);
58 SAL_DLLPRIVATE void impl_init();
60 protected:
61 ::com::sun::star::lang::XComponent* m_pComponent;
63 ::osl::Condition m_aNoAccessCountCondition;
64 sal_Int32 volatile m_nAccessCount;
66 bool volatile m_bDisposed;
67 bool volatile m_bInDispose;
69 bool m_bLongLastingCallsCancelable;
70 ::osl::Condition m_aNoLongLastingCallCountCondition;
71 sal_Int32 volatile m_nLongLastingCallCount;
74 class CloseableLifeTimeManager : public LifeTimeManager
76 protected:
77 ::com::sun::star::util::XCloseable* m_pCloseable;
79 ::osl::Condition m_aEndTryClosingCondition;
80 bool volatile m_bClosed;
81 bool volatile m_bInTryClose;
82 //the ownership between model and controller is not clear at first
83 //each controller might consider him as owner of the model first
84 //at start the model is not considered as owner of itself
85 bool volatile m_bOwnership;
87 public:
88 OOO_DLLPUBLIC_CHARTTOOLS CloseableLifeTimeManager( ::com::sun::star::util::XCloseable* pCloseable
89 , ::com::sun::star::lang::XComponent* pComponent
90 , bool bLongLastingCallsCancelable = false );
91 OOO_DLLPUBLIC_CHARTTOOLS virtual ~CloseableLifeTimeManager();
93 OOO_DLLPUBLIC_CHARTTOOLS bool impl_isDisposedOrClosed( bool bAssert=true );
94 OOO_DLLPUBLIC_CHARTTOOLS bool g_close_startTryClose(bool bDeliverOwnership)
95 throw ( ::com::sun::star::uno::Exception );
96 OOO_DLLPUBLIC_CHARTTOOLS bool g_close_isNeedToCancelLongLastingCalls( bool bDeliverOwnership, ::com::sun::star::util::CloseVetoException& ex )
97 throw ( ::com::sun::star::util::CloseVetoException );
98 OOO_DLLPUBLIC_CHARTTOOLS void g_close_endTryClose(bool bDeliverOwnership, bool bMyVeto );
99 OOO_DLLPUBLIC_CHARTTOOLS void g_close_endTryClose_doClose();
100 OOO_DLLPUBLIC_CHARTTOOLS bool g_addCloseListener( const ::com::sun::star::uno::Reference<
101 ::com::sun::star::util::XCloseListener > & xListener )
102 throw(::com::sun::star::uno::RuntimeException);
104 protected:
105 virtual bool impl_canStartApiCall() SAL_OVERRIDE;
106 virtual void impl_apiCallCountReachedNull() SAL_OVERRIDE;
108 void impl_setOwnership( bool bDeliverOwnership, bool bMyVeto );
109 bool impl_shouldCloseAtNextChance() { return m_bOwnership;}
110 void impl_doClose();
112 void impl_init()
114 m_bClosed = false;
115 m_bInTryClose = false;
116 m_bOwnership = false;
117 m_aEndTryClosingCondition.set();
122 Use this Guard in your apicalls to protect access on resources
123 which will be released in dispose.
124 It's guarantied, that the release of resources only starts if your
125 guarded call has finished.
126 ! It's only partly guaranteed that this resources will not change during the call.
127 See the example for details.
129 This class is to be used as described in the example.
131 If this guard is used in all api calls of an XCloseable object
132 it's guarantied, that the closeable will close itself after finishing the last call
133 if it should do so.
135 ::ApiCall
137 //hold no mutex!!!
138 LifeTimeGuard aLifeTimeGuard(m_aLifeTimeManager);
140 //mutex is acquired; call is not registered
142 if(!aLifeTimeGuard.startApiCall())
143 return ; //behave as passive as possible, if disposed or closed
145 //mutex is acquired, call is registered
147 //you might access some private members here
148 //but than you need to protect access to these members always like this
149 //never call to the outside here
152 aLifeTimeGuard.clear(); //!!!
154 //Mutex is released, the running call is still registered
155 //this call will finish before the 'release-section' in dispose is allowed to start
158 //you might access some private members here guarded with your own mutex
159 //but release your mutex at the end of this block
162 //you can call to the outside (without holding the mutex) without becoming disposed
164 //End of method -> ~LifeTimeGuard
165 //-> call is unregistered
166 //-> this object might be disposed now
169 your XComponent::dispose method has to be implemented in the following way:
171 ::dispose()
173 //hold no mutex!!!
174 if( !m_aLifeTimeManager.dispose() )
175 return;
177 //--release all resources and references
183 class OOO_DLLPUBLIC_CHARTTOOLS LifeTimeGuard
186 public:
187 LifeTimeGuard( LifeTimeManager& rManager )
188 : m_guard( rManager.m_aAccessMutex )
189 , m_rManager(rManager)
190 , m_bCallRegistered(false)
191 , m_bLongLastingCallRegistered(false)
195 bool startApiCall(bool bLongLastingCall=false);
196 ~LifeTimeGuard();
197 void clear() { m_guard.clear(); }
199 private:
200 osl::ClearableMutexGuard m_guard;
201 LifeTimeManager& m_rManager;
202 bool m_bCallRegistered;
203 bool m_bLongLastingCallRegistered;
205 private:
206 LifeTimeGuard( const LifeTimeGuard& ) SAL_DELETED_FUNCTION;
207 LifeTimeGuard& operator= ( const LifeTimeGuard& ) SAL_DELETED_FUNCTION;
210 template<class T>
211 class NegativeGuard
213 protected:
214 T * m_pT;
215 public:
217 NegativeGuard(T * pT) : m_pT(pT)
219 m_pT->release();
222 NegativeGuard(T & t) : m_pT(&t)
224 m_pT->release();
227 ~NegativeGuard()
229 m_pT->acquire();
233 }//end namespace apphelper
234 #endif
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */