LibreOffice Module UnoControls (master) 1
OConnectionPointHelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
23
24#include <com/sun/star/lang/InvalidListenerException.hpp>
27
28// namespaces
29
30using namespace ::osl;
31using namespace ::cppu;
32using namespace ::com::sun::star::uno;
33using namespace ::com::sun::star::lang;
34
35namespace unocontrols {
36
37// construct/destruct
38
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 )
47{
48}
49
51{
52}
53
54// XInterface
55
57{
58 // Attention:
59 // Don't use mutex or guard in this method!!! Is a method of XInterface.
60
61 // Ask for my own supported interfaces ...
62 Any aReturn ( ::cppu::queryInterface( aType ,
63 static_cast< XConnectionPoint* > ( this )
64 )
65 );
66
67 // If searched interface not supported by this class ...
68 if ( !aReturn.hasValue() )
69 {
70 // ... ask baseclasses.
71 aReturn = OWeakObject::queryInterface( aType );
72 }
73
74 return aReturn;
75}
76
77// XInterface
78
79void SAL_CALL OConnectionPointHelper::acquire() noexcept
80{
81 // Attention:
82 // Don't use mutex or guard in this method!!! Is a method of XInterface.
83
84 // Forward to baseclass
85 OWeakObject::acquire();
86}
87
88// XInterface
89
90void SAL_CALL OConnectionPointHelper::release() noexcept
91{
92 // Attention:
93 // Don't use mutex or guard in this method!!! Is a method of XInterface.
94
95 // Forward to baseclass
96 OWeakObject::release();
97}
98
99// XConnectionPoint
100
102{
103 // Ready for multithreading
104 MutexGuard aGuard( m_aSharedMutex );
105
106 // Set default return value, if method failed.
107 if ( !impl_LockContainer() )
108 {
109 throw RuntimeException("Container does not exist!");
110 }
111
112 // If container reference valid, return right type of supported interfaces of THIS connectionpoint.
113 Type aReturnType = m_aInterfaceType;
114 // Don't forget this!
116
117 return aReturnType;
118}
119
120// XConnectionPoint
121
122Reference< XConnectionPointContainer > SAL_CALL OConnectionPointHelper::getConnectionPointContainer()
123{
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 );
128}
129
130// XConnectionPoint
131
132void SAL_CALL OConnectionPointHelper::advise( const Reference< XInterface >& xListener )
133{
134 // Ready for multithreading
135 MutexGuard aGuard( m_aSharedMutex );
136
137 // If type of listener not the same for this special container ...
138 Any aCheckType = xListener->queryInterface( m_aInterfaceType );
139 if ( aCheckType.hasValue() )
140 {
141 // ... throw an exception.
142 throw InvalidListenerException();
143 }
144
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 ...
148
149 // Operation is permitted only, if reference to container is valid!
150 if ( !impl_LockContainer() )
151 {
152 throw RuntimeException("Container does not exist!");
153 }
154 // Forward it to OConnectionPointHelperContainer!
156 // Don't forget this!
158}
159
160// XConnectionPoint
161
162void SAL_CALL OConnectionPointHelper::unadvise( const Reference< XInterface >& xListener )
163{
164 // Ready for multithreading
165 MutexGuard aGuard( m_aSharedMutex );
166 // Operation is permitted only, if reference to container is valid!
167 if ( !impl_LockContainer() )
168 {
169 throw RuntimeException("Container does not exist!");
170
171 }
172 // Forward it to OConnectionPointHelperContainer!
174 // Don't forget this!
176}
177
178// XConnectionPoint
179
180Sequence< Reference< XInterface > > SAL_CALL OConnectionPointHelper::getConnections()
181{
182 // Ready for multithreading
183 MutexGuard aGuard( m_aSharedMutex );
184 // Operation is permitted only, if reference to container is valid!
185 if ( !impl_LockContainer() )
186 {
187 throw RuntimeException("Container does not exist!");
188 }
189 // Set default return value, if method failed.
190 Sequence< Reference< XInterface > > seqReturnConnections;
191 // Get reference to private member of OConnectionPointHelperContainer!
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 )
197 {
198 seqReturnConnections = comphelper::containerToSequence(pSpecialContainer->getElements());
199 }
200 // Don't forget this!
202
203 return seqReturnConnections;
204}
205
206// private method
207
209{
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!
214 return m_xLock.is();
215}
216
217// private method
218
220{
221 // Free hard uno3-reference to container.
222 // see also "impl_LockContainer()"
223 m_xLock.clear();
224}
225
226} // namespace unocontrols
227
228/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::vector< css::uno::Reference< css::uno::XInterface > > getElements() const
OInterfaceContainerHelper2 * getContainer(const css::uno::Type &rKey) const
comphelper::OMultiTypeInterfaceContainerHelper2 & impl_getMultiTypeContainer()
virtual void SAL_CALL unadvise(const css::uno::Type &aType, const css::uno::Reference< css::uno::XInterface > &xListener) override
virtual void SAL_CALL advise(const css::uno::Type &aType, const css::uno::Reference< css::uno::XInterface > &xListener) override
virtual css::uno::Reference< css::lang::XConnectionPointContainer > SAL_CALL getConnectionPointContainer() override
virtual css::uno::Type SAL_CALL getConnectionType() override
virtual void SAL_CALL acquire() noexcept override
increment refcount @seealso XInterface @seealso release() @onerror A RuntimeException is thrown.
css::uno::WeakReference< css::lang::XConnectionPointContainer > m_oContainerWeakReference
css::uno::Reference< css::uno::XInterface > m_xLock
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) override
give answer, if interface is supported @descr The interfaces are searched by type.
virtual void SAL_CALL release() noexcept override
decrement refcount @seealso XInterface @seealso acquire() @onerror A RuntimeException is thrown.
virtual void SAL_CALL advise(const css::uno::Reference< css::uno::XInterface > &xListener) override
OConnectionPointContainerHelper * m_pContainerImplementation
virtual void SAL_CALL unadvise(const css::uno::Reference< css::uno::XInterface > &xListener) override
virtual css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > SAL_CALL getConnections() override
OConnectionPointHelper(::osl::Mutex &aMutex, OConnectionPointContainerHelper *pContainerImplementation, css::uno::Type const &aType)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Type
std::mutex aMutex