update dev300-m57
[ooovba.git] / comphelper / source / property / opropertybag.cxx
blob7a410a0f5da32ebde176fed89067a18f97004bb8
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: opropertybag.cxx,v $
10 * $Revision: 1.3.44.1 $
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"
34 #include "opropertybag.hxx"
35 #include "comphelper_module.hxx"
37 #include <com/sun/star/beans/PropertyAttribute.hpp>
38 #include <com/sun/star/beans/NamedValue.hpp>
39 #include <com/sun/star/beans/Property.hpp>
41 #include <comphelper/namedvaluecollection.hxx>
43 #include <cppuhelper/exc_hlp.hxx>
44 #include <osl/thread.h>
46 #include <algorithm>
47 #include <functional>
50 //--------------------------------------------------------------------------
51 using namespace ::com::sun::star;
53 void createRegistryInfo_OPropertyBag()
55 static ::comphelper::module::OAutoRegistration< ::comphelper::OPropertyBag > aAutoRegistration;
58 //........................................................................
59 namespace comphelper
61 //........................................................................
63 using namespace ::com::sun::star::uno;
64 using namespace ::com::sun::star::lang;
65 using namespace ::com::sun::star::beans;
66 using namespace ::com::sun::star::util;
67 using namespace ::com::sun::star::container;
69 //====================================================================
70 //= OPropertyBag
71 //====================================================================
72 //--------------------------------------------------------------------
73 OPropertyBag::OPropertyBag( const Reference< XComponentContext >& _rxContext )
74 :OPropertyBag_PBase( GetBroadcastHelper(), this )
75 ,::cppu::IEventNotificationHook()
76 ,m_aContext( _rxContext )
77 ,m_bAutoAddProperties( false )
78 ,m_NotifyListeners(m_aMutex)
79 ,m_isModified(false)
84 //--------------------------------------------------------------------
85 OPropertyBag::~OPropertyBag()
89 //--------------------------------------------------------------------
90 IMPLEMENT_FORWARD_XINTERFACE2( OPropertyBag, OPropertyBag_Base, OPropertyBag_PBase )
91 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyBag, OPropertyBag_Base, OPropertyBag_PBase )
93 //--------------------------------------------------------------------
94 Sequence< ::rtl::OUString > OPropertyBag::getSupportedServiceNames_static() throw( RuntimeException )
96 Sequence< ::rtl::OUString > aServices(1);
97 aServices[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.beans.PropertyBag" ) );
98 return aServices;
101 //--------------------------------------------------------------------
102 void SAL_CALL OPropertyBag::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException)
104 ::comphelper::NamedValueCollection aArguments( _rArguments );
106 Sequence< Type > aTypes;
107 if ( aArguments.get_ensureType( "AllowedTypes", aTypes ) )
108 ::std::copy(
109 aTypes.getConstArray(),
110 aTypes.getConstArray() + aTypes.getLength(),
111 ::std::insert_iterator< TypeBag >( m_aAllowedTypes, m_aAllowedTypes.begin() )
114 aArguments.get_ensureType( "AutomaticAddition", m_bAutoAddProperties );
115 bool AllowEmptyPropertyName(false);
116 aArguments.get_ensureType( "AllowEmptyPropertyName",
117 AllowEmptyPropertyName );
118 if (AllowEmptyPropertyName) {
119 m_aDynamicProperties.setAllowEmptyPropertyName(
120 AllowEmptyPropertyName);
124 //--------------------------------------------------------------------
125 ::rtl::OUString OPropertyBag::getImplementationName_static() throw( RuntimeException )
127 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.comphelper.OPropertyBag" ) );
130 //--------------------------------------------------------------------
131 Reference< XInterface > SAL_CALL OPropertyBag::Create( const Reference< XComponentContext >& _rxContext )
133 return *new OPropertyBag( _rxContext );
136 //--------------------------------------------------------------------
137 ::rtl::OUString SAL_CALL OPropertyBag::getImplementationName() throw (RuntimeException)
139 return getImplementationName_static();
142 //--------------------------------------------------------------------
143 ::sal_Bool SAL_CALL OPropertyBag::supportsService( const ::rtl::OUString& rServiceName ) throw (RuntimeException)
145 Sequence< ::rtl::OUString > aServices( getSupportedServiceNames_static() );
146 const ::rtl::OUString* pStart = aServices.getConstArray();
147 const ::rtl::OUString* pEnd = aServices.getConstArray() + aServices.getLength();
148 return ::std::find( pStart, pEnd, rServiceName ) != pEnd;
151 //--------------------------------------------------------------------
152 Sequence< ::rtl::OUString > SAL_CALL OPropertyBag::getSupportedServiceNames( ) throw (RuntimeException)
154 return getSupportedServiceNames_static();
157 //--------------------------------------------------------------------
158 void OPropertyBag::fireEvents(
159 sal_Int32 * /*pnHandles*/,
160 sal_Int32 nCount,
161 sal_Bool bVetoable,
162 bool bIgnoreRuntimeExceptionsWhileFiring)
164 if (nCount && !bVetoable) {
165 setModifiedImpl(sal_True, bIgnoreRuntimeExceptionsWhileFiring);
169 void OPropertyBag::setModifiedImpl(::sal_Bool bModified,
170 bool bIgnoreRuntimeExceptionsWhileFiring)
172 { // do not lock mutex while notifying (#i93514#) to prevent deadlock
173 ::osl::MutexGuard aGuard( m_aMutex );
174 m_isModified = bModified;
176 if (bModified) {
177 try {
178 Reference<XInterface> xThis(*this);
179 EventObject event(xThis);
180 m_NotifyListeners.notifyEach(
181 &XModifyListener::modified, event);
182 } catch (RuntimeException &) {
183 if (!bIgnoreRuntimeExceptionsWhileFiring) {
184 throw;
186 } catch (Exception &) {
187 // ignore
192 //--------------------------------------------------------------------
193 ::sal_Bool SAL_CALL OPropertyBag::isModified()
194 throw (RuntimeException)
196 ::osl::MutexGuard aGuard( m_aMutex );
197 return m_isModified;
200 void SAL_CALL OPropertyBag::setModified( ::sal_Bool bModified )
201 throw (PropertyVetoException, RuntimeException)
203 setModifiedImpl(bModified, false);
206 void SAL_CALL OPropertyBag::addModifyListener(
207 const Reference< XModifyListener > & xListener)
208 throw (RuntimeException)
210 m_NotifyListeners.addInterface(xListener);
213 void SAL_CALL OPropertyBag::removeModifyListener(
214 const Reference< XModifyListener > & xListener)
215 throw (RuntimeException)
217 m_NotifyListeners.removeInterface(xListener);
220 //--------------------------------------------------------------------
221 Reference< XPropertySetInfo > SAL_CALL OPropertyBag::getPropertySetInfo( ) throw(RuntimeException)
223 return createPropertySetInfo( getInfoHelper() );
226 //--------------------------------------------------------------------
227 ::sal_Bool SAL_CALL OPropertyBag::has( const Any& /*aElement*/ ) throw (RuntimeException)
229 // XSet is only a workaround for addProperty not being able to add default-void properties.
230 // So, everything of XSet except insert is implemented empty
231 return sal_False;
234 //--------------------------------------------------------------------
235 void SAL_CALL OPropertyBag::insert( const Any& _element ) throw (IllegalArgumentException, ElementExistException, RuntimeException)
237 // This is a workaround for addProperty not being able to add default-void properties.
238 // If we ever have a smarter XPropertyContainer::addProperty interface, we can remove this, ehm, well, hack.
239 Property aProperty;
240 if ( !( _element >>= aProperty ) )
241 throw IllegalArgumentException( ::rtl::OUString(), *this, 1 );
243 ::osl::MutexGuard aGuard( m_aMutex );
245 // check whether the type is allowed, everything else will be checked
246 // by m_aDynamicProperties
247 if ( !m_aAllowedTypes.empty()
248 && m_aAllowedTypes.find( aProperty.Type ) == m_aAllowedTypes.end()
250 throw IllegalTypeException( ::rtl::OUString(), *this );
252 m_aDynamicProperties.addVoidProperty( aProperty.Name, aProperty.Type, findFreeHandle(), aProperty.Attributes );
254 // our property info is dirty
255 m_pArrayHelper.reset();
257 setModified(sal_True);
260 //--------------------------------------------------------------------
261 void SAL_CALL OPropertyBag::remove( const Any& /*aElement*/ ) throw (IllegalArgumentException, NoSuchElementException, RuntimeException)
263 // XSet is only a workaround for addProperty not being able to add default-void properties.
264 // So, everything of XSet except insert is implemented empty
265 throw NoSuchElementException( ::rtl::OUString(), *this );
269 //--------------------------------------------------------------------
270 Reference< XEnumeration > SAL_CALL OPropertyBag::createEnumeration( ) throw (RuntimeException)
272 // XSet is only a workaround for addProperty not being able to add default-void properties.
273 // So, everything of XSet except insert is implemented empty
274 return NULL;
277 //--------------------------------------------------------------------
278 Type SAL_CALL OPropertyBag::getElementType( ) throw (RuntimeException)
280 // XSet is only a workaround for addProperty not being able to add default-void properties.
281 // So, everything of XSet except insert is implemented empty
282 return Type();
285 //--------------------------------------------------------------------
286 ::sal_Bool SAL_CALL OPropertyBag::hasElements( ) throw (RuntimeException)
288 // XSet is only a workaround for addProperty not being able to add default-void properties.
289 // So, everything of XSet except insert is implemented empty
290 return sal_False;
293 //--------------------------------------------------------------------
294 void SAL_CALL OPropertyBag::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
296 m_aDynamicProperties.getFastPropertyValue( _nHandle, _rValue );
299 //--------------------------------------------------------------------
300 sal_Bool SAL_CALL OPropertyBag::convertFastPropertyValue( Any& _rConvertedValue, Any& _rOldValue, sal_Int32 _nHandle, const Any& _rValue ) throw (IllegalArgumentException)
302 return m_aDynamicProperties.convertFastPropertyValue( _nHandle, _rValue, _rConvertedValue, _rOldValue );
305 //--------------------------------------------------------------------
306 void SAL_CALL OPropertyBag::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue ) throw (Exception)
308 m_aDynamicProperties.setFastPropertyValue( nHandle, rValue );
311 //--------------------------------------------------------------------
312 ::cppu::IPropertyArrayHelper& SAL_CALL OPropertyBag::getInfoHelper()
314 if ( !m_pArrayHelper.get() )
316 Sequence< Property > aProperties;
317 m_aDynamicProperties.describeProperties( aProperties );
318 m_pArrayHelper.reset( new ::cppu::OPropertyArrayHelper( aProperties ) );
320 return *m_pArrayHelper;
324 //--------------------------------------------------------------------
325 sal_Int32 OPropertyBag::findFreeHandle() const
327 const sal_Int32 nPrime = 1009;
328 const sal_Int32 nSeed = 11;
330 sal_Int32 nCheck = nSeed;
331 while ( m_aDynamicProperties.hasPropertyByHandle( nCheck ) && ( nCheck != 1 ) )
333 nCheck = ( nCheck * nSeed ) % nPrime;
336 if ( nCheck == 1 )
337 { // uh ... we already have 1008 handles used up
338 // -> simply count upwards
339 while ( m_aDynamicProperties.hasPropertyByHandle( nCheck ) )
340 ++nCheck;
343 return nCheck;
346 //--------------------------------------------------------------------
347 void SAL_CALL OPropertyBag::addProperty( const ::rtl::OUString& _rName, ::sal_Int16 _nAttributes, const Any& _rInitialValue ) throw (PropertyExistException, IllegalTypeException, IllegalArgumentException, RuntimeException)
349 ::osl::MutexGuard aGuard( m_aMutex );
351 // check whether the type is allowed, everything else will be checked
352 // by m_aDynamicProperties
353 Type aPropertyType = _rInitialValue.getValueType();
354 if ( _rInitialValue.hasValue()
355 && !m_aAllowedTypes.empty()
356 && m_aAllowedTypes.find( aPropertyType ) == m_aAllowedTypes.end()
358 throw IllegalTypeException( ::rtl::OUString(), *this );
360 m_aDynamicProperties.addProperty( _rName, findFreeHandle(), _nAttributes, _rInitialValue );
362 // our property info is dirty
363 m_pArrayHelper.reset();
365 setModified(sal_True);
368 //--------------------------------------------------------------------
369 void SAL_CALL OPropertyBag::removeProperty( const ::rtl::OUString& _rName ) throw (UnknownPropertyException, NotRemoveableException, RuntimeException)
371 ::osl::MutexGuard aGuard( m_aMutex );
373 m_aDynamicProperties.removeProperty( _rName );
375 // our property info is dirty
376 m_pArrayHelper.reset();
378 setModified(sal_True);
381 //--------------------------------------------------------------------
382 namespace
384 struct ComparePropertyValueByName : public ::std::binary_function< PropertyValue, PropertyValue, bool >
386 bool operator()( const PropertyValue& _rLHS, const PropertyValue& _rRHS )
388 return _rLHS.Name < _rRHS.Name;
392 template< typename CLASS >
393 struct TransformPropertyToName : public ::std::unary_function< CLASS, ::rtl::OUString >
395 const ::rtl::OUString& operator()( const CLASS& _rProp )
397 return _rProp.Name;
401 struct ExtractPropertyValue : public ::std::unary_function< PropertyValue, Any >
403 const Any& operator()( const PropertyValue& _rProp )
405 return _rProp.Value;
410 //--------------------------------------------------------------------
411 Sequence< PropertyValue > SAL_CALL OPropertyBag::getPropertyValues( ) throw (RuntimeException)
413 ::osl::MutexGuard aGuard( m_aMutex );
415 // all registered properties
416 Sequence< Property > aProperties;
417 m_aDynamicProperties.describeProperties( aProperties );
419 // their names
420 Sequence< ::rtl::OUString > aNames( aProperties.getLength() );
421 ::std::transform(
422 aProperties.getConstArray(),
423 aProperties.getConstArray() + aProperties.getLength(),
424 aNames.getArray(),
425 TransformPropertyToName< Property >()
428 // their values
429 Sequence< Any > aValues;
432 aValues = OPropertyBag_PBase::getPropertyValues( aNames );
433 if ( aValues.getLength() != aNames.getLength() )
434 throw RuntimeException();
436 catch( const RuntimeException& )
438 throw;
440 catch( const Exception& )
442 // ignore
445 // merge names and values, and retrieve the state/handle
446 ::cppu::IPropertyArrayHelper& rPropInfo = getInfoHelper();
448 Sequence< PropertyValue > aPropertyValues( aNames.getLength() );
449 const ::rtl::OUString* pName = aNames.getConstArray();
450 const ::rtl::OUString* pNamesEnd = aNames.getConstArray() + aNames.getLength();
451 const Any* pValue = aValues.getArray();
452 PropertyValue* pPropertyValue = aPropertyValues.getArray();
454 for ( ; pName != pNamesEnd; ++pName, ++pValue, ++pPropertyValue )
456 pPropertyValue->Name = *pName;
457 pPropertyValue->Handle = rPropInfo.getHandleByName( *pName );
458 pPropertyValue->Value = *pValue;
459 pPropertyValue->State = getPropertyStateByHandle( pPropertyValue->Handle );
462 return aPropertyValues;
465 //--------------------------------------------------------------------
466 void OPropertyBag::impl_setPropertyValues_throw( const Sequence< PropertyValue >& _rProps )
468 // sort (the XMultiPropertySet interface requires this)
469 Sequence< PropertyValue > aProperties( _rProps );
470 ::std::sort(
471 aProperties.getArray(),
472 aProperties.getArray() + aProperties.getLength(),
473 ComparePropertyValueByName()
476 // a sequence of names
477 Sequence< ::rtl::OUString > aNames( aProperties.getLength() );
478 ::std::transform(
479 aProperties.getConstArray(),
480 aProperties.getConstArray() + aProperties.getLength(),
481 aNames.getArray(),
482 TransformPropertyToName< PropertyValue >()
487 ::cppu::IPropertyArrayHelper& rPropInfo = getInfoHelper();
489 // check for unknown properties
490 // we cannot simply rely on the XMultiPropertySet::setPropertyValues
491 // implementation of our base class, since it does not throw
492 // an UnknownPropertyException. More precise, XMultiPropertySet::setPropertyValues
493 // does not allow to throw this exception, while XPropertyAccess::setPropertyValues
494 // requires it
495 sal_Int32 nCount = aNames.getLength();
497 Sequence< sal_Int32 > aHandles( nCount );
498 sal_Int32* pHandle = aHandles.getArray();
499 const PropertyValue* pProperty = aProperties.getConstArray();
500 for ( const ::rtl::OUString* pName = aNames.getConstArray();
501 pName != aNames.getConstArray() + aNames.getLength();
502 ++pName, ++pHandle, ++pProperty
505 *pHandle = rPropInfo.getHandleByName( *pName );
506 if ( *pHandle != -1 )
507 continue;
509 // there's a property requested which we do not know
510 if ( m_bAutoAddProperties )
512 // add the property
513 sal_Int16 nAttributes = PropertyAttribute::BOUND | PropertyAttribute::REMOVEABLE | PropertyAttribute::MAYBEDEFAULT;
514 addProperty( *pName, nAttributes, pProperty->Value );
515 // rPropInfo is invalid, refetch
516 rPropInfo = getInfoHelper();
517 *pHandle = rPropInfo.getHandleByName( *pName );
518 continue;
521 // no way out
522 throw UnknownPropertyException( *pName, *this );
525 // a sequence of values
526 Sequence< Any > aValues( aProperties.getLength() );
527 ::std::transform(
528 aProperties.getConstArray(),
529 aProperties.getConstArray() + aProperties.getLength(),
530 aValues.getArray(),
531 ExtractPropertyValue()
534 setFastPropertyValues( nCount, aHandles.getArray(), aValues.getConstArray(), nCount );
536 catch( const PropertyVetoException& ) { throw; }
537 catch( const IllegalArgumentException& ) { throw; }
538 catch( const WrappedTargetException& ) { throw; }
539 catch( const RuntimeException& ) { throw; }
540 catch( const UnknownPropertyException& ) { throw; }
541 catch( const Exception& )
543 throw WrappedTargetException( ::rtl::OUString(), *this, ::cppu::getCaughtException() );
547 //--------------------------------------------------------------------
548 void SAL_CALL OPropertyBag::setPropertyValues( const Sequence< PropertyValue >& _rProps ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
550 ::osl::MutexGuard aGuard( m_aMutex );
551 impl_setPropertyValues_throw( _rProps );
554 //--------------------------------------------------------------------
555 PropertyState OPropertyBag::getPropertyStateByHandle( sal_Int32 _nHandle )
557 // for properties which do not support the MAYBEDEFAULT attribute, don't rely on the base class, but
558 // assume they're always in DIRECT state.
559 // (Note that this probably would belong into the base class. However, this would mean we would need
560 // to check all existent usages of the base class, where MAYBEDEFAULT is *not* set, but
561 // a default is nonetheless supplied/used. This is hard to accomplish reliably, in the
562 // current phase.
563 // #i78593# / 2007-07-07 / frank.schoenheit@sun.com
565 ::cppu::IPropertyArrayHelper& rPropInfo = getInfoHelper();
566 sal_Int16 nAttributes(0);
567 OSL_VERIFY( rPropInfo.fillPropertyMembersByHandle( NULL, &nAttributes, _nHandle ) );
568 if ( ( nAttributes & PropertyAttribute::MAYBEDEFAULT ) == 0 )
569 return PropertyState_DIRECT_VALUE;
571 return OPropertyBag_PBase::getPropertyStateByHandle( _nHandle );
574 //--------------------------------------------------------------------
575 Any OPropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle ) const
577 Any aDefault;
578 m_aDynamicProperties.getPropertyDefaultByHandle( _nHandle, aDefault );
579 return aDefault;
582 //........................................................................
583 } // namespace comphelper
584 //........................................................................