Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / UnoControls / source / controls / OConnectionPointHelper.cxx
blobb6ddaaa513a019020b66e626160aef78b1f24781
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 .
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>
28 // namespaces
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 {
37 // construct/destruct
39 OConnectionPointHelper::OConnectionPointHelper(
40 Mutex& aMutex ,
41 OConnectionPointContainerHelper* pContainerImplementation ,
42 Type const & aType
43 ) : m_aSharedMutex ( aMutex )
44 , m_oContainerWeakReference ( pContainerImplementation )
45 , m_pContainerImplementation ( pContainerImplementation )
46 , m_aInterfaceType ( aType )
50 OConnectionPointHelper::~OConnectionPointHelper()
54 // XInterface
56 Any SAL_CALL OConnectionPointHelper::queryInterface( const Type& aType )
58 // Attention:
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 );
74 return aReturn;
77 // XInterface
79 void SAL_CALL OConnectionPointHelper::acquire() noexcept
81 // Attention:
82 // Don't use mutex or guard in this method!!! Is a method of XInterface.
84 // Forward to baseclass
85 OWeakObject::acquire();
88 // XInterface
90 void SAL_CALL OConnectionPointHelper::release() noexcept
92 // Attention:
93 // Don't use mutex or guard in this method!!! Is a method of XInterface.
95 // Forward to baseclass
96 OWeakObject::release();
99 // XConnectionPoint
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();
117 return aReturnType;
120 // XConnectionPoint
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 );
130 // XConnectionPoint
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();
160 // XConnectionPoint
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();
178 // XConnectionPoint
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;
206 // private method
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();
214 return m_xLock.is();
217 // private method
219 void OConnectionPointHelper::impl_UnlockContainer()
221 // Free hard uno3-reference to container.
222 // see also "impl_LockContainer()"
223 m_xLock.clear();
226 } // namespace unocontrols
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */