2016-01-19 19:45:45 +02:00
|
|
|
/* -*- 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 <comphelper/interfacecontainer2.hxx>
|
2021-08-07 09:24:46 +02:00
|
|
|
#include <comphelper/multicontainer2.hxx>
|
2016-01-19 19:45:45 +02:00
|
|
|
|
2022-04-26 15:08:36 +02:00
|
|
|
#include <o3tl/safeint.hxx>
|
2016-01-19 19:45:45 +02:00
|
|
|
#include <osl/diagnose.h>
|
|
|
|
#include <osl/mutex.hxx>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <com/sun/star/lang/XEventListener.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
using namespace osl;
|
|
|
|
using namespace com::sun::star::uno;
|
|
|
|
using namespace com::sun::star::lang;
|
|
|
|
|
|
|
|
namespace comphelper
|
|
|
|
{
|
|
|
|
|
|
|
|
OInterfaceIteratorHelper2::OInterfaceIteratorHelper2( OInterfaceContainerHelper2 & rCont_ )
|
Guard read of rCont_.bIsList with rCont_.rMutex
...as had been the case prior to 12dce07aec980562fa449fa1884e0e8379d680fb
"loplugin:unusedfields - look for fields that can be const, in comphelper"
(which this commit partly reverts; the const'ness of
OInterfaceIteratorHelper2::bIsList had already been reverted, but for no
apparent reason, in 31b0be0f21479323408e128f2e8a1a795e037e74 "improve pahole
script and pack a few classes").
That may or may not have been the cause of the apparently-sporadic
UITest_sw_table failure at <https://ci.libreoffice.org//job/lo_ubsan/2096/>,
> /comphelper/source/container/interfacecontainer2.cxx:54:29: runtime error: member call on address 0x606000ed5d20 which does not point to an object of type 'com::sun::star::uno::XInterface'
> 0x606000ed5d20: note: object has invalid vptr
> 22 04 00 14 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 be be be be b0 11 46 00
> ^~~~~~~~~~~~~~~~~~~~~~~
> invalid vptr
> #0 0x2ab564a71e15 in comphelper::OInterfaceIteratorHelper2::OInterfaceIteratorHelper2(comphelper::OInterfaceContainerHelper2&) /comphelper/source/container/interfacecontainer2.cxx:54:29
> #1 0x2ab57614588f in void comphelper::OInterfaceContainerHelper2::forEach<com::sun::star::document::XDocumentEventListener, comphelper::OInterfaceContainerHelper2::NotifySingleListener<com::sun::star::document::XDocumentEventListener, com::sun::star::document::DocumentEvent> >(comphelper::OInterfaceContainerHelper2::NotifySingleListener<com::sun::star::document::XDocumentEventListener, com::sun::star::document::DocumentEvent> const&) /include/comphelper/interfacecontainer2.hxx:261:31
> #2 0x2ab576144a95 in void comphelper::OInterfaceContainerHelper2::notifyEach<com::sun::star::document::XDocumentEventListener, com::sun::star::document::DocumentEvent>(void (com::sun::star::document::XDocumentEventListener::*)(com::sun::star::document::DocumentEvent const&), com::sun::star::document::DocumentEvent const&) /include/comphelper/interfacecontainer2.hxx:279:5
> #3 0x2ab57613b277 in (anonymous namespace)::SfxGlobalEvents_Impl::implts_notifyListener(com::sun::star::document::DocumentEvent const&) /sfx2/source/notify/globalevents.cxx:505:26
> #4 0x2ab576131faa in (anonymous namespace)::SfxGlobalEvents_Impl::documentEventOccured(com::sun::star::document::DocumentEvent const&) /sfx2/source/notify/globalevents.cxx:256:5
> #5 0x2ab575f4abfc in (anonymous namespace)::NotifySingleListenerIgnoreRE<com::sun::star::document::XDocumentEventListener, com::sun::star::document::DocumentEvent>::operator()(com::sun::star::uno::Reference<com::sun::star::document::XDocumentEventListener> const&) const /sfx2/source/doc/sfxbasemodel.cxx:3200:13
> #6 0x2ab575f002ec in void cppu::OInterfaceContainerHelper::forEach<com::sun::star::document::XDocumentEventListener, (anonymous namespace)::NotifySingleListenerIgnoreRE<com::sun::star::document::XDocumentEventListener, com::sun::star::document::DocumentEvent> >((anonymous namespace)::NotifySingleListenerIgnoreRE<com::sun::star::document::XDocumentEventListener, com::sun::star::document::DocumentEvent> const&) /include/cppuhelper/interfacecontainer.h:292:17
> #7 0x2ab575efdd37 in SfxBaseModel::postEvent_Impl(rtl::OUString const&, com::sun::star::uno::Reference<com::sun::star::frame::XController2> const&) /sfx2/source/doc/sfxbasemodel.cxx:3234:14
> #8 0x2ab575efa257 in SfxBaseModel::Notify(SfxBroadcaster&, SfxHint const&) /sfx2/source/doc/sfxbasemodel.cxx:2883:9
> #9 0x2ab57a796d83 in SfxBroadcaster::Broadcast(SfxHint const&) /svl/source/notify/SfxBroadcaster.cxx:39:24
> #10 0x2ab6010f0e1f in SwLayIdle::SwLayIdle(SwRootFrame*, SwViewShellImp*) /sw/source/core/layout/layact.cxx:2375:24
> #11 0x2ab60385d4e2 in SwViewShell::LayoutIdle() /sw/source/core/view/viewsh.cxx:723:19
> #12 0x2ab5ff549ffa in sw::DocumentTimerManager::DoIdleJobs(Timer*) /sw/source/core/doc/DocumentTimerManager.cxx:177:42
> #13 0x2ab5ff54552a in sw::DocumentTimerManager::LinkStubDoIdleJobs(void*, Timer*) /sw/source/core/doc/DocumentTimerManager.cxx:157:1
> #14 0x2ab599cf5f41 in Link<Timer*, void>::Call(Timer*) const /include/tools/link.hxx:111:45
> #15 0x2ab599cf5498 in Timer::Invoke() /vcl/source/app/timer.cxx:75:21
> #16 0x2ab599b388ea in Scheduler::CallbackTaskScheduling() /vcl/source/app/scheduler.cxx:471:20
> #17 0x2ab59ad34490 in SalTimer::CallCallback() /vcl/inc/saltimer.hxx:54:13
> #18 0x2ab59ad2785c in SvpSalInstance::CheckTimeout(bool) /vcl/headless/svpinst.cxx:215:53
> #19 0x2ab59ad2e67c in SvpSalInstance::DoYield(bool, bool) /vcl/headless/svpinst.cxx:460:21
> #20 0x2ab599c35aa9 in ImplYield(bool, bool) /vcl/source/app/svapp.cxx:465:48
> #21 0x2ab599c34c67 in Application::Yield() /vcl/source/app/svapp.cxx:532:5
> #22 0x2ab599c348ae in Application::Execute() /vcl/source/app/svapp.cxx:444:9
> #23 0x2ab55d699b4b in desktop::Desktop::Main() /desktop/source/app/app.cxx:1603:13
> #24 0x2ab599cde4e9 in ImplSVMain() /vcl/source/app/svmain.cxx:199:35
> #25 0x2ab599ce7510 in SVMain() /vcl/source/app/svmain.cxx:231:12
> #26 0x2ab55d890775 in soffice_main /desktop/source/app/sofficemain.cxx:98:12
> #27 0x4efc6c in sal_main /desktop/source/app/main.c:49:15
> #28 0x4efc46 in main /desktop/source/app/main.c:47:1
> #29 0x2ab55f48e554 in __libc_start_main (/lib64/libc.so.6+0x22554)
> #30 0x41b5c1 in _start (/instdir/program/soffice.bin+0x41b5c1)
Change-Id: Iaa930f1612a3bb6ad0bcad12f3995b7dbb2e5fa7
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/120205
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Tested-by: Jenkins
2021-08-09 13:29:06 +02:00
|
|
|
: rCont( rCont_ )
|
2016-01-19 19:45:45 +02:00
|
|
|
{
|
|
|
|
MutexGuard aGuard( rCont.rMutex );
|
|
|
|
if( rCont.bInUse )
|
|
|
|
// worst case, two iterators at the same time
|
|
|
|
rCont.copyAndResetInUse();
|
Guard read of rCont_.bIsList with rCont_.rMutex
...as had been the case prior to 12dce07aec980562fa449fa1884e0e8379d680fb
"loplugin:unusedfields - look for fields that can be const, in comphelper"
(which this commit partly reverts; the const'ness of
OInterfaceIteratorHelper2::bIsList had already been reverted, but for no
apparent reason, in 31b0be0f21479323408e128f2e8a1a795e037e74 "improve pahole
script and pack a few classes").
That may or may not have been the cause of the apparently-sporadic
UITest_sw_table failure at <https://ci.libreoffice.org//job/lo_ubsan/2096/>,
> /comphelper/source/container/interfacecontainer2.cxx:54:29: runtime error: member call on address 0x606000ed5d20 which does not point to an object of type 'com::sun::star::uno::XInterface'
> 0x606000ed5d20: note: object has invalid vptr
> 22 04 00 14 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 be be be be b0 11 46 00
> ^~~~~~~~~~~~~~~~~~~~~~~
> invalid vptr
> #0 0x2ab564a71e15 in comphelper::OInterfaceIteratorHelper2::OInterfaceIteratorHelper2(comphelper::OInterfaceContainerHelper2&) /comphelper/source/container/interfacecontainer2.cxx:54:29
> #1 0x2ab57614588f in void comphelper::OInterfaceContainerHelper2::forEach<com::sun::star::document::XDocumentEventListener, comphelper::OInterfaceContainerHelper2::NotifySingleListener<com::sun::star::document::XDocumentEventListener, com::sun::star::document::DocumentEvent> >(comphelper::OInterfaceContainerHelper2::NotifySingleListener<com::sun::star::document::XDocumentEventListener, com::sun::star::document::DocumentEvent> const&) /include/comphelper/interfacecontainer2.hxx:261:31
> #2 0x2ab576144a95 in void comphelper::OInterfaceContainerHelper2::notifyEach<com::sun::star::document::XDocumentEventListener, com::sun::star::document::DocumentEvent>(void (com::sun::star::document::XDocumentEventListener::*)(com::sun::star::document::DocumentEvent const&), com::sun::star::document::DocumentEvent const&) /include/comphelper/interfacecontainer2.hxx:279:5
> #3 0x2ab57613b277 in (anonymous namespace)::SfxGlobalEvents_Impl::implts_notifyListener(com::sun::star::document::DocumentEvent const&) /sfx2/source/notify/globalevents.cxx:505:26
> #4 0x2ab576131faa in (anonymous namespace)::SfxGlobalEvents_Impl::documentEventOccured(com::sun::star::document::DocumentEvent const&) /sfx2/source/notify/globalevents.cxx:256:5
> #5 0x2ab575f4abfc in (anonymous namespace)::NotifySingleListenerIgnoreRE<com::sun::star::document::XDocumentEventListener, com::sun::star::document::DocumentEvent>::operator()(com::sun::star::uno::Reference<com::sun::star::document::XDocumentEventListener> const&) const /sfx2/source/doc/sfxbasemodel.cxx:3200:13
> #6 0x2ab575f002ec in void cppu::OInterfaceContainerHelper::forEach<com::sun::star::document::XDocumentEventListener, (anonymous namespace)::NotifySingleListenerIgnoreRE<com::sun::star::document::XDocumentEventListener, com::sun::star::document::DocumentEvent> >((anonymous namespace)::NotifySingleListenerIgnoreRE<com::sun::star::document::XDocumentEventListener, com::sun::star::document::DocumentEvent> const&) /include/cppuhelper/interfacecontainer.h:292:17
> #7 0x2ab575efdd37 in SfxBaseModel::postEvent_Impl(rtl::OUString const&, com::sun::star::uno::Reference<com::sun::star::frame::XController2> const&) /sfx2/source/doc/sfxbasemodel.cxx:3234:14
> #8 0x2ab575efa257 in SfxBaseModel::Notify(SfxBroadcaster&, SfxHint const&) /sfx2/source/doc/sfxbasemodel.cxx:2883:9
> #9 0x2ab57a796d83 in SfxBroadcaster::Broadcast(SfxHint const&) /svl/source/notify/SfxBroadcaster.cxx:39:24
> #10 0x2ab6010f0e1f in SwLayIdle::SwLayIdle(SwRootFrame*, SwViewShellImp*) /sw/source/core/layout/layact.cxx:2375:24
> #11 0x2ab60385d4e2 in SwViewShell::LayoutIdle() /sw/source/core/view/viewsh.cxx:723:19
> #12 0x2ab5ff549ffa in sw::DocumentTimerManager::DoIdleJobs(Timer*) /sw/source/core/doc/DocumentTimerManager.cxx:177:42
> #13 0x2ab5ff54552a in sw::DocumentTimerManager::LinkStubDoIdleJobs(void*, Timer*) /sw/source/core/doc/DocumentTimerManager.cxx:157:1
> #14 0x2ab599cf5f41 in Link<Timer*, void>::Call(Timer*) const /include/tools/link.hxx:111:45
> #15 0x2ab599cf5498 in Timer::Invoke() /vcl/source/app/timer.cxx:75:21
> #16 0x2ab599b388ea in Scheduler::CallbackTaskScheduling() /vcl/source/app/scheduler.cxx:471:20
> #17 0x2ab59ad34490 in SalTimer::CallCallback() /vcl/inc/saltimer.hxx:54:13
> #18 0x2ab59ad2785c in SvpSalInstance::CheckTimeout(bool) /vcl/headless/svpinst.cxx:215:53
> #19 0x2ab59ad2e67c in SvpSalInstance::DoYield(bool, bool) /vcl/headless/svpinst.cxx:460:21
> #20 0x2ab599c35aa9 in ImplYield(bool, bool) /vcl/source/app/svapp.cxx:465:48
> #21 0x2ab599c34c67 in Application::Yield() /vcl/source/app/svapp.cxx:532:5
> #22 0x2ab599c348ae in Application::Execute() /vcl/source/app/svapp.cxx:444:9
> #23 0x2ab55d699b4b in desktop::Desktop::Main() /desktop/source/app/app.cxx:1603:13
> #24 0x2ab599cde4e9 in ImplSVMain() /vcl/source/app/svmain.cxx:199:35
> #25 0x2ab599ce7510 in SVMain() /vcl/source/app/svmain.cxx:231:12
> #26 0x2ab55d890775 in soffice_main /desktop/source/app/sofficemain.cxx:98:12
> #27 0x4efc6c in sal_main /desktop/source/app/main.c:49:15
> #28 0x4efc46 in main /desktop/source/app/main.c:47:1
> #29 0x2ab55f48e554 in __libc_start_main (/lib64/libc.so.6+0x22554)
> #30 0x41b5c1 in _start (/instdir/program/soffice.bin+0x41b5c1)
Change-Id: Iaa930f1612a3bb6ad0bcad12f3995b7dbb2e5fa7
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/120205
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Tested-by: Jenkins
2021-08-09 13:29:06 +02:00
|
|
|
bIsList = rCont_.bIsList;
|
2016-01-19 19:45:45 +02:00
|
|
|
aData = rCont_.aData;
|
|
|
|
if( bIsList )
|
|
|
|
{
|
|
|
|
rCont.bInUse = true;
|
|
|
|
nRemain = aData.pAsVector->size();
|
|
|
|
}
|
|
|
|
else if( aData.pAsInterface )
|
|
|
|
{
|
|
|
|
aData.pAsInterface->acquire();
|
|
|
|
nRemain = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nRemain = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
OInterfaceIteratorHelper2::~OInterfaceIteratorHelper2()
|
|
|
|
{
|
|
|
|
bool bShared;
|
|
|
|
{
|
|
|
|
MutexGuard aGuard( rCont.rMutex );
|
|
|
|
// bResetInUse protect the iterator against recursion
|
|
|
|
bShared = aData.pAsVector == rCont.aData.pAsVector && rCont.bIsList;
|
|
|
|
if( bShared )
|
|
|
|
{
|
|
|
|
OSL_ENSURE( rCont.bInUse, "OInterfaceContainerHelper2 must be in use" );
|
|
|
|
rCont.bInUse = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !bShared )
|
|
|
|
{
|
|
|
|
if( bIsList )
|
|
|
|
// Sequence owned by the iterator
|
|
|
|
delete aData.pAsVector;
|
|
|
|
else if( aData.pAsInterface )
|
|
|
|
// Interface is acquired by the iterator
|
|
|
|
aData.pAsInterface->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XInterface * OInterfaceIteratorHelper2::next()
|
|
|
|
{
|
|
|
|
if( nRemain )
|
|
|
|
{
|
|
|
|
nRemain--;
|
|
|
|
if( bIsList )
|
|
|
|
return (*aData.pAsVector)[nRemain].get();
|
|
|
|
else if( aData.pAsInterface )
|
|
|
|
return aData.pAsInterface;
|
|
|
|
}
|
|
|
|
// exception
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OInterfaceIteratorHelper2::remove()
|
|
|
|
{
|
|
|
|
if( bIsList )
|
|
|
|
{
|
|
|
|
OSL_ASSERT( nRemain >= 0 &&
|
2022-04-26 15:08:36 +02:00
|
|
|
o3tl::make_unsigned(nRemain) < aData.pAsVector->size() );
|
2016-01-19 19:45:45 +02:00
|
|
|
rCont.removeInterface( (*aData.pAsVector)[nRemain] );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OSL_ASSERT( 0 == nRemain );
|
|
|
|
rCont.removeInterface( aData.pAsInterface );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OInterfaceContainerHelper2::OInterfaceContainerHelper2( Mutex & rMutex_ )
|
|
|
|
: rMutex( rMutex_ )
|
|
|
|
, bInUse( false )
|
|
|
|
, bIsList( false )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
OInterfaceContainerHelper2::~OInterfaceContainerHelper2()
|
|
|
|
{
|
|
|
|
OSL_ENSURE( !bInUse, "~OInterfaceContainerHelper2 but is in use" );
|
|
|
|
if( bIsList )
|
|
|
|
delete aData.pAsVector;
|
|
|
|
else if( aData.pAsInterface )
|
|
|
|
aData.pAsInterface->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
sal_Int32 OInterfaceContainerHelper2::getLength() const
|
|
|
|
{
|
|
|
|
MutexGuard aGuard( rMutex );
|
|
|
|
if( bIsList )
|
|
|
|
return aData.pAsVector->size();
|
|
|
|
else if( aData.pAsInterface )
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector< Reference<XInterface> > OInterfaceContainerHelper2::getElements() const
|
|
|
|
{
|
|
|
|
std::vector< Reference<XInterface> > rVec;
|
|
|
|
MutexGuard aGuard( rMutex );
|
|
|
|
if( bIsList )
|
|
|
|
rVec = *aData.pAsVector;
|
|
|
|
else if( aData.pAsInterface )
|
|
|
|
{
|
2017-09-08 16:55:37 +02:00
|
|
|
rVec.emplace_back( aData.pAsInterface );
|
2016-01-19 19:45:45 +02:00
|
|
|
}
|
|
|
|
return rVec;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OInterfaceContainerHelper2::copyAndResetInUse()
|
|
|
|
{
|
|
|
|
OSL_ENSURE( bInUse, "OInterfaceContainerHelper2 not in use" );
|
|
|
|
if( bInUse )
|
|
|
|
{
|
2019-04-10 16:05:40 +00:00
|
|
|
// this should be the worst case. If an iterator is active
|
2016-01-19 19:45:45 +02:00
|
|
|
// and a new Listener is added.
|
|
|
|
if( bIsList )
|
|
|
|
aData.pAsVector = new std::vector< Reference< XInterface > >( *aData.pAsVector );
|
|
|
|
else if( aData.pAsInterface )
|
|
|
|
aData.pAsInterface->acquire();
|
|
|
|
|
|
|
|
bInUse = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sal_Int32 OInterfaceContainerHelper2::addInterface( const Reference<XInterface> & rListener )
|
|
|
|
{
|
|
|
|
OSL_ASSERT( rListener.is() );
|
|
|
|
MutexGuard aGuard( rMutex );
|
|
|
|
if( bInUse )
|
|
|
|
copyAndResetInUse();
|
|
|
|
|
|
|
|
if( bIsList )
|
|
|
|
{
|
|
|
|
aData.pAsVector->push_back( rListener );
|
|
|
|
return aData.pAsVector->size();
|
|
|
|
}
|
|
|
|
else if( aData.pAsInterface )
|
|
|
|
{
|
|
|
|
std::vector< Reference< XInterface > > * pVec = new std::vector< Reference< XInterface > >( 2 );
|
|
|
|
(*pVec)[0] = aData.pAsInterface;
|
|
|
|
(*pVec)[1] = rListener;
|
|
|
|
aData.pAsInterface->release();
|
|
|
|
aData.pAsVector = pVec;
|
|
|
|
bIsList = true;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aData.pAsInterface = rListener.get();
|
|
|
|
if( rListener.is() )
|
|
|
|
rListener->acquire();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sal_Int32 OInterfaceContainerHelper2::removeInterface( const Reference<XInterface> & rListener )
|
|
|
|
{
|
|
|
|
OSL_ASSERT( rListener.is() );
|
|
|
|
MutexGuard aGuard( rMutex );
|
|
|
|
if( bInUse )
|
|
|
|
copyAndResetInUse();
|
|
|
|
|
|
|
|
if( bIsList )
|
|
|
|
{
|
2019-03-10 21:51:45 +03:00
|
|
|
// It is not valid to compare the pointer directly, but it's faster.
|
|
|
|
auto it = std::find_if(aData.pAsVector->begin(), aData.pAsVector->end(),
|
|
|
|
[&rListener](const css::uno::Reference<css::uno::XInterface>& rItem) {
|
|
|
|
return rItem.get() == rListener.get(); });
|
2016-01-19 19:45:45 +02:00
|
|
|
|
|
|
|
// interface not found, use the correct compare method
|
2019-03-10 21:51:45 +03:00
|
|
|
if (it == aData.pAsVector->end())
|
|
|
|
it = std::find(aData.pAsVector->begin(), aData.pAsVector->end(), rListener);
|
|
|
|
|
|
|
|
if (it != aData.pAsVector->end())
|
|
|
|
aData.pAsVector->erase(it);
|
2016-01-19 19:45:45 +02:00
|
|
|
|
|
|
|
if( aData.pAsVector->size() == 1 )
|
|
|
|
{
|
|
|
|
XInterface * p = (*aData.pAsVector)[0].get();
|
|
|
|
p->acquire();
|
|
|
|
delete aData.pAsVector;
|
|
|
|
aData.pAsInterface = p;
|
|
|
|
bIsList = false;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return aData.pAsVector->size();
|
|
|
|
}
|
|
|
|
else if( aData.pAsInterface && Reference<XInterface>( aData.pAsInterface ) == rListener )
|
|
|
|
{
|
|
|
|
aData.pAsInterface->release();
|
|
|
|
aData.pAsInterface = nullptr;
|
|
|
|
}
|
|
|
|
return aData.pAsInterface ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:48:06 +02:00
|
|
|
Reference<XInterface> OInterfaceContainerHelper2::getInterface( sal_Int32 nIndex ) const
|
|
|
|
{
|
|
|
|
MutexGuard aGuard( rMutex );
|
|
|
|
|
|
|
|
if( bIsList )
|
|
|
|
return (*aData.pAsVector)[nIndex];
|
|
|
|
else if( aData.pAsInterface )
|
|
|
|
{
|
|
|
|
if (nIndex == 0)
|
|
|
|
return aData.pAsInterface;
|
|
|
|
}
|
|
|
|
throw std::out_of_range("index out of range");
|
|
|
|
}
|
|
|
|
|
2016-01-19 19:45:45 +02:00
|
|
|
void OInterfaceContainerHelper2::disposeAndClear( const EventObject & rEvt )
|
|
|
|
{
|
|
|
|
ClearableMutexGuard aGuard( rMutex );
|
|
|
|
OInterfaceIteratorHelper2 aIt( *this );
|
|
|
|
// Release container, in case new entries come while disposing
|
|
|
|
OSL_ENSURE( !bIsList || bInUse, "OInterfaceContainerHelper2 not in use" );
|
|
|
|
if( !bIsList && aData.pAsInterface )
|
|
|
|
aData.pAsInterface->release();
|
|
|
|
// set the member to null, use the iterator to delete the values
|
|
|
|
aData.pAsInterface = nullptr;
|
|
|
|
bIsList = false;
|
|
|
|
bInUse = false;
|
|
|
|
aGuard.clear();
|
|
|
|
while( aIt.hasMoreElements() )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Reference<XEventListener > xLst( aIt.next(), UNO_QUERY );
|
|
|
|
if( xLst.is() )
|
|
|
|
xLst->disposing( rEvt );
|
|
|
|
}
|
|
|
|
catch ( RuntimeException & )
|
|
|
|
{
|
|
|
|
// be robust, if e.g. a remote bridge has disposed already.
|
|
|
|
// there is no way to delegate the error to the caller :o(.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OInterfaceContainerHelper2::clear()
|
|
|
|
{
|
2020-01-25 17:41:43 +02:00
|
|
|
MutexGuard aGuard( rMutex );
|
2016-01-19 19:45:45 +02:00
|
|
|
// Release container, in case new entries come while disposing
|
|
|
|
OSL_ENSURE( !bIsList || bInUse, "OInterfaceContainerHelper2 not in use" );
|
2020-01-25 17:41:43 +02:00
|
|
|
if (bInUse)
|
|
|
|
copyAndResetInUse();
|
|
|
|
if (bIsList)
|
|
|
|
delete aData.pAsVector;
|
|
|
|
else if (aData.pAsInterface)
|
2016-01-19 19:45:45 +02:00
|
|
|
aData.pAsInterface->release();
|
|
|
|
aData.pAsInterface = nullptr;
|
|
|
|
bIsList = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-07 09:24:46 +02:00
|
|
|
|
|
|
|
// specialized class for type
|
|
|
|
|
|
|
|
OMultiTypeInterfaceContainerHelper2::OMultiTypeInterfaceContainerHelper2( Mutex & rMutex_ )
|
|
|
|
: rMutex( rMutex_ )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
OMultiTypeInterfaceContainerHelper2::~OMultiTypeInterfaceContainerHelper2()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector< css::uno::Type > OMultiTypeInterfaceContainerHelper2::getContainedTypes() const
|
|
|
|
{
|
|
|
|
::osl::MutexGuard aGuard( rMutex );
|
|
|
|
std::vector< Type > aInterfaceTypes;
|
|
|
|
aInterfaceTypes.reserve( m_aMap.size() );
|
|
|
|
for (const auto& rItem : m_aMap)
|
|
|
|
{
|
|
|
|
// are interfaces added to this container?
|
|
|
|
if( rItem.second->getLength() )
|
|
|
|
// yes, put the type in the array
|
|
|
|
aInterfaceTypes.push_back(rItem.first);
|
|
|
|
}
|
|
|
|
return aInterfaceTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
OMultiTypeInterfaceContainerHelper2::t_type2ptr::iterator OMultiTypeInterfaceContainerHelper2::findType(const Type & rKey )
|
|
|
|
{
|
|
|
|
return std::find_if(m_aMap.begin(), m_aMap.end(),
|
|
|
|
[&rKey](const t_type2ptr::value_type& rItem) { return rItem.first == rKey; });
|
|
|
|
}
|
|
|
|
|
|
|
|
OMultiTypeInterfaceContainerHelper2::t_type2ptr::const_iterator OMultiTypeInterfaceContainerHelper2::findType(const Type & rKey ) const
|
|
|
|
{
|
|
|
|
return std::find_if(m_aMap.begin(), m_aMap.end(),
|
|
|
|
[&rKey](const t_type2ptr::value_type& rItem) { return rItem.first == rKey; });
|
|
|
|
}
|
|
|
|
|
|
|
|
OInterfaceContainerHelper2 * OMultiTypeInterfaceContainerHelper2::getContainer( const Type & rKey ) const
|
|
|
|
{
|
|
|
|
::osl::MutexGuard aGuard( rMutex );
|
|
|
|
|
|
|
|
auto iter = findType( rKey );
|
|
|
|
if( iter != m_aMap.end() )
|
|
|
|
return (*iter).second.get();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
sal_Int32 OMultiTypeInterfaceContainerHelper2::addInterface(
|
|
|
|
const Type & rKey, const Reference< XInterface > & rListener )
|
|
|
|
{
|
|
|
|
::osl::MutexGuard aGuard( rMutex );
|
|
|
|
auto iter = findType( rKey );
|
|
|
|
if( iter == m_aMap.end() )
|
|
|
|
{
|
|
|
|
OInterfaceContainerHelper2 * pLC = new OInterfaceContainerHelper2( rMutex );
|
|
|
|
m_aMap.emplace_back(rKey, pLC);
|
|
|
|
return pLC->addInterface( rListener );
|
|
|
|
}
|
|
|
|
return (*iter).second->addInterface( rListener );
|
|
|
|
}
|
|
|
|
|
|
|
|
sal_Int32 OMultiTypeInterfaceContainerHelper2::removeInterface(
|
|
|
|
const Type & rKey, const Reference< XInterface > & rListener )
|
|
|
|
{
|
|
|
|
::osl::MutexGuard aGuard( rMutex );
|
|
|
|
|
|
|
|
// search container with id nUik
|
|
|
|
auto iter = findType( rKey );
|
|
|
|
// container found?
|
|
|
|
if( iter != m_aMap.end() )
|
|
|
|
return (*iter).second->removeInterface( rListener );
|
|
|
|
|
|
|
|
// no container with this id. Always return 0
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OMultiTypeInterfaceContainerHelper2::disposeAndClear( const EventObject & rEvt )
|
|
|
|
{
|
|
|
|
t_type2ptr::size_type nSize = 0;
|
|
|
|
std::unique_ptr<OInterfaceContainerHelper2 *[]> ppListenerContainers;
|
|
|
|
{
|
|
|
|
::osl::MutexGuard aGuard( rMutex );
|
|
|
|
nSize = m_aMap.size();
|
|
|
|
if( nSize )
|
|
|
|
{
|
|
|
|
typedef OInterfaceContainerHelper2* ppp;
|
|
|
|
ppListenerContainers.reset(new ppp[nSize]);
|
|
|
|
|
|
|
|
t_type2ptr::size_type i = 0;
|
|
|
|
for (const auto& rItem : m_aMap)
|
|
|
|
{
|
|
|
|
ppListenerContainers[i++] = rItem.second.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a copy, because do not fire event in a guarded section
|
|
|
|
for( t_type2ptr::size_type i = 0; i < nSize; i++ )
|
|
|
|
{
|
|
|
|
if( ppListenerContainers[i] )
|
|
|
|
ppListenerContainers[i]->disposeAndClear( rEvt );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OMultiTypeInterfaceContainerHelper2::clear()
|
|
|
|
{
|
|
|
|
::osl::MutexGuard aGuard( rMutex );
|
|
|
|
|
|
|
|
for (auto& rItem : m_aMap)
|
|
|
|
rItem.second->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-19 19:45:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|