Bump version to 6.0-36
[LibreOffice.git] / UnoControls / source / controls / OConnectionPointHelper.cxx
blobf6814b9e9e4c14e520a60577938066294d5afc5c
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 // Container not exist! It's a runtime error.
109 throw RuntimeException();
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 // Container not exist! It's a runtime error.
153 throw RuntimeException();
155 // Forward it to OConnectionPointHelperContainer!
156 m_pContainerImplementation->advise( m_aInterfaceType, xListener );
157 // Don't forget this!
158 impl_UnlockContainer();
161 // XConnectionPoint
163 void SAL_CALL OConnectionPointHelper::unadvise( const Reference< XInterface >& xListener )
165 // Ready for multithreading
166 MutexGuard aGuard( m_aSharedMutex );
167 // Operation is permitted only, if reference to container is valid!
168 if ( !impl_LockContainer() )
170 // Container not exist! It's a runtime error.
171 throw RuntimeException();
174 // Forward it to OConnectionPointHelperContainer!
175 m_pContainerImplementation->unadvise( m_aInterfaceType, xListener );
176 // Don't forget this!
177 impl_UnlockContainer();
180 // XConnectionPoint
182 Sequence< Reference< XInterface > > SAL_CALL OConnectionPointHelper::getConnections()
184 // Ready for multithreading
185 MutexGuard aGuard( m_aSharedMutex );
186 // Operation is permitted only, if reference to container is valid!
187 if ( !impl_LockContainer() )
189 // Container not exist! It's a runtime error.
190 throw RuntimeException();
192 // Set default return value, if method failed.
193 Sequence< Reference< XInterface > > seqReturnConnections;
194 // Get reference to private member of OConnectionPointHelperContainer!
195 OMultiTypeInterfaceContainerHelper& aSharedContainer = m_pContainerImplementation->impl_getMultiTypeContainer();
196 // Get pointer to specialized container which hold all interfaces of searched type.
197 OInterfaceContainerHelper* pSpecialContainer = aSharedContainer.getContainer( m_aInterfaceType );
198 // Get elements of searched type, if some else exist.
199 if ( pSpecialContainer != nullptr )
201 seqReturnConnections = pSpecialContainer->getElements();
203 // Don't forget this!
204 impl_UnlockContainer();
206 return seqReturnConnections;
209 // private method
211 bool OConnectionPointHelper::impl_LockContainer()
213 // Convert weakreference to hard uno3-reference and return state.
214 // If this reference different from NULL, there exist a hard reference to container. Container-instance can't be destroyed.
215 // Don't forget to "unlock" this reference!
216 m_xLock = m_oContainerWeakReference.get();
217 return m_xLock.is();
220 // private method
222 void OConnectionPointHelper::impl_UnlockContainer()
224 // Free hard uno3-reference to container.
225 // see also "impl_LockContainer()"
226 m_xLock.clear();
229 } // namespace unocontrols
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */