cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / UnoControls / source / controls / OConnectionPointHelper.cxx
blob26b04a6cc9af2e2628baf961f8e116334a62678e
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
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 (aReturn.hasValue())
67 return aReturn;
69 // If searched interface not supported by this class ...
70 // ... ask baseclasses.
71 return OWeakObject::queryInterface(aType);
74 // XInterface
75 void SAL_CALL OConnectionPointHelper::acquire() noexcept
77 // Attention:
78 // Don't use mutex or guard in this method!!! Is a method of XInterface.
80 // Forward to baseclass
81 OWeakObject::acquire();
84 // XInterface
85 void SAL_CALL OConnectionPointHelper::release() noexcept
87 // Attention:
88 // Don't use mutex or guard in this method!!! Is a method of XInterface.
90 // Forward to baseclass
91 OWeakObject::release();
94 // XConnectionPoint
96 Type SAL_CALL OConnectionPointHelper::getConnectionType()
98 // Ready for multithreading
99 MutexGuard aGuard( m_aSharedMutex );
101 // Set default return value, if method failed.
102 if ( !impl_LockContainer() )
104 throw RuntimeException(u"Container does not exist!"_ustr);
107 // If container reference valid, return right type of supported interfaces of THIS connectionpoint.
108 Type aReturnType = m_aInterfaceType;
109 // Don't forget this!
110 impl_UnlockContainer();
112 return aReturnType;
115 // XConnectionPoint
117 Reference< XConnectionPointContainer > SAL_CALL OConnectionPointHelper::getConnectionPointContainer()
119 // Ready for multithreading
120 MutexGuard aGuard( m_aSharedMutex );
121 // Convert weakreference to correct uno3-reference and return value. It can be NULL, if container destroyed!
122 return Reference< XConnectionPointContainer >( m_oContainerWeakReference.get(), UNO_QUERY );
125 // XConnectionPoint
127 void SAL_CALL OConnectionPointHelper::advise( const Reference< XInterface >& xListener )
129 // Ready for multithreading
130 MutexGuard aGuard( m_aSharedMutex );
132 // If type of listener not the same for this special container ...
133 Any aCheckType = xListener->queryInterface( m_aInterfaceType );
134 if ( aCheckType.hasValue() )
136 // ... throw an exception.
137 throw InvalidListenerException();
140 // ListenerExistException is obsolete!?
141 // It's the same container for XConnectionPointContainer and XConnectionPoint. But only here we must control, if a listener already exist!?
142 // You can add a listener more than one time at XConnectionPointContainer, but here only one ...
144 // Operation is permitted only, if reference to container is valid!
145 if ( !impl_LockContainer() )
147 throw RuntimeException(u"Container does not exist!"_ustr);
149 // Forward it to OConnectionPointHelperContainer!
150 m_pContainerImplementation->advise( m_aInterfaceType, xListener );
151 // Don't forget this!
152 impl_UnlockContainer();
155 // XConnectionPoint
157 void SAL_CALL OConnectionPointHelper::unadvise( const Reference< XInterface >& xListener )
159 // Ready for multithreading
160 MutexGuard aGuard( m_aSharedMutex );
161 // Operation is permitted only, if reference to container is valid!
162 if ( !impl_LockContainer() )
164 throw RuntimeException(u"Container does not exist!"_ustr);
167 // Forward it to OConnectionPointHelperContainer!
168 m_pContainerImplementation->unadvise( m_aInterfaceType, xListener );
169 // Don't forget this!
170 impl_UnlockContainer();
173 // XConnectionPoint
175 Sequence< Reference< XInterface > > SAL_CALL OConnectionPointHelper::getConnections()
177 // Ready for multithreading
178 MutexGuard aGuard( m_aSharedMutex );
179 // Operation is permitted only, if reference to container is valid!
180 if ( !impl_LockContainer() )
182 throw RuntimeException(u"Container does not exist!"_ustr);
184 // Set default return value, if method failed.
185 Sequence< Reference< XInterface > > seqReturnConnections;
186 // Get reference to private member of OConnectionPointHelperContainer!
187 comphelper::OMultiTypeInterfaceContainerHelper2& aSharedContainer = m_pContainerImplementation->impl_getMultiTypeContainer();
188 // Get pointer to specialized container which hold all interfaces of searched type.
189 comphelper::OInterfaceContainerHelper2* pSpecialContainer = aSharedContainer.getContainer( m_aInterfaceType );
190 // Get elements of searched type, if some else exist.
191 if ( pSpecialContainer != nullptr )
193 seqReturnConnections = comphelper::containerToSequence(pSpecialContainer->getElements());
195 // Don't forget this!
196 impl_UnlockContainer();
198 return seqReturnConnections;
201 // private method
203 bool OConnectionPointHelper::impl_LockContainer()
205 // Convert weakreference to hard uno3-reference and return state.
206 // If this reference different from NULL, there exist a hard reference to container. Container-instance can't be destroyed.
207 // Don't forget to "unlock" this reference!
208 m_xLock = m_oContainerWeakReference.get();
209 return m_xLock.is();
212 // private method
214 void OConnectionPointHelper::impl_UnlockContainer()
216 // Free hard uno3-reference to container.
217 // see also "impl_LockContainer()"
218 m_xLock.clear();
221 } // namespace unocontrols
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */