simplify writeBitmapObject
[LibreOffice.git] / comphelper / source / property / propmultiplex2.cxx
bloba2a1599c4b2edea83fb9c2a65ec97afbac70845d
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 #include <com/sun/star/beans/XPropertySet.hpp>
21 #include <comphelper/propmultiplex2.hxx>
22 #include <osl/diagnose.h>
24 namespace comphelper
26 using namespace ::com::sun::star::uno;
27 using namespace ::com::sun::star::lang;
28 using namespace ::com::sun::star::beans;
30 OPropertyChangeListener2::~OPropertyChangeListener2()
32 if (m_xAdapter.is())
33 m_xAdapter->onListenerDestruction();
36 void OPropertyChangeListener2::disposeAdapter(std::unique_lock<std::mutex>& rGuard)
38 if (m_xAdapter.is())
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)
55 : m_rMutex(rMutex)
56 , m_xSet(_rxSet)
57 , m_pListener(_pListener)
58 , m_nLockCount(0)
59 , m_bListening(false)
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)
72 if (!m_bListening)
73 return;
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;
83 m_bListening = false;
85 m_xSet = nullptr;
88 void OPropertyChangeMultiplexer2::onListenerDestruction()
90 if (!m_bListening)
91 return;
93 Reference<XPropertyChangeListener> xPreventDelete(this);
95 for (const OUString& rProp : m_aProperties)
96 m_xSet->removePropertyChangeListener(rProp, static_cast<XPropertyChangeListener*>(this));
99 // XEventListener
101 void SAL_CALL OPropertyChangeMultiplexer2::disposing(const EventObject& /*_rSource*/)
103 std::unique_lock g(m_rMutex);
104 // disconnect the listener
105 if (m_pListener)
106 m_pListener->setAdapter(g, nullptr);
108 m_pListener = nullptr;
109 m_bListening = false;
111 m_xSet = nullptr;
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)
124 if (m_xSet.is())
126 m_xSet->addPropertyChangeListener(_sPropertyName,
127 static_cast<XPropertyChangeListener*>(this));
128 m_aProperties.push_back(_sPropertyName);
129 m_bListening = true;
134 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */