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"
24 #include <com/sun/star/beans/PropertyAttribute.hpp>
25 #include <com/sun/star/beans/NamedValue.hpp>
26 #include <com/sun/star/beans/Property.hpp>
28 #include <comphelper/namedvaluecollection.hxx>
30 #include <cppuhelper/exc_hlp.hxx>
31 #include <osl/thread.h>
38 //--------------------------------------------------------------------------
39 using namespace ::com::sun::star
;
41 void createRegistryInfo_OPropertyBag()
43 static ::comphelper::module::OAutoRegistration
< ::comphelper::OPropertyBag
> aAutoRegistration
;
46 //........................................................................
49 //........................................................................
51 using namespace ::com::sun::star::uno
;
52 using namespace ::com::sun::star::lang
;
53 using namespace ::com::sun::star::beans
;
54 using namespace ::com::sun::star::util
;
55 using namespace ::com::sun::star::container
;
57 //====================================================================
59 //====================================================================
60 //--------------------------------------------------------------------
61 OPropertyBag::OPropertyBag()
62 :OPropertyBag_PBase( GetBroadcastHelper(), this )
63 ,::cppu::IEventNotificationHook()
64 ,m_bAutoAddProperties( false )
65 ,m_NotifyListeners(m_aMutex
)
71 //--------------------------------------------------------------------
72 OPropertyBag::~OPropertyBag()
76 //--------------------------------------------------------------------
77 IMPLEMENT_FORWARD_XINTERFACE2( OPropertyBag
, OPropertyBag_Base
, OPropertyBag_PBase
)
78 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyBag
, OPropertyBag_Base
, OPropertyBag_PBase
)
80 //--------------------------------------------------------------------
81 Sequence
< OUString
> OPropertyBag::getSupportedServiceNames_static() throw( RuntimeException
)
83 Sequence
< OUString
> aServices(1);
84 aServices
[0] = "com.sun.star.beans.PropertyBag";
88 //--------------------------------------------------------------------
89 void SAL_CALL
OPropertyBag::initialize( const Sequence
< Any
>& _rArguments
) throw (Exception
, RuntimeException
)
91 Sequence
< Type
> aTypes
;
92 bool AllowEmptyPropertyName(false);
93 bool AutomaticAddition(false);
95 if (_rArguments
.getLength() == 3
96 && (_rArguments
[0] >>= aTypes
)
97 && (_rArguments
[1] >>= AllowEmptyPropertyName
)
98 && (_rArguments
[2] >>= AutomaticAddition
))
101 aTypes
.getConstArray(),
102 aTypes
.getConstArray() + aTypes
.getLength(),
103 ::std::insert_iterator
< TypeBag
>( m_aAllowedTypes
, m_aAllowedTypes
.begin() )
105 m_bAutoAddProperties
= AutomaticAddition
;
108 ::comphelper::NamedValueCollection
aArguments( _rArguments
);
110 if ( aArguments
.get_ensureType( "AllowedTypes", aTypes
) )
112 aTypes
.getConstArray(),
113 aTypes
.getConstArray() + aTypes
.getLength(),
114 ::std::insert_iterator
< TypeBag
>( m_aAllowedTypes
, m_aAllowedTypes
.begin() )
117 aArguments
.get_ensureType( "AutomaticAddition", m_bAutoAddProperties
);
118 aArguments
.get_ensureType( "AllowEmptyPropertyName",
119 AllowEmptyPropertyName
);
121 if (AllowEmptyPropertyName
) {
122 m_aDynamicProperties
.setAllowEmptyPropertyName(
123 AllowEmptyPropertyName
);
127 //--------------------------------------------------------------------
128 OUString
OPropertyBag::getImplementationName_static() throw( RuntimeException
)
130 return OUString( "com.sun.star.comp.comphelper.OPropertyBag" );
133 //--------------------------------------------------------------------
134 Reference
< XInterface
> SAL_CALL
OPropertyBag::Create( SAL_UNUSED_PARAMETER
const Reference
< XComponentContext
>& )
136 return *new OPropertyBag
;
139 //--------------------------------------------------------------------
140 OUString SAL_CALL
OPropertyBag::getImplementationName() throw (RuntimeException
)
142 return getImplementationName_static();
145 //--------------------------------------------------------------------
146 ::sal_Bool SAL_CALL
OPropertyBag::supportsService( const OUString
& rServiceName
) throw (RuntimeException
)
148 Sequence
< OUString
> aServices( getSupportedServiceNames_static() );
149 const OUString
* pStart
= aServices
.getConstArray();
150 const OUString
* pEnd
= aServices
.getConstArray() + aServices
.getLength();
151 return ::std::find( pStart
, pEnd
, rServiceName
) != pEnd
;
154 //--------------------------------------------------------------------
155 Sequence
< OUString
> SAL_CALL
OPropertyBag::getSupportedServiceNames( ) throw (RuntimeException
)
157 return getSupportedServiceNames_static();
160 //--------------------------------------------------------------------
161 void OPropertyBag::fireEvents(
162 sal_Int32
* /*pnHandles*/,
165 bool bIgnoreRuntimeExceptionsWhileFiring
)
167 if (nCount
&& !bVetoable
) {
168 setModifiedImpl(sal_True
, bIgnoreRuntimeExceptionsWhileFiring
);
172 void OPropertyBag::setModifiedImpl(::sal_Bool bModified
,
173 bool bIgnoreRuntimeExceptionsWhileFiring
)
175 { // do not lock mutex while notifying (#i93514#) to prevent deadlock
176 ::osl::MutexGuard
aGuard( m_aMutex
);
177 m_isModified
= bModified
;
181 Reference
<XInterface
> xThis(*this);
182 EventObject
event(xThis
);
183 m_NotifyListeners
.notifyEach(
184 &XModifyListener::modified
, event
);
185 } catch (RuntimeException
&) {
186 if (!bIgnoreRuntimeExceptionsWhileFiring
) {
189 } catch (Exception
&) {
195 //--------------------------------------------------------------------
196 ::sal_Bool SAL_CALL
OPropertyBag::isModified()
197 throw (RuntimeException
)
199 ::osl::MutexGuard
aGuard( m_aMutex
);
203 void SAL_CALL
OPropertyBag::setModified( ::sal_Bool bModified
)
204 throw (PropertyVetoException
, RuntimeException
)
206 setModifiedImpl(bModified
, false);
209 void SAL_CALL
OPropertyBag::addModifyListener(
210 const Reference
< XModifyListener
> & xListener
)
211 throw (RuntimeException
)
213 m_NotifyListeners
.addInterface(xListener
);
216 void SAL_CALL
OPropertyBag::removeModifyListener(
217 const Reference
< XModifyListener
> & xListener
)
218 throw (RuntimeException
)
220 m_NotifyListeners
.removeInterface(xListener
);
223 //--------------------------------------------------------------------
224 Reference
< XPropertySetInfo
> SAL_CALL
OPropertyBag::getPropertySetInfo( ) throw(RuntimeException
)
226 return createPropertySetInfo( getInfoHelper() );
229 //--------------------------------------------------------------------
230 ::sal_Bool SAL_CALL
OPropertyBag::has( const Any
& /*aElement*/ ) throw (RuntimeException
)
232 // XSet is only a workaround for addProperty not being able to add default-void properties.
233 // So, everything of XSet except insert is implemented empty
237 //--------------------------------------------------------------------
238 void SAL_CALL
OPropertyBag::insert( const Any
& _element
) throw (IllegalArgumentException
, ElementExistException
, RuntimeException
)
240 // This is a workaround for addProperty not being able to add default-void properties.
241 // If we ever have a smarter XPropertyContainer::addProperty interface, we can remove this, ehm, well, hack.
243 if ( !( _element
>>= aProperty
) )
244 throw IllegalArgumentException( OUString(), *this, 1 );
246 ::osl::ClearableMutexGuard
g( m_aMutex
);
248 // check whether the type is allowed, everything else will be checked
249 // by m_aDynamicProperties
250 if ( !m_aAllowedTypes
.empty()
251 && m_aAllowedTypes
.find( aProperty
.Type
) == m_aAllowedTypes
.end()
253 throw IllegalTypeException( OUString(), *this );
255 m_aDynamicProperties
.addVoidProperty( aProperty
.Name
, aProperty
.Type
, findFreeHandle(), aProperty
.Attributes
);
257 // our property info is dirty
258 m_pArrayHelper
.reset();
261 setModified(sal_True
);
264 //--------------------------------------------------------------------
265 void SAL_CALL
OPropertyBag::remove( const Any
& /*aElement*/ ) throw (IllegalArgumentException
, NoSuchElementException
, RuntimeException
)
267 // XSet is only a workaround for addProperty not being able to add default-void properties.
268 // So, everything of XSet except insert is implemented empty
269 throw NoSuchElementException( OUString(), *this );
273 //--------------------------------------------------------------------
274 Reference
< XEnumeration
> SAL_CALL
OPropertyBag::createEnumeration( ) throw (RuntimeException
)
276 // XSet is only a workaround for addProperty not being able to add default-void properties.
277 // So, everything of XSet except insert is implemented empty
281 //--------------------------------------------------------------------
282 Type SAL_CALL
OPropertyBag::getElementType( ) throw (RuntimeException
)
284 // XSet is only a workaround for addProperty not being able to add default-void properties.
285 // So, everything of XSet except insert is implemented empty
289 //--------------------------------------------------------------------
290 ::sal_Bool SAL_CALL
OPropertyBag::hasElements( ) throw (RuntimeException
)
292 // XSet is only a workaround for addProperty not being able to add default-void properties.
293 // So, everything of XSet except insert is implemented empty
297 //--------------------------------------------------------------------
298 void SAL_CALL
OPropertyBag::getFastPropertyValue( Any
& _rValue
, sal_Int32 _nHandle
) const
300 m_aDynamicProperties
.getFastPropertyValue( _nHandle
, _rValue
);
303 //--------------------------------------------------------------------
304 sal_Bool SAL_CALL
OPropertyBag::convertFastPropertyValue( Any
& _rConvertedValue
, Any
& _rOldValue
, sal_Int32 _nHandle
, const Any
& _rValue
) throw (IllegalArgumentException
)
306 return m_aDynamicProperties
.convertFastPropertyValue( _nHandle
, _rValue
, _rConvertedValue
, _rOldValue
);
309 //--------------------------------------------------------------------
310 void SAL_CALL
OPropertyBag::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle
, const Any
& rValue
) throw (Exception
)
312 m_aDynamicProperties
.setFastPropertyValue( nHandle
, rValue
);
315 //--------------------------------------------------------------------
316 ::cppu::IPropertyArrayHelper
& SAL_CALL
OPropertyBag::getInfoHelper()
318 if ( !m_pArrayHelper
.get() )
320 Sequence
< Property
> aProperties
;
321 m_aDynamicProperties
.describeProperties( aProperties
);
322 m_pArrayHelper
.reset( new ::cppu::OPropertyArrayHelper( aProperties
) );
324 return *m_pArrayHelper
;
328 //--------------------------------------------------------------------
329 sal_Int32
OPropertyBag::findFreeHandle() const
331 const sal_Int32 nPrime
= 1009;
332 const sal_Int32 nSeed
= 11;
334 sal_Int32 nCheck
= nSeed
;
335 while ( m_aDynamicProperties
.hasPropertyByHandle( nCheck
) && ( nCheck
!= 1 ) )
337 nCheck
= ( nCheck
* nSeed
) % nPrime
;
341 { // uh ... we already have 1008 handles used up
342 // -> simply count upwards
343 while ( m_aDynamicProperties
.hasPropertyByHandle( nCheck
) )
350 //--------------------------------------------------------------------
351 void SAL_CALL
OPropertyBag::addProperty( const OUString
& _rName
, ::sal_Int16 _nAttributes
, const Any
& _rInitialValue
) throw (PropertyExistException
, IllegalTypeException
, IllegalArgumentException
, RuntimeException
)
353 ::osl::ClearableMutexGuard
g( m_aMutex
);
355 // check whether the type is allowed, everything else will be checked
356 // by m_aDynamicProperties
357 Type aPropertyType
= _rInitialValue
.getValueType();
358 if ( _rInitialValue
.hasValue()
359 && !m_aAllowedTypes
.empty()
360 && m_aAllowedTypes
.find( aPropertyType
) == m_aAllowedTypes
.end()
362 throw IllegalTypeException( OUString(), *this );
364 m_aDynamicProperties
.addProperty( _rName
, findFreeHandle(), _nAttributes
, _rInitialValue
);
366 // our property info is dirty
367 m_pArrayHelper
.reset();
370 setModified(sal_True
);
373 //--------------------------------------------------------------------
374 void SAL_CALL
OPropertyBag::removeProperty( const OUString
& _rName
) throw (UnknownPropertyException
, NotRemoveableException
, RuntimeException
)
376 ::osl::ClearableMutexGuard
g( m_aMutex
);
378 m_aDynamicProperties
.removeProperty( _rName
);
380 // our property info is dirty
381 m_pArrayHelper
.reset();
384 setModified(sal_True
);
387 //--------------------------------------------------------------------
390 struct ComparePropertyValueByName
: public ::std::binary_function
< PropertyValue
, PropertyValue
, bool >
392 bool operator()( const PropertyValue
& _rLHS
, const PropertyValue
& _rRHS
)
394 return _rLHS
.Name
< _rRHS
.Name
;
398 template< typename CLASS
>
399 struct TransformPropertyToName
: public ::std::unary_function
< CLASS
, OUString
>
401 const OUString
& operator()( const CLASS
& _rProp
)
407 struct ExtractPropertyValue
: public ::std::unary_function
< PropertyValue
, Any
>
409 const Any
& operator()( const PropertyValue
& _rProp
)
416 //--------------------------------------------------------------------
417 Sequence
< PropertyValue
> SAL_CALL
OPropertyBag::getPropertyValues( ) throw (RuntimeException
)
419 ::osl::MutexGuard
aGuard( m_aMutex
);
421 // all registered properties
422 Sequence
< Property
> aProperties
;
423 m_aDynamicProperties
.describeProperties( aProperties
);
426 Sequence
< OUString
> aNames( aProperties
.getLength() );
428 aProperties
.getConstArray(),
429 aProperties
.getConstArray() + aProperties
.getLength(),
431 TransformPropertyToName
< Property
>()
435 Sequence
< Any
> aValues
;
438 aValues
= OPropertyBag_PBase::getPropertyValues( aNames
);
439 if ( aValues
.getLength() != aNames
.getLength() )
440 throw RuntimeException();
442 catch( const RuntimeException
& )
446 catch( const Exception
& )
451 // merge names and values, and retrieve the state/handle
452 ::cppu::IPropertyArrayHelper
& rPropInfo
= getInfoHelper();
454 Sequence
< PropertyValue
> aPropertyValues( aNames
.getLength() );
455 const OUString
* pName
= aNames
.getConstArray();
456 const OUString
* pNamesEnd
= aNames
.getConstArray() + aNames
.getLength();
457 const Any
* pValue
= aValues
.getArray();
458 PropertyValue
* pPropertyValue
= aPropertyValues
.getArray();
460 for ( ; pName
!= pNamesEnd
; ++pName
, ++pValue
, ++pPropertyValue
)
462 pPropertyValue
->Name
= *pName
;
463 pPropertyValue
->Handle
= rPropInfo
.getHandleByName( *pName
);
464 pPropertyValue
->Value
= *pValue
;
465 pPropertyValue
->State
= getPropertyStateByHandle( pPropertyValue
->Handle
);
468 return aPropertyValues
;
471 //--------------------------------------------------------------------
472 void OPropertyBag::impl_setPropertyValues_throw( const Sequence
< PropertyValue
>& _rProps
)
474 // sort (the XMultiPropertySet interface requires this)
475 Sequence
< PropertyValue
> aProperties( _rProps
);
477 aProperties
.getArray(),
478 aProperties
.getArray() + aProperties
.getLength(),
479 ComparePropertyValueByName()
482 // a sequence of names
483 Sequence
< OUString
> aNames( aProperties
.getLength() );
485 aProperties
.getConstArray(),
486 aProperties
.getConstArray() + aProperties
.getLength(),
488 TransformPropertyToName
< PropertyValue
>()
493 // check for unknown properties
494 // we cannot simply rely on the XMultiPropertySet::setPropertyValues
495 // implementation of our base class, since it does not throw
496 // an UnknownPropertyException. More precise, XMultiPropertySet::setPropertyValues
497 // does not allow to throw this exception, while XPropertyAccess::setPropertyValues
499 sal_Int32 nCount
= aNames
.getLength();
501 Sequence
< sal_Int32
> aHandles( nCount
);
502 sal_Int32
* pHandle
= aHandles
.getArray();
503 const PropertyValue
* pProperty
= aProperties
.getConstArray();
504 for ( const OUString
* pName
= aNames
.getConstArray();
505 pName
!= aNames
.getConstArray() + aNames
.getLength();
506 ++pName
, ++pHandle
, ++pProperty
509 ::cppu::IPropertyArrayHelper
& rPropInfo
= getInfoHelper();
510 *pHandle
= rPropInfo
.getHandleByName( *pName
);
511 if ( *pHandle
!= -1 )
514 // there's a property requested which we do not know
515 if ( m_bAutoAddProperties
)
518 sal_Int16 nAttributes
= PropertyAttribute::BOUND
| PropertyAttribute::REMOVABLE
| PropertyAttribute::MAYBEDEFAULT
;
519 addProperty( *pName
, nAttributes
, pProperty
->Value
);
524 throw UnknownPropertyException( *pName
, *this );
527 // a sequence of values
528 Sequence
< Any
> aValues( aProperties
.getLength() );
530 aProperties
.getConstArray(),
531 aProperties
.getConstArray() + aProperties
.getLength(),
533 ExtractPropertyValue()
536 setFastPropertyValues( nCount
, aHandles
.getArray(), aValues
.getConstArray(), nCount
);
538 catch( const PropertyVetoException
& ) { throw; }
539 catch( const IllegalArgumentException
& ) { throw; }
540 catch( const WrappedTargetException
& ) { throw; }
541 catch( const RuntimeException
& ) { throw; }
542 catch( const UnknownPropertyException
& ) { throw; }
543 catch( const Exception
& )
545 throw WrappedTargetException( OUString(), *this, ::cppu::getCaughtException() );
549 //--------------------------------------------------------------------
550 void SAL_CALL
OPropertyBag::setPropertyValues( const Sequence
< PropertyValue
>& _rProps
) throw (UnknownPropertyException
, PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
, RuntimeException
)
552 ::osl::MutexGuard
aGuard( m_aMutex
);
553 impl_setPropertyValues_throw( _rProps
);
556 //--------------------------------------------------------------------
557 PropertyState
OPropertyBag::getPropertyStateByHandle( sal_Int32 _nHandle
)
559 // for properties which do not support the MAYBEDEFAULT attribute, don't rely on the base class, but
560 // assume they're always in DIRECT state.
561 // (Note that this probably would belong into the base class. However, this would mean we would need
562 // to check all existent usages of the base class, where MAYBEDEFAULT is *not* set, but
563 // a default is nonetheless supplied/used. This is hard to accomplish reliably, in the
565 // #i78593# / 2007-07-07 / frank.schoenheit@sun.com
567 ::cppu::IPropertyArrayHelper
& rPropInfo
= getInfoHelper();
568 sal_Int16
nAttributes(0);
569 OSL_VERIFY( rPropInfo
.fillPropertyMembersByHandle( NULL
, &nAttributes
, _nHandle
) );
570 if ( ( nAttributes
& PropertyAttribute::MAYBEDEFAULT
) == 0 )
571 return PropertyState_DIRECT_VALUE
;
573 return OPropertyBag_PBase::getPropertyStateByHandle( _nHandle
);
576 //--------------------------------------------------------------------
577 Any
OPropertyBag::getPropertyDefaultByHandle( sal_Int32 _nHandle
) const
580 m_aDynamicProperties
.getPropertyDefaultByHandle( _nHandle
, aDefault
);
584 //........................................................................
585 } // namespace comphelper
586 //........................................................................
588 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */