bump product version to 6.3.0.0.beta1
[LibreOffice.git] / comphelper / source / property / genericpropertyset.cxx
blob5ce75e30323a11d5179bb3d19791ba5de751f8d6
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/interfacecontainer.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <comphelper/propertysethelper.hxx>
30 #include <osl/mutex.hxx>
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 struct IMPL_GenericPropertySet_MutexContainer
47 Mutex maMutex;
50 class GenericPropertySet : public OWeakAggObject,
51 public XServiceInfo,
52 public XTypeProvider,
53 public PropertySetHelper,
54 private IMPL_GenericPropertySet_MutexContainer
56 private:
57 std::map<OUString, Any> maAnyMap;
58 cppu::OMultiTypeInterfaceContainerHelperVar<OUString> m_aListener;
60 protected:
61 virtual void _setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues ) override;
62 virtual void _getPropertyValues( const PropertyMapEntry** ppEntries, Any* pValue ) override;
64 public:
65 explicit GenericPropertySet( PropertySetInfo* pInfo ) throw();
67 // XInterface
68 virtual Any SAL_CALL queryAggregation( const Type & rType ) override;
69 virtual Any SAL_CALL queryInterface( const Type & rType ) override;
70 virtual void SAL_CALL acquire() throw() override;
71 virtual void SAL_CALL release() throw() override;
73 // XTypeProvider
74 virtual Sequence< Type > SAL_CALL getTypes( ) override;
75 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId( ) override;
77 // XServiceInfo
78 virtual OUString SAL_CALL getImplementationName() override;
79 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
80 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
82 // XPropertySet
83 virtual void SAL_CALL addPropertyChangeListener( const OUString& aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener >& xListener ) override;
84 virtual void SAL_CALL removePropertyChangeListener( const OUString& aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener >& aListener ) override;
90 GenericPropertySet::GenericPropertySet( PropertySetInfo* pInfo ) throw()
91 : PropertySetHelper( pInfo )
92 ,m_aListener(maMutex)
96 void SAL_CALL GenericPropertySet::addPropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener )
98 Reference < XPropertySetInfo > xInfo = getPropertySetInfo( );
99 if ( xInfo.is() )
101 if ( aPropertyName.isEmpty() )
103 Sequence< Property> aSeq = xInfo->getProperties();
104 const Property* pIter = aSeq.getConstArray();
105 const Property* pEnd = pIter + aSeq.getLength();
106 for( ; pIter != pEnd ; ++pIter)
108 m_aListener.addInterface(pIter->Name,xListener);
111 else if ( xInfo->hasPropertyByName(aPropertyName) )
112 m_aListener.addInterface(aPropertyName,xListener);
113 else
114 throw UnknownPropertyException( aPropertyName, *this );
118 void SAL_CALL GenericPropertySet::removePropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener )
120 ClearableMutexGuard aGuard( maMutex );
121 Reference < XPropertySetInfo > xInfo = getPropertySetInfo( );
122 aGuard.clear();
123 if ( xInfo.is() )
125 if ( aPropertyName.isEmpty() )
127 Sequence< Property> aSeq = xInfo->getProperties();
128 const Property* pIter = aSeq.getConstArray();
129 const Property* pEnd = pIter + aSeq.getLength();
130 for( ; pIter != pEnd ; ++pIter)
132 m_aListener.removeInterface(pIter->Name,xListener);
135 else if ( xInfo->hasPropertyByName(aPropertyName) )
136 m_aListener.removeInterface(aPropertyName,xListener);
137 else
138 throw UnknownPropertyException( aPropertyName, *this );
142 void GenericPropertySet::_setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues )
144 ResettableMutexGuard aGuard( maMutex );
146 while( *ppEntries )
148 OInterfaceContainerHelper * pHelper = m_aListener.getContainer((*ppEntries)->maName);
150 maAnyMap[ (*ppEntries)->maName ] = *pValues;
152 if ( pHelper )
154 PropertyChangeEvent aEvt;
155 aEvt.PropertyName = (*ppEntries)->maName;
156 aEvt.NewValue = *pValues;
157 aGuard.clear();
158 pHelper->notifyEach( &XPropertyChangeListener::propertyChange, aEvt );
159 aGuard.reset();
162 ppEntries++;
163 pValues++;
167 void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, Any* pValue )
169 MutexGuard aGuard( maMutex );
171 while( *ppEntries )
173 *pValue = maAnyMap[ (*ppEntries)->maName ];
175 ppEntries++;
176 pValue++;
180 // XInterface
182 Any SAL_CALL GenericPropertySet::queryInterface( const Type & rType )
184 return OWeakAggObject::queryInterface( rType );
187 Any SAL_CALL GenericPropertySet::queryAggregation( const Type & rType )
189 Any aAny;
191 if( rType == cppu::UnoType<XServiceInfo>::get())
192 aAny <<= Reference< XServiceInfo >(this);
193 else if( rType == cppu::UnoType<XTypeProvider>::get())
194 aAny <<= Reference< XTypeProvider >(this);
195 else if( rType == cppu::UnoType<XPropertySet>::get())
196 aAny <<= Reference< XPropertySet >(this);
197 else if( rType == cppu::UnoType<XMultiPropertySet>::get())
198 aAny <<= Reference< XMultiPropertySet >(this);
199 else
200 aAny = OWeakAggObject::queryAggregation( rType );
202 return aAny;
205 void SAL_CALL GenericPropertySet::acquire() throw()
207 OWeakAggObject::acquire();
210 void SAL_CALL GenericPropertySet::release() throw()
212 OWeakAggObject::release();
215 uno::Sequence< uno::Type > SAL_CALL GenericPropertySet::getTypes()
217 return uno::Sequence {
218 cppu::UnoType<XAggregation>::get(),
219 cppu::UnoType<XServiceInfo>::get(),
220 cppu::UnoType<XTypeProvider>::get(),
221 cppu::UnoType<XPropertySet>::get(),
222 cppu::UnoType<XMultiPropertySet>::get() };
225 uno::Sequence< sal_Int8 > SAL_CALL GenericPropertySet::getImplementationId()
227 return css::uno::Sequence<sal_Int8>();
230 // XServiceInfo
231 sal_Bool SAL_CALL GenericPropertySet::supportsService( const OUString& ServiceName )
233 return cppu::supportsService(this, ServiceName);
236 OUString SAL_CALL GenericPropertySet::getImplementationName()
238 return OUString( "com.sun.star.comp.comphelper.GenericPropertySet" );
241 Sequence< OUString > SAL_CALL GenericPropertySet::getSupportedServiceNames( )
243 Sequence<OUString> aSNS { "com.sun.star.beans.XPropertySet" };
244 return aSNS;
247 css::uno::Reference< css::beans::XPropertySet > comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo* pInfo )
249 return static_cast<XPropertySet*>(new GenericPropertySet( pInfo ));
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */