Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / cppuhelper / source / component.cxx
blobd906243bfddb102856a8e26f13b558c48b7bed59
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 <rtl/string.hxx>
21 #include <osl/diagnose.h>
22 #include <cppuhelper/component.hxx>
23 #include <cppuhelper/queryinterface.hxx>
24 #include <cppuhelper/typeprovider.hxx>
25 #include <com/sun/star/uno/RuntimeException.hpp>
27 using namespace osl;
28 using namespace com::sun::star;
29 using namespace com::sun::star::uno;
30 using namespace com::sun::star::lang;
32 using ::rtl::OString;
33 using ::rtl::OUStringToOString;
35 namespace cppu
39 // class OComponentHelper
42 OComponentHelper::OComponentHelper( Mutex & rMutex )
43 : rBHelper( rMutex )
46 OComponentHelper::~OComponentHelper()
50 Any OComponentHelper::queryInterface( Type const & rType )
52 return OWeakAggObject::queryInterface( rType );
54 Any OComponentHelper::queryAggregation( Type const & rType )
56 if (rType == cppu::UnoType<lang::XComponent>::get())
58 void * p = static_cast< lang::XComponent * >( this );
59 return Any( &p, rType );
61 if (rType == cppu::UnoType<lang::XTypeProvider>::get())
63 void * p = static_cast< lang::XTypeProvider * >( this );
64 return Any( &p, rType );
66 return OWeakAggObject::queryAggregation( rType );
68 void OComponentHelper::acquire() throw ()
70 OWeakAggObject::acquire();
73 void OComponentHelper::release() throw()
75 Reference<XInterface > x( xDelegator );
76 if (! x.is())
78 if (osl_atomic_decrement( &m_refCount ) == 0)
80 if (! rBHelper.bDisposed)
82 // *before* again incrementing our ref count, ensure that our weak connection point
83 // will not create references to us anymore (via XAdapter::queryAdapted)
84 disposeWeakConnectionPoint();
86 Reference<XInterface > xHoldAlive( *this );
87 // First dispose
88 try
90 dispose();
92 catch (css::uno::RuntimeException & exc)
94 // release should not throw exceptions
95 SAL_WARN( "cppuhelper", exc );
98 // only the alive ref holds the object
99 OSL_ASSERT( m_refCount == 1 );
100 // destroy the object if xHoldAlive decrement the refcount to 0
101 return;
104 // restore the reference count
105 osl_atomic_increment( &m_refCount );
107 OWeakAggObject::release();
110 Sequence< Type > OComponentHelper::getTypes()
112 static OTypeCollection * s_pTypes = nullptr;
113 if (! s_pTypes)
115 MutexGuard aGuard( Mutex::getGlobalMutex() );
116 if (! s_pTypes)
118 static OTypeCollection s_aTypes(
119 cppu::UnoType<lang::XComponent>::get(),
120 cppu::UnoType<lang::XTypeProvider>::get(),
121 cppu::UnoType<XAggregation>::get(),
122 cppu::UnoType<XWeak>::get() );
123 s_pTypes = &s_aTypes;
126 return s_pTypes->getTypes();
129 // XComponent
130 void OComponentHelper::disposing()
134 // XComponent
135 void OComponentHelper::dispose()
137 // An frequently programming error is to release the last
138 // reference to this object in the disposing message.
139 // Make it robust, hold a self Reference.
140 Reference<XComponent > xSelf( this );
142 // Guard dispose against multiple threading
143 // Remark: It is an error to call dispose more than once
144 bool bDoDispose = false;
146 MutexGuard aGuard( rBHelper.rMutex );
147 if( !rBHelper.bDisposed && !rBHelper.bInDispose )
149 // only one call go into this section
150 rBHelper.bInDispose = true;
151 bDoDispose = true;
155 // Do not hold the mutex because we are broadcasting
156 if( bDoDispose )
158 // Create an event with this as sender
163 Reference<XInterface > xSource(
164 Reference<XInterface >::query( static_cast<XComponent *>(this) ) );
165 EventObject aEvt;
166 aEvt.Source = xSource;
167 // inform all listeners to release this object
168 // The listener container are automatically cleared
169 rBHelper.aLC.disposeAndClear( aEvt );
170 // notify subclasses to do their dispose
171 disposing();
173 catch (...)
175 MutexGuard aGuard( rBHelper.rMutex );
176 // bDispose and bInDisposing must be set in this order:
177 rBHelper.bDisposed = true;
178 rBHelper.bInDispose = false;
179 throw;
181 MutexGuard aGuard( rBHelper.rMutex );
182 // bDispose and bInDisposing must be set in this order:
183 rBHelper.bDisposed = true;
184 rBHelper.bInDispose = false;
186 catch (RuntimeException &)
188 throw;
190 catch (Exception & exc)
192 throw RuntimeException(
193 "unexpected UNO exception caught: " + exc.Message );
196 else
198 // in a multithreaded environment, it can't be avoided
199 // that dispose is called twice.
200 // However this condition is traced, because it MAY indicate an error.
201 SAL_WARN("cppuhelper", "OComponentHelper::dispose() - dispose called twice" );
205 // XComponent
206 void OComponentHelper::addEventListener(
207 const Reference<XEventListener > & rxListener )
209 ClearableMutexGuard aGuard( rBHelper.rMutex );
210 if (rBHelper.bDisposed || rBHelper.bInDispose)
212 aGuard.clear();
213 Reference< XInterface > x( static_cast<XComponent *>(this), UNO_QUERY );
214 rxListener->disposing( EventObject( x ) );
216 else
218 rBHelper.addListener( cppu::UnoType<decltype(rxListener)>::get(), rxListener );
222 // XComponent
223 void OComponentHelper::removeEventListener(
224 const Reference<XEventListener > & rxListener )
226 rBHelper.removeListener( cppu::UnoType<decltype(rxListener)>::get(), rxListener );
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */