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 <osl/diagnose.h>
22 #include <cppuhelper/implbase.hxx>
23 #include <cppuhelper/queryinterface.hxx>
24 #include <cppuhelper/propshlp.hxx>
25 #include <cppuhelper/exc_hlp.hxx>
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <com/sun/star/lang/DisposedException.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
30 #include <sal/log.hxx>
33 using namespace com::sun::star::uno
;
34 using namespace com::sun::star::beans
;
35 using namespace com::sun::star::lang
;
40 IPropertyArrayHelper::~IPropertyArrayHelper()
44 static const css::uno::Type
& getPropertyTypeIdentifier( )
46 return cppu::UnoType
<XPropertyChangeListener
>::get();
48 static const css::uno::Type
& getPropertiesTypeIdentifier()
50 return cppu::UnoType
<XPropertiesChangeListener
>::get();
52 static const css::uno::Type
& getVetoableTypeIdentifier()
54 return cppu::UnoType
<XVetoableChangeListener
>::get();
59 static int compare_OUString_Property_Impl( const void *arg1
, const void *arg2
)
62 return static_cast<OUString
const *>(arg1
)->compareTo( static_cast<Property
const *>(arg2
)->Name
);
68 * The class which implements the PropertySetInfo interface.
73 class OPropertySetHelperInfo_Impl
74 : public WeakImplHelper
< css::beans::XPropertySetInfo
>
76 Sequence
< Property
> aInfos
;
79 explicit OPropertySetHelperInfo_Impl( IPropertyArrayHelper
& rHelper_
);
81 // XPropertySetInfo-methods
82 virtual Sequence
< Property
> SAL_CALL
getProperties() override
;
83 virtual Property SAL_CALL
getPropertyByName(const OUString
& PropertyName
) override
;
84 virtual sal_Bool SAL_CALL
hasPropertyByName(const OUString
& PropertyName
) override
;
90 * Create an object that implements XPropertySetInfo IPropertyArrayHelper.
92 OPropertySetHelperInfo_Impl::OPropertySetHelperInfo_Impl(
93 IPropertyArrayHelper
& rHelper_
)
94 :aInfos( rHelper_
.getProperties() )
99 * Return the sequence of properties, which are provided through the constructor.
101 Sequence
< Property
> OPropertySetHelperInfo_Impl::getProperties()
107 * Return the sequence of properties, which are provided through the constructor.
109 Property
OPropertySetHelperInfo_Impl::getPropertyByName( const OUString
& PropertyName
)
112 pR
= static_cast<Property
*>(bsearch( &PropertyName
, aInfos
.getConstArray(), aInfos
.getLength(),
114 compare_OUString_Property_Impl
));
116 throw UnknownPropertyException(PropertyName
);
123 * Return the sequence of properties, which are provided through the constructor.
125 sal_Bool
OPropertySetHelperInfo_Impl::hasPropertyByName( const OUString
& PropertyName
)
128 pR
= static_cast<Property
*>(bsearch( &PropertyName
, aInfos
.getConstArray(), aInfos
.getLength(),
130 compare_OUString_Property_Impl
));
131 return pR
!= nullptr;
136 class OPropertySetHelper::Impl
{
139 Impl( bool i_bIgnoreRuntimeExceptionsWhileFiring
,
140 IEventNotificationHook
*i_pFireEvents
142 :m_bIgnoreRuntimeExceptionsWhileFiring( i_bIgnoreRuntimeExceptionsWhileFiring
)
144 ,m_pFireEvents( i_pFireEvents
)
148 bool m_bIgnoreRuntimeExceptionsWhileFiring
;
150 class IEventNotificationHook
* const m_pFireEvents
;
152 std::vector
< sal_Int32
> m_handles
;
153 std::vector
< Any
> m_newValues
;
154 std::vector
< Any
> m_oldValues
;
159 OPropertySetHelper::OPropertySetHelper(
160 OBroadcastHelper
& rBHelper_
)
161 : rBHelper( rBHelper_
),
162 aBoundLC( rBHelper_
.rMutex
),
163 aVetoableLC( rBHelper_
.rMutex
),
164 m_pReserved( new Impl(false, nullptr) )
168 OPropertySetHelper::OPropertySetHelper(
169 OBroadcastHelper
& rBHelper_
, bool bIgnoreRuntimeExceptionsWhileFiring
)
170 : rBHelper( rBHelper_
),
171 aBoundLC( rBHelper_
.rMutex
),
172 aVetoableLC( rBHelper_
.rMutex
),
173 m_pReserved( new Impl( bIgnoreRuntimeExceptionsWhileFiring
, nullptr ) )
177 OPropertySetHelper::OPropertySetHelper(
178 OBroadcastHelper
& rBHelper_
, IEventNotificationHook
* i_pFireEvents
,
179 bool bIgnoreRuntimeExceptionsWhileFiring
)
180 : rBHelper( rBHelper_
),
181 aBoundLC( rBHelper_
.rMutex
),
182 aVetoableLC( rBHelper_
.rMutex
),
184 new Impl( bIgnoreRuntimeExceptionsWhileFiring
, i_pFireEvents
) )
188 OPropertySetHelper2::OPropertySetHelper2(
189 OBroadcastHelper
& irBHelper
,
190 IEventNotificationHook
*i_pFireEvents
,
191 bool bIgnoreRuntimeExceptionsWhileFiring
)
192 :OPropertySetHelper( irBHelper
, i_pFireEvents
, bIgnoreRuntimeExceptionsWhileFiring
)
197 * You must call disposing before.
199 OPropertySetHelper::~OPropertySetHelper()
203 OPropertySetHelper2::~OPropertySetHelper2()
208 Any
OPropertySetHelper::queryInterface( const css::uno::Type
& rType
)
210 return ::cppu::queryInterface(
212 static_cast< XPropertySet
* >( this ),
213 static_cast< XMultiPropertySet
* >( this ),
214 static_cast< XFastPropertySet
* >( this ) );
217 Any
OPropertySetHelper2::queryInterface( const css::uno::Type
& rType
)
219 Any
cnd(cppu::queryInterface(rType
, static_cast< XPropertySetOption
* >(this)));
220 if ( cnd
.hasValue() )
222 return OPropertySetHelper::queryInterface(rType
);
226 * called from the derivee's XTypeProvider::getTypes implementation
228 css::uno::Sequence
< css::uno::Type
> OPropertySetHelper::getTypes()
231 UnoType
<css::beans::XPropertySet
>::get(),
232 UnoType
<css::beans::XMultiPropertySet
>::get(),
233 UnoType
<css::beans::XFastPropertySet
>::get()};
237 void OPropertySetHelper::disposing()
239 // Create an event with this as sender
240 Reference
< XPropertySet
> rSource
= this;
242 aEvt
.Source
= rSource
;
244 // inform all listeners to release this object
245 // The listener containers are automatically cleared
246 aBoundLC
.disposeAndClear( aEvt
);
247 aVetoableLC
.disposeAndClear( aEvt
);
250 Reference
< XPropertySetInfo
> OPropertySetHelper::createPropertySetInfo(
251 IPropertyArrayHelper
& rProperties
)
253 return new OPropertySetHelperInfo_Impl(rProperties
);
257 void OPropertySetHelper::setPropertyValue(
258 const OUString
& rPropertyName
, const Any
& rValue
)
261 IPropertyArrayHelper
& rPH
= getInfoHelper();
262 // map the name to the handle
263 sal_Int32 nHandle
= rPH
.getHandleByName( rPropertyName
);
264 // call the method of the XFastPropertySet interface
265 setFastPropertyValue( nHandle
, rValue
);
269 Any
OPropertySetHelper::getPropertyValue(
270 const OUString
& rPropertyName
)
273 IPropertyArrayHelper
& rPH
= getInfoHelper();
274 // map the name to the handle
275 sal_Int32 nHandle
= rPH
.getHandleByName( rPropertyName
);
276 // call the method of the XFastPropertySet interface
277 return getFastPropertyValue( nHandle
);
281 void OPropertySetHelper::addPropertyChangeListener(
282 const OUString
& rPropertyName
,
283 const Reference
< XPropertyChangeListener
> & rxListener
)
285 MutexGuard
aGuard( rBHelper
.rMutex
);
286 OSL_ENSURE( !rBHelper
.bInDispose
, "do not addPropertyChangeListener in the dispose call" );
287 OSL_ENSURE( !rBHelper
.bDisposed
, "object is disposed" );
288 if( rBHelper
.bInDispose
|| rBHelper
.bDisposed
)
291 // only add listeners if you are not disposed
292 // a listener with no name means all properties
293 if( !rPropertyName
.isEmpty() )
296 IPropertyArrayHelper
& rPH
= getInfoHelper();
297 // map the name to the handle
298 sal_Int32 nHandle
= rPH
.getHandleByName( rPropertyName
);
299 if( nHandle
== -1 ) {
300 // property not known throw exception
301 throw UnknownPropertyException(rPropertyName
);
304 sal_Int16 nAttributes
;
305 rPH
.fillPropertyMembersByHandle( nullptr, &nAttributes
, nHandle
);
306 if( !(nAttributes
& css::beans::PropertyAttribute::BOUND
) )
308 OSL_FAIL( "add listener to an unbound property" );
309 // silent ignore this
312 // add the change listener to the helper container
314 aBoundLC
.addInterface( nHandle
, rxListener
);
317 // add the change listener to the helper container
318 rBHelper
.aLC
.addInterface(
319 getPropertyTypeIdentifier( ),
326 void OPropertySetHelper::removePropertyChangeListener(
327 const OUString
& rPropertyName
,
328 const Reference
< XPropertyChangeListener
>& rxListener
)
330 MutexGuard
aGuard( rBHelper
.rMutex
);
331 OSL_ENSURE( !rBHelper
.bDisposed
, "object is disposed" );
332 // all listeners are automatically released in a dispose call
333 if( rBHelper
.bInDispose
|| rBHelper
.bDisposed
)
336 if( !rPropertyName
.isEmpty() )
339 IPropertyArrayHelper
& rPH
= getInfoHelper();
340 // map the name to the handle
341 sal_Int32 nHandle
= rPH
.getHandleByName( rPropertyName
);
343 // property not known throw exception
344 throw UnknownPropertyException(rPropertyName
);
345 aBoundLC
.removeInterface( nHandle
, rxListener
);
348 // remove the change listener to the helper container
349 rBHelper
.aLC
.removeInterface(
350 getPropertyTypeIdentifier( ),
357 void OPropertySetHelper::addVetoableChangeListener(
358 const OUString
& rPropertyName
,
359 const Reference
< XVetoableChangeListener
> & rxListener
)
361 MutexGuard
aGuard( rBHelper
.rMutex
);
362 OSL_ENSURE( !rBHelper
.bInDispose
, "do not addVetoableChangeListener in the dispose call" );
363 OSL_ENSURE( !rBHelper
.bDisposed
, "object is disposed" );
364 if( rBHelper
.bInDispose
|| rBHelper
.bDisposed
)
367 // only add listeners if you are not disposed
368 // a listener with no name means all properties
369 if( !rPropertyName
.isEmpty() )
372 IPropertyArrayHelper
& rPH
= getInfoHelper();
373 // map the name to the handle
374 sal_Int32 nHandle
= rPH
.getHandleByName( rPropertyName
);
375 if( nHandle
== -1 ) {
376 // property not known throw exception
377 throw UnknownPropertyException(rPropertyName
);
380 sal_Int16 nAttributes
;
381 rPH
.fillPropertyMembersByHandle( nullptr, &nAttributes
, nHandle
);
382 if( !(nAttributes
& PropertyAttribute::CONSTRAINED
) )
384 OSL_FAIL( "addVetoableChangeListener, and property is not constrained" );
385 // silent ignore this
388 // add the vetoable listener to the helper container
389 aVetoableLC
.addInterface( nHandle
, rxListener
);
392 // add the vetoable listener to the helper container
393 rBHelper
.aLC
.addInterface(
394 getVetoableTypeIdentifier( ),
400 void OPropertySetHelper::removeVetoableChangeListener(
401 const OUString
& rPropertyName
,
402 const Reference
< XVetoableChangeListener
> & rxListener
)
404 MutexGuard
aGuard( rBHelper
.rMutex
);
405 OSL_ENSURE( !rBHelper
.bDisposed
, "object is disposed" );
406 // all listeners are automatically released in a dispose call
407 if( rBHelper
.bInDispose
|| rBHelper
.bDisposed
)
410 if( !rPropertyName
.isEmpty() )
413 IPropertyArrayHelper
& rPH
= getInfoHelper();
414 // map the name to the handle
415 sal_Int32 nHandle
= rPH
.getHandleByName( rPropertyName
);
416 if( nHandle
== -1 ) {
417 // property not known throw exception
418 throw UnknownPropertyException(rPropertyName
);
420 // remove the vetoable listener to the helper container
421 aVetoableLC
.removeInterface( nHandle
, rxListener
);
424 // add the vetoable listener to the helper container
425 rBHelper
.aLC
.removeInterface(
426 getVetoableTypeIdentifier( ),
431 void OPropertySetHelper::setDependentFastPropertyValue( sal_Int32 i_handle
, const css::uno::Any
& i_value
)
433 //OSL_PRECOND( rBHelper.rMutex.isAcquired(), "OPropertySetHelper::setDependentFastPropertyValue: to be called with a locked mutex only!" );
434 // there is no such thing as Mutex.isAcquired, sadly ...
436 sal_Int16
nAttributes(0);
437 IPropertyArrayHelper
& rInfo
= getInfoHelper();
438 if ( !rInfo
.fillPropertyMembersByHandle( nullptr, &nAttributes
, i_handle
) )
440 throw UnknownPropertyException(OUString::number(i_handle
));
442 // no need to check for READONLY-ness of the property. The method is intended to be called internally, which
443 // implies it might be invoked for properties which are read-only to the instance's clients, but well allowed
444 // to change their value.
446 Any aConverted
, aOld
;
447 bool bChanged
= convertFastPropertyValue( aConverted
, aOld
, i_handle
, i_value
);
451 // don't fire vetoable events. This method is called with our mutex locked, so calling into listeners would not be
452 // a good idea. The caller is responsible for not invoking this for constrained properties.
453 OSL_ENSURE( ( nAttributes
& PropertyAttribute::CONSTRAINED
) == 0,
454 "OPropertySetHelper::setDependentFastPropertyValue: not to be used for constrained properties!" );
456 // actually set the new value
459 setFastPropertyValue_NoBroadcast( i_handle
, aConverted
);
461 catch (const UnknownPropertyException
& ) { throw; /* allowed to leave */ }
462 catch (const PropertyVetoException
& ) { throw; /* allowed to leave */ }
463 catch (const IllegalArgumentException
& ) { throw; /* allowed to leave */ }
464 catch (const WrappedTargetException
& ) { throw; /* allowed to leave */ }
465 catch (const RuntimeException
& ) { throw; /* allowed to leave */ }
466 catch (const Exception
& )
468 // not allowed to leave this method
469 WrappedTargetException aWrapped
;
470 aWrapped
.TargetException
= ::cppu::getCaughtException();
471 aWrapped
.Context
= static_cast< XPropertySet
* >( this );
475 // remember the handle/values, for the events to be fired later
476 m_pReserved
->m_handles
.push_back( i_handle
);
477 m_pReserved
->m_newValues
.push_back( aConverted
); // TODO: setFastPropertyValue notifies the unconverted value here ...?
478 m_pReserved
->m_oldValues
.push_back( aOld
);
482 void OPropertySetHelper::setFastPropertyValue( sal_Int32 nHandle
, const Any
& rValue
)
484 OSL_ENSURE( !rBHelper
.bInDispose
, "do not setFastPropertyValue in the dispose call" );
485 OSL_ENSURE( !rBHelper
.bDisposed
, "object is disposed" );
487 IPropertyArrayHelper
& rInfo
= getInfoHelper();
488 sal_Int16 nAttributes
;
489 if( !rInfo
.fillPropertyMembersByHandle( nullptr, &nAttributes
, nHandle
) ) {
491 throw UnknownPropertyException(OUString::number(nHandle
));
493 if( nAttributes
& PropertyAttribute::READONLY
)
494 throw PropertyVetoException();
499 // Will the property change?
502 MutexGuard
aGuard( rBHelper
.rMutex
);
503 bChanged
= convertFastPropertyValue( aConvertedVal
, aOldVal
, nHandle
, rValue
);
504 // release guard to fire events
509 // Is it a constrained property?
510 if( nAttributes
& PropertyAttribute::CONSTRAINED
)
512 // In aValue is the converted rValue
513 // fire a constrained event
514 // second parameter NULL means constrained
515 fire( &nHandle
, &rValue
, &aOldVal
, 1, true );
519 MutexGuard
aGuard( rBHelper
.rMutex
);
522 // set the property to the new value
523 setFastPropertyValue_NoBroadcast( nHandle
, aConvertedVal
);
525 catch (const css::beans::UnknownPropertyException
& ) { throw; /* allowed to leave */ }
526 catch (const css::beans::PropertyVetoException
& ) { throw; /* allowed to leave */ }
527 catch (const css::lang::IllegalArgumentException
& ) { throw; /* allowed to leave */ }
528 catch (const css::lang::WrappedTargetException
& ) { throw; /* allowed to leave */ }
529 catch (const css::uno::RuntimeException
& ) { throw; /* allowed to leave */ }
530 catch (const css::uno::Exception
& e
)
532 // not allowed to leave this method
533 css::lang::WrappedTargetException aWrap
;
534 aWrap
.Context
= static_cast< css::beans::XPropertySet
* >( this );
535 aWrap
.TargetException
<<= e
;
540 // release guard to fire events
542 // file a change event, if the value changed
543 impl_fireAll( &nHandle
, &rValue
, &aOldVal
, 1 );
547 Any
OPropertySetHelper::getFastPropertyValue( sal_Int32 nHandle
)
550 IPropertyArrayHelper
& rInfo
= getInfoHelper();
551 if( !rInfo
.fillPropertyMembersByHandle( nullptr, nullptr, nHandle
) )
553 throw UnknownPropertyException(OUString::number(nHandle
));
556 MutexGuard
aGuard( rBHelper
.rMutex
);
557 getFastPropertyValue( aRet
, nHandle
);
562 void OPropertySetHelper::impl_fireAll( sal_Int32
* i_handles
, const Any
* i_newValues
, const Any
* i_oldValues
, sal_Int32 i_count
)
564 ClearableMutexGuard
aGuard( rBHelper
.rMutex
);
565 if ( m_pReserved
->m_handles
.empty() )
568 fire( i_handles
, i_newValues
, i_oldValues
, i_count
, false );
572 const size_t additionalEvents
= m_pReserved
->m_handles
.size();
573 OSL_ENSURE( additionalEvents
== m_pReserved
->m_newValues
.size()
574 && additionalEvents
== m_pReserved
->m_oldValues
.size(),
575 "OPropertySetHelper::impl_fireAll: inconsistency!" );
577 std::vector
< sal_Int32
> allHandles( additionalEvents
+ i_count
);
578 std::copy( m_pReserved
->m_handles
.begin(), m_pReserved
->m_handles
.end(), allHandles
.begin() );
579 std::copy( i_handles
, i_handles
+ i_count
, allHandles
.begin() + additionalEvents
);
581 std::vector
< Any
> allNewValues( additionalEvents
+ i_count
);
582 std::copy( m_pReserved
->m_newValues
.begin(), m_pReserved
->m_newValues
.end(), allNewValues
.begin() );
583 std::copy( i_newValues
, i_newValues
+ i_count
, allNewValues
.begin() + additionalEvents
);
585 std::vector
< Any
> allOldValues( additionalEvents
+ i_count
);
586 std::copy( m_pReserved
->m_oldValues
.begin(), m_pReserved
->m_oldValues
.end(), allOldValues
.begin() );
587 std::copy( i_oldValues
, i_oldValues
+ i_count
, allOldValues
.begin() + additionalEvents
);
589 m_pReserved
->m_handles
.clear();
590 m_pReserved
->m_newValues
.clear();
591 m_pReserved
->m_oldValues
.clear();
594 fire( allHandles
.data(), allNewValues
.data(), allOldValues
.data(), additionalEvents
+ i_count
, false );
598 void OPropertySetHelper::fire
600 sal_Int32
* pnHandles
,
601 const Any
* pNewValues
,
602 const Any
* pOldValues
,
603 sal_Int32 nHandles
, // This is the Count of the array
607 if (! m_pReserved
->m_bFireEvents
)
610 if (m_pReserved
->m_pFireEvents
) {
611 m_pReserved
->m_pFireEvents
->fireEvents(
612 pnHandles
, nHandles
, bVetoable
,
613 m_pReserved
->m_bIgnoreRuntimeExceptionsWhileFiring
);
616 // Only fire, if one or more properties changed
620 // create the event sequence of all changed properties
621 Sequence
< PropertyChangeEvent
> aEvts( nHandles
);
622 PropertyChangeEvent
* pEvts
= aEvts
.getArray();
623 Reference
< XInterface
> xSource( static_cast<XPropertySet
*>(this), UNO_QUERY
);
625 sal_Int32 nChangesLen
= 0;
626 // Loop over all changed properties to fill the event struct
627 for( i
= 0; i
< nHandles
; i
++ )
629 // Vetoable fire and constrained attribute set or
630 // Change fire and Changed and bound attribute set
631 IPropertyArrayHelper
& rInfo
= getInfoHelper();
632 sal_Int16 nAttributes
;
634 rInfo
.fillPropertyMembersByHandle( &aPropName
, &nAttributes
, pnHandles
[i
] );
637 (bVetoable
&& (nAttributes
& PropertyAttribute::CONSTRAINED
)) ||
638 (!bVetoable
&& (nAttributes
& PropertyAttribute::BOUND
))
641 pEvts
[nChangesLen
].Source
= xSource
;
642 pEvts
[nChangesLen
].PropertyName
= aPropName
;
643 pEvts
[nChangesLen
].PropertyHandle
= pnHandles
[i
];
644 pEvts
[nChangesLen
].OldValue
= pOldValues
[i
];
645 pEvts
[nChangesLen
].NewValue
= pNewValues
[i
];
650 bool bIgnoreRuntimeExceptionsWhileFiring
=
651 m_pReserved
->m_bIgnoreRuntimeExceptionsWhileFiring
;
653 // fire the events for all changed properties
654 for( i
= 0; i
< nChangesLen
; i
++ )
656 // get the listener container for the property name
657 OInterfaceContainerHelper
* pLC
;
658 if( bVetoable
) // fire change Events?
659 pLC
= aVetoableLC
.getContainer( pEvts
[i
].PropertyHandle
);
661 pLC
= aBoundLC
.getContainer( pEvts
[i
].PropertyHandle
);
664 // Iterate over all listeners and send events
665 OInterfaceIteratorHelper
aIt( *pLC
);
666 while( aIt
.hasMoreElements() )
668 XInterface
* pL
= aIt
.next();
673 if( bVetoable
) // fire change Events?
675 static_cast<XVetoableChangeListener
*>(pL
)->vetoableChange(
680 static_cast<XPropertyChangeListener
*>(pL
)->propertyChange(
684 catch (DisposedException
& exc
)
686 OSL_ENSURE( exc
.Context
.is(),
687 "DisposedException without Context!" );
688 if (exc
.Context
== pL
)
694 catch (RuntimeException
& exc
)
696 SAL_INFO("cppuhelper", "caught RuntimeException while firing listeners: " << exc
);
697 if (! bIgnoreRuntimeExceptionsWhileFiring
)
702 // broadcast to all listeners with "" property name
704 // fire change Events?
705 pLC
= rBHelper
.aLC
.getContainer(
706 getVetoableTypeIdentifier()
710 pLC
= rBHelper
.aLC
.getContainer(
711 getPropertyTypeIdentifier( )
716 // Iterate over all listeners and send events.
717 OInterfaceIteratorHelper
aIt( *pLC
);
718 while( aIt
.hasMoreElements() )
720 XInterface
* pL
= aIt
.next();
725 if( bVetoable
) // fire change Events?
727 static_cast<XVetoableChangeListener
*>(pL
)->vetoableChange(
732 static_cast<XPropertyChangeListener
*>(pL
)->propertyChange(
736 catch (DisposedException
& exc
)
738 OSL_ENSURE( exc
.Context
.is(),
739 "DisposedException without Context!" );
740 if (exc
.Context
== pL
)
746 catch (RuntimeException
& exc
)
748 SAL_INFO("cppuhelper", "caught RuntimeException while firing listeners: " << exc
);
749 if (! bIgnoreRuntimeExceptionsWhileFiring
)
756 // reduce array to changed properties
757 aEvts
.realloc( nChangesLen
);
762 auto pCont
= rBHelper
.aLC
.getContainer(getPropertiesTypeIdentifier());
766 // Here is a Bug, unbound properties are also fired
767 OInterfaceIteratorHelper
aIt( *pCont
);
768 while( aIt
.hasMoreElements() )
770 XPropertiesChangeListener
* pL
=
771 static_cast<XPropertiesChangeListener
*>(aIt
.next());
776 // fire the whole event sequence to the
777 // XPropertiesChangeListener's
778 pL
->propertiesChange( aEvts
);
780 catch (DisposedException
& exc
)
782 OSL_ENSURE( exc
.Context
.is(),
783 "DisposedException without Context!" );
784 if (exc
.Context
== pL
)
790 catch (RuntimeException
& exc
)
792 SAL_INFO("cppuhelper", "caught RuntimeException while firing listeners: " << exc
);
793 if (! bIgnoreRuntimeExceptionsWhileFiring
)
799 // OPropertySetHelper
800 void OPropertySetHelper::setFastPropertyValues(
802 sal_Int32
* pHandles
,
804 sal_Int32 nHitCount
)
806 OSL_ENSURE( !rBHelper
.bInDispose
, "do not getFastPropertyValue in the dispose call" );
807 OSL_ENSURE( !rBHelper
.bDisposed
, "object is disposed" );
810 IPropertyArrayHelper
& rPH
= getInfoHelper();
812 std::unique_ptr
<Any
[]> pConvertedValues(new Any
[ nHitCount
]);
813 std::unique_ptr
<Any
[]> pOldValues(new Any
[ nHitCount
]);
818 // must lock the mutex outside the loop. So all values are consistent.
819 MutexGuard
aGuard( rBHelper
.rMutex
);
820 for( i
= 0; i
< nSeqLen
; i
++ )
822 if( pHandles
[i
] != -1 )
824 sal_Int16 nAttributes
;
825 rPH
.fillPropertyMembersByHandle( nullptr, &nAttributes
, pHandles
[i
] );
826 if( nAttributes
& PropertyAttribute::READONLY
) {
827 throw PropertyVetoException();
829 // Will the property change?
830 if( convertFastPropertyValue( pConvertedValues
[ n
], pOldValues
[n
],
831 pHandles
[i
], pValues
[i
] ) )
833 // only increment if the property really change
834 pHandles
[n
] = pHandles
[i
];
839 // release guard to fire events
842 // fire vetoable events
843 fire( pHandles
, pConvertedValues
.get(), pOldValues
.get(), n
, true );
846 // must lock the mutex outside the loop.
847 MutexGuard
aGuard( rBHelper
.rMutex
);
848 // Loop over all changed properties
849 for( i
= 0; i
< n
; i
++ )
851 // Will the property change?
852 setFastPropertyValue_NoBroadcast( pHandles
[i
], pConvertedValues
[i
] );
854 // release guard to fire events
857 // fire change events
858 impl_fireAll( pHandles
, pConvertedValues
.get(), pOldValues
.get(), n
);
863 * The sequence may be contain not known properties. The implementation
864 * must ignore these properties.
866 void OPropertySetHelper::setPropertyValues(
867 const Sequence
<OUString
>& rPropertyNames
,
868 const Sequence
<Any
>& rValues
)
870 sal_Int32 nSeqLen
= rPropertyNames
.getLength();
871 if (nSeqLen
!= rValues
.getLength())
872 throw IllegalArgumentException("lengths do not match", static_cast<XPropertySet
*>(this),
874 std::unique_ptr
<sal_Int32
[]> pHandles(new sal_Int32
[ nSeqLen
]);
876 IPropertyArrayHelper
& rPH
= getInfoHelper();
877 // fill the handle array
878 sal_Int32 nHitCount
= rPH
.fillHandles( pHandles
.get(), rPropertyNames
);
880 setFastPropertyValues( nSeqLen
, pHandles
.get(), rValues
.getConstArray(), nHitCount
);
884 Sequence
<Any
> OPropertySetHelper::getPropertyValues( const Sequence
<OUString
>& rPropertyNames
)
886 sal_Int32 nSeqLen
= rPropertyNames
.getLength();
887 std::unique_ptr
<sal_Int32
[]> pHandles(new sal_Int32
[ nSeqLen
]);
888 Sequence
< Any
> aValues( nSeqLen
);
891 IPropertyArrayHelper
& rPH
= getInfoHelper();
892 // fill the handle array
893 rPH
.fillHandles( pHandles
.get(), rPropertyNames
);
895 Any
* pValues
= aValues
.getArray();
897 MutexGuard
aGuard( rBHelper
.rMutex
);
898 // fill the sequence with the values
899 for( sal_Int32 i
= 0; i
< nSeqLen
; i
++ )
900 getFastPropertyValue( pValues
[i
], pHandles
[i
] );
906 void OPropertySetHelper::addPropertiesChangeListener(
907 const Sequence
<OUString
> & ,
908 const Reference
< XPropertiesChangeListener
> & rListener
)
910 rBHelper
.addListener( cppu::UnoType
<decltype(rListener
)>::get(), rListener
);
914 void OPropertySetHelper::removePropertiesChangeListener(
915 const Reference
< XPropertiesChangeListener
> & rListener
)
917 rBHelper
.removeListener( cppu::UnoType
<decltype(rListener
)>::get(), rListener
);
921 void OPropertySetHelper::firePropertiesChangeEvent(
922 const Sequence
<OUString
>& rPropertyNames
,
923 const Reference
< XPropertiesChangeListener
>& rListener
)
925 sal_Int32 nLen
= rPropertyNames
.getLength();
926 std::unique_ptr
<sal_Int32
[]> pHandles(new sal_Int32
[nLen
]);
927 IPropertyArrayHelper
& rPH
= getInfoHelper();
928 rPH
.fillHandles( pHandles
.get(), rPropertyNames
);
929 const OUString
* pNames
= rPropertyNames
.getConstArray();
931 // get the count of matching properties
932 sal_Int32 nFireLen
= 0;
934 for( i
= 0; i
< nLen
; i
++ )
935 if( pHandles
[i
] != -1 )
938 Sequence
<PropertyChangeEvent
> aChanges( nFireLen
);
939 PropertyChangeEvent
* pChanges
= aChanges
.getArray();
942 // must lock the mutex outside the loop. So all values are consistent.
943 MutexGuard
aGuard( rBHelper
.rMutex
);
944 Reference
< XInterface
> xSource( static_cast<XPropertySet
*>(this), UNO_QUERY
);
945 sal_Int32 nFirePos
= 0;
946 for( i
= 0; i
< nLen
; i
++ )
948 if( pHandles
[i
] != -1 )
950 pChanges
[nFirePos
].Source
= xSource
;
951 pChanges
[nFirePos
].PropertyName
= pNames
[i
];
952 pChanges
[nFirePos
].PropertyHandle
= pHandles
[i
];
953 getFastPropertyValue( pChanges
[nFirePos
].OldValue
, pHandles
[i
] );
954 pChanges
[nFirePos
].NewValue
= pChanges
[nFirePos
].OldValue
;
958 // release guard to fire events
961 rListener
->propertiesChange( aChanges
);
964 void OPropertySetHelper2::enableChangeListenerNotification( sal_Bool bEnable
)
966 m_pReserved
->m_bFireEvents
= bEnable
;
971 static int compare_Property_Impl( const void *arg1
, const void *arg2
)
974 return static_cast<Property
const *>(arg1
)->Name
.compareTo( static_cast<Property
const *>(arg2
)->Name
);
979 void OPropertyArrayHelper::init( sal_Bool bSorted
)
981 sal_Int32 i
, nElements
= aInfos
.getLength();
982 const Property
* pProperties
= aInfos
.getConstArray();
984 for( i
= 1; i
< nElements
; i
++ )
986 if( pProperties
[i
-1].Name
> pProperties
[i
].Name
)
989 OSL_FAIL( "Property array is not sorted" );
992 qsort( aInfos
.getArray(), nElements
, sizeof( Property
),
993 compare_Property_Impl
);
994 pProperties
= aInfos
.getConstArray();
998 for( i
= 0; i
< nElements
; i
++ )
999 if( pProperties
[i
].Handle
!= i
)
1001 // The handle is the index
1002 bRightOrdered
= true;
1005 OPropertyArrayHelper::OPropertyArrayHelper(
1009 : m_pReserved(nullptr)
1010 , aInfos(pProps
, nEle
)
1011 , bRightOrdered( false )
1016 OPropertyArrayHelper::OPropertyArrayHelper(
1017 const Sequence
< Property
> & aProps
,
1019 : m_pReserved(nullptr)
1021 , bRightOrdered( false )
1027 sal_Int32
OPropertyArrayHelper::getCount() const
1029 return aInfos
.getLength();
1033 sal_Bool
OPropertyArrayHelper::fillPropertyMembersByHandle
1035 OUString
* pPropName
,
1036 sal_Int16
* pAttributes
,
1040 const Property
* pProperties
= aInfos
.getConstArray();
1041 sal_Int32 nElements
= aInfos
.getLength();
1045 if( nHandle
< 0 || nHandle
>= nElements
)
1048 *pPropName
= pProperties
[ nHandle
].Name
;
1050 *pAttributes
= pProperties
[ nHandle
].Attributes
;
1053 // normally the array is sorted
1054 for( sal_Int32 i
= 0; i
< nElements
; i
++ )
1056 if( pProperties
[i
].Handle
== nHandle
)
1059 *pPropName
= pProperties
[ i
].Name
;
1061 *pAttributes
= pProperties
[ i
].Attributes
;
1069 Sequence
< Property
> OPropertyArrayHelper::getProperties()
1075 Property
OPropertyArrayHelper::getPropertyByName(const OUString
& aPropertyName
)
1078 pR
= static_cast<Property
*>(bsearch( &aPropertyName
, aInfos
.getConstArray(), aInfos
.getLength(),
1080 compare_OUString_Property_Impl
));
1082 throw UnknownPropertyException(aPropertyName
);
1088 sal_Bool
OPropertyArrayHelper::hasPropertyByName(const OUString
& aPropertyName
)
1091 pR
= static_cast<Property
*>(bsearch( &aPropertyName
, aInfos
.getConstArray(), aInfos
.getLength(),
1093 compare_OUString_Property_Impl
));
1094 return pR
!= nullptr;
1098 sal_Int32
OPropertyArrayHelper::getHandleByName( const OUString
& rPropName
)
1101 pR
= static_cast<Property
*>(bsearch( &rPropName
, aInfos
.getConstArray(), aInfos
.getLength(),
1103 compare_OUString_Property_Impl
));
1104 return pR
? pR
->Handle
: -1;
1108 sal_Int32
OPropertyArrayHelper::fillHandles( sal_Int32
* pHandles
, const Sequence
< OUString
> & rPropNames
)
1110 sal_Int32 nHitCount
= 0;
1111 const OUString
* pReqProps
= rPropNames
.getConstArray();
1112 sal_Int32 nReqLen
= rPropNames
.getLength();
1113 const Property
* pCur
= aInfos
.getConstArray();
1114 const Property
* pEnd
= pCur
+ aInfos
.getLength();
1116 for( sal_Int32 i
= 0; i
< nReqLen
; i
++ )
1118 // Calculate logarithm
1119 sal_Int32 n
= static_cast<sal_Int32
>(pEnd
- pCur
);
1127 // Number of properties to search for * Log2 of the number of remaining
1128 // properties to search in.
1129 if( (nReqLen
- i
) * nLog
>= pEnd
- pCur
)
1131 // linear search is better
1132 while( pCur
< pEnd
&& pReqProps
[i
] > pCur
->Name
)
1136 if( pCur
< pEnd
&& pReqProps
[i
] == pCur
->Name
)
1138 pHandles
[i
] = pCur
->Handle
;
1146 // binary search is better
1147 sal_Int32 nCompVal
= 1;
1148 const Property
* pOldEnd
= pEnd
--;
1149 const Property
* pMid
= pCur
;
1151 while( nCompVal
!= 0 && pCur
<= pEnd
)
1153 pMid
= (pEnd
- pCur
) / 2 + pCur
;
1155 nCompVal
= pReqProps
[i
].compareTo( pMid
->Name
);
1165 pHandles
[i
] = pMid
->Handle
;
1169 else if( nCompVal
> 0 )
1185 } // end namespace cppu
1188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */