update dev300-m58
[ooovba.git] / comphelper / source / property / genericpropertyset.cxx
blob11bfe93f153928cba06e503aabb41c7d5fc93b03
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: genericpropertyset.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <com/sun/star/lang/XServiceInfo.hpp>
34 #include <com/sun/star/lang/XTypeProvider.hpp>
35 #include <cppuhelper/weakagg.hxx>
36 #include <cppuhelper/interfacecontainer.hxx>
37 #include <comphelper/propertysethelper.hxx>
38 #include <osl/mutex.hxx>
39 #include <comphelper/genericpropertyset.hxx>
40 #include <comphelper/propertysetinfo.hxx>
41 #include <comphelper/stl_types.hxx>
42 #include <vos/mutex.hxx>
43 #include <rtl/uuid.h>
44 #include <boost/mem_fn.hpp>
45 #include <boost/bind.hpp>
46 #include <boost/utility.hpp>
48 ///////////////////////////////////////////////////////////////////////
50 using namespace ::rtl;
51 using namespace ::osl;
52 using namespace ::cppu;
53 using namespace ::comphelper;
54 using namespace ::com::sun::star;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::beans;
57 using namespace ::com::sun::star::lang;
59 DECLARE_STL_USTRINGACCESS_MAP( Any, GenericAnyMapImpl );
61 namespace comphelper
63 struct IMPL_GenericPropertySet_MutexContainer
65 Mutex maMutex ;
66 } ;
68 class GenericPropertySet : public OWeakAggObject,
69 public XServiceInfo,
70 public XTypeProvider,
71 public PropertySetHelper,
72 private IMPL_GenericPropertySet_MutexContainer
74 private:
75 GenericAnyMapImpl maAnyMap;
76 ::cppu::OMultiTypeInterfaceContainerHelperVar< ::rtl::OUString,UStringHash,UStringEqual> m_aListener;
78 protected:
79 virtual void _setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues ) throw( UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException );
80 virtual void _getPropertyValues( const PropertyMapEntry** ppEntries, Any* pValue ) throw( UnknownPropertyException, WrappedTargetException );
82 public:
83 GenericPropertySet( PropertySetInfo* pInfo ) throw();
84 virtual ~GenericPropertySet() throw();
86 // XInterface
87 virtual Any SAL_CALL queryAggregation( const Type & rType ) throw( RuntimeException);
88 virtual Any SAL_CALL queryInterface( const Type & rType ) throw( RuntimeException);
89 virtual void SAL_CALL acquire() throw();
90 virtual void SAL_CALL release() throw();
92 // XTypeProvider
93 virtual Sequence< Type > SAL_CALL getTypes( ) throw( RuntimeException);
94 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId( ) throw( RuntimeException);
96 // XServiceInfo
97 virtual rtl::OUString SAL_CALL getImplementationName() throw( RuntimeException );
98 virtual sal_Bool SAL_CALL supportsService( const rtl::OUString& ServiceName ) throw( RuntimeException );
99 virtual Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames() throw( RuntimeException );
101 // XPropertySet
102 virtual void SAL_CALL addPropertyChangeListener( const ::rtl::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);
103 virtual void SAL_CALL removePropertyChangeListener( const ::rtl::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);
108 ///////////////////////////////////////////////////////////////////////
110 GenericPropertySet::GenericPropertySet( PropertySetInfo* pInfo ) throw()
111 : PropertySetHelper( pInfo )
112 ,m_aListener(maMutex)
116 GenericPropertySet::~GenericPropertySet() throw()
119 void SAL_CALL GenericPropertySet::addPropertyChangeListener( const ::rtl::OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
121 Reference < XPropertySetInfo > xInfo = getPropertySetInfo( );
122 if ( xInfo.is() )
124 if ( !aPropertyName.getLength() )
126 Sequence< Property> aSeq = xInfo->getProperties();
127 const Property* pIter = aSeq.getConstArray();
128 const Property* pEnd = pIter + aSeq.getLength();
129 for( ; pIter != pEnd ; ++pIter)
131 m_aListener.addInterface(pIter->Name,xListener);
134 else if ( xInfo->hasPropertyByName(aPropertyName) )
135 m_aListener.addInterface(aPropertyName,xListener);
136 else
137 throw UnknownPropertyException( aPropertyName, *this );
141 void SAL_CALL GenericPropertySet::removePropertyChangeListener( const ::rtl::OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
143 ResettableMutexGuard aGuard( maMutex );
144 Reference < XPropertySetInfo > xInfo = getPropertySetInfo( );
145 aGuard.clear();
146 if ( xInfo.is() )
148 if ( !aPropertyName.getLength() )
150 Sequence< Property> aSeq = xInfo->getProperties();
151 const Property* pIter = aSeq.getConstArray();
152 const Property* pEnd = pIter + aSeq.getLength();
153 for( ; pIter != pEnd ; ++pIter)
155 m_aListener.removeInterface(pIter->Name,xListener);
158 else if ( xInfo->hasPropertyByName(aPropertyName) )
159 m_aListener.removeInterface(aPropertyName,xListener);
160 else
161 throw UnknownPropertyException( aPropertyName, *this );
165 void GenericPropertySet::_setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues )
166 throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException )
168 ResettableMutexGuard aGuard( maMutex );
170 while( *ppEntries )
172 const OUString aPropertyName( (*ppEntries)->mpName, (*ppEntries)->mnNameLen, RTL_TEXTENCODING_ASCII_US );
173 OInterfaceContainerHelper * pHelper = m_aListener.getContainer(aPropertyName);
175 maAnyMap[ aPropertyName ] = *pValues;
177 if ( pHelper )
179 PropertyChangeEvent aEvt;
180 aEvt.PropertyName = aPropertyName;
181 aEvt.NewValue = *pValues;
182 aGuard.clear();
183 pHelper->forEach<XPropertyChangeListener>(
184 ::boost::bind(&XPropertyChangeListener::propertyChange,_1,boost::cref(aEvt)));
185 aGuard.reset();
188 ppEntries++;
189 pValues++;
193 void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, Any* pValue )
194 throw( UnknownPropertyException, WrappedTargetException )
196 MutexGuard aGuard( maMutex );
198 while( *ppEntries )
200 const OUString aPropertyName( (*ppEntries)->mpName, (*ppEntries)->mnNameLen, RTL_TEXTENCODING_ASCII_US );
201 *pValue = maAnyMap[ aPropertyName ];
203 ppEntries++;
204 pValue++;
208 // XInterface
210 Any SAL_CALL GenericPropertySet::queryInterface( const Type & rType )
211 throw( RuntimeException )
213 return OWeakAggObject::queryInterface( rType );
216 Any SAL_CALL GenericPropertySet::queryAggregation( const Type & rType )
217 throw(RuntimeException)
219 Any aAny;
221 if( rType == ::getCppuType((const Reference< XServiceInfo >*)0) )
222 aAny <<= Reference< XServiceInfo >(this);
223 else if( rType == ::getCppuType((const Reference< XTypeProvider >*)0) )
224 aAny <<= Reference< XTypeProvider >(this);
225 else if( rType == ::getCppuType((const Reference< XPropertySet >*)0) )
226 aAny <<= Reference< XPropertySet >(this);
227 else if( rType == ::getCppuType((const Reference< XMultiPropertySet >*)0) )
228 aAny <<= Reference< XMultiPropertySet >(this);
229 else
230 aAny <<= OWeakAggObject::queryAggregation( rType );
232 return aAny;
235 void SAL_CALL GenericPropertySet::acquire() throw()
237 OWeakAggObject::acquire();
240 void SAL_CALL GenericPropertySet::release() throw()
242 OWeakAggObject::release();
245 uno::Sequence< uno::Type > SAL_CALL GenericPropertySet::getTypes()
246 throw (uno::RuntimeException)
248 uno::Sequence< uno::Type > aTypes( 5 );
249 uno::Type* pTypes = aTypes.getArray();
251 *pTypes++ = ::getCppuType((const uno::Reference< XAggregation>*)0);
252 *pTypes++ = ::getCppuType((const uno::Reference< XServiceInfo>*)0);
253 *pTypes++ = ::getCppuType((const uno::Reference< XTypeProvider>*)0);
254 *pTypes++ = ::getCppuType((const uno::Reference< XPropertySet>*)0);
255 *pTypes++ = ::getCppuType((const uno::Reference< XMultiPropertySet>*)0);
257 return aTypes;
260 uno::Sequence< sal_Int8 > SAL_CALL GenericPropertySet::getImplementationId()
261 throw (uno::RuntimeException)
263 MutexGuard aGuard( maMutex );
265 static uno::Sequence< sal_Int8 > aId;
266 if( aId.getLength() == 0 )
268 aId.realloc( 16 );
269 rtl_createUuid( (sal_uInt8 *)aId.getArray(), 0, sal_True );
271 return aId;
274 // XServiceInfo
276 sal_Bool SAL_CALL GenericPropertySet::supportsService( const OUString& ServiceName ) throw(RuntimeException)
278 Sequence< OUString > aSNL( getSupportedServiceNames() );
279 const OUString * pArray = aSNL.getConstArray();
281 for( sal_Int32 i = 0; i < aSNL.getLength(); ++i )
282 if( pArray[i] == ServiceName )
283 return sal_True;
285 return sal_False;
288 OUString SAL_CALL GenericPropertySet::getImplementationName() throw( RuntimeException )
290 return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.comphelper.GenericPropertySet") );
293 Sequence< OUString > SAL_CALL GenericPropertySet::getSupportedServiceNames( )
294 throw( RuntimeException )
296 Sequence< OUString > aSNS( 1 );
297 aSNS.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.beans.XPropertySet" ));
298 return aSNS;
301 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo* pInfo )
303 return (XPropertySet*)new GenericPropertySet( pInfo );