LibreOffice Module chart2 (master) 1
LifeTime.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19#pragma once
20
21#include <mutex>
22#include <osl/conditn.hxx>
24#include "charttoolsdllapi.hxx"
25
26namespace com::sun::star::document { class XStorageChangeListener; }
27namespace com::sun::star::lang { class XComponent; }
28namespace com::sun::star::lang { class XEventListener; }
29namespace com::sun::star::util { class CloseVetoException; }
30namespace com::sun::star::util { class XCloseListener; }
31namespace com::sun::star::util { class XCloseable; }
32namespace com::sun::star::util { class XModifyListener; }
33namespace com::sun::star::view { class XSelectionChangeListener; }
34
35namespace apphelper
36{
37
39{
40friend class LifeTimeGuard;
41public:
42 LifeTimeManager( css::lang::XComponent* pComponent );
43 virtual ~LifeTimeManager();
44
45 bool impl_isDisposed( bool bAssert=true );
47 bool dispose();
48
49 mutable std::mutex m_aAccessMutex;
55
56protected:
57 SAL_DLLPRIVATE virtual bool impl_canStartApiCall();
58 SAL_DLLPRIVATE virtual void impl_apiCallCountReachedNull(std::unique_lock<std::mutex>& /*rGuard*/){}
59
60 SAL_DLLPRIVATE void impl_registerApiCall(bool bLongLastingCall);
61 SAL_DLLPRIVATE void impl_unregisterApiCall(std::unique_lock<std::mutex>& rGuard, bool bLongLastingCall);
62
63 css::lang::XComponent* m_pComponent;
64 ::osl::Condition m_aNoAccessCountCondition;
65 sal_Int32 m_nAccessCount;
66 bool volatile m_bDisposed;
67 bool volatile m_bInDispose;
70};
71
73{
74 css::util::XCloseable* m_pCloseable;
75
76 ::osl::Condition m_aEndTryClosingCondition;
77 bool volatile m_bClosed;
78 bool volatile m_bInTryClose;
79 //the ownership between model and controller is not clear at first
80 //each controller might consider him as owner of the model first
81 //at start the model is not considered as owner of itself
82 bool volatile m_bOwnership;
83
84public:
85 CloseableLifeTimeManager( css::util::XCloseable* pCloseable
86 , css::lang::XComponent* pComponent );
87 virtual ~CloseableLifeTimeManager() override;
88
89 bool impl_isDisposedOrClosed( bool bAssert=true );
91 bool g_close_startTryClose(bool bDeliverOwnership);
93 void g_close_isNeedToCancelLongLastingCalls( bool bDeliverOwnership, css::util::CloseVetoException const & ex );
94 void g_close_endTryClose(bool bDeliverOwnership );
97 void g_addCloseListener( const css::uno::Reference< css::util::XCloseListener > & xListener );
98
99private:
100 virtual bool impl_canStartApiCall() override;
101 virtual void impl_apiCallCountReachedNull(std::unique_lock<std::mutex>& rGuard) override;
102
103 void impl_setOwnership( bool bDeliverOwnership, bool bMyVeto );
104 void impl_doClose(std::unique_lock<std::mutex>& rGuard);
105};
106
107/*
108Use this Guard in your ApiCalls to protect access on resources
109which will be released in dispose.
110It's guaranteed that the release of resources only starts if your
111guarded call has finished.
112! It's only partly guaranteed that this resources will not change during the call.
113See the example for details.
114
115This class is to be used as described in the example.
116
117If this guard is used in all api calls of an XCloseable object
118it's guaranteed that the closeable will close itself after finishing the last call
119if it should do so.
120
121 ::ApiCall
122{
123 //hold no mutex!!!
124 LifeTimeGuard aLifeTimeGuard(m_aLifeTimeManager);
125
126 //mutex is acquired; call is not registered
127
128 if(!aLifeTimeGuard.startApiCall())
129 return ; //behave as passive as possible, if disposed or closed
130
131 //mutex is acquired, call is registered
132 {
133 //you might access some private members here
134 //but then you need to protect access to these members always like this
135 //never call to the outside here
136 }
137
138 aLifeTimeGuard.clear(); //!!!
139
140 //Mutex is released, the running call is still registered
141 //this call will finish before the 'release-section' in dispose is allowed to start
142
143 {
144 //you might access some private members here guarded with your own mutex
145 //but release your mutex at the end of this block
146 }
147
148 //you can call to the outside (without holding the mutex) without becoming disposed
149
150 //End of method -> ~LifeTimeGuard
151 //-> call is unregistered
152 //-> this object might be disposed now
153}
154
155your XComponent::dispose method has to be implemented in the following way:
156
157 ::dispose()
158{
159 //hold no mutex!!!
160 if( !m_aLifeTimeManager.dispose() )
161 return;
162
163 //--release all resources and references
164
165}
166
167*/
168
170{
171
172public:
174 : m_guard( rManager.m_aAccessMutex )
175 , m_rManager(rManager)
176 , m_bCallRegistered(false)
178 {
179
180 }
181 bool startApiCall(bool bLongLastingCall=false);
183 void clear() { m_guard.unlock(); }
184
185private:
186 std::unique_lock<std::mutex> m_guard;
190
191private:
192 LifeTimeGuard( const LifeTimeGuard& ) = delete;
194};
195
196}//end namespace apphelper
197
198/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define OOO_DLLPUBLIC_CHARTTOOLS
::osl::Condition m_aEndTryClosingCondition
Definition: LifeTime.hxx:76
virtual bool impl_canStartApiCall() override
Definition: LifeTime.cxx:360
void impl_doClose(std::unique_lock< std::mutex > &rGuard)
Definition: LifeTime.cxx:305
void g_close_endTryClose(bool bDeliverOwnership)
Definition: LifeTime.cxx:240
void impl_setOwnership(bool bDeliverOwnership, bool bMyVeto)
Definition: LifeTime.cxx:292
void g_addCloseListener(const css::uno::Reference< css::util::XCloseListener > &xListener)
Definition: LifeTime.cxx:348
bool impl_isDisposedOrClosed(bool bAssert=true)
Definition: LifeTime.cxx:170
virtual ~CloseableLifeTimeManager() override
Definition: LifeTime.cxx:166
virtual void impl_apiCallCountReachedNull(std::unique_lock< std::mutex > &rGuard) override
Definition: LifeTime.cxx:297
void g_close_isNeedToCancelLongLastingCalls(bool bDeliverOwnership, css::util::CloseVetoException const &ex)
Definition: LifeTime.cxx:254
CloseableLifeTimeManager(css::util::XCloseable *pCloseable, css::lang::XComponent *pComponent)
Definition: LifeTime.cxx:155
css::util::XCloseable * m_pCloseable
Definition: LifeTime.hxx:74
bool g_close_startTryClose(bool bDeliverOwnership)
Definition: LifeTime.cxx:186
std::unique_lock< std::mutex > m_guard
Definition: LifeTime.hxx:186
LifeTimeGuard(const LifeTimeGuard &)=delete
bool startApiCall(bool bLongLastingCall=false)
Definition: LifeTime.cxx:387
LifeTimeGuard(LifeTimeManager &rManager)
Definition: LifeTime.hxx:173
LifeTimeGuard & operator=(const LifeTimeGuard &)=delete
LifeTimeManager & m_rManager
Definition: LifeTime.hxx:187
::comphelper::OInterfaceContainerHelper4< css::util::XModifyListener > m_aModifyListeners
Definition: LifeTime.hxx:51
::osl::Condition m_aNoLongLastingCallCountCondition
Definition: LifeTime.hxx:68
::comphelper::OInterfaceContainerHelper4< css::lang::XEventListener > m_aEventListeners
Definition: LifeTime.hxx:53
bool volatile m_bInDispose
Definition: LifeTime.hxx:67
bool volatile m_bDisposed
Definition: LifeTime.hxx:66
::comphelper::OInterfaceContainerHelper4< css::view::XSelectionChangeListener > m_aSelectionChangeListeners
Definition: LifeTime.hxx:54
virtual SAL_DLLPRIVATE void impl_apiCallCountReachedNull(std::unique_lock< std::mutex > &)
Definition: LifeTime.hxx:58
::comphelper::OInterfaceContainerHelper4< css::util::XCloseListener > m_aCloseListeners
Definition: LifeTime.hxx:50
::osl::Condition m_aNoAccessCountCondition
Definition: LifeTime.hxx:64
::comphelper::OInterfaceContainerHelper4< css::document::XStorageChangeListener > m_aStorageChangeListeners
Definition: LifeTime.hxx:52
sal_Int32 m_nLongLastingCallCount
Definition: LifeTime.hxx:69
css::lang::XComponent * m_pComponent
Definition: LifeTime.hxx:63
void dispose()