bump product version to 4.1.6.2
[LibreOffice.git] / comphelper / source / property / genericpropertyset.cxx
blob831dc5908e00908cfcc7d851e8c284bda92120fc
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/lang/XServiceInfo.hpp>
21 #include <com/sun/star/lang/XTypeProvider.hpp>
22 #include <cppuhelper/weakagg.hxx>
23 #include <cppuhelper/interfacecontainer.hxx>
24 #include <comphelper/propertysethelper.hxx>
25 #include <osl/mutex.hxx>
26 #include <comphelper/genericpropertyset.hxx>
27 #include <comphelper/propertysetinfo.hxx>
28 #include <comphelper/stl_types.hxx>
29 #include <comphelper/servicehelper.hxx>
30 #include <rtl/uuid.h>
32 ///////////////////////////////////////////////////////////////////////
34 using namespace ::rtl;
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 DECLARE_STL_USTRINGACCESS_MAP( 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, OUStringHash,UStringEqual> m_aListener;
62 protected:
63 virtual void _setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues ) throw( UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException );
64 virtual void _getPropertyValues( const PropertyMapEntry** ppEntries, Any* pValue ) throw( UnknownPropertyException, WrappedTargetException );
66 public:
67 GenericPropertySet( PropertySetInfo* pInfo ) throw();
68 virtual ~GenericPropertySet() throw();
70 // XInterface
71 virtual Any SAL_CALL queryAggregation( const Type & rType ) throw( RuntimeException);
72 virtual Any SAL_CALL queryInterface( const Type & rType ) throw( RuntimeException);
73 virtual void SAL_CALL acquire() throw();
74 virtual void SAL_CALL release() throw();
76 // XTypeProvider
77 virtual Sequence< Type > SAL_CALL getTypes( ) throw( RuntimeException);
78 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId( ) throw( RuntimeException);
80 // XServiceInfo
81 virtual OUString SAL_CALL getImplementationName() throw( RuntimeException );
82 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw( RuntimeException );
83 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw( RuntimeException );
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);
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);
92 ///////////////////////////////////////////////////////////////////////
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)
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)
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 const OUString aPropertyName( (*ppEntries)->mpName, (*ppEntries)->mnNameLen, RTL_TEXTENCODING_ASCII_US );
157 OInterfaceContainerHelper * pHelper = m_aListener.getContainer(aPropertyName);
159 maAnyMap[ aPropertyName ] = *pValues;
161 if ( pHelper )
163 PropertyChangeEvent aEvt;
164 aEvt.PropertyName = aPropertyName;
165 aEvt.NewValue = *pValues;
166 aGuard.clear();
167 pHelper->notifyEach( &XPropertyChangeListener::propertyChange, aEvt );
168 aGuard.reset();
171 ppEntries++;
172 pValues++;
176 void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, Any* pValue )
177 throw( UnknownPropertyException, WrappedTargetException )
179 MutexGuard aGuard( maMutex );
181 while( *ppEntries )
183 const OUString aPropertyName( (*ppEntries)->mpName, (*ppEntries)->mnNameLen, RTL_TEXTENCODING_ASCII_US );
184 *pValue = maAnyMap[ aPropertyName ];
186 ppEntries++;
187 pValue++;
191 // XInterface
193 Any SAL_CALL GenericPropertySet::queryInterface( const Type & rType )
194 throw( RuntimeException )
196 return OWeakAggObject::queryInterface( rType );
199 Any SAL_CALL GenericPropertySet::queryAggregation( const Type & rType )
200 throw(RuntimeException)
202 Any aAny;
204 if( rType == ::getCppuType((const Reference< XServiceInfo >*)0) )
205 aAny <<= Reference< XServiceInfo >(this);
206 else if( rType == ::getCppuType((const Reference< XTypeProvider >*)0) )
207 aAny <<= Reference< XTypeProvider >(this);
208 else if( rType == ::getCppuType((const Reference< XPropertySet >*)0) )
209 aAny <<= Reference< XPropertySet >(this);
210 else if( rType == ::getCppuType((const Reference< XMultiPropertySet >*)0) )
211 aAny <<= Reference< XMultiPropertySet >(this);
212 else
213 aAny <<= OWeakAggObject::queryAggregation( rType );
215 return aAny;
218 void SAL_CALL GenericPropertySet::acquire() throw()
220 OWeakAggObject::acquire();
223 void SAL_CALL GenericPropertySet::release() throw()
225 OWeakAggObject::release();
228 uno::Sequence< uno::Type > SAL_CALL GenericPropertySet::getTypes()
229 throw (uno::RuntimeException)
231 uno::Sequence< uno::Type > aTypes( 5 );
232 uno::Type* pTypes = aTypes.getArray();
234 *pTypes++ = ::getCppuType((const uno::Reference< XAggregation>*)0);
235 *pTypes++ = ::getCppuType((const uno::Reference< XServiceInfo>*)0);
236 *pTypes++ = ::getCppuType((const uno::Reference< XTypeProvider>*)0);
237 *pTypes++ = ::getCppuType((const uno::Reference< XPropertySet>*)0);
238 *pTypes++ = ::getCppuType((const uno::Reference< XMultiPropertySet>*)0);
240 return aTypes;
243 namespace
245 class theGenericPropertySetImplmentationId : public rtl::Static< UnoTunnelIdInit, theGenericPropertySetImplmentationId > {};
248 uno::Sequence< sal_Int8 > SAL_CALL GenericPropertySet::getImplementationId()
249 throw (uno::RuntimeException)
251 return theGenericPropertySetImplmentationId::get().getSeq();
254 // XServiceInfo
256 sal_Bool SAL_CALL GenericPropertySet::supportsService( const OUString& ServiceName ) throw(RuntimeException)
258 Sequence< OUString > aSNL( getSupportedServiceNames() );
259 const OUString * pArray = aSNL.getConstArray();
261 for( sal_Int32 i = 0; i < aSNL.getLength(); ++i )
262 if( pArray[i] == ServiceName )
263 return sal_True;
265 return sal_False;
268 OUString SAL_CALL GenericPropertySet::getImplementationName() throw( RuntimeException )
270 return OUString( "com.sun.star.comp.comphelper.GenericPropertySet" );
273 Sequence< OUString > SAL_CALL GenericPropertySet::getSupportedServiceNames( )
274 throw( RuntimeException )
276 Sequence< OUString > aSNS( 1 );
277 aSNS.getArray()[0] = "com.sun.star.beans.XPropertySet";
278 return aSNS;
281 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo* pInfo )
283 return (XPropertySet*)new GenericPropertySet( pInfo );
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */