Bump version to 21.06.18.1
[LibreOffice.git] / UnoControls / source / controls / OConnectionPointHelper.cxx
blobf2c99421d3c61629f29580e2e2dca27e3a7c8436
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>
27 // namespaces
29 using namespace ::osl;
30 using namespace ::cppu;
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::lang;
34 namespace unocontrols {
36 // construct/destruct
38 OConnectionPointHelper::OConnectionPointHelper(
39 Mutex& aMutex ,
40 OConnectionPointContainerHelper* pContainerImplementation ,
41 Type const & aType
42 ) : m_aSharedMutex ( aMutex )
43 , m_oContainerWeakReference ( pContainerImplementation )
44 , m_pContainerImplementation ( pContainerImplementation )
45 , m_aInterfaceType ( aType )
49 OConnectionPointHelper::~OConnectionPointHelper()
53 // XInterface
55 Any SAL_CALL OConnectionPointHelper::queryInterface( const Type& aType )
57 // Attention:
58 // Don't use mutex or guard in this method!!! Is a method of XInterface.
60 // Ask for my own supported interfaces ...
61 Any aReturn ( ::cppu::queryInterface( aType ,
62 static_cast< XConnectionPoint* > ( this )
66 // If searched interface not supported by this class ...
67 if ( !aReturn.hasValue() )
69 // ... ask baseclasses.
70 aReturn = OWeakObject::queryInterface( aType );
73 return aReturn;
76 // XInterface
78 void SAL_CALL OConnectionPointHelper::acquire() throw()
80 // Attention:
81 // Don't use mutex or guard in this method!!! Is a method of XInterface.
83 // Forward to baseclass
84 OWeakObject::acquire();
87 // XInterface
89 void SAL_CALL OConnectionPointHelper::release() throw()
91 // Attention:
92 // Don't use mutex or guard in this method!!! Is a method of XInterface.
94 // Forward to baseclass
95 OWeakObject::release();
98 // XConnectionPoint
100 Type SAL_CALL OConnectionPointHelper::getConnectionType()
102 // Ready for multithreading
103 MutexGuard aGuard( m_aSharedMutex );
105 // Set default return value, if method failed.
106 if ( !impl_LockContainer() )
108 throw RuntimeException("Container does not exist!");
111 // If container reference valid, return right type of supported interfaces of THIS connectionpoint.
112 Type aReturnType = m_aInterfaceType;
113 // Don't forget this!
114 impl_UnlockContainer();
116 return aReturnType;
119 // XConnectionPoint
121 Reference< XConnectionPointContainer > SAL_CALL OConnectionPointHelper::getConnectionPointContainer()
123 // Ready for multithreading
124 MutexGuard aGuard( m_aSharedMutex );
125 // Convert weakreference to correct uno3-reference and return value. It can be NULL, if container destroyed!
126 return Reference< XConnectionPointContainer >( m_oContainerWeakReference.get(), UNO_QUERY );
129 // XConnectionPoint
131 void SAL_CALL OConnectionPointHelper::advise( const Reference< XInterface >& xListener )
133 // Ready for multithreading
134 MutexGuard aGuard( m_aSharedMutex );
136 // If type of listener not the same for this special container ...
137 Any aCheckType = xListener->queryInterface( m_aInterfaceType );
138 if ( aCheckType.hasValue() )
140 // ... throw an exception.
141 throw InvalidListenerException();
144 // ListenerExistException is obsolete!?
145 // It's the same container for XConnectionPointContainer and XConnectionPoint. But only here we must control, if a listener already exist!?
146 // You can add a listener more than one time at XConnectionPointContainer, but here only one ...
148 // Operation is permitted only, if reference to container is valid!
149 if ( !impl_LockContainer() )
151 throw RuntimeException("Container does not exist!");
153 // Forward it to OConnectionPointHelperContainer!
154 m_pContainerImplementation->advise( m_aInterfaceType, xListener );
155 // Don't forget this!
156 impl_UnlockContainer();
159 // XConnectionPoint
161 void SAL_CALL OConnectionPointHelper::unadvise( const Reference< XInterface >& xListener )
163 // Ready for multithreading
164 MutexGuard aGuard( m_aSharedMutex );
165 // Operation is permitted only, if reference to container is valid!
166 if ( !impl_LockContainer() )
168 throw RuntimeException("Container does not exist!");
171 // Forward it to OConnectionPointHelperContainer!
172 m_pContainerImplementation->unadvise( m_aInterfaceType, xListener );
173 // Don't forget this!
174 impl_UnlockContainer();
177 // XConnectionPoint
179 Sequence< Reference< XInterface > > SAL_CALL OConnectionPointHelper::getConnections()
181 // Ready for multithreading
182 MutexGuard aGuard( m_aSharedMutex );
183 // Operation is permitted only, if reference to container is valid!
184 if ( !impl_LockContainer() )
186 throw RuntimeException("Container does not exist!");
188 // Set default return value, if method failed.
189 Sequence< Reference< XInterface > > seqReturnConnections;
190 // Get reference to private member of OConnectionPointHelperContainer!
191 OMultiTypeInterfaceContainerHelper& aSharedContainer = m_pContainerImplementation->impl_getMultiTypeContainer();
192 // Get pointer to specialized container which hold all interfaces of searched type.
193 OInterfaceContainerHelper* pSpecialContainer = aSharedContainer.getContainer( m_aInterfaceType );
194 // Get elements of searched type, if some else exist.
195 if ( pSpecialContainer != nullptr )
197 seqReturnConnections = pSpecialContainer->getElements();
199 // Don't forget this!
200 impl_UnlockContainer();
202 return seqReturnConnections;
205 // private method
207 bool OConnectionPointHelper::impl_LockContainer()
209 // Convert weakreference to hard uno3-reference and return state.
210 // If this reference different from NULL, there exist a hard reference to container. Container-instance can't be destroyed.
211 // Don't forget to "unlock" this reference!
212 m_xLock = m_oContainerWeakReference.get();
213 return m_xLock.is();
216 // private method
218 void OConnectionPointHelper::impl_UnlockContainer()
220 // Free hard uno3-reference to container.
221 // see also "impl_LockContainer()"
222 m_xLock.clear();
225 } // namespace unocontrols
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */