1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <OConnectionPointHelper.hxx>
22 #include <OConnectionPointContainerHelper.hxx>
24 #include <com/sun/star/lang/InvalidListenerException.hpp>
25 #include <cppuhelper/queryinterface.hxx>
26 #include <comphelper/sequence.hxx>
30 using namespace ::osl
;
31 using namespace ::cppu
;
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::lang
;
35 namespace unocontrols
{
39 OConnectionPointHelper::OConnectionPointHelper(
41 OConnectionPointContainerHelper
* pContainerImplementation
,
43 ) : m_aSharedMutex ( aMutex
)
44 , m_oContainerWeakReference ( pContainerImplementation
)
45 , m_pContainerImplementation ( pContainerImplementation
)
46 , m_aInterfaceType ( aType
)
50 OConnectionPointHelper::~OConnectionPointHelper()
56 Any SAL_CALL
OConnectionPointHelper::queryInterface( const Type
& aType
)
59 // Don't use mutex or guard in this method!!! Is a method of XInterface.
61 // Ask for my own supported interfaces ...
62 Any
aReturn ( ::cppu::queryInterface( aType
,
63 static_cast< XConnectionPoint
* > ( this )
67 // If searched interface not supported by this class ...
68 if ( !aReturn
.hasValue() )
70 // ... ask baseclasses.
71 aReturn
= OWeakObject::queryInterface( aType
);
79 void SAL_CALL
OConnectionPointHelper::acquire() noexcept
82 // Don't use mutex or guard in this method!!! Is a method of XInterface.
84 // Forward to baseclass
85 OWeakObject::acquire();
90 void SAL_CALL
OConnectionPointHelper::release() noexcept
93 // Don't use mutex or guard in this method!!! Is a method of XInterface.
95 // Forward to baseclass
96 OWeakObject::release();
101 Type SAL_CALL
OConnectionPointHelper::getConnectionType()
103 // Ready for multithreading
104 MutexGuard
aGuard( m_aSharedMutex
);
106 // Set default return value, if method failed.
107 if ( !impl_LockContainer() )
109 throw RuntimeException("Container does not exist!");
112 // If container reference valid, return right type of supported interfaces of THIS connectionpoint.
113 Type aReturnType
= m_aInterfaceType
;
114 // Don't forget this!
115 impl_UnlockContainer();
122 Reference
< XConnectionPointContainer
> SAL_CALL
OConnectionPointHelper::getConnectionPointContainer()
124 // Ready for multithreading
125 MutexGuard
aGuard( m_aSharedMutex
);
126 // Convert weakreference to correct uno3-reference and return value. It can be NULL, if container destroyed!
127 return Reference
< XConnectionPointContainer
>( m_oContainerWeakReference
.get(), UNO_QUERY
);
132 void SAL_CALL
OConnectionPointHelper::advise( const Reference
< XInterface
>& xListener
)
134 // Ready for multithreading
135 MutexGuard
aGuard( m_aSharedMutex
);
137 // If type of listener not the same for this special container ...
138 Any aCheckType
= xListener
->queryInterface( m_aInterfaceType
);
139 if ( aCheckType
.hasValue() )
141 // ... throw an exception.
142 throw InvalidListenerException();
145 // ListenerExistException is obsolete!?
146 // It's the same container for XConnectionPointContainer and XConnectionPoint. But only here we must control, if a listener already exist!?
147 // You can add a listener more than one time at XConnectionPointContainer, but here only one ...
149 // Operation is permitted only, if reference to container is valid!
150 if ( !impl_LockContainer() )
152 throw RuntimeException("Container does not exist!");
154 // Forward it to OConnectionPointHelperContainer!
155 m_pContainerImplementation
->advise( m_aInterfaceType
, xListener
);
156 // Don't forget this!
157 impl_UnlockContainer();
162 void SAL_CALL
OConnectionPointHelper::unadvise( const Reference
< XInterface
>& xListener
)
164 // Ready for multithreading
165 MutexGuard
aGuard( m_aSharedMutex
);
166 // Operation is permitted only, if reference to container is valid!
167 if ( !impl_LockContainer() )
169 throw RuntimeException("Container does not exist!");
172 // Forward it to OConnectionPointHelperContainer!
173 m_pContainerImplementation
->unadvise( m_aInterfaceType
, xListener
);
174 // Don't forget this!
175 impl_UnlockContainer();
180 Sequence
< Reference
< XInterface
> > SAL_CALL
OConnectionPointHelper::getConnections()
182 // Ready for multithreading
183 MutexGuard
aGuard( m_aSharedMutex
);
184 // Operation is permitted only, if reference to container is valid!
185 if ( !impl_LockContainer() )
187 throw RuntimeException("Container does not exist!");
189 // Set default return value, if method failed.
190 Sequence
< Reference
< XInterface
> > seqReturnConnections
;
191 // Get reference to private member of OConnectionPointHelperContainer!
192 comphelper::OMultiTypeInterfaceContainerHelper2
& aSharedContainer
= m_pContainerImplementation
->impl_getMultiTypeContainer();
193 // Get pointer to specialized container which hold all interfaces of searched type.
194 comphelper::OInterfaceContainerHelper2
* pSpecialContainer
= aSharedContainer
.getContainer( m_aInterfaceType
);
195 // Get elements of searched type, if some else exist.
196 if ( pSpecialContainer
!= nullptr )
198 seqReturnConnections
= comphelper::containerToSequence(pSpecialContainer
->getElements());
200 // Don't forget this!
201 impl_UnlockContainer();
203 return seqReturnConnections
;
208 bool OConnectionPointHelper::impl_LockContainer()
210 // Convert weakreference to hard uno3-reference and return state.
211 // If this reference different from NULL, there exist a hard reference to container. Container-instance can't be destroyed.
212 // Don't forget to "unlock" this reference!
213 m_xLock
= m_oContainerWeakReference
.get();
219 void OConnectionPointHelper::impl_UnlockContainer()
221 // Free hard uno3-reference to container.
222 // see also "impl_LockContainer()"
226 } // namespace unocontrols
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */