/src/libreoffice/include/comphelper/accessiblecontexthelper.hxx
Line | Count | Source |
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 | | |
20 | | #pragma once |
21 | | |
22 | | #include <comphelper/OAccessible.hxx> |
23 | | #include <comphelper/solarmutex.hxx> |
24 | | |
25 | | |
26 | | namespace comphelper |
27 | | { |
28 | | |
29 | | |
30 | | //= OContextEntryGuard |
31 | | |
32 | | /** helper class for guarding the entry into OAccessible methods. |
33 | | |
34 | | <p>The class has two responsibilities: |
35 | | <ul><li>it locks the mutex of an OAccessible instance, as long as the guard lives</li> |
36 | | <li>it checks if a given OAccessible instance is alive, else an exception is thrown |
37 | | our of the constructor of the guard</li> |
38 | | </ul> |
39 | | <br/> |
40 | | This makes it your first choice (hopefully :) for guarding any interface method implementations of |
41 | | you derived class. |
42 | | </p> |
43 | | */ |
44 | | class OContextEntryGuard : public ::osl::ClearableMutexGuard |
45 | | { |
46 | | public: |
47 | | /** constructs the guard |
48 | | |
49 | | <p>The given context (it's mutex, respectively) is locked, and an exception is thrown if the context |
50 | | is not alive anymore. In the latter case, of course, the mutex is freed, again.</p> |
51 | | |
52 | | @param _pContext |
53 | | the context which shall be guarded |
54 | | @precond <arg>_pContext</arg> != NULL |
55 | | */ |
56 | | inline OContextEntryGuard(OAccessible* _pContext); |
57 | | }; |
58 | | |
59 | | inline OContextEntryGuard::OContextEntryGuard(OAccessible* _pContext) |
60 | 0 | : ::osl::ClearableMutexGuard( _pContext->GetMutex() ) |
61 | 0 | { |
62 | 0 | _pContext->ensureAlive(); |
63 | 0 | } |
64 | | |
65 | | |
66 | | //= OExternalLockGuard |
67 | | |
68 | | class OExternalLockGuard |
69 | | :public osl::Guard<SolarMutex> |
70 | | ,public OContextEntryGuard |
71 | | { |
72 | | public: |
73 | | inline OExternalLockGuard(OAccessible* _pContext); |
74 | | }; |
75 | | |
76 | | inline OExternalLockGuard::OExternalLockGuard(OAccessible* _pContext) |
77 | 0 | : osl::Guard<SolarMutex>(SolarMutex::get()) |
78 | 0 | , OContextEntryGuard(_pContext) |
79 | 0 | { |
80 | | // Only lock the external mutex, |
81 | | // release the ::osl::Mutex of the OAccessible instance. |
82 | | // If you call into another UNO object with locked ::osl::Mutex, |
83 | | // this may lead to dead locks. |
84 | 0 | clear(); |
85 | 0 | } |
86 | | |
87 | | |
88 | | } // namespace comphelper |
89 | | |
90 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |