/src/libreoffice/comphelper/source/property/propmultiplex2.cxx
Line | Count | Source (jump to first uncovered line) |
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 | | #include <com/sun/star/beans/XPropertySet.hpp> |
21 | | #include <comphelper/propmultiplex2.hxx> |
22 | | #include <osl/diagnose.h> |
23 | | |
24 | | namespace comphelper |
25 | | { |
26 | | using namespace ::com::sun::star::uno; |
27 | | using namespace ::com::sun::star::lang; |
28 | | using namespace ::com::sun::star::beans; |
29 | | |
30 | | OPropertyChangeListener2::~OPropertyChangeListener2() |
31 | 0 | { |
32 | 0 | if (m_xAdapter.is()) |
33 | 0 | m_xAdapter->onListenerDestruction(); |
34 | 0 | } |
35 | | |
36 | | void OPropertyChangeListener2::disposeAdapter(std::unique_lock<std::mutex>& rGuard) |
37 | 0 | { |
38 | 0 | if (m_xAdapter.is()) |
39 | 0 | m_xAdapter->dispose(rGuard); |
40 | | |
41 | | // will automatically set a new adapter |
42 | 0 | OSL_ENSURE(!m_xAdapter.is(), "OPropertyChangeListener::disposeAdapter: what did dispose do?"); |
43 | 0 | } |
44 | | |
45 | | void OPropertyChangeListener2::setAdapter(std::unique_lock<std::mutex>& /*rGuard*/, |
46 | | OPropertyChangeMultiplexer2* pAdapter) |
47 | 0 | { |
48 | 0 | m_xAdapter = pAdapter; |
49 | 0 | } |
50 | | |
51 | | OPropertyChangeMultiplexer2::OPropertyChangeMultiplexer2(std::mutex& rMutex, |
52 | | std::unique_lock<std::mutex>& rGuard, |
53 | | OPropertyChangeListener2* _pListener, |
54 | | const Reference<XPropertySet>& _rxSet) |
55 | 0 | : m_rMutex(rMutex) |
56 | 0 | , m_xSet(_rxSet) |
57 | 0 | , m_pListener(_pListener) |
58 | 0 | , m_nLockCount(0) |
59 | 0 | , m_bListening(false) |
60 | 0 | { |
61 | 0 | m_pListener->setAdapter(rGuard, this); |
62 | 0 | } |
63 | | |
64 | 0 | OPropertyChangeMultiplexer2::~OPropertyChangeMultiplexer2() {} |
65 | | |
66 | 0 | void OPropertyChangeMultiplexer2::lock() { ++m_nLockCount; } |
67 | | |
68 | 0 | void OPropertyChangeMultiplexer2::unlock() { --m_nLockCount; } |
69 | | |
70 | | void OPropertyChangeMultiplexer2::dispose(std::unique_lock<std::mutex>& rGuard) |
71 | 0 | { |
72 | 0 | if (!m_bListening) |
73 | 0 | return; |
74 | | |
75 | 0 | Reference<XPropertyChangeListener> xPreventDelete(this); |
76 | |
|
77 | 0 | for (const OUString& rProp : m_aProperties) |
78 | 0 | m_xSet->removePropertyChangeListener(rProp, static_cast<XPropertyChangeListener*>(this)); |
79 | |
|
80 | 0 | m_pListener->setAdapter(rGuard, nullptr); |
81 | |
|
82 | 0 | m_pListener = nullptr; |
83 | 0 | m_bListening = false; |
84 | |
|
85 | 0 | m_xSet = nullptr; |
86 | 0 | } |
87 | | |
88 | | void OPropertyChangeMultiplexer2::onListenerDestruction() |
89 | 0 | { |
90 | 0 | if (!m_bListening) |
91 | 0 | return; |
92 | | |
93 | 0 | Reference<XPropertyChangeListener> xPreventDelete(this); |
94 | |
|
95 | 0 | for (const OUString& rProp : m_aProperties) |
96 | 0 | m_xSet->removePropertyChangeListener(rProp, static_cast<XPropertyChangeListener*>(this)); |
97 | 0 | } |
98 | | |
99 | | // XEventListener |
100 | | |
101 | | void SAL_CALL OPropertyChangeMultiplexer2::disposing(const EventObject& /*_rSource*/) |
102 | 0 | { |
103 | 0 | std::unique_lock g(m_rMutex); |
104 | | // disconnect the listener |
105 | 0 | if (m_pListener) |
106 | 0 | m_pListener->setAdapter(g, nullptr); |
107 | |
|
108 | 0 | m_pListener = nullptr; |
109 | 0 | m_bListening = false; |
110 | |
|
111 | 0 | m_xSet = nullptr; |
112 | 0 | } |
113 | | |
114 | | // XPropertyChangeListener |
115 | | |
116 | | void SAL_CALL OPropertyChangeMultiplexer2::propertyChange(const PropertyChangeEvent& _rEvent) |
117 | 0 | { |
118 | 0 | if (m_pListener && !locked()) |
119 | 0 | m_pListener->_propertyChanged(_rEvent); |
120 | 0 | } |
121 | | |
122 | | void OPropertyChangeMultiplexer2::addProperty(const OUString& _sPropertyName) |
123 | 0 | { |
124 | 0 | if (m_xSet.is()) |
125 | 0 | { |
126 | 0 | m_xSet->addPropertyChangeListener(_sPropertyName, |
127 | 0 | static_cast<XPropertyChangeListener*>(this)); |
128 | 0 | m_aProperties.push_back(_sPropertyName); |
129 | 0 | m_bListening = true; |
130 | 0 | } |
131 | 0 | } |
132 | | } |
133 | | |
134 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |