1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
29 using namespace com::sun::star
;
30 using namespace com::sun::star::uno
;
31 using namespace com::sun::star::lang
;
37 // class OComponentHelper
40 OComponentHelper::OComponentHelper( Mutex
& 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
);
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 );
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
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();
120 void OComponentHelper::disposing()
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;
145 // Do not hold the mutex because we are broadcasting
148 // Create an event with this as sender
153 Reference
<XInterface
> xSource(
154 Reference
<XInterface
>::query( static_cast<XComponent
*>(this) ) );
156 aEvt
.Source
= xSource
;
157 // inform all listeners to release this object
158 // The listener container are automatically cleared
159 rBHelper
.aLC
.disposeAndClear( aEvt
);
160 // notify subclasses to do their dispose
165 MutexGuard
aGuard( rBHelper
.rMutex
);
166 // bDispose and bInDisposing must be set in this order:
167 rBHelper
.bDisposed
= true;
168 rBHelper
.bInDispose
= false;
171 MutexGuard
aGuard( rBHelper
.rMutex
);
172 // bDispose and bInDisposing must be set in this order:
173 rBHelper
.bDisposed
= true;
174 rBHelper
.bInDispose
= false;
176 catch (RuntimeException
&)
180 catch (Exception
& exc
)
182 css::uno::Any anyEx
= cppu::getCaughtException();
183 throw lang::WrappedTargetRuntimeException(
184 "unexpected UNO exception caught: " + exc
.Message
,
190 // in a multithreaded environment, it can't be avoided
191 // that dispose is called twice.
192 // However this condition is traced, because it MAY indicate an error.
193 SAL_WARN("cppuhelper", "OComponentHelper::dispose() - dispose called twice" );
198 void OComponentHelper::addEventListener(
199 const Reference
<XEventListener
> & rxListener
)
201 ClearableMutexGuard
aGuard( rBHelper
.rMutex
);
202 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
205 Reference
< XInterface
> x( static_cast<XComponent
*>(this), UNO_QUERY
);
206 rxListener
->disposing( EventObject( x
) );
210 rBHelper
.addListener( cppu::UnoType
<decltype(rxListener
)>::get(), rxListener
);
215 void OComponentHelper::removeEventListener(
216 const Reference
<XEventListener
> & rxListener
)
218 rBHelper
.removeListener( cppu::UnoType
<decltype(rxListener
)>::get(), rxListener
);
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */