bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / property / genericpropertyset.cxx
blob5ce4ab16bade52e3fd3d0a2132eff638e8c6f4d9
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 <comphelper/genericpropertyset.hxx>
32 #include <comphelper/propertysetinfo.hxx>
33 #include <comphelper/servicehelper.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 typedef std::map<OUString, Any> GenericAnyMapImpl;
45 namespace comphelper
47 struct IMPL_GenericPropertySet_MutexContainer
49 Mutex maMutex ;
50 } ;
52 class GenericPropertySet : public OWeakAggObject,
53 public XServiceInfo,
54 public XTypeProvider,
55 public PropertySetHelper,
56 private IMPL_GenericPropertySet_MutexContainer
58 private:
59 GenericAnyMapImpl maAnyMap;
60 cppu::OMultiTypeInterfaceContainerHelperVar<OUString> m_aListener;
62 protected:
63 virtual void _setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues ) throw( UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException ) SAL_OVERRIDE;
64 virtual void _getPropertyValues( const PropertyMapEntry** ppEntries, Any* pValue ) throw( UnknownPropertyException, WrappedTargetException ) SAL_OVERRIDE;
66 public:
67 GenericPropertySet( PropertySetInfo* pInfo ) throw();
68 virtual ~GenericPropertySet() throw();
70 // XInterface
71 virtual Any SAL_CALL queryAggregation( const Type & rType ) throw( RuntimeException, std::exception) SAL_OVERRIDE;
72 virtual Any SAL_CALL queryInterface( const Type & rType ) throw( RuntimeException, std::exception) SAL_OVERRIDE;
73 virtual void SAL_CALL acquire() throw() SAL_OVERRIDE;
74 virtual void SAL_CALL release() throw() SAL_OVERRIDE;
76 // XTypeProvider
77 virtual Sequence< Type > SAL_CALL getTypes( ) throw( RuntimeException, std::exception) SAL_OVERRIDE;
78 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId( ) throw( RuntimeException, std::exception) SAL_OVERRIDE;
80 // XServiceInfo
81 virtual OUString SAL_CALL getImplementationName() throw( RuntimeException, std::exception ) SAL_OVERRIDE;
82 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
83 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw( RuntimeException, std::exception ) SAL_OVERRIDE;
85 // XPropertySet
86 virtual void SAL_CALL addPropertyChangeListener( const OUString& aPropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& xListener ) throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
87 virtual void SAL_CALL removePropertyChangeListener( const OUString& aPropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& aListener ) throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
94 GenericPropertySet::GenericPropertySet( PropertySetInfo* pInfo ) throw()
95 : PropertySetHelper( pInfo )
96 ,m_aListener(maMutex)
100 GenericPropertySet::~GenericPropertySet() throw()
103 void SAL_CALL GenericPropertySet::addPropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
105 Reference < XPropertySetInfo > xInfo = getPropertySetInfo( );
106 if ( xInfo.is() )
108 if ( aPropertyName.isEmpty() )
110 Sequence< Property> aSeq = xInfo->getProperties();
111 const Property* pIter = aSeq.getConstArray();
112 const Property* pEnd = pIter + aSeq.getLength();
113 for( ; pIter != pEnd ; ++pIter)
115 m_aListener.addInterface(pIter->Name,xListener);
118 else if ( xInfo->hasPropertyByName(aPropertyName) )
119 m_aListener.addInterface(aPropertyName,xListener);
120 else
121 throw UnknownPropertyException( aPropertyName, *this );
125 void SAL_CALL GenericPropertySet::removePropertyChangeListener( const OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException, std::exception)
127 ResettableMutexGuard aGuard( maMutex );
128 Reference < XPropertySetInfo > xInfo = getPropertySetInfo( );
129 aGuard.clear();
130 if ( xInfo.is() )
132 if ( aPropertyName.isEmpty() )
134 Sequence< Property> aSeq = xInfo->getProperties();
135 const Property* pIter = aSeq.getConstArray();
136 const Property* pEnd = pIter + aSeq.getLength();
137 for( ; pIter != pEnd ; ++pIter)
139 m_aListener.removeInterface(pIter->Name,xListener);
142 else if ( xInfo->hasPropertyByName(aPropertyName) )
143 m_aListener.removeInterface(aPropertyName,xListener);
144 else
145 throw UnknownPropertyException( aPropertyName, *this );
149 void GenericPropertySet::_setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues )
150 throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException )
152 ResettableMutexGuard aGuard( maMutex );
154 while( *ppEntries )
156 OInterfaceContainerHelper * pHelper = m_aListener.getContainer((*ppEntries)->maName);
158 maAnyMap[ (*ppEntries)->maName ] = *pValues;
160 if ( pHelper )
162 PropertyChangeEvent aEvt;
163 aEvt.PropertyName = (*ppEntries)->maName;
164 aEvt.NewValue = *pValues;
165 aGuard.clear();
166 pHelper->notifyEach( &XPropertyChangeListener::propertyChange, aEvt );
167 aGuard.reset();
170 ppEntries++;
171 pValues++;
175 void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, Any* pValue )
176 throw( UnknownPropertyException, WrappedTargetException )
178 MutexGuard aGuard( maMutex );
180 while( *ppEntries )
182 *pValue = maAnyMap[ (*ppEntries)->maName ];
184 ppEntries++;
185 pValue++;
189 // XInterface
191 Any SAL_CALL GenericPropertySet::queryInterface( const Type & rType )
192 throw( RuntimeException, std::exception )
194 return OWeakAggObject::queryInterface( rType );
197 Any SAL_CALL GenericPropertySet::queryAggregation( const Type & rType )
198 throw(RuntimeException, std::exception)
200 Any aAny;
202 if( rType == cppu::UnoType<XServiceInfo>::get())
203 aAny <<= Reference< XServiceInfo >(this);
204 else if( rType == cppu::UnoType<XTypeProvider>::get())
205 aAny <<= Reference< XTypeProvider >(this);
206 else if( rType == cppu::UnoType<XPropertySet>::get())
207 aAny <<= Reference< XPropertySet >(this);
208 else if( rType == cppu::UnoType<XMultiPropertySet>::get())
209 aAny <<= Reference< XMultiPropertySet >(this);
210 else
211 aAny <<= OWeakAggObject::queryAggregation( rType );
213 return aAny;
216 void SAL_CALL GenericPropertySet::acquire() throw()
218 OWeakAggObject::acquire();
221 void SAL_CALL GenericPropertySet::release() throw()
223 OWeakAggObject::release();
226 uno::Sequence< uno::Type > SAL_CALL GenericPropertySet::getTypes()
227 throw (uno::RuntimeException, std::exception)
229 uno::Sequence< uno::Type > aTypes( 5 );
230 uno::Type* pTypes = aTypes.getArray();
232 *pTypes++ = cppu::UnoType<XAggregation>::get();
233 *pTypes++ = cppu::UnoType<XServiceInfo>::get();
234 *pTypes++ = cppu::UnoType<XTypeProvider>::get();
235 *pTypes++ = cppu::UnoType<XPropertySet>::get();
236 *pTypes++ = cppu::UnoType<XMultiPropertySet>::get();
238 return aTypes;
241 uno::Sequence< sal_Int8 > SAL_CALL GenericPropertySet::getImplementationId()
242 throw (uno::RuntimeException, std::exception)
244 return css::uno::Sequence<sal_Int8>();
247 // XServiceInfo
248 sal_Bool SAL_CALL GenericPropertySet::supportsService( const OUString& ServiceName ) throw(RuntimeException, std::exception)
250 return cppu::supportsService(this, ServiceName);
253 OUString SAL_CALL GenericPropertySet::getImplementationName() throw( RuntimeException, std::exception )
255 return OUString( "com.sun.star.comp.comphelper.GenericPropertySet" );
258 Sequence< OUString > SAL_CALL GenericPropertySet::getSupportedServiceNames( )
259 throw( RuntimeException, std::exception )
261 Sequence< OUString > aSNS( 1 );
262 aSNS.getArray()[0] = "com.sun.star.beans.XPropertySet";
263 return aSNS;
266 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo* pInfo )
268 return (XPropertySet*)new GenericPropertySet( pInfo );
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */