update dev300-m58
[ooovba.git] / comphelper / source / misc / proxyaggregation.cxx
blob68d862c626b298d7c5798cd317a48311745c5d18
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: proxyaggregation.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <comphelper/proxyaggregation.hxx>
34 #include <com/sun/star/reflection/XProxyFactory.hpp>
36 //.............................................................................
37 namespace comphelper
39 //.............................................................................
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::lang;
43 using namespace ::com::sun::star::reflection;
45 //=========================================================================
46 //= OProxyAggregation
47 //=========================================================================
48 //-------------------------------------------------------------------------
49 OProxyAggregation::OProxyAggregation( const Reference< XMultiServiceFactory >& _rxORB )
50 :m_xORB( _rxORB )
54 //-------------------------------------------------------------------------
55 void OProxyAggregation::baseAggregateProxyFor( const Reference< XInterface >& _rxComponent, oslInterlockedCount& _rRefCount,
56 ::cppu::OWeakObject& _rDelegator )
58 // first a factory for the proxy
59 Reference< XProxyFactory > xFactory(
60 m_xORB->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.reflection.ProxyFactory" ) ) ),
61 UNO_QUERY
63 OSL_ENSURE( xFactory.is(), "OProxyAggregation::baseAggregateProxyFor: could not create a proxy factory!" );
65 // then the proxy itself
66 if ( xFactory.is() )
68 { // i36686 OJ: achieve the desctruction of the tempoary -> otherwise it leads to _rRefCount -= 2
69 m_xProxyAggregate = xFactory->createProxy( _rxComponent );
71 if ( m_xProxyAggregate.is() )
72 m_xProxyAggregate->queryAggregation( ::getCppuType( &m_xProxyTypeAccess ) ) >>= m_xProxyTypeAccess;
74 // aggregate the proxy
75 osl_incrementInterlockedCount( &_rRefCount );
76 if ( m_xProxyAggregate.is() )
78 // At this point in time, the proxy has a ref count of exactly two - in m_xControlContextProxy,
79 // and in m_xProxyTypeAccess.
80 // Remember to _not_ reset these members unless the delegator of the proxy has been reset, too!
81 m_xProxyAggregate->setDelegator( _rDelegator );
83 osl_decrementInterlockedCount( &_rRefCount );
87 //-------------------------------------------------------------------------
88 Any SAL_CALL OProxyAggregation::queryAggregation( const Type& _rType ) throw (RuntimeException)
90 return m_xProxyAggregate.is() ? m_xProxyAggregate->queryAggregation( _rType ) : Any();
93 //-------------------------------------------------------------------------
94 Sequence< Type > SAL_CALL OProxyAggregation::getTypes( ) throw (RuntimeException)
96 Sequence< Type > aTypes;
97 if ( m_xProxyAggregate.is() )
99 if ( m_xProxyTypeAccess.is() )
100 aTypes = m_xProxyTypeAccess->getTypes();
102 return aTypes;
105 //-------------------------------------------------------------------------
106 OProxyAggregation::~OProxyAggregation()
108 if ( m_xProxyAggregate.is() )
109 m_xProxyAggregate->setDelegator( NULL );
110 m_xProxyAggregate.clear();
111 m_xProxyTypeAccess.clear();
112 // this should remove the _two_only_ "real" references (means not delegated to
113 // ourself) to this proxy, and thus delete it
116 //=========================================================================
117 //= OComponentProxyAggregationHelper
118 //=========================================================================
119 //-------------------------------------------------------------------------
120 OComponentProxyAggregationHelper::OComponentProxyAggregationHelper( const Reference< XMultiServiceFactory >& _rxORB,
121 ::cppu::OBroadcastHelper& _rBHelper )
122 :OProxyAggregation( _rxORB )
123 ,m_rBHelper( _rBHelper )
125 OSL_ENSURE( _rxORB.is(), "OComponentProxyAggregationHelper::OComponentProxyAggregationHelper: invalid arguments!" );
128 //-------------------------------------------------------------------------
129 void OComponentProxyAggregationHelper::componentAggregateProxyFor(
130 const Reference< XComponent >& _rxComponent, oslInterlockedCount& _rRefCount,
131 ::cppu::OWeakObject& _rDelegator )
133 OSL_ENSURE( _rxComponent.is(), "OComponentProxyAggregationHelper::componentAggregateProxyFor: invalid inner component!" );
134 m_xInner = _rxComponent;
136 // aggregate a proxy for the object
137 baseAggregateProxyFor( m_xInner.get(), _rRefCount, _rDelegator );
139 // add as event listener to the inner context, because we want to be notified of disposals
140 osl_incrementInterlockedCount( &_rRefCount );
142 if ( m_xInner.is() )
143 m_xInner->addEventListener( this );
145 osl_decrementInterlockedCount( &_rRefCount );
148 //-------------------------------------------------------------------------
149 Any SAL_CALL OComponentProxyAggregationHelper::queryInterface( const Type& _rType ) throw (RuntimeException)
151 Any aReturn( BASE::queryInterface( _rType ) );
152 if ( !aReturn.hasValue() )
153 aReturn = OProxyAggregation::queryAggregation( _rType );
154 return aReturn;
157 //-------------------------------------------------------------------------
158 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OComponentProxyAggregationHelper, BASE, OProxyAggregation )
160 //-------------------------------------------------------------------------
161 OComponentProxyAggregationHelper::~OComponentProxyAggregationHelper( )
163 OSL_ENSURE( m_rBHelper.bDisposed, "OComponentProxyAggregationHelper::~OComponentProxyAggregationHelper: you should dispose your derived class in the dtor, if necessary!" );
164 // if this asserts, add the following to your derived class dtor:
166 // if ( !m_rBHelper.bDisposed )
167 // {
168 // acquire(); // to prevent duplicate dtor calls
169 // dispose();
170 // }
172 m_xInner.clear();
175 //-------------------------------------------------------------------------
176 void SAL_CALL OComponentProxyAggregationHelper::disposing( const EventObject& _rSource ) throw (RuntimeException)
178 if ( _rSource.Source == m_xInner )
179 { // it's our inner context which is dying -> dispose ourself
180 if ( !m_rBHelper.bDisposed && !m_rBHelper.bInDispose )
181 { // (if necessary only, of course)
182 dispose();
187 //-------------------------------------------------------------------------
188 void SAL_CALL OComponentProxyAggregationHelper::dispose() throw( RuntimeException )
190 ::osl::MutexGuard aGuard( m_rBHelper.rMutex );
192 // dispose our inner context
193 // before we do this, remove ourself as listener - else in disposing( EventObject ), we
194 // would dispose ourself a second time
195 Reference< XComponent > xComp( m_xInner, UNO_QUERY );
196 if ( xComp.is() )
198 xComp->removeEventListener( this );
199 xComp->dispose();
200 xComp.clear();
204 //=========================================================================
205 //= OComponentProxyAggregation
206 //=========================================================================
207 //-------------------------------------------------------------------------
208 OComponentProxyAggregation::OComponentProxyAggregation( const Reference< XMultiServiceFactory >& _rxORB,
209 const Reference< XComponent >& _rxComponent )
210 :OComponentProxyAggregation_CBase( m_aMutex )
211 ,OComponentProxyAggregationHelper( _rxORB, rBHelper )
213 OSL_ENSURE( _rxComponent.is(), "OComponentProxyAggregation::OComponentProxyAggregation: accessible is no XComponent!" );
214 if ( _rxComponent.is() )
215 componentAggregateProxyFor( _rxComponent, m_refCount, *this );
218 //-------------------------------------------------------------------------
219 OComponentProxyAggregation::~OComponentProxyAggregation()
221 implEnsureDisposeInDtor( );
224 //-------------------------------------------------------------------------
225 IMPLEMENT_FORWARD_XINTERFACE2( OComponentProxyAggregation, OComponentProxyAggregation_CBase, OComponentProxyAggregationHelper )
227 //-------------------------------------------------------------------------
228 IMPLEMENT_GET_IMPLEMENTATION_ID( OComponentProxyAggregation )
230 //-------------------------------------------------------------------------
231 Sequence< Type > SAL_CALL OComponentProxyAggregation::getTypes( ) throw (RuntimeException)
233 Sequence< Type > aTypes( OComponentProxyAggregationHelper::getTypes() );
235 // append XComponent, coming from OComponentProxyAggregation_CBase
236 sal_Int32 nLen = aTypes.getLength();
237 aTypes.realloc( nLen + 1 );
238 aTypes[ nLen ] = ::getCppuType( static_cast< Reference< XComponent >* >( NULL ) );
240 return aTypes;
243 //-------------------------------------------------------------------------
244 void OComponentProxyAggregation::implEnsureDisposeInDtor( )
246 if ( !rBHelper.bDisposed )
248 acquire(); // to prevent duplicate dtor calls
249 dispose();
253 //--------------------------------------------------------------------
254 void SAL_CALL OComponentProxyAggregation::disposing( const EventObject& _rSource ) throw (RuntimeException)
256 // simly disambiguate - this is necessary for MSVC to distinguish
257 // "disposing( EventObject )" from "disposing()"
258 OComponentProxyAggregationHelper::disposing( _rSource );
261 //--------------------------------------------------------------------
262 void SAL_CALL OComponentProxyAggregation::disposing() throw (RuntimeException)
264 // call the dispose-functionality of the base, which will dispose our aggregated component
265 OComponentProxyAggregationHelper::dispose();
268 //--------------------------------------------------------------------
269 void SAL_CALL OComponentProxyAggregation::dispose() throw( RuntimeException )
271 // simply disambiguate
272 OComponentProxyAggregation_CBase::dispose();
276 //.............................................................................
277 } // namespace comphelper
278 //.............................................................................