Branch libreoffice-24-8-3
[LibreOffice.git] / cppuhelper / source / component.cxx
blob5c7226f93a1a730665ab14fe64c875d2deab65af
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 <osl/diagnose.h>
21 #include <sal/log.hxx>
22 #include <cppuhelper/component.hxx>
23 #include <cppuhelper/exc_hlp.hxx>
24 #include <cppuhelper/typeprovider.hxx>
25 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
26 #include <com/sun/star/uno/RuntimeException.hpp>
28 using namespace osl;
29 using namespace com::sun::star;
30 using namespace com::sun::star::uno;
31 using namespace com::sun::star::lang;
33 namespace cppu
37 // class OComponentHelper
40 OComponentHelper::OComponentHelper( Mutex & rMutex )
41 : rBHelper( rMutex )
44 OComponentHelper::~OComponentHelper()
48 Any OComponentHelper::queryInterface( Type const & rType )
50 return OWeakAggObject::queryInterface( rType );
52 Any OComponentHelper::queryAggregation( Type const & rType )
54 if (rType == cppu::UnoType<lang::XComponent>::get())
56 void * p = static_cast< lang::XComponent * >( this );
57 return Any( &p, rType );
59 if (rType == cppu::UnoType<lang::XTypeProvider>::get())
61 void * p = static_cast< lang::XTypeProvider * >( this );
62 return Any( &p, rType );
64 return OWeakAggObject::queryAggregation( rType );
66 void OComponentHelper::acquire() noexcept
68 OWeakAggObject::acquire();
71 void OComponentHelper::release() noexcept
73 Reference<XInterface > x( xDelegator );
74 if (! x.is())
76 if (osl_atomic_decrement( &m_refCount ) == 0)
78 if (! rBHelper.bDisposed)
80 // *before* again incrementing our ref count, ensure that our weak connection point
81 // will not create references to us anymore (via XAdapter::queryAdapted)
82 disposeWeakConnectionPoint();
84 Reference<XInterface > xHoldAlive( *this );
85 // First dispose
86 try
88 dispose();
90 catch (css::uno::RuntimeException & exc)
92 // release should not throw exceptions
93 SAL_WARN( "cppuhelper", exc );
96 // only the alive ref holds the object
97 OSL_ASSERT( m_refCount == 1 );
98 // destroy the object if xHoldAlive decrement the refcount to 0
99 return;
102 // restore the reference count
103 osl_atomic_increment( &m_refCount );
105 OWeakAggObject::release();
108 Sequence< Type > OComponentHelper::getTypes()
110 static OTypeCollection s_aTypes(
111 cppu::UnoType<lang::XComponent>::get(),
112 cppu::UnoType<lang::XTypeProvider>::get(),
113 cppu::UnoType<XAggregation>::get(),
114 cppu::UnoType<XWeak>::get() );
116 return s_aTypes.getTypes();
119 // XComponent
120 void OComponentHelper::disposing()
124 // XComponent
125 void OComponentHelper::dispose()
127 // An frequently programming error is to release the last
128 // reference to this object in the disposing message.
129 // Make it robust, hold a self Reference.
130 Reference<XComponent > xSelf( this );
132 // Guard dispose against multiple threading
133 // Remark: It is an error to call dispose more than once
134 bool bDoDispose = false;
136 MutexGuard aGuard( rBHelper.rMutex );
137 if( !rBHelper.bDisposed && !rBHelper.bInDispose )
139 // only one call go into this section
140 rBHelper.bInDispose = true;
141 bDoDispose = true;
145 // Do not hold the mutex because we are broadcasting
146 if( bDoDispose )
148 // Create an event with this as sender
153 EventObject aEvt;
154 aEvt.Source = Reference<XInterface >::query( static_cast<XComponent *>(this) );
155 // inform all listeners to release this object
156 // The listener container are automatically cleared
157 rBHelper.aLC.disposeAndClear( aEvt );
158 // notify subclasses to do their dispose
159 disposing();
161 catch (...)
163 MutexGuard aGuard( rBHelper.rMutex );
164 // bDispose and bInDisposing must be set in this order:
165 rBHelper.bDisposed = true;
166 rBHelper.bInDispose = false;
167 throw;
169 MutexGuard aGuard( rBHelper.rMutex );
170 // bDispose and bInDisposing must be set in this order:
171 rBHelper.bDisposed = true;
172 rBHelper.bInDispose = false;
174 catch (RuntimeException &)
176 throw;
178 catch (Exception & exc)
180 css::uno::Any anyEx = cppu::getCaughtException();
181 throw lang::WrappedTargetRuntimeException(
182 "unexpected UNO exception caught: " + exc.Message,
183 nullptr, anyEx );
186 else
188 // in a multithreaded environment, it can't be avoided
189 // that dispose is called twice.
190 // However this condition is traced, because it MAY indicate an error.
191 SAL_WARN("cppuhelper", "OComponentHelper::dispose() - dispose called twice" );
195 // XComponent
196 void OComponentHelper::addEventListener(
197 const Reference<XEventListener > & rxListener )
199 ClearableMutexGuard aGuard( rBHelper.rMutex );
200 if (rBHelper.bDisposed || rBHelper.bInDispose)
202 aGuard.clear();
203 Reference< XInterface > x( static_cast<XComponent *>(this), UNO_QUERY );
204 rxListener->disposing( EventObject( x ) );
206 else
208 rBHelper.addListener( cppu::UnoType<decltype(rxListener)>::get(), rxListener );
212 // XComponent
213 void OComponentHelper::removeEventListener(
214 const Reference<XEventListener > & rxListener )
216 rBHelper.removeListener( cppu::UnoType<decltype(rxListener)>::get(), rxListener );
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */