summaryrefslogtreecommitdiff
path: root/comphelper/source/property/propshlp.cxx
blob: ca1459582827d1d8f95aa5f5a7124e7953fd1513 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <osl/diagnose.h>
#include <cppuhelper/implbase.hxx>
#include <cppuhelper/queryinterface.hxx>
#include <comphelper/propshlp.hxx>
#include <cppuhelper/exc_hlp.hxx>
#include <com/sun/star/beans/PropertyAttribute.hpp>
#include <com/sun/star/lang/DisposedException.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <memory>
#include <sal/log.hxx>

using namespace com::sun::star::uno;
using namespace com::sun::star::beans;
using namespace com::sun::star::lang;
using namespace cppu;

namespace comphelper
{
extern "C" {

static int compare_OUString_Property_Impl(const void* arg1, const void* arg2) SAL_THROW_EXTERN_C()
{
    return static_cast<OUString const*>(arg1)->compareTo(static_cast<Property const*>(arg2)->Name);
}
}

/**
 * The class which implements the PropertySetInfo interface.
 */

namespace
{
class OPropertySetHelperInfo_Impl : public WeakImplHelper<css::beans::XPropertySetInfo>
{
    Sequence<Property> aInfos;

public:
    explicit OPropertySetHelperInfo_Impl(IPropertyArrayHelper& rHelper_);

    // XPropertySetInfo-methods
    virtual Sequence<Property> SAL_CALL getProperties() override;
    virtual Property SAL_CALL getPropertyByName(const OUString& PropertyName) override;
    virtual sal_Bool SAL_CALL hasPropertyByName(const OUString& PropertyName) override;
};
}

/**
 * Create an object that implements XPropertySetInfo IPropertyArrayHelper.
 */
OPropertySetHelperInfo_Impl::OPropertySetHelperInfo_Impl(IPropertyArrayHelper& rHelper_)
    : aInfos(rHelper_.getProperties())
{
}

/**
 * Return the sequence of properties, which are provided through the constructor.
 */
Sequence<Property> OPropertySetHelperInfo_Impl::getProperties() { return aInfos; }

/**
 * Return the sequence of properties, which are provided through the constructor.
 */
Property OPropertySetHelperInfo_Impl::getPropertyByName(const OUString& PropertyName)
{
    Property* pR
        = static_cast<Property*>(bsearch(&PropertyName, aInfos.getConstArray(), aInfos.getLength(),
                                         sizeof(Property), compare_OUString_Property_Impl));
    if (!pR)
        throw UnknownPropertyException(PropertyName);

    return *pR;
}

/**
 * Return the sequence of properties, which are provided through the constructor.
 */
sal_Bool OPropertySetHelperInfo_Impl::hasPropertyByName(const OUString& PropertyName)
{
    Property* pR
        = static_cast<Property*>(bsearch(&PropertyName, aInfos.getConstArray(), aInfos.getLength(),
                                         sizeof(Property), compare_OUString_Property_Impl));
    return pR != nullptr;
}

OPropertySetHelper::OPropertySetHelper() {}

OPropertySetHelper::OPropertySetHelper(bool bIgnoreRuntimeExceptionsWhileFiring)
    : m_bIgnoreRuntimeExceptionsWhileFiring(bIgnoreRuntimeExceptionsWhileFiring)
{
}

/**
 * You must call disposing before.
 */
OPropertySetHelper::~OPropertySetHelper() {}

// XInterface
Any OPropertySetHelper::queryInterface(const css::uno::Type& rType)
{
    return ::cppu::queryInterface(rType, static_cast<XPropertySet*>(this),
                                  static_cast<XMultiPropertySet*>(this),
                                  static_cast<XFastPropertySet*>(this));
}

/**
 * called from the derivee's XTypeProvider::getTypes implementation
 */
css::uno::Sequence<css::uno::Type> OPropertySetHelper::getTypes()
{
    return { UnoType<css::beans::XPropertySet>::get(),
             UnoType<css::beans::XMultiPropertySet>::get(),
             UnoType<css::beans::XFastPropertySet>::get() };
}

// ComponentHelper
void OPropertySetHelper::disposing(std::unique_lock<std::mutex>& rGuard)
{
    // Create an event with this as sender
    Reference<XPropertySet> rSource = this;
    EventObject aEvt;
    aEvt.Source = rSource;

    // inform all listeners to release this object
    // The listener containers are automatically cleared
    aBoundLC.disposeAndClear(rGuard, aEvt);
    aVetoableLC.disposeAndClear(rGuard, aEvt);
}

Reference<XPropertySetInfo>
OPropertySetHelper::createPropertySetInfo(IPropertyArrayHelper& rProperties)
{
    return new OPropertySetHelperInfo_Impl(rProperties);
}

// XPropertySet
void OPropertySetHelper::setPropertyValue(const OUString& rPropertyName, const Any& rValue)
{
    // get the map table
    IPropertyArrayHelper& rPH = getInfoHelper();
    // map the name to the handle
    sal_Int32 nHandle = rPH.getHandleByName(rPropertyName);
    std::unique_lock aGuard(m_aMutex);
    setFastPropertyValueImpl(aGuard, nHandle, rValue);
}

// XPropertySet
Any OPropertySetHelper::getPropertyValue(const OUString& rPropertyName)
{
    std::unique_lock aGuard(m_aMutex);
    return getPropertyValueImpl(aGuard, rPropertyName);
}

Any OPropertySetHelper::getPropertyValueImpl(std::unique_lock<std::mutex>& rGuard,
                                             const OUString& rPropertyName)
{
    // get the map table
    IPropertyArrayHelper& rPH = getInfoHelper();
    // map the name to the handle
    sal_Int32 nHandle = rPH.getHandleByName(rPropertyName);
    // call the method of the XFastPropertySet interface
    Any aAny;
    getFastPropertyValue(rGuard, aAny, nHandle);
    return aAny;
}

// XPropertySet
void OPropertySetHelper::addPropertyChangeListener(
    const OUString& rPropertyName, const Reference<XPropertyChangeListener>& rxListener)
{
    std::unique_lock aGuard(m_aMutex);
    OSL_ENSURE(!m_bDisposed, "object is disposed");
    if (m_bDisposed)
        return;

    // only add listeners if you are not disposed
    // a listener with no name means all properties
    if (!rPropertyName.isEmpty())
    {
        // get the map table
        IPropertyArrayHelper& rPH = getInfoHelper();
        // map the name to the handle
        sal_Int32 nHandle = rPH.getHandleByName(rPropertyName);
        if (nHandle == -1)
        {
            // property not known throw exception
            throw UnknownPropertyException(rPropertyName);
        }

        sal_Int16 nAttributes;
        rPH.fillPropertyMembersByHandle(nullptr, &nAttributes, nHandle);
        if (!(nAttributes & css::beans::PropertyAttribute::BOUND))
        {
            OSL_FAIL("add listener to an unbound property");
            // silent ignore this
            return;
        }
        // add the change listener to the helper container
        aBoundLC.addInterface(aGuard, nHandle, rxListener);
    }
    else
        // add the change listener to the helper container
        maPropertyChangeListeners.addInterface(aGuard, rxListener);
}

// XPropertySet
void OPropertySetHelper::removePropertyChangeListener(
    const OUString& rPropertyName, const Reference<XPropertyChangeListener>& rxListener)
{
    std::unique_lock aGuard(m_aMutex);
    OSL_ENSURE(!m_bDisposed, "object is disposed");
    // all listeners are automatically released in a dispose call
    if (m_bDisposed)
        return;

    if (!rPropertyName.isEmpty())
    {
        // get the map table
        IPropertyArrayHelper& rPH = getInfoHelper();
        // map the name to the handle
        sal_Int32 nHandle = rPH.getHandleByName(rPropertyName);
        if (nHandle == -1)
            // property not known throw exception
            throw UnknownPropertyException(rPropertyName);
        aBoundLC.removeInterface(aGuard, nHandle, rxListener);
    }
    else
    {
        // remove the change listener to the helper container
        maPropertyChangeListeners.removeInterface(aGuard, rxListener);
    }
}

// XPropertySet
void OPropertySetHelper::addVetoableChangeListener(
    const OUString& rPropertyName, const Reference<XVetoableChangeListener>& rxListener)
{
    std::unique_lock aGuard(m_aMutex);
    OSL_ENSURE(!m_bDisposed, "object is disposed");
    if (m_bDisposed)
        return;

    // only add listeners if you are not disposed
    // a listener with no name means all properties
    if (!rPropertyName.isEmpty())
    {
        // get the map table
        IPropertyArrayHelper& rPH = getInfoHelper();
        // map the name to the handle
        sal_Int32 nHandle = rPH.getHandleByName(rPropertyName);
        if (nHandle == -1)
        {
            // property not known throw exception
            throw UnknownPropertyException(rPropertyName);
        }

        sal_Int16 nAttributes;
        rPH.fillPropertyMembersByHandle(nullptr, &nAttributes, nHandle);
        if (!(nAttributes & PropertyAttribute::CONSTRAINED))
        {
            OSL_FAIL("addVetoableChangeListener, and property is not constrained");
            // silent ignore this
            return;
        }
        // add the vetoable listener to the helper container
        aVetoableLC.addInterface(aGuard, nHandle, rxListener);
    }
    else
        // add the vetoable listener to the helper container
        maVetoableChangeListeners.addInterface(aGuard, rxListener);
}

// XPropertySet
void OPropertySetHelper::removeVetoableChangeListener(
    const OUString& rPropertyName, const Reference<XVetoableChangeListener>& rxListener)
{
    std::unique_lock aGuard(m_aMutex);
    OSL_ENSURE(!m_bDisposed, "object is disposed");
    // all listeners are automatically released in a dispose call
    if (m_bDisposed)
        return;

    if (!rPropertyName.isEmpty())
    {
        // get the map table
        IPropertyArrayHelper& rPH = getInfoHelper();
        // map the name to the handle
        sal_Int32 nHandle = rPH.getHandleByName(rPropertyName);
        if (nHandle == -1)
        {
            // property not known throw exception
            throw UnknownPropertyException(rPropertyName);
        }
        // remove the vetoable listener to the helper container
        aVetoableLC.removeInterface(aGuard, nHandle, rxListener);
    }
    else
        // add the vetoable listener to the helper container
        maVetoableChangeListeners.removeInterface(aGuard, rxListener);
}

void OPropertySetHelper::setDependentFastPropertyValue(std::unique_lock<std::mutex>& rGuard,
                                                       sal_Int32 i_handle,
                                                       const css::uno::Any& i_value)
{
    sal_Int16 nAttributes(0);
    IPropertyArrayHelper& rInfo = getInfoHelper();
    if (!rInfo.fillPropertyMembersByHandle(nullptr, &nAttributes, i_handle))
        // unknown property
        throw UnknownPropertyException(OUString::number(i_handle));

    // no need to check for READONLY-ness of the property. The method is intended to be called internally, which
    // implies it might be invoked for properties which are read-only to the instance's clients, but well allowed
    // to change their value.

    Any aConverted, aOld;
    bool bChanged = convertFastPropertyValue(rGuard, aConverted, aOld, i_handle, i_value);
    if (!bChanged)
        return;

    // don't fire vetoable events. This method is called with our mutex locked, so calling into listeners would not be
    // a good idea. The caller is responsible for not invoking this for constrained properties.
    OSL_ENSURE((nAttributes & PropertyAttribute::CONSTRAINED) == 0,
               "OPropertySetHelper::setDependentFastPropertyValue: not to be used for constrained "
               "properties!");

    // actually set the new value
    try
    {
        setFastPropertyValue_NoBroadcast(rGuard, i_handle, aConverted);
    }
    catch (const UnknownPropertyException&)
    {
        throw; /* allowed to leave */
    }
    catch (const PropertyVetoException&)
    {
        throw; /* allowed to leave */
    }
    catch (const IllegalArgumentException&)
    {
        throw; /* allowed to leave */
    }
    catch (const WrappedTargetException&)
    {
        throw; /* allowed to leave */
    }
    catch (const RuntimeException&)
    {
        throw; /* allowed to leave */
    }
    catch (const Exception&)
    {
        // not allowed to leave this method
        WrappedTargetException aWrapped;
        aWrapped.TargetException = ::cppu::getCaughtException();
        aWrapped.Context = static_cast<XPropertySet*>(this);
        throw aWrapped;
    }

    // remember the handle/values, for the events to be fired later
    m_handles.push_back(i_handle);
    m_newValues.push_back(
        aConverted); // TODO: setFastPropertyValue notifies the unconverted value here ...?
    m_oldValues.push_back(aOld);
}

// XFastPropertySet
void OPropertySetHelper::setFastPropertyValue(sal_Int32 nHandle, const Any& rValue)
{
    std::unique_lock aGuard(m_aMutex);
    setFastPropertyValueImpl(aGuard, nHandle, rValue);
}

void OPropertySetHelper::setFastPropertyValueImpl(std::unique_lock<std::mutex>& rGuard,
                                                  sal_Int32 nHandle, const Any& rValue)
{
    OSL_ENSURE(!m_bDisposed, "object is disposed");

    IPropertyArrayHelper& rInfo = getInfoHelper();
    sal_Int16 nAttributes;
    if (!rInfo.fillPropertyMembersByHandle(nullptr, &nAttributes, nHandle))
    {
        // unknown property
        throw UnknownPropertyException(OUString::number(nHandle));
    }
    if (nAttributes & PropertyAttribute::READONLY)
        throw PropertyVetoException();

    Any aConvertedVal;
    Any aOldVal;

    // Will the property change?
    bool bChanged = convertFastPropertyValue(rGuard, aConvertedVal, aOldVal, nHandle, rValue);
    if (!bChanged)
        return;

    // Is it a constrained property?
    if (nAttributes & PropertyAttribute::CONSTRAINED)
    {
        // In aValue is the converted rValue
        // fire a constrained event
        // second parameter NULL means constrained
        fire(rGuard, &nHandle, &rValue, &aOldVal, 1, true);
    }

    try
    {
        // set the property to the new value
        setFastPropertyValue_NoBroadcast(rGuard, nHandle, aConvertedVal);
    }
    catch (const css::beans::UnknownPropertyException&)
    {
        throw; /* allowed to leave */
    }
    catch (const css::beans::PropertyVetoException&)
    {
        throw; /* allowed to leave */
    }
    catch (const css::lang::IllegalArgumentException&)
    {
        throw; /* allowed to leave */
    }
    catch (const css::lang::WrappedTargetException&)
    {
        throw; /* allowed to leave */
    }
    catch (const css::uno::RuntimeException&)
    {
        throw; /* allowed to leave */
    }
    catch (const css::uno::Exception& e)
    {
        // not allowed to leave this method
        css::lang::WrappedTargetException aWrap;
        aWrap.Context = static_cast<css::beans::XPropertySet*>(this);
        aWrap.TargetException <<= e;

        throw aWrap;
    }

    // file a change event, if the value changed
    impl_fireAll(rGuard, &nHandle, &rValue, &aOldVal, 1);
}

// XFastPropertySet
Any OPropertySetHelper::getFastPropertyValue(sal_Int32 nHandle)
{
    IPropertyArrayHelper& rInfo = getInfoHelper();
    if (!rInfo.fillPropertyMembersByHandle(nullptr, nullptr, nHandle))
        // unknown property
        throw UnknownPropertyException(OUString::number(nHandle));

    Any aRet;
    std::unique_lock aGuard(m_aMutex);
    getFastPropertyValue(aGuard, aRet, nHandle);
    return aRet;
}

void OPropertySetHelper::impl_fireAll(std::unique_lock<std::mutex>& rGuard, sal_Int32* i_handles,
                                      const Any* i_newValues, const Any* i_oldValues,
                                      sal_Int32 i_count)
{
    if (m_handles.empty())
    {
        fire(rGuard, i_handles, i_newValues, i_oldValues, i_count, false);
        return;
    }

    const size_t additionalEvents = m_handles.size();
    OSL_ENSURE(additionalEvents == m_newValues.size() && additionalEvents == m_oldValues.size(),
               "OPropertySetHelper::impl_fireAll: inconsistency!");

    std::vector<sal_Int32> allHandles(additionalEvents + i_count);
    std::copy(m_handles.begin(), m_handles.end(), allHandles.begin());
    std::copy(i_handles, i_handles + i_count, allHandles.begin() + additionalEvents);

    std::vector<Any> allNewValues(additionalEvents + i_count);
    std::copy(m_newValues.begin(), m_newValues.end(), allNewValues.begin());
    std::copy(i_newValues, i_newValues + i_count, allNewValues.begin() + additionalEvents);

    std::vector<Any> allOldValues(additionalEvents + i_count);
    std::copy(m_oldValues.begin(), m_oldValues.end(), allOldValues.begin());
    std::copy(i_oldValues, i_oldValues + i_count, allOldValues.begin() + additionalEvents);

    m_handles.clear();
    m_newValues.clear();
    m_oldValues.clear();

    fire(rGuard, allHandles.data(), allNewValues.data(), allOldValues.data(),
         additionalEvents + i_count, false);
}

void OPropertySetHelper::fire(std::unique_lock<std::mutex>& rGuard, sal_Int32* pnHandles,
                              const Any* pNewValues, const Any* pOldValues,
                              sal_Int32 nHandles, // This is the Count of the array
                              bool bVetoable)
{
    // Only fire, if one or more properties changed
    if (!nHandles)
        return;

    // create the event sequence of all changed properties
    Sequence<PropertyChangeEvent> aEvts(nHandles);
    PropertyChangeEvent* pEvts = aEvts.getArray();
    Reference<XInterface> xSource(static_cast<XPropertySet*>(this), UNO_QUERY);
    sal_Int32 i;
    sal_Int32 nChangesLen = 0;
    // Loop over all changed properties to fill the event struct
    for (i = 0; i < nHandles; i++)
    {
        // Vetoable fire and constrained attribute set or
        // Change fire and Changed and bound attribute set
        IPropertyArrayHelper& rInfo = getInfoHelper();
        sal_Int16 nAttributes;
        OUString aPropName;
        rInfo.fillPropertyMembersByHandle(&aPropName, &nAttributes, pnHandles[i]);

        if ((bVetoable && (nAttributes & PropertyAttribute::CONSTRAINED))
            || (!bVetoable && (nAttributes & PropertyAttribute::BOUND)))
        {
            pEvts[nChangesLen].Source = xSource;
            pEvts[nChangesLen].PropertyName = aPropName;
            pEvts[nChangesLen].PropertyHandle = pnHandles[i];
            pEvts[nChangesLen].OldValue = pOldValues[i];
            pEvts[nChangesLen].NewValue = pNewValues[i];
            nChangesLen++;
        }
    }

    bool bIgnoreRuntimeExceptionsWhileFiring = m_bIgnoreRuntimeExceptionsWhileFiring;

    // fire the events for all changed properties
    for (i = 0; i < nChangesLen; i++)
    {
        if (bVetoable) // fire change Events?
            fireVetoableChangeListeners(
                rGuard, aVetoableLC.getContainer(rGuard, pEvts[i].PropertyHandle), pEvts[i]);
        else
            // get the listener container for the property name
            firePropertyChangeListeners(
                rGuard, aBoundLC.getContainer(rGuard, pEvts[i].PropertyHandle), pEvts[i]);

        // broadcast to all listeners with "" property name
        if (bVetoable)
            // fire change Events?
            fireVetoableChangeListeners(rGuard, &maVetoableChangeListeners, pEvts[i]);
        else
            firePropertyChangeListeners(rGuard, &maPropertyChangeListeners, pEvts[i]);
    }

    // reduce array to changed properties
    aEvts.realloc(nChangesLen);

    if (bVetoable)
        return;

    if (!maPropertiesChangeListeners.getLength(rGuard))
        return;

    // Here is a Bug, unbound properties are also fired
    OInterfaceIteratorHelper4 aIt(rGuard, maPropertiesChangeListeners);
    rGuard.unlock();
    while (aIt.hasMoreElements())
    {
        XPropertiesChangeListener* pL = aIt.next().get();
        try
        {
            try
            {
                // fire the whole event sequence to the
                // XPropertiesChangeListener's
                pL->propertiesChange(aEvts);
            }
            catch (DisposedException& exc)
            {
                OSL_ENSURE(exc.Context.is(), "DisposedException without Context!");
                if (exc.Context == pL)
                {
                    rGuard.lock();
                    aIt.remove(rGuard);
                    rGuard.unlock();
                }
                else
                    throw;
            }
        }
        catch (RuntimeException& exc)
        {
            SAL_INFO("cppuhelper", "caught RuntimeException while firing listeners: " << exc);
            if (!bIgnoreRuntimeExceptionsWhileFiring)
                throw;
        }
    }
    rGuard.lock();
}

void OPropertySetHelper::fireVetoableChangeListeners(
    std::unique_lock<std::mutex>& rGuard,
    comphelper::OInterfaceContainerHelper4<css::beans::XVetoableChangeListener>* pListeners,
    const css::beans::PropertyChangeEvent& rChangeEvent)
{
    if (!pListeners || !pListeners->getLength(rGuard))
        return;
    // Iterate over all listeners and send events
    OInterfaceIteratorHelper4 aIt(rGuard, *pListeners);
    rGuard.unlock();
    while (aIt.hasMoreElements())
    {
        XVetoableChangeListener* pL = aIt.next().get();
        try
        {
            try
            {
                pL->vetoableChange(rChangeEvent);
            }
            catch (DisposedException& exc)
            {
                OSL_ENSURE(exc.Context.is(), "DisposedException without Context!");
                if (exc.Context == pL)
                {
                    rGuard.lock();
                    aIt.remove(rGuard);
                    rGuard.unlock();
                }
                else
                    throw;
            }
        }
        catch (RuntimeException& exc)
        {
            SAL_INFO("cppuhelper", "caught RuntimeException while firing listeners: " << exc);
            if (!m_bIgnoreRuntimeExceptionsWhileFiring)
                throw;
        }
    }
    rGuard.lock();
}

void OPropertySetHelper::firePropertyChangeListeners(
    std::unique_lock<std::mutex>& rGuard,
    comphelper::OInterfaceContainerHelper4<css::beans::XPropertyChangeListener>* pListeners,
    const css::beans::PropertyChangeEvent& rChangeEvent)
{
    if (!pListeners || !pListeners->getLength(rGuard))
        return;
    // Iterate over all listeners and send events
    OInterfaceIteratorHelper4 aIt(rGuard, *pListeners);
    rGuard.unlock();
    while (aIt.hasMoreElements())
    {
        XPropertyChangeListener* pL = aIt.next().get();
        try
        {
            try
            {
                pL->propertyChange(rChangeEvent);
            }
            catch (DisposedException& exc)
            {
                OSL_ENSURE(exc.Context.is(), "DisposedException without Context!");
                if (exc.Context == pL)
                {
                    rGuard.lock();
                    aIt.remove(rGuard);
                    rGuard.unlock();
                }
                else
                    throw;
            }
        }
        catch (RuntimeException& exc)
        {
            SAL_INFO("cppuhelper", "caught RuntimeException while firing listeners: " << exc);
            if (!m_bIgnoreRuntimeExceptionsWhileFiring)
                throw;
        }
    }
    rGuard.lock();
}

// OPropertySetHelper
void OPropertySetHelper::setFastPropertyValues(std::unique_lock<std::mutex>& rGuard,
                                               sal_Int32 nSeqLen, sal_Int32* pHandles,
                                               const Any* pValues, sal_Int32 nHitCount)
{
    OSL_ENSURE(!m_bDisposed, "object is disposed");

    // get the map table
    IPropertyArrayHelper& rPH = getInfoHelper();

    std::unique_ptr<Any[]> pConvertedValues(new Any[nHitCount]);
    std::unique_ptr<Any[]> pOldValues(new Any[nHitCount]);
    sal_Int32 n = 0;
    sal_Int32 i;

    for (i = 0; i < nSeqLen; i++)
    {
        if (pHandles[i] != -1)
        {
            sal_Int16 nAttributes;
            rPH.fillPropertyMembersByHandle(nullptr, &nAttributes, pHandles[i]);
            if (nAttributes & PropertyAttribute::READONLY)
                throw PropertyVetoException();
            // Will the property change?
            if (convertFastPropertyValue(rGuard, pConvertedValues[n], pOldValues[n], pHandles[i],
                                         pValues[i]))
            {
                // only increment if the property really change
                pHandles[n] = pHandles[i];
                n++;
            }
        }
    }

    // fire vetoable events
    fire(rGuard, pHandles, pConvertedValues.get(), pOldValues.get(), n, true);

    // Loop over all changed properties
    for (i = 0; i < n; i++)
    {
        // Will the property change?
        setFastPropertyValue_NoBroadcast(rGuard, pHandles[i], pConvertedValues[i]);
    }

    // fire change events
    impl_fireAll(rGuard, pHandles, pConvertedValues.get(), pOldValues.get(), n);
}

// XMultiPropertySet
/**
 * The sequence may be contain not known properties. The implementation
 * must ignore these properties.
 */
void OPropertySetHelper::setPropertyValues(const Sequence<OUString>& rPropertyNames,
                                           const Sequence<Any>& rValues)
{
    sal_Int32 nSeqLen = rPropertyNames.getLength();
    if (nSeqLen != rValues.getLength())
        throw IllegalArgumentException("lengths do not match", static_cast<XPropertySet*>(this),
                                       -1);
    std::unique_ptr<sal_Int32[]> pHandles(new sal_Int32[nSeqLen]);
    // get the map table
    IPropertyArrayHelper& rPH = getInfoHelper();
    // fill the handle array
    sal_Int32 nHitCount = rPH.fillHandles(pHandles.get(), rPropertyNames);
    if (nHitCount == 0)
        return;
    std::unique_lock aGuard(m_aMutex);
    setFastPropertyValues(aGuard, nSeqLen, pHandles.get(), rValues.getConstArray(), nHitCount);
}

// XMultiPropertySet
Sequence<Any> OPropertySetHelper::getPropertyValues(const Sequence<OUString>& rPropertyNames)
{
    sal_Int32 nSeqLen = rPropertyNames.getLength();
    std::unique_ptr<sal_Int32[]> pHandles(new sal_Int32[nSeqLen]);
    Sequence<Any> aValues(nSeqLen);

    // get the map table
    IPropertyArrayHelper& rPH = getInfoHelper();
    // fill the handle array
    rPH.fillHandles(pHandles.get(), rPropertyNames);

    Any* pValues = aValues.getArray();

    std::unique_lock aGuard(m_aMutex);
    // fill the sequence with the values
    for (sal_Int32 i = 0; i < nSeqLen; i++)
        getFastPropertyValue(aGuard, pValues[i], pHandles[i]);

    return aValues;
}

// XMultiPropertySet
void OPropertySetHelper::addPropertiesChangeListener(
    const Sequence<OUString>&, const Reference<XPropertiesChangeListener>& rListener)
{
    std::unique_lock g(m_aMutex);
    maPropertiesChangeListeners.addInterface(g, rListener);
}

// XMultiPropertySet
void OPropertySetHelper::removePropertiesChangeListener(
    const Reference<XPropertiesChangeListener>& rListener)
{
    std::unique_lock g(m_aMutex);
    maPropertiesChangeListeners.removeInterface(g, rListener);
}

// XMultiPropertySet
void OPropertySetHelper::firePropertiesChangeEvent(
    const Sequence<OUString>& rPropertyNames, const Reference<XPropertiesChangeListener>& rListener)
{
    sal_Int32 nLen = rPropertyNames.getLength();
    std::unique_ptr<sal_Int32[]> pHandles(new sal_Int32[nLen]);
    IPropertyArrayHelper& rPH = getInfoHelper();
    rPH.fillHandles(pHandles.get(), rPropertyNames);
    const OUString* pNames = rPropertyNames.getConstArray();

    // get the count of matching properties
    sal_Int32 nFireLen = 0;
    sal_Int32 i;
    for (i = 0; i < nLen; i++)
        if (pHandles[i] != -1)
            nFireLen++;

    Sequence<PropertyChangeEvent> aChanges(nFireLen);
    PropertyChangeEvent* pChanges = aChanges.getArray();

    {
        // must lock the mutex outside the loop. So all values are consistent.
        std::unique_lock aGuard(m_aMutex);
        Reference<XInterface> xSource(static_cast<XPropertySet*>(this), UNO_QUERY);
        sal_Int32 nFirePos = 0;
        for (i = 0; i < nLen; i++)
        {
            if (pHandles[i] != -1)
            {
                pChanges[nFirePos].Source = xSource;
                pChanges[nFirePos].PropertyName = pNames[i];
                pChanges[nFirePos].PropertyHandle = pHandles[i];
                getFastPropertyValue(aGuard, pChanges[nFirePos].OldValue, pHandles[i]);
                pChanges[nFirePos].NewValue = pChanges[nFirePos].OldValue;
                nFirePos++;
            }
        }
        // release guard to fire events
    }
    if (nFireLen)
        rListener->propertiesChange(aChanges);
}

UnoImplBase::~UnoImplBase() {}

} // end namespace comphelper

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */