Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / comphelper / accessiblecontexthelper.hxx
blob7163817280a32a3e090c6ad1e4db44bc6a530ca6
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 .
20 #ifndef INCLUDED_COMPHELPER_ACCESSIBLECONTEXTHELPER_HXX
21 #define INCLUDED_COMPHELPER_ACCESSIBLECONTEXTHELPER_HXX
23 #include <cppuhelper/compbase2.hxx>
24 #include <cppuhelper/basemutex.hxx>
25 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
26 #include <com/sun/star/accessibility/XAccessibleEventBroadcaster.hpp>
27 #include <com/sun/star/lang/DisposedException.hpp>
28 #include <comphelper/comphelperdllapi.h>
29 #include <memory>
32 namespace comphelper
36 //= IMutex
39 // This whole thingie here (own mutex classes and such) is a HACK. I hate the SolarMutex.
40 // See below for more explanations ....
42 /** abstract interface for implementing a mutex
44 class COMPHELPER_DLLPUBLIC IMutex
46 public:
47 virtual ~IMutex();
48 virtual void acquire() = 0;
49 virtual void release() = 0;
53 //= OMutexGuard
56 class OMutexGuard
58 IMutex* m_pMutex;
59 public:
60 OMutexGuard( IMutex* _pMutex )
61 :m_pMutex( _pMutex )
63 if ( m_pMutex )
64 m_pMutex->acquire();
67 ~OMutexGuard( )
69 if ( m_pMutex )
70 m_pMutex->release();
75 //= OAccessibleContextHelper
78 class OContextHelper_Impl;
79 typedef ::cppu::WeakAggComponentImplHelper2 < css::accessibility::XAccessibleContext,
80 css::accessibility::XAccessibleEventBroadcaster
81 > OAccessibleContextHelper_Base;
83 /** helper class for implementing an AccessibleContext
85 class COMPHELPER_DLLPUBLIC OAccessibleContextHelper
86 :public ::cppu::BaseMutex
87 ,public OAccessibleContextHelper_Base
89 private:
90 std::unique_ptr<OContextHelper_Impl> m_pImpl;
92 protected:
93 virtual ~OAccessibleContextHelper( ) override;
95 /** ctor
97 <p>If you need additional object safety for your class, and want to ensure that your own
98 mutex is locked before the mutex this class provides is, than use this ctor.</p>
100 <p>Beware that this is a hack. Unfortunately, OpenOffice.org has two different mutex hierarchies,
101 which are not compatible. In addition, wide parts of the code (especially VCL) is not thread-safe,
102 but instead relies on a <em>single global mutex</em>. As a consequence, components using
103 directly or indirectly such code need to care for this global mutex. Yes, this is as ugly as
104 anything.</p>
106 <p>Note that the external lock is used as additional lock, not as the only one. The own mutex of the
107 instance is used for internal actions, and every action which potentially involves external code
108 (for instance every call to a virtual method overridden by derivees) is <em>additionally</em> and
109 <em>first</em> guarded by with the external lock.</p>
111 <p>Beware of the lifetime of the lock - you must ensure that the lock exists at least as long as
112 the context does. A good approach to implement the lock may be to derive you own context
113 not only from OAccessibleContextHelper, but also from IMutex.</p>
115 <p>One more note. This lock is definitely not used once the dtor is reached. Means whatever
116 the dtor implementation does, it does <em>not</em> guard the external lock. See this as a contract.
117 <br/>You should ensure the same thing for own derivees which do not supply the lock themself,
118 but get them from yet another derivee.</p>
119 @see forgetExternalLock
121 OAccessibleContextHelper( IMutex* _pExternalLock );
123 /** late construction
124 @param _rxAccessible
125 the Accessible object which created this context.
126 <p>If your derived implementation implements the XAccessible (and does not follow the proposed
127 separation of XAccessible from XAccessibleContext), you may pass <code>this</code> here.</p>
129 <p>The object is hold weak, so its life time is not affected.</p>
131 <p>The object is needed for performance reasons: for <method>getAccessibleIndexInParent</method>,
132 all children (which are XAccessible's theirself) of our parent have to be asked. If we know our
133 XAccessible, we can compare it with all the children, instead of asking all children for their
134 context and comparing this context with ourself.</p>
136 void lateInit( const css::uno::Reference< css::accessibility::XAccessible >& _rxAccessible );
138 /** retrieves the creator previously set with <method>lateInit</method>
140 css::uno::Reference< css::accessibility::XAccessible >
141 getAccessibleCreator( ) const;
143 public:
144 // XAccessibleEventBroadcaster
145 virtual void SAL_CALL addAccessibleEventListener( const css::uno::Reference< css::accessibility::XAccessibleEventListener >& xListener ) override;
146 virtual void SAL_CALL removeAccessibleEventListener( const css::uno::Reference< css::accessibility::XAccessibleEventListener >& xListener ) override;
148 // XAccessibleContext - still waiting to be overwritten
149 virtual sal_Int32 SAL_CALL getAccessibleChildCount( ) override = 0;
150 virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleChild( sal_Int32 i ) override = 0;
151 virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleParent( ) override = 0;
152 virtual sal_Int16 SAL_CALL getAccessibleRole( ) override = 0;
153 virtual OUString SAL_CALL getAccessibleDescription( ) override = 0;
154 virtual OUString SAL_CALL getAccessibleName( ) override = 0;
155 virtual css::uno::Reference< css::accessibility::XAccessibleRelationSet > SAL_CALL getAccessibleRelationSet( ) override = 0;
156 virtual css::uno::Reference< css::accessibility::XAccessibleStateSet > SAL_CALL getAccessibleStateSet( ) override = 0;
158 // XAccessibleContext - default implementations
159 /** default implementation for retrieving the index of this object within the parent
160 <p>This basic implementation here returns the index <code>i</code> of the child for which
161 <code>&lt;parent&gt;.getAccessibleChild( i )</code> equals our creator.</p>
163 virtual sal_Int32 SAL_CALL getAccessibleIndexInParent( ) override;
164 /** default implementation for retrieving the locale
165 <p>This basic implementation returns the locale of the parent context,
166 as retrieved via getAccessibleParent()->getAccessibleContext.</p>
168 virtual css::lang::Locale SAL_CALL getLocale( ) override;
170 public:
171 // helper struct for granting selective access rights
172 struct OAccessControl
174 friend class OContextEntryGuard;
175 friend class OContextHelper_Impl;
176 friend class OExternalLockGuard;
177 private:
178 OAccessControl() { }
181 // ensures that the object is alive
182 inline void ensureAlive( const OAccessControl& ) const;
183 inline IMutex* getExternalLock( const OAccessControl& );
184 inline ::osl::Mutex& GetMutex( const OAccessControl& );
186 protected:
187 // OComponentHelper
188 virtual void SAL_CALL disposing() override;
190 protected:
191 // helper
192 /** notifies all AccessibleEventListeners of a certain event
194 @precond not to be called with our mutex locked
195 @param _nEventId
196 the id of the event. See AccessibleEventType
197 @param _rOldValue
198 the old value to be notified
199 @param _rNewValue
200 the new value to be notified
202 void NotifyAccessibleEvent(
203 const sal_Int16 _nEventId,
204 const css::uno::Any& _rOldValue,
205 const css::uno::Any& _rNewValue
208 // life time control
209 /// checks whether the object is alive (returns <TRUE/> then) or disposed
210 bool isAlive() const;
211 /// checks for being alive. If the object is already disposed (i.e. not alive), an exception is thrown.
212 void ensureAlive() const;
214 /** ensures that the object is disposed.
215 @precond
216 to be called from within the destructor of your derived class only!
218 void ensureDisposed( );
220 /** shortcut for retrieving the context of the parent (returned by getAccessibleParent)
222 css::uno::Reference< css::accessibility::XAccessibleContext >
223 implGetParentContext();
225 // access to the base class' broadcast helper/mutex
226 ::osl::Mutex& GetMutex() { return m_aMutex; }
227 IMutex* getExternalLock( );
231 inline void OAccessibleContextHelper::ensureAlive( const OAccessControl& ) const
233 ensureAlive();
237 inline IMutex* OAccessibleContextHelper::getExternalLock( const OAccessControl& )
239 return getExternalLock();
243 inline ::osl::Mutex& OAccessibleContextHelper::GetMutex( const OAccessControl& )
245 return GetMutex();
249 //= OContextEntryGuard
251 typedef ::osl::ClearableMutexGuard OContextEntryGuard_Base;
252 /** helper class for guarding the entry into OAccessibleContextHelper methods.
254 <p>The class has two responsibilities:
255 <ul><li>it locks the mutex of an OAccessibleContextHelper instance, as long as the guard lives</li>
256 <li>it checks if an given OAccessibleContextHelper instance is alive, else an exception is thrown
257 our of the constructor of the guard</li>
258 </ul>
259 <br/>
260 This makes it your first choice (hopefully :) for guarding any interface method implementations of
261 you derived class.
262 </p>
264 class OContextEntryGuard : public OContextEntryGuard_Base
266 public:
267 /** constructs the guard
269 <p>The given context (it's mutex, respectively) is locked, and an exception is thrown if the context
270 is not alive anymore. In the latter case, of course, the mutex is freed, again.</p>
272 @param _pContext
273 the context which shall be guarded
274 @precond <arg>_pContext</arg> != NULL
276 inline OContextEntryGuard( OAccessibleContextHelper* _pContext );
280 inline OContextEntryGuard::OContextEntryGuard( OAccessibleContextHelper* _pContext )
281 :OContextEntryGuard_Base( _pContext->GetMutex( OAccessibleContextHelper::OAccessControl() ) )
283 _pContext->ensureAlive( OAccessibleContextHelper::OAccessControl() );
287 //= OExternalLockGuard
289 class OExternalLockGuard
290 :public OMutexGuard
291 ,public OContextEntryGuard
293 public:
294 inline OExternalLockGuard( OAccessibleContextHelper* _pContext );
298 inline OExternalLockGuard::OExternalLockGuard( OAccessibleContextHelper* _pContext )
299 :OMutexGuard( _pContext->getExternalLock( OAccessibleContextHelper::OAccessControl() ) )
300 ,OContextEntryGuard( _pContext )
302 // Only lock the external mutex,
303 // release the ::osl::Mutex of the OAccessibleContextHelper instance.
304 // If you call into another UNO object with locked ::osl::Mutex,
305 // this may lead to dead locks.
306 clear();
310 } // namespace comphelper
313 #endif // INCLUDED_COMPHELPER_ACCESSIBLECONTEXTHELPER_HXX
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */