1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include "opropertybag.hxx"
22 #include "comphelper_module.hxx"
23 #include "comphelper_services.hxx"
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <com/sun/star/beans/NamedValue.hpp>
27 #include <com/sun/star/beans/Property.hpp>
29 #include <comphelper/namedvaluecollection.hxx>
30 #include <cppuhelper/supportsservice.hxx>
32 #include <cppuhelper/exc_hlp.hxx>
33 #include <osl/thread.h>
41 using namespace ::com::sun::star
;
43 void createRegistryInfo_OPropertyBag()
45 static ::comphelper::module::OAutoRegistration
< ::comphelper::OPropertyBag
> aAutoRegistration
;
53 using namespace ::com::sun::star::uno
;
54 using namespace ::com::sun::star::lang
;
55 using namespace ::com::sun::star::beans
;
56 using namespace ::com::sun::star::util
;
57 using namespace ::com::sun::star::container
;
59 OPropertyBag::OPropertyBag()
60 :OPropertyBag_PBase( GetBroadcastHelper(), this )
61 ,::cppu::IEventNotificationHook()
62 ,m_bAutoAddProperties( false )
63 ,m_NotifyListeners(m_aMutex
)
70 OPropertyBag::~OPropertyBag()
75 IMPLEMENT_FORWARD_XINTERFACE2( OPropertyBag
, OPropertyBag_Base
, OPropertyBag_PBase
)
76 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyBag
, OPropertyBag_Base
, OPropertyBag_PBase
)
79 Sequence
< OUString
> OPropertyBag::getSupportedServiceNames_static() throw( RuntimeException
)
81 Sequence
< OUString
> aServices(1);
82 aServices
[0] = "com.sun.star.beans.PropertyBag";
87 void SAL_CALL
OPropertyBag::initialize( const Sequence
< Any
>& _rArguments
) throw (Exception
, RuntimeException
, std::exception
)
89 Sequence
< Type
> aTypes
;
90 bool AllowEmptyPropertyName(false);
91 bool AutomaticAddition(false);
93 if (_rArguments
.getLength() == 3
94 && (_rArguments
[0] >>= aTypes
)
95 && (_rArguments
[1] >>= AllowEmptyPropertyName
)
96 && (_rArguments
[2] >>= AutomaticAddition
))
99 aTypes
.getConstArray(),
100 aTypes
.getConstArray() + aTypes
.getLength(),
101 ::std::insert_iterator
< TypeBag
>( m_aAllowedTypes
, m_aAllowedTypes
.begin() )
103 m_bAutoAddProperties
= AutomaticAddition
;
106 ::comphelper::NamedValueCollection
aArguments( _rArguments
);
108 if ( aArguments
.get_ensureType( "AllowedTypes", aTypes
) )
110 aTypes
.getConstArray(),
111 aTypes
.getConstArray() + aTypes
.getLength(),
112 ::std::insert_iterator
< TypeBag
>( m_aAllowedTypes
, m_aAllowedTypes
.begin() )
115 aArguments
.get_ensureType( "AutomaticAddition", m_bAutoAddProperties
);
116 aArguments
.get_ensureType( "AllowEmptyPropertyName",
117 AllowEmptyPropertyName
);
119 if (AllowEmptyPropertyName
) {
120 m_aDynamicProperties
.setAllowEmptyPropertyName(
121 AllowEmptyPropertyName
);
126 OUString
OPropertyBag::getImplementationName_static() throw( RuntimeException
)
128 return OUString( "com.sun.star.comp.comphelper.OPropertyBag" );
132 Reference
< XInterface
> SAL_CALL
OPropertyBag::Create( SAL_UNUSED_PARAMETER
const Reference
< XComponentContext
>& )
134 return *new OPropertyBag
;
138 OUString SAL_CALL
OPropertyBag::getImplementationName() throw (RuntimeException
, std::exception
)
140 return getImplementationName_static();
143 sal_Bool SAL_CALL
OPropertyBag::supportsService( const OUString
& rServiceName
) throw (RuntimeException
, std::exception
)
145 return cppu::supportsService(this, rServiceName
);
149 Sequence
< OUString
> SAL_CALL
OPropertyBag::getSupportedServiceNames( ) throw (RuntimeException
, std::exception
)
151 return getSupportedServiceNames_static();
155 void OPropertyBag::fireEvents(
156 sal_Int32
* /*pnHandles*/,
159 bool bIgnoreRuntimeExceptionsWhileFiring
)
161 if (nCount
&& !bVetoable
) {
162 setModifiedImpl(true, bIgnoreRuntimeExceptionsWhileFiring
);
166 void OPropertyBag::setModifiedImpl(bool bModified
,
167 bool bIgnoreRuntimeExceptionsWhileFiring
)
169 { // do not lock mutex while notifying (#i93514#) to prevent deadlock
170 ::osl::MutexGuard
aGuard( m_aMutex
);
171 m_isModified
= bModified
;
175 Reference
<XInterface
> xThis(*this);
176 EventObject
event(xThis
);
177 m_NotifyListeners
.notifyEach(
178 &XModifyListener::modified
, event
);
179 } catch (RuntimeException
&) {
180 if (!bIgnoreRuntimeExceptionsWhileFiring
) {
183 } catch (Exception
&) {
190 sal_Bool SAL_CALL
OPropertyBag::isModified()
191 throw (RuntimeException
, std::exception
)
193 ::osl::MutexGuard
aGuard( m_aMutex
);
197 void SAL_CALL
OPropertyBag::setModified( sal_Bool bModified
)
198 throw (PropertyVetoException
, RuntimeException
, std::exception
)
200 setModifiedImpl(bModified
, false);
203 void SAL_CALL
OPropertyBag::addModifyListener(
204 const Reference
< XModifyListener
> & xListener
)
205 throw (RuntimeException
, std::exception
)
207 m_NotifyListeners
.addInterface(xListener
);
210 void SAL_CALL
OPropertyBag::removeModifyListener(
211 const Reference
< XModifyListener
> & xListener
)
212 throw (RuntimeException
, std::exception
)
214 m_NotifyListeners
.removeInterface(xListener
);
218 Reference
< XPropertySetInfo
> SAL_CALL
OPropertyBag::getPropertySetInfo( ) throw(RuntimeException
, std::exception
)
220 return createPropertySetInfo( getInfoHelper() );
224 sal_Bool SAL_CALL
OPropertyBag::has( const Any
& /*aElement*/ ) throw (RuntimeException
, std::exception
)
226 // XSet is only a workaround for addProperty not being able to add default-void properties.
227 // So, everything of XSet except insert is implemented empty
232 void SAL_CALL
OPropertyBag::insert( const Any
& _element
) throw (IllegalArgumentException
, ElementExistException
, RuntimeException
, std::exception
)
234 // This is a workaround for addProperty not being able to add default-void properties.
235 // If we ever have a smarter XPropertyContainer::addProperty interface, we can remove this, ehm, well, hack.
237 if ( !( _element
>>= aProperty
) )
238 throw IllegalArgumentException( OUString(), *this, 1 );
240 ::osl::ClearableMutexGuard
g( m_aMutex
);
242 // check whether the type is allowed, everything else will be checked
243 // by m_aDynamicProperties
244 if ( !m_aAllowedTypes
.empty()
245 && m_aAllowedTypes
.find( aProperty
.Type
) == m_aAllowedTypes
.end()
247 throw IllegalArgumentException( OUString(), *this, 1 );
249 m_aDynamicProperties
.addVoidProperty( aProperty
.Name
, aProperty
.Type
, findFreeHandle(), aProperty
.Attributes
);
251 // our property info is dirty
252 m_pArrayHelper
.reset();
255 setModified(sal_True
);
259 void SAL_CALL
OPropertyBag::remove( const Any
& /*aElement*/ ) throw (IllegalArgumentException
, NoSuchElementException
, RuntimeException
, std::exception
)
261 // XSet is only a workaround for addProperty not being able to add default-void properties.
262 // So, everything of XSet except insert is implemented empty
263 throw NoSuchElementException( OUString(), *this );
268 Reference
< XEnumeration
> SAL_CALL
OPropertyBag::createEnumeration( ) throw (RuntimeException
, std::exception
)
270 // XSet is only a workaround for addProperty not being able to add default-void properties.
271 // So, everything of XSet except insert is implemented empty
276 Type SAL_CALL
OPropertyBag::getElementType( ) throw (RuntimeException
, std::exception
)
278 // XSet is only a workaround for addProperty not being able to add default-void properties.
279 // So, everything of XSet except insert is implemented empty
284 sal_Bool SAL_CALL
OPropertyBag::hasElements( ) throw (RuntimeException
, std::exception
)
286 // XSet is only a workaround for addProperty not being able to add default-void properties.
287 // So, everything of XSet except insert is implemented empty
292 void SAL_CALL
OPropertyBag::getFastPropertyValue( Any
& _rValue
, sal_Int32 _nHandle
) const
294 m_aDynamicProperties
.getFastPropertyValue( _nHandle
, _rValue
);
297 sal_Bool SAL_CALL
OPropertyBag::convertFastPropertyValue( Any
& _rConvertedValue
, Any
& _rOldValue
, sal_Int32 _nHandle
, const Any
& _rValue
) throw (IllegalArgumentException
, UnknownPropertyException
)
299 return m_aDynamicProperties
.convertFastPropertyValue( _nHandle
, _rValue
, _rConvertedValue
, _rOldValue
);
302 void SAL_CALL
OPropertyBag::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
, const Any
& rValue
) throw (Exception
, std::exception
)
304 m_aDynamicProperties
.setFastPropertyValue( nHandle
, rValue
);
308 ::cppu::IPropertyArrayHelper
& SAL_CALL
OPropertyBag::getInfoHelper()
310 if ( !m_pArrayHelper
.get() )
312 Sequence
< Property
> aProperties
;
313 m_aDynamicProperties
.describeProperties( aProperties
);
314 m_pArrayHelper
.reset( new ::cppu::OPropertyArrayHelper( aProperties
) );
316 return *m_pArrayHelper
;
321 sal_Int32
OPropertyBag::findFreeHandle() const
323 const sal_Int32 nPrime
= 1009;
324 const sal_Int32 nSeed
= 11;
326 sal_Int32 nCheck
= nSeed
;
327 while ( m_aDynamicProperties
.hasPropertyByHandle( nCheck
) && ( nCheck
!= 1 ) )
329 nCheck
= ( nCheck
* nSeed
) % nPrime
;
333 { // uh ... we already have 1008 handles used up
334 // -> simply count upwards
335 while ( m_aDynamicProperties
.hasPropertyByHandle( nCheck
) )
343 void SAL_CALL
OPropertyBag::addProperty( const OUString
& _rName
, ::sal_Int16 _nAttributes
, const Any
& _rInitialValue
) throw (PropertyExistException
, IllegalTypeException
, IllegalArgumentException
, RuntimeException
, std::exception
)
345 ::osl::ClearableMutexGuard
g( m_aMutex
);
347 // check whether the type is allowed, everything else will be checked
348 // by m_aDynamicProperties
349 Type aPropertyType
= _rInitialValue
.getValueType();
350 if ( _rInitialValue
.hasValue()
351 && !m_aAllowedTypes
.empty()
352 && m_aAllowedTypes
.find( aPropertyType
) == m_aAllowedTypes
.end()
354 throw IllegalTypeException( OUString(), *this );
356 m_aDynamicProperties
.addProperty( _rName
, findFreeHandle(), _nAttributes
, _rInitialValue
);
358 // our property info is dirty
359 m_pArrayHelper
.reset();
362 setModified(sal_True
);
366 void SAL_CALL
OPropertyBag::removeProperty( const OUString
& _rName
) throw (UnknownPropertyException
, NotRemoveableException
, RuntimeException
, std::exception
)
368 ::osl::ClearableMutexGuard
g( m_aMutex
);
370 m_aDynamicProperties
.removeProperty( _rName
);
372 // our property info is dirty
373 m_pArrayHelper
.reset();
376 setModified(sal_True
);
382 struct ComparePropertyValueByName
: public ::std::binary_function
< PropertyValue
, PropertyValue
, bool >
384 bool operator()( const PropertyValue
& _rLHS
, const PropertyValue
& _rRHS
)
386 return _rLHS
.Name
< _rRHS
.Name
;
390 template< typename CLASS
>
391 struct TransformPropertyToName
: public ::std::unary_function
< CLASS
, OUString
>
393 const OUString
& operator()( const CLASS
& _rProp
)
399 struct ExtractPropertyValue
: public ::std::unary_function
< PropertyValue
, Any
>
401 const Any
& operator()( const PropertyValue
& _rProp
)
409 Sequence
< PropertyValue
> SAL_CALL
OPropertyBag::getPropertyValues( ) throw (RuntimeException
, std::exception
)
411 ::osl::MutexGuard
aGuard( m_aMutex
);
413 // all registered properties
414 Sequence
< Property
> aProperties
;
415 m_aDynamicProperties
.describeProperties( aProperties
);
418 Sequence
< OUString
> aNames( aProperties
.getLength() );
420 aProperties
.getConstArray(),
421 aProperties
.getConstArray() + aProperties
.getLength(),
423 TransformPropertyToName
< Property
>()
427 Sequence
< Any
> aValues
;
430 aValues
= OPropertyBag_PBase::getPropertyValues( aNames
);
431 if ( aValues
.getLength() != aNames
.getLength() )
432 throw RuntimeException();
434 catch( const RuntimeException
& )
438 catch( const Exception
& )
443 // merge names and values, and retrieve the state/handle
444 ::cppu::IPropertyArrayHelper
& rPropInfo
= getInfoHelper();
446 Sequence
< PropertyValue
> aPropertyValues( aNames
.getLength() );
447 const OUString
* pName
= aNames
.getConstArray();
448 const OUString
* pNamesEnd
= aNames
.getConstArray() + aNames
.getLength();
449 const Any
* pValue
= aValues
.getArray();
450 PropertyValue
* pPropertyValue
= aPropertyValues
.getArray();
452 for ( ; pName
!= pNamesEnd
; ++pName
, ++pValue
, ++pPropertyValue
)
454 pPropertyValue
->Name
= *pName
;
455 pPropertyValue
->Handle
= rPropInfo
.getHandleByName( *pName
);
456 pPropertyValue
->Value
= *pValue
;
457 pPropertyValue
->State
= getPropertyStateByHandle( pPropertyValue
->Handle
);
460 return aPropertyValues
;
464 void OPropertyBag::impl_setPropertyValues_throw( const Sequence
< PropertyValue
>& _rProps
)
466 // sort (the XMultiPropertySet interface requires this)
467 Sequence
< PropertyValue
> aProperties( _rProps
);
469 aProperties
.getArray(),
470 aProperties
.getArray() + aProperties
.getLength(),
471 ComparePropertyValueByName()
474 // a sequence of names
475 Sequence
< OUString
> aNames( aProperties
.getLength() );
477 aProperties
.getConstArray(),
478 aProperties
.getConstArray() + aProperties
.getLength(),
480 TransformPropertyToName
< PropertyValue
>()
485 // check for unknown properties
486 // we cannot simply rely on the XMultiPropertySet::setPropertyValues
487 // implementation of our base class, since it does not throw
488 // an UnknownPropertyException. More precise, XMultiPropertySet::setPropertyValues
489 // does not allow to throw this exception, while XPropertyAccess::setPropertyValues
491 sal_Int32 nCount
= aNames
.getLength();
493 Sequence
< sal_Int32
> aHandles( nCount
);
494 sal_Int32
* pHandle
= aHandles
.getArray();
495 const PropertyValue
* pProperty
= aProperties
.getConstArray();
496 for ( const OUString
* pName
= aNames
.getConstArray();
497 pName
!= aNames
.getConstArray() + aNames
.getLength();
498 ++pName
, ++pHandle
, ++pProperty
501 ::cppu::IPropertyArrayHelper
& rPropInfo
= getInfoHelper();
502 *pHandle
= rPropInfo
.getHandleByName( *pName
);
503 if ( *pHandle
!= -1 )
506 // there's a property requested which we do not know
507 if ( m_bAutoAddProperties
)
510 sal_Int16 nAttributes
= PropertyAttribute::BOUND
| PropertyAttribute::REMOVABLE
| PropertyAttribute::MAYBEDEFAULT
;
511 addProperty( *pName
, nAttributes
, pProperty
->Value
);
516 throw UnknownPropertyException( *pName
, *this );
519 // a sequence of values
520 Sequence
< Any
> aValues( aProperties
.getLength() );
522 aProperties
.getConstArray(),
523 aProperties
.getConstArray() + aProperties
.getLength(),
525 ExtractPropertyValue()
528 setFastPropertyValues( nCount
, aHandles
.getArray(), aValues
.getConstArray(), nCount
);
530 catch( const PropertyVetoException
& ) { throw; }
531 catch( const IllegalArgumentException
& ) { throw; }
532 catch( const WrappedTargetException
& ) { throw; }
533 catch( const RuntimeException
& ) { throw; }
534 catch( const UnknownPropertyException
& ) { throw; }
535 catch( const Exception
& )
537 throw WrappedTargetException( OUString(), *this, ::cppu::getCaughtException() );
542 void SAL_CALL
OPropertyBag::setPropertyValues( const Sequence
< PropertyValue
>& _rProps
) throw (UnknownPropertyException
, PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
, RuntimeException
, std::exception
)
544 ::osl::MutexGuard
aGuard( m_aMutex
);
545 impl_setPropertyValues_throw( _rProps
);
549 PropertyState
OPropertyBag::getPropertyStateByHandle( sal_Int32 _nHandle
)
551 // for properties which do not support the MAYBEDEFAULT attribute, don't rely on the base class, but
552 // assume they're always in DIRECT state.
553 // (Note that this probably would belong into the base class. However, this would mean we would need
554 // to check all existent usages of the base class, where MAYBEDEFAULT is *not* set, but
555 // a default is nonetheless supplied/used. This is hard to accomplish reliably, in the
557 // #i78593# / 2007-07-07 / frank.schoenheit@sun.com
559 ::cppu::IPropertyArrayHelper
& rPropInfo
= getInfoHelper();
560 sal_Int16
nAttributes(0);
561 OSL_VERIFY( rPropInfo
.fillPropertyMembersByHandle( NULL
, &nAttributes
, _nHandle
) );
562 if ( ( nAttributes
& PropertyAttribute::MAYBEDEFAULT
) == 0 )
563 return PropertyState_DIRECT_VALUE
;
565 return OPropertyBag_PBase::getPropertyStateByHandle( _nHandle
);
569 Any
OPropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle
) const
572 m_aDynamicProperties
.getPropertyDefaultByHandle( _nHandle
, aDefault
);
577 } // namespace comphelper
580 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */