LibreOffice Module comphelper (master) 1
propmultiplex2.cxx
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
20#include <com/sun/star/beans/XPropertySet.hpp>
22#include <osl/diagnose.h>
23
24namespace comphelper
25{
26using namespace ::com::sun::star::uno;
27using namespace ::com::sun::star::lang;
28using namespace ::com::sun::star::beans;
29
31{
32 if (m_xAdapter.is())
33 m_xAdapter->onListenerDestruction();
34}
35
36void OPropertyChangeListener2::disposeAdapter(std::unique_lock<std::mutex>& rGuard)
37{
38 if (m_xAdapter.is())
39 m_xAdapter->dispose(rGuard);
40
41 // will automatically set a new adapter
42 OSL_ENSURE(!m_xAdapter.is(), "OPropertyChangeListener::disposeAdapter: what did dispose do?");
43}
44
45void OPropertyChangeListener2::setAdapter(std::unique_lock<std::mutex>& /*rGuard*/,
47{
48 m_xAdapter = pAdapter;
49}
50
52 std::unique_lock<std::mutex>& rGuard,
53 OPropertyChangeListener2* _pListener,
54 const Reference<XPropertySet>& _rxSet)
55 : m_rMutex(rMutex)
56 , m_xSet(_rxSet)
57 , m_pListener(_pListener)
58 , m_nLockCount(0)
59 , m_bListening(false)
60{
61 m_pListener->setAdapter(rGuard, this);
62}
63
65
67
69
70void OPropertyChangeMultiplexer2::dispose(std::unique_lock<std::mutex>& rGuard)
71{
72 if (!m_bListening)
73 return;
74
75 Reference<XPropertyChangeListener> xPreventDelete(this);
76
77 for (const OUString& rProp : m_aProperties)
78 m_xSet->removePropertyChangeListener(rProp, static_cast<XPropertyChangeListener*>(this));
79
80 m_pListener->setAdapter(rGuard, nullptr);
81
82 m_pListener = nullptr;
83 m_bListening = false;
84
85 m_xSet = nullptr;
86}
87
89{
90 if (!m_bListening)
91 return;
92
93 Reference<XPropertyChangeListener> xPreventDelete(this);
94
95 for (const OUString& rProp : m_aProperties)
96 m_xSet->removePropertyChangeListener(rProp, static_cast<XPropertyChangeListener*>(this));
97}
98
99// XEventListener
100
101void SAL_CALL OPropertyChangeMultiplexer2::disposing(const EventObject& /*_rSource*/)
102{
103 std::unique_lock g(m_rMutex);
104 if (m_pListener)
105 {
106 // disconnect the listener
107 if (m_pListener) // may have been reset whilst calling into _disposing
108 m_pListener->setAdapter(g, nullptr);
109 }
110
111 m_pListener = nullptr;
112 m_bListening = false;
113
114 m_xSet = nullptr;
115}
116
117// XPropertyChangeListener
118
119void SAL_CALL OPropertyChangeMultiplexer2::propertyChange(const PropertyChangeEvent& _rEvent)
120{
121 if (m_pListener && !locked())
123}
124
125void OPropertyChangeMultiplexer2::addProperty(const OUString& _sPropertyName)
126{
127 if (m_xSet.is())
128 {
129 m_xSet->addPropertyChangeListener(_sPropertyName,
130 static_cast<XPropertyChangeListener*>(this));
131 m_aProperties.push_back(_sPropertyName);
132 m_bListening = true;
133 }
134}
135}
136
137/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
simple listener adapter for property sets
virtual void _propertyChanged(const css::beans::PropertyChangeEvent &_rEvent)=0
void setAdapter(std::unique_lock< std::mutex > &rGuard, OPropertyChangeMultiplexer2 *_pAdapter)
void disposeAdapter(std::unique_lock< std::mutex > &rGuard)
If the derivee also owns the mutex which we know as reference, then call this within your derivee's d...
rtl::Reference< OPropertyChangeMultiplexer2 > m_xAdapter
sal_Int32 locked() const
get the lock count
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
void dispose(std::unique_lock< std::mutex > &rGuard)
virtual void SAL_CALL propertyChange(const css::beans::PropertyChangeEvent &evt) override
void addProperty(const OUString &aPropertyName)
css::uno::Reference< css::beans::XPropertySet > m_xSet
OPropertyChangeMultiplexer2(std::mutex &rMutex, std::unique_lock< std::mutex > &rGuard, OPropertyChangeListener2 *_pListener, const css::uno::Reference< css::beans::XPropertySet > &_rxSet)
OPropertyChangeListener2 * m_pListener
::osl::Mutex & m_rMutex
std::mutex mutex
Definition: random.cxx:41