Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / comphelper / source / property / genericpropertyset.cxx
blob3d389172c4963bc4e49ca6e44e006214d6b93d62
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 <sal/config.h>
22 #include <map>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/lang/XTypeProvider.hpp>
26 #include <cppuhelper/weakagg.hxx>
27 #include <cppuhelper/supportsservice.hxx>
28 #include <comphelper/multiinterfacecontainer4.hxx>
29 #include <comphelper/propertysethelper.hxx>
30 #include <mutex>
31 #include <rtl/ref.hxx>
32 #include <comphelper/genericpropertyset.hxx>
33 #include <comphelper/propertysetinfo.hxx>
35 using namespace ::osl;
36 using namespace ::cppu;
37 using namespace ::comphelper;
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::lang;
43 namespace comphelper
45 namespace {
47 class GenericPropertySet : public OWeakAggObject,
48 public XServiceInfo,
49 public XTypeProvider,
50 public PropertySetHelper
52 private:
53 std::map<OUString, Any> maAnyMap;
54 std::mutex maMutex;
55 comphelper::OMultiTypeInterfaceContainerHelperVar4<OUString, XPropertyChangeListener> m_aListener;
57 protected:
58 virtual void _setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues ) override;
59 virtual void _getPropertyValues( const PropertyMapEntry** ppEntries, Any* pValue ) override;
61 public:
62 explicit GenericPropertySet( PropertySetInfo* pInfo ) noexcept;
64 // XInterface
65 virtual Any SAL_CALL queryAggregation( const Type & rType ) override;
66 virtual Any SAL_CALL queryInterface( const Type & rType ) override;
67 virtual void SAL_CALL acquire() noexcept override;
68 virtual void SAL_CALL release() noexcept override;
70 // XTypeProvider
71 virtual Sequence< Type > SAL_CALL getTypes( ) override;
72 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId( ) override;
74 // XServiceInfo
75 virtual OUString SAL_CALL getImplementationName() override;
76 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
77 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
79 // XPropertySet
80 virtual void SAL_CALL addPropertyChangeListener( const OUString& aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener >& xListener ) override;
81 virtual void SAL_CALL removePropertyChangeListener( const OUString& aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener >& aListener ) override;
88 GenericPropertySet::GenericPropertySet( PropertySetInfo* pInfo ) noexcept
89 : PropertySetHelper( pInfo )
93 void SAL_CALL GenericPropertySet::addPropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener )
95 Reference < XPropertySetInfo > xInfo = getPropertySetInfo( );
96 if ( !xInfo.is() )
97 return;
99 std::unique_lock aGuard(maMutex);
100 if ( aPropertyName.isEmpty() )
102 Sequence< Property> aSeq = xInfo->getProperties();
103 const Property* pIter = aSeq.getConstArray();
104 const Property* pEnd = pIter + aSeq.getLength();
105 for( ; pIter != pEnd ; ++pIter)
107 m_aListener.addInterface(aGuard, pIter->Name,xListener);
110 else if ( xInfo->hasPropertyByName(aPropertyName) )
111 m_aListener.addInterface(aGuard, aPropertyName,xListener);
112 else
113 throw UnknownPropertyException( aPropertyName, *this );
116 void SAL_CALL GenericPropertySet::removePropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener )
118 Reference < XPropertySetInfo > xInfo = getPropertySetInfo( );
119 if ( !xInfo.is() )
120 return;
122 std::unique_lock aGuard(maMutex);
123 if ( aPropertyName.isEmpty() )
125 Sequence< Property> aSeq = xInfo->getProperties();
126 const Property* pIter = aSeq.getConstArray();
127 const Property* pEnd = pIter + aSeq.getLength();
128 for( ; pIter != pEnd ; ++pIter)
130 m_aListener.removeInterface(aGuard, pIter->Name,xListener);
133 else if ( xInfo->hasPropertyByName(aPropertyName) )
134 m_aListener.removeInterface(aGuard, aPropertyName,xListener);
135 else
136 throw UnknownPropertyException( aPropertyName, *this );
139 void GenericPropertySet::_setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues )
141 std::unique_lock aGuard(maMutex);
143 while( *ppEntries )
145 OInterfaceContainerHelper4<XPropertyChangeListener> * pHelper = m_aListener.getContainer(aGuard, (*ppEntries)->maName);
147 maAnyMap[ (*ppEntries)->maName ] = *pValues;
149 if ( pHelper )
151 PropertyChangeEvent aEvt;
152 aEvt.PropertyName = (*ppEntries)->maName;
153 aEvt.NewValue = *pValues;
154 pHelper->notifyEach( aGuard, &XPropertyChangeListener::propertyChange, aEvt );
157 ppEntries++;
158 pValues++;
162 void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, Any* pValue )
164 std::unique_lock aGuard(maMutex);
166 while( *ppEntries )
168 *pValue = maAnyMap[ (*ppEntries)->maName ];
170 ppEntries++;
171 pValue++;
175 // XInterface
177 Any SAL_CALL GenericPropertySet::queryInterface( const Type & rType )
179 return OWeakAggObject::queryInterface( rType );
182 Any SAL_CALL GenericPropertySet::queryAggregation( const Type & rType )
184 Any aAny;
186 if( rType == cppu::UnoType<XServiceInfo>::get())
187 aAny <<= Reference< XServiceInfo >(this);
188 else if( rType == cppu::UnoType<XTypeProvider>::get())
189 aAny <<= Reference< XTypeProvider >(this);
190 else if( rType == cppu::UnoType<XPropertySet>::get())
191 aAny <<= Reference< XPropertySet >(this);
192 else if( rType == cppu::UnoType<XMultiPropertySet>::get())
193 aAny <<= Reference< XMultiPropertySet >(this);
194 else
195 aAny = OWeakAggObject::queryAggregation( rType );
197 return aAny;
200 void SAL_CALL GenericPropertySet::acquire() noexcept
202 OWeakAggObject::acquire();
205 void SAL_CALL GenericPropertySet::release() noexcept
207 OWeakAggObject::release();
210 uno::Sequence< uno::Type > SAL_CALL GenericPropertySet::getTypes()
212 return uno::Sequence {
213 cppu::UnoType<XAggregation>::get(),
214 cppu::UnoType<XServiceInfo>::get(),
215 cppu::UnoType<XTypeProvider>::get(),
216 cppu::UnoType<XPropertySet>::get(),
217 cppu::UnoType<XMultiPropertySet>::get() };
220 uno::Sequence< sal_Int8 > SAL_CALL GenericPropertySet::getImplementationId()
222 return css::uno::Sequence<sal_Int8>();
225 // XServiceInfo
226 sal_Bool SAL_CALL GenericPropertySet::supportsService( const OUString& ServiceName )
228 return cppu::supportsService(this, ServiceName);
231 OUString SAL_CALL GenericPropertySet::getImplementationName()
233 return "com.sun.star.comp.comphelper.GenericPropertySet";
236 Sequence< OUString > SAL_CALL GenericPropertySet::getSupportedServiceNames( )
238 return { "com.sun.star.beans.XPropertySet" };
241 css::uno::Reference< css::beans::XPropertySet > comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo* pInfo )
243 return static_cast<XPropertySet*>(new GenericPropertySet( pInfo ));
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */