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>
26 
27 // namespaces
28 
29 using namespace ::osl;
30 using namespace ::cppu;
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::lang;
33 
34 namespace unocontrols {
35 
36 // construct/destruct
37 
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 )
46 {
47 }
48 
50 {
51 }
52 
53 // XInterface
54 
56 {
57  // Attention:
58  // Don't use mutex or guard in this method!!! Is a method of XInterface.
59 
60  // Ask for my own supported interfaces ...
61  Any aReturn ( ::cppu::queryInterface( aType ,
62  static_cast< XConnectionPoint* > ( this )
63  )
64  );
65 
66  // If searched interface not supported by this class ...
67  if ( !aReturn.hasValue() )
68  {
69  // ... ask baseclasses.
70  aReturn = OWeakObject::queryInterface( aType );
71  }
72 
73  return aReturn;
74 }
75 
76 // XInterface
77 
78 void SAL_CALL OConnectionPointHelper::acquire() throw()
79 {
80  // Attention:
81  // Don't use mutex or guard in this method!!! Is a method of XInterface.
82 
83  // Forward to baseclass
84  OWeakObject::acquire();
85 }
86 
87 // XInterface
88 
89 void SAL_CALL OConnectionPointHelper::release() throw()
90 {
91  // Attention:
92  // Don't use mutex or guard in this method!!! Is a method of XInterface.
93 
94  // Forward to baseclass
95  OWeakObject::release();
96 }
97 
98 // XConnectionPoint
99 
101 {
102  // Ready for multithreading
103  MutexGuard aGuard( m_aSharedMutex );
104 
105  // Set default return value, if method failed.
106  if ( !impl_LockContainer() )
107  {
108  throw RuntimeException("Container does not exist!");
109  }
110 
111  // If container reference valid, return right type of supported interfaces of THIS connectionpoint.
112  Type aReturnType = m_aInterfaceType;
113  // Don't forget this!
115 
116  return aReturnType;
117 }
118 
119 // XConnectionPoint
120 
121 Reference< XConnectionPointContainer > SAL_CALL OConnectionPointHelper::getConnectionPointContainer()
122 {
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 );
127 }
128 
129 // XConnectionPoint
130 
132 {
133  // Ready for multithreading
134  MutexGuard aGuard( m_aSharedMutex );
135 
136  // If type of listener not the same for this special container ...
137  Any aCheckType = xListener->queryInterface( m_aInterfaceType );
138  if ( aCheckType.hasValue() )
139  {
140  // ... throw an exception.
141  throw InvalidListenerException();
142  }
143 
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 ...
147 
148  // Operation is permitted only, if reference to container is valid!
149  if ( !impl_LockContainer() )
150  {
151  throw RuntimeException("Container does not exist!");
152  }
153  // Forward it to OConnectionPointHelperContainer!
155  // Don't forget this!
157 }
158 
159 // XConnectionPoint
160 
162 {
163  // Ready for multithreading
164  MutexGuard aGuard( m_aSharedMutex );
165  // Operation is permitted only, if reference to container is valid!
166  if ( !impl_LockContainer() )
167  {
168  throw RuntimeException("Container does not exist!");
169 
170  }
171  // Forward it to OConnectionPointHelperContainer!
173  // Don't forget this!
175 }
176 
177 // XConnectionPoint
178 
179 Sequence< Reference< XInterface > > SAL_CALL OConnectionPointHelper::getConnections()
180 {
181  // Ready for multithreading
182  MutexGuard aGuard( m_aSharedMutex );
183  // Operation is permitted only, if reference to container is valid!
184  if ( !impl_LockContainer() )
185  {
186  throw RuntimeException("Container does not exist!");
187  }
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 )
196  {
197  seqReturnConnections = pSpecialContainer->getElements();
198  }
199  // Don't forget this!
201 
202  return seqReturnConnections;
203 }
204 
205 // private method
206 
208 {
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!
213  return m_xLock.is();
214 }
215 
216 // private method
217 
219 {
220  // Free hard uno3-reference to container.
221  // see also "impl_LockContainer()"
222  m_xLock.clear();
223 }
224 
225 } // namespace unocontrols
226 
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
::cppu::OMultiTypeInterfaceContainerHelper & impl_getMultiTypeContainer()
css::uno::Reference< css::uno::XInterface > m_xLock
virtual css::uno::Reference< css::lang::XConnectionPointContainer > SAL_CALL getConnectionPointContainer() override
virtual void SAL_CALL advise(const css::uno::Type &aType, const css::uno::Reference< css::uno::XInterface > &xListener) override
virtual void SAL_CALL advise(const css::uno::Reference< css::uno::XInterface > &xListener) override
virtual void SAL_CALL acquire() override
increment refcount XInterface release() A RuntimeException is thrown.
virtual void SAL_CALL unadvise(const css::uno::Type &aType, const css::uno::Reference< css::uno::XInterface > &xListener) override
OConnectionPointContainerHelper * m_pContainerImplementation
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) override
give answer, if interface is supported The interfaces are searched by type.
OConnectionPointHelper(::osl::Mutex &aMutex, OConnectionPointContainerHelper *pContainerImplementation, css::uno::Type const &aType)
virtual css::uno::Type SAL_CALL getConnectionType() override
css::uno::WeakReference< css::lang::XConnectionPointContainer > m_oContainerWeakReference
virtual void SAL_CALL release() override
decrement refcount XInterface acquire() A RuntimeException is thrown.
virtual css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > SAL_CALL getConnections() override
css::uno::Sequence< css::uno::Reference< css::uno::XInterface > > SAL_CALL getElements() const
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
OInterfaceContainerHelper *SAL_CALL getContainer(const css::uno::Type &rKey) const
virtual void SAL_CALL unadvise(const css::uno::Reference< css::uno::XInterface > &xListener) override