Avoid potential negative array index access to cached text.
[LibreOffice.git] / cppuhelper / source / propshlp.cxx
blob18ee9d1bb7706e71f421679e96d90b6e06077a28
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
29 #include <memory>
30 #include <sal/log.hxx>
32 using namespace osl;
33 using namespace com::sun::star::uno;
34 using namespace com::sun::star::beans;
35 using namespace com::sun::star::lang;
36 using namespace cppu;
38 namespace cppu {
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();
57 extern "C" {
59 static int compare_OUString_Property_Impl( const void *arg1, const void *arg2 )
60 SAL_THROW_EXTERN_C()
62 return static_cast<OUString const *>(arg1)->compareTo( static_cast<Property const *>(arg2)->Name );
67 /**
68 * The class which implements the PropertySetInfo interface.
71 namespace {
73 class OPropertySetHelperInfo_Impl
74 : public WeakImplHelper< css::beans::XPropertySetInfo >
76 Sequence < Property > aInfos;
78 public:
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;
89 /**
90 * Create an object that implements XPropertySetInfo IPropertyArrayHelper.
92 OPropertySetHelperInfo_Impl::OPropertySetHelperInfo_Impl(
93 IPropertyArrayHelper & rHelper_ )
94 :aInfos( rHelper_.getProperties() )
98 /**
99 * Return the sequence of properties, which are provided through the constructor.
101 Sequence< Property > OPropertySetHelperInfo_Impl::getProperties()
103 return aInfos;
107 * Return the sequence of properties, which are provided through the constructor.
109 Property OPropertySetHelperInfo_Impl::getPropertyByName( const OUString & PropertyName )
111 Property * pR;
112 pR = static_cast<Property *>(bsearch( &PropertyName, aInfos.getConstArray(), aInfos.getLength(),
113 sizeof( Property ),
114 compare_OUString_Property_Impl ));
115 if( !pR ) {
116 throw UnknownPropertyException(PropertyName);
119 return *pR;
123 * Return the sequence of properties, which are provided through the constructor.
125 sal_Bool OPropertySetHelperInfo_Impl::hasPropertyByName( const OUString & PropertyName )
127 Property * pR;
128 pR = static_cast<Property *>(bsearch( &PropertyName, aInfos.getConstArray(), aInfos.getLength(),
129 sizeof( Property ),
130 compare_OUString_Property_Impl ));
131 return pR != nullptr;
136 class OPropertySetHelper::Impl {
138 public:
139 Impl( bool i_bIgnoreRuntimeExceptionsWhileFiring,
140 IEventNotificationHook *i_pFireEvents
142 :m_bIgnoreRuntimeExceptionsWhileFiring( i_bIgnoreRuntimeExceptionsWhileFiring )
143 ,m_bFireEvents(true)
144 ,m_pFireEvents( i_pFireEvents )
148 bool m_bIgnoreRuntimeExceptionsWhileFiring;
149 bool m_bFireEvents;
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 ),
183 m_pReserved(
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()
201 delete m_pReserved;
203 OPropertySetHelper2::~OPropertySetHelper2()
207 // XInterface
208 Any OPropertySetHelper::queryInterface( const css::uno::Type & rType )
210 return ::cppu::queryInterface(
211 rType,
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() )
221 return cnd;
222 return OPropertySetHelper::queryInterface(rType);
226 * called from the derivee's XTypeProvider::getTypes implementation
228 css::uno::Sequence< css::uno::Type > OPropertySetHelper::getTypes()
230 return {
231 UnoType<css::beans::XPropertySet>::get(),
232 UnoType<css::beans::XMultiPropertySet>::get(),
233 UnoType<css::beans::XFastPropertySet>::get()};
236 // ComponentHelper
237 void OPropertySetHelper::disposing()
239 // Create an event with this as sender
240 Reference < XPropertySet > rSource = this;
241 EventObject aEvt;
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);
256 // XPropertySet
257 void OPropertySetHelper::setPropertyValue(
258 const OUString& rPropertyName, const Any& rValue )
260 // get the map table
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 );
268 // XPropertySet
269 Any OPropertySetHelper::getPropertyValue(
270 const OUString& rPropertyName )
272 // get the map table
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 );
280 // XPropertySet
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 )
289 return;
291 // only add listeners if you are not disposed
292 // a listener with no name means all properties
293 if( !rPropertyName.isEmpty() )
295 // get the map table
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
310 return;
312 // add the change listener to the helper container
314 aBoundLC.addInterface( nHandle, rxListener );
316 else
317 // add the change listener to the helper container
318 rBHelper.aLC.addInterface(
319 getPropertyTypeIdentifier( ),
320 rxListener
325 // XPropertySet
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 )
334 return;
336 if( !rPropertyName.isEmpty() )
338 // get the map table
339 IPropertyArrayHelper & rPH = getInfoHelper();
340 // map the name to the handle
341 sal_Int32 nHandle = rPH.getHandleByName( rPropertyName );
342 if( nHandle == -1 )
343 // property not known throw exception
344 throw UnknownPropertyException(rPropertyName);
345 aBoundLC.removeInterface( nHandle, rxListener );
347 else {
348 // remove the change listener to the helper container
349 rBHelper.aLC.removeInterface(
350 getPropertyTypeIdentifier( ),
351 rxListener
356 // XPropertySet
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 )
365 return;
367 // only add listeners if you are not disposed
368 // a listener with no name means all properties
369 if( !rPropertyName.isEmpty() )
371 // get the map table
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
386 return;
388 // add the vetoable listener to the helper container
389 aVetoableLC.addInterface( nHandle, rxListener );
391 else
392 // add the vetoable listener to the helper container
393 rBHelper.aLC.addInterface(
394 getVetoableTypeIdentifier( ),
395 rxListener
399 // XPropertySet
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 )
408 return;
410 if( !rPropertyName.isEmpty() )
412 // get the map table
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 );
423 else
424 // add the vetoable listener to the helper container
425 rBHelper.aLC.removeInterface(
426 getVetoableTypeIdentifier( ),
427 rxListener
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 ) )
439 // unknown property
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 );
448 if ( !bChanged )
449 return;
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 );
472 throw aWrapped;
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 );
481 // XFastPropertySet
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 ) ) {
490 // unknown property
491 throw UnknownPropertyException(OUString::number(nHandle));
493 if( nAttributes & PropertyAttribute::READONLY )
494 throw PropertyVetoException();
496 Any aConvertedVal;
497 Any aOldVal;
499 // Will the property change?
500 bool bChanged;
502 MutexGuard aGuard( rBHelper.rMutex );
503 bChanged = convertFastPropertyValue( aConvertedVal, aOldVal, nHandle, rValue );
504 // release guard to fire events
506 if( !bChanged )
507 return;
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;
537 throw aWrap;
540 // release guard to fire events
542 // file a change event, if the value changed
543 impl_fireAll( &nHandle, &rValue, &aOldVal, 1 );
546 // XFastPropertySet
547 Any OPropertySetHelper::getFastPropertyValue( sal_Int32 nHandle )
550 IPropertyArrayHelper & rInfo = getInfoHelper();
551 if( !rInfo.fillPropertyMembersByHandle( nullptr, nullptr, nHandle ) )
552 // unknown property
553 throw UnknownPropertyException(OUString::number(nHandle));
555 Any aRet;
556 MutexGuard aGuard( rBHelper.rMutex );
557 getFastPropertyValue( aRet, nHandle );
558 return aRet;
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() )
567 aGuard.clear();
568 fire( i_handles, i_newValues, i_oldValues, i_count, false );
569 return;
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();
593 aGuard.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
604 sal_Bool bVetoable
607 if (! m_pReserved->m_bFireEvents)
608 return;
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
617 if( !nHandles )
618 return;
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 );
624 sal_Int32 i;
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;
633 OUString aPropName;
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];
646 nChangesLen++;
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 );
660 else
661 pLC = aBoundLC.getContainer( pEvts[i].PropertyHandle );
662 if( pLC )
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(
676 pEvts[i] );
678 else
680 static_cast<XPropertyChangeListener *>(pL)->propertyChange(
681 pEvts[i] );
684 catch (DisposedException & exc)
686 OSL_ENSURE( exc.Context.is(),
687 "DisposedException without Context!" );
688 if (exc.Context == pL)
689 aIt.remove();
690 else
691 throw;
694 catch (RuntimeException & exc)
696 SAL_INFO("cppuhelper", "caught RuntimeException while firing listeners: " << exc);
697 if (! bIgnoreRuntimeExceptionsWhileFiring)
698 throw;
702 // broadcast to all listeners with "" property name
703 if( bVetoable ){
704 // fire change Events?
705 pLC = rBHelper.aLC.getContainer(
706 getVetoableTypeIdentifier()
709 else {
710 pLC = rBHelper.aLC.getContainer(
711 getPropertyTypeIdentifier( )
714 if( pLC )
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(
728 pEvts[i] );
730 else
732 static_cast<XPropertyChangeListener *>(pL)->propertyChange(
733 pEvts[i] );
736 catch (DisposedException & exc)
738 OSL_ENSURE( exc.Context.is(),
739 "DisposedException without Context!" );
740 if (exc.Context == pL)
741 aIt.remove();
742 else
743 throw;
746 catch (RuntimeException & exc)
748 SAL_INFO("cppuhelper", "caught RuntimeException while firing listeners: " << exc);
749 if (! bIgnoreRuntimeExceptionsWhileFiring)
750 throw;
756 // reduce array to changed properties
757 aEvts.realloc( nChangesLen );
759 if( bVetoable )
760 return;
762 auto pCont = rBHelper.aLC.getContainer(getPropertiesTypeIdentifier());
763 if (!pCont)
764 return;
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)
785 aIt.remove();
786 else
787 throw;
790 catch (RuntimeException & exc)
792 SAL_INFO("cppuhelper", "caught RuntimeException while firing listeners: " << exc);
793 if (! bIgnoreRuntimeExceptionsWhileFiring)
794 throw;
799 // OPropertySetHelper
800 void OPropertySetHelper::setFastPropertyValues(
801 sal_Int32 nSeqLen,
802 sal_Int32 * pHandles,
803 const Any * pValues,
804 sal_Int32 nHitCount )
806 OSL_ENSURE( !rBHelper.bInDispose, "do not getFastPropertyValue in the dispose call" );
807 OSL_ENSURE( !rBHelper.bDisposed, "object is disposed" );
809 // get the map table
810 IPropertyArrayHelper & rPH = getInfoHelper();
812 std::unique_ptr<Any[]> pConvertedValues(new Any[ nHitCount ]);
813 std::unique_ptr<Any[]> pOldValues(new Any[ nHitCount ]);
814 sal_Int32 n = 0;
815 sal_Int32 i;
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];
835 n++;
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 );
861 // XMultiPropertySet
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),
873 -1);
874 std::unique_ptr<sal_Int32[]> pHandles(new sal_Int32[ nSeqLen ]);
875 // get the map table
876 IPropertyArrayHelper & rPH = getInfoHelper();
877 // fill the handle array
878 sal_Int32 nHitCount = rPH.fillHandles( pHandles.get(), rPropertyNames );
879 if( nHitCount != 0 )
880 setFastPropertyValues( nSeqLen, pHandles.get(), rValues.getConstArray(), nHitCount );
883 // XMultiPropertySet
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 );
890 // get the map table
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] );
902 return aValues;
905 // XMultiPropertySet
906 void OPropertySetHelper::addPropertiesChangeListener(
907 const Sequence<OUString> & ,
908 const Reference < XPropertiesChangeListener > & rListener )
910 rBHelper.addListener( cppu::UnoType<decltype(rListener)>::get(), rListener );
913 // XMultiPropertySet
914 void OPropertySetHelper::removePropertiesChangeListener(
915 const Reference < XPropertiesChangeListener > & rListener )
917 rBHelper.removeListener( cppu::UnoType<decltype(rListener)>::get(), rListener );
920 // XMultiPropertySet
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;
933 sal_Int32 i;
934 for( i = 0; i < nLen; i++ )
935 if( pHandles[i] != -1 )
936 nFireLen++;
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;
955 nFirePos++;
958 // release guard to fire events
960 if( nFireLen )
961 rListener->propertiesChange( aChanges );
964 void OPropertySetHelper2::enableChangeListenerNotification( sal_Bool bEnable )
966 m_pReserved->m_bFireEvents = bEnable;
969 extern "C" {
971 static int compare_Property_Impl( const void *arg1, const void *arg2 )
972 SAL_THROW_EXTERN_C()
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 )
988 if (bSorted) {
989 OSL_FAIL( "Property array is not sorted" );
991 // not sorted
992 qsort( aInfos.getArray(), nElements, sizeof( Property ),
993 compare_Property_Impl );
994 pProperties = aInfos.getConstArray();
995 break;
998 for( i = 0; i < nElements; i++ )
999 if( pProperties[i].Handle != i )
1000 return;
1001 // The handle is the index
1002 bRightOrdered = true;
1005 OPropertyArrayHelper::OPropertyArrayHelper(
1006 Property * pProps,
1007 sal_Int32 nEle,
1008 sal_Bool bSorted )
1009 : m_pReserved(nullptr)
1010 , aInfos(pProps, nEle)
1011 , bRightOrdered( false )
1013 init( bSorted );
1016 OPropertyArrayHelper::OPropertyArrayHelper(
1017 const Sequence< Property > & aProps,
1018 sal_Bool bSorted )
1019 : m_pReserved(nullptr)
1020 , aInfos(aProps)
1021 , bRightOrdered( false )
1023 init( bSorted );
1027 sal_Int32 OPropertyArrayHelper::getCount() const
1029 return aInfos.getLength();
1033 sal_Bool OPropertyArrayHelper::fillPropertyMembersByHandle
1035 OUString * pPropName,
1036 sal_Int16 * pAttributes,
1037 sal_Int32 nHandle
1040 const Property* pProperties = aInfos.getConstArray();
1041 sal_Int32 nElements = aInfos.getLength();
1043 if( bRightOrdered )
1045 if( nHandle < 0 || nHandle >= nElements )
1046 return false;
1047 if( pPropName )
1048 *pPropName = pProperties[ nHandle ].Name;
1049 if( pAttributes )
1050 *pAttributes = pProperties[ nHandle ].Attributes;
1051 return true;
1053 // normally the array is sorted
1054 for( sal_Int32 i = 0; i < nElements; i++ )
1056 if( pProperties[i].Handle == nHandle )
1058 if( pPropName )
1059 *pPropName = pProperties[ i ].Name;
1060 if( pAttributes )
1061 *pAttributes = pProperties[ i ].Attributes;
1062 return true;
1065 return false;
1069 Sequence< Property > OPropertyArrayHelper::getProperties()
1071 return aInfos;
1075 Property OPropertyArrayHelper::getPropertyByName(const OUString& aPropertyName)
1077 Property * pR;
1078 pR = static_cast<Property *>(bsearch( &aPropertyName, aInfos.getConstArray(), aInfos.getLength(),
1079 sizeof( Property ),
1080 compare_OUString_Property_Impl ));
1081 if( !pR ) {
1082 throw UnknownPropertyException(aPropertyName);
1084 return *pR;
1088 sal_Bool OPropertyArrayHelper::hasPropertyByName(const OUString& aPropertyName)
1090 Property * pR;
1091 pR = static_cast<Property *>(bsearch( &aPropertyName, aInfos.getConstArray(), aInfos.getLength(),
1092 sizeof( Property ),
1093 compare_OUString_Property_Impl ));
1094 return pR != nullptr;
1098 sal_Int32 OPropertyArrayHelper::getHandleByName( const OUString & rPropName )
1100 Property * pR;
1101 pR = static_cast<Property *>(bsearch( &rPropName, aInfos.getConstArray(), aInfos.getLength(),
1102 sizeof( Property ),
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);
1120 sal_Int32 nLog = 0;
1121 while( n )
1123 nLog += 1;
1124 n = n >> 1;
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 )
1134 pCur++;
1136 if( pCur < pEnd && pReqProps[i] == pCur->Name )
1138 pHandles[i] = pCur->Handle;
1139 nHitCount++;
1141 else
1142 pHandles[i] = -1;
1144 else
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 );
1157 if( nCompVal > 0 )
1158 pCur = pMid + 1;
1159 else
1160 pEnd = pMid - 1;
1163 if( nCompVal == 0 )
1165 pHandles[i] = pMid->Handle;
1166 nHitCount++;
1167 pCur = pMid +1;
1169 else if( nCompVal > 0 )
1171 pHandles[i] = -1;
1172 pCur = pMid +1;
1174 else
1176 pHandles[i] = -1;
1177 pCur = pMid;
1179 pEnd = pOldEnd;
1182 return nHitCount;
1185 } // end namespace cppu
1188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */