1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #include <com/sun/star/beans/XPropertySet.hpp>
21 #include <comphelper/propmultiplex2.hxx>
22 #include <osl/diagnose.h>
26 using namespace ::com::sun::star::uno
;
27 using namespace ::com::sun::star::lang
;
28 using namespace ::com::sun::star::beans
;
30 OPropertyChangeListener2::~OPropertyChangeListener2()
33 m_xAdapter
->onListenerDestruction();
36 void OPropertyChangeListener2::disposeAdapter(std::unique_lock
<std::mutex
>& rGuard
)
39 m_xAdapter
->dispose(rGuard
);
41 // will automatically set a new adapter
42 OSL_ENSURE(!m_xAdapter
.is(), "OPropertyChangeListener::disposeAdapter: what did dispose do?");
45 void OPropertyChangeListener2::setAdapter(std::unique_lock
<std::mutex
>& /*rGuard*/,
46 OPropertyChangeMultiplexer2
* pAdapter
)
48 m_xAdapter
= pAdapter
;
51 OPropertyChangeMultiplexer2::OPropertyChangeMultiplexer2(std::mutex
& rMutex
,
52 std::unique_lock
<std::mutex
>& rGuard
,
53 OPropertyChangeListener2
* _pListener
,
54 const Reference
<XPropertySet
>& _rxSet
)
57 , m_pListener(_pListener
)
61 m_pListener
->setAdapter(rGuard
, this);
64 OPropertyChangeMultiplexer2::~OPropertyChangeMultiplexer2() {}
66 void OPropertyChangeMultiplexer2::lock() { ++m_nLockCount
; }
68 void OPropertyChangeMultiplexer2::unlock() { --m_nLockCount
; }
70 void OPropertyChangeMultiplexer2::dispose(std::unique_lock
<std::mutex
>& rGuard
)
75 Reference
<XPropertyChangeListener
> xPreventDelete(this);
77 for (const OUString
& rProp
: m_aProperties
)
78 m_xSet
->removePropertyChangeListener(rProp
, static_cast<XPropertyChangeListener
*>(this));
80 m_pListener
->setAdapter(rGuard
, nullptr);
82 m_pListener
= nullptr;
88 void OPropertyChangeMultiplexer2::onListenerDestruction()
93 Reference
<XPropertyChangeListener
> xPreventDelete(this);
95 for (const OUString
& rProp
: m_aProperties
)
96 m_xSet
->removePropertyChangeListener(rProp
, static_cast<XPropertyChangeListener
*>(this));
101 void SAL_CALL
OPropertyChangeMultiplexer2::disposing(const EventObject
& /*_rSource*/)
103 std::unique_lock
g(m_rMutex
);
104 // disconnect the listener
106 m_pListener
->setAdapter(g
, nullptr);
108 m_pListener
= nullptr;
109 m_bListening
= false;
114 // XPropertyChangeListener
116 void SAL_CALL
OPropertyChangeMultiplexer2::propertyChange(const PropertyChangeEvent
& _rEvent
)
118 if (m_pListener
&& !locked())
119 m_pListener
->_propertyChanged(_rEvent
);
122 void OPropertyChangeMultiplexer2::addProperty(const OUString
& _sPropertyName
)
126 m_xSet
->addPropertyChangeListener(_sPropertyName
,
127 static_cast<XPropertyChangeListener
*>(this));
128 m_aProperties
.push_back(_sPropertyName
);
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */