2010-10-14 08:27:31 +02:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-06-21 14:30:25 +01:00
/*
* This file is part of the LibreOffice project .
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http : //mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice :
*
* Licensed to the Apache Software Foundation ( ASF ) under one or more
* contributor license agreements . See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership . The ASF licenses this file to you under the Apache
* License , Version 2.0 ( the " License " ) ; you may not use this file
* except in compliance with the License . You may obtain a copy of
* the License at http : //www.apache.org/licenses/LICENSE-2.0 .
*/
2003-05-19 11:57:46 +00:00
2013-03-28 13:28:03 +01:00
# include "sal/config.h"
# include <cassert>
2003-05-19 11:57:46 +00:00
# include <comphelper/proxyaggregation.hxx>
2012-11-27 11:00:51 +02:00
# include <com/sun/star/reflection/ProxyFactory.hpp>
2003-05-19 11:57:46 +00:00
//.............................................................................
namespace comphelper
{
//.............................................................................
using namespace : : com : : sun : : star : : uno ;
using namespace : : com : : sun : : star : : lang ;
using namespace : : com : : sun : : star : : reflection ;
//=========================================================================
//= OProxyAggregation
//=========================================================================
//-------------------------------------------------------------------------
2012-11-27 11:00:51 +02:00
OProxyAggregation : : OProxyAggregation ( const Reference < XComponentContext > & _rxContext )
: m_xContext ( _rxContext )
2003-05-19 11:57:46 +00:00
{
}
//-------------------------------------------------------------------------
2006-06-19 21:49:01 +00:00
void OProxyAggregation : : baseAggregateProxyFor ( const Reference < XInterface > & _rxComponent , oslInterlockedCount & _rRefCount ,
2003-05-19 11:57:46 +00:00
: : cppu : : OWeakObject & _rDelegator )
{
// first a factory for the proxy
2012-11-27 11:00:51 +02:00
Reference < XProxyFactory > xFactory = ProxyFactory : : create ( m_xContext ) ;
2003-05-19 11:57:46 +00:00
// then the proxy itself
2012-11-27 11:00:51 +02:00
{ // i36686 OJ: achieve the desctruction of the tempoary -> otherwise it leads to _rRefCount -= 2
m_xProxyAggregate = xFactory - > createProxy ( _rxComponent ) ;
}
if ( m_xProxyAggregate . is ( ) )
m_xProxyAggregate - > queryAggregation ( : : getCppuType ( & m_xProxyTypeAccess ) ) > > = m_xProxyTypeAccess ;
// aggregate the proxy
osl_atomic_increment ( & _rRefCount ) ;
if ( m_xProxyAggregate . is ( ) )
2003-05-19 11:57:46 +00:00
{
2012-11-27 11:00:51 +02:00
// At this point in time, the proxy has a ref count of exactly two - in m_xControlContextProxy,
// and in m_xProxyTypeAccess.
// Remember to _not_ reset these members unless the delegator of the proxy has been reset, too!
m_xProxyAggregate - > setDelegator ( _rDelegator ) ;
2003-05-19 11:57:46 +00:00
}
2012-11-27 11:00:51 +02:00
osl_atomic_decrement ( & _rRefCount ) ;
2003-05-19 11:57:46 +00:00
}
//-------------------------------------------------------------------------
Any SAL_CALL OProxyAggregation : : queryAggregation ( const Type & _rType ) throw ( RuntimeException )
{
return m_xProxyAggregate . is ( ) ? m_xProxyAggregate - > queryAggregation ( _rType ) : Any ( ) ;
}
//-------------------------------------------------------------------------
Sequence < Type > SAL_CALL OProxyAggregation : : getTypes ( ) throw ( RuntimeException )
{
Sequence < Type > aTypes ;
if ( m_xProxyAggregate . is ( ) )
{
2004-03-25 14:01:03 +00:00
if ( m_xProxyTypeAccess . is ( ) )
aTypes = m_xProxyTypeAccess - > getTypes ( ) ;
2003-05-19 11:57:46 +00:00
}
return aTypes ;
}
//-------------------------------------------------------------------------
OProxyAggregation : : ~ OProxyAggregation ( )
{
if ( m_xProxyAggregate . is ( ) )
m_xProxyAggregate - > setDelegator ( NULL ) ;
m_xProxyAggregate . clear ( ) ;
2004-03-25 14:01:03 +00:00
m_xProxyTypeAccess . clear ( ) ;
// this should remove the _two_only_ "real" references (means not delegated to
2003-05-19 11:57:46 +00:00
// ourself) to this proxy, and thus delete it
}
//=========================================================================
//= OComponentProxyAggregationHelper
//=========================================================================
//-------------------------------------------------------------------------
2012-11-27 11:00:51 +02:00
OComponentProxyAggregationHelper : : OComponentProxyAggregationHelper ( const Reference < XComponentContext > & _rxContext ,
2003-05-19 11:57:46 +00:00
: : cppu : : OBroadcastHelper & _rBHelper )
2012-11-27 11:00:51 +02:00
: OProxyAggregation ( _rxContext )
2003-05-19 11:57:46 +00:00
, m_rBHelper ( _rBHelper )
{
2012-11-27 11:00:51 +02:00
OSL_ENSURE ( _rxContext . is ( ) , " OComponentProxyAggregationHelper::OComponentProxyAggregationHelper: invalid arguments! " ) ;
2003-05-19 11:57:46 +00:00
}
//-------------------------------------------------------------------------
2006-06-19 21:49:01 +00:00
void OComponentProxyAggregationHelper : : componentAggregateProxyFor (
2003-05-19 11:57:46 +00:00
const Reference < XComponent > & _rxComponent , oslInterlockedCount & _rRefCount ,
: : cppu : : OWeakObject & _rDelegator )
{
2006-06-19 21:49:01 +00:00
OSL_ENSURE ( _rxComponent . is ( ) , " OComponentProxyAggregationHelper::componentAggregateProxyFor: invalid inner component! " ) ;
2003-05-19 11:57:46 +00:00
m_xInner = _rxComponent ;
// aggregate a proxy for the object
2006-06-19 21:49:01 +00:00
baseAggregateProxyFor ( m_xInner . get ( ) , _rRefCount , _rDelegator ) ;
2003-05-19 11:57:46 +00:00
// add as event listener to the inner context, because we want to be notified of disposals
2012-09-22 01:51:12 -05:00
osl_atomic_increment ( & _rRefCount ) ;
2003-05-19 11:57:46 +00:00
{
if ( m_xInner . is ( ) )
m_xInner - > addEventListener ( this ) ;
}
2012-09-22 01:51:12 -05:00
osl_atomic_decrement ( & _rRefCount ) ;
2003-05-19 11:57:46 +00:00
}
//-------------------------------------------------------------------------
Any SAL_CALL OComponentProxyAggregationHelper : : queryInterface ( const Type & _rType ) throw ( RuntimeException )
{
Any aReturn ( BASE : : queryInterface ( _rType ) ) ;
if ( ! aReturn . hasValue ( ) )
aReturn = OProxyAggregation : : queryAggregation ( _rType ) ;
return aReturn ;
}
//-------------------------------------------------------------------------
IMPLEMENT_FORWARD_XTYPEPROVIDER2 ( OComponentProxyAggregationHelper , BASE , OProxyAggregation )
//-------------------------------------------------------------------------
OComponentProxyAggregationHelper : : ~ OComponentProxyAggregationHelper ( )
{
OSL_ENSURE ( m_rBHelper . bDisposed , " OComponentProxyAggregationHelper::~OComponentProxyAggregationHelper: you should dispose your derived class in the dtor, if necessary! " ) ;
// if this asserts, add the following to your derived class dtor:
//
// if ( !m_rBHelper.bDisposed )
// {
// acquire(); // to prevent duplicate dtor calls
// dispose();
// }
m_xInner . clear ( ) ;
}
//-------------------------------------------------------------------------
void SAL_CALL OComponentProxyAggregationHelper : : disposing ( const EventObject & _rSource ) throw ( RuntimeException )
{
if ( _rSource . Source = = m_xInner )
{ // it's our inner context which is dying -> dispose ourself
if ( ! m_rBHelper . bDisposed & & ! m_rBHelper . bInDispose )
{ // (if necessary only, of course)
dispose ( ) ;
}
}
}
//-------------------------------------------------------------------------
void SAL_CALL OComponentProxyAggregationHelper : : dispose ( ) throw ( RuntimeException )
{
: : osl : : MutexGuard aGuard ( m_rBHelper . rMutex ) ;
// dispose our inner context
// before we do this, remove ourself as listener - else in disposing( EventObject ), we
// would dispose ourself a second time
Reference < XComponent > xComp ( m_xInner , UNO_QUERY ) ;
if ( xComp . is ( ) )
{
xComp - > removeEventListener ( this ) ;
xComp - > dispose ( ) ;
xComp . clear ( ) ;
}
}
//=========================================================================
//= OComponentProxyAggregation
//=========================================================================
//-------------------------------------------------------------------------
2012-11-27 11:00:51 +02:00
OComponentProxyAggregation : : OComponentProxyAggregation ( const Reference < XComponentContext > & _rxContext ,
2003-05-19 11:57:46 +00:00
const Reference < XComponent > & _rxComponent )
2013-03-28 15:35:05 +01:00
: WeakComponentImplHelperBase ( m_aMutex )
2012-11-27 11:00:51 +02:00
, OComponentProxyAggregationHelper ( _rxContext , rBHelper )
2003-05-19 11:57:46 +00:00
{
OSL_ENSURE ( _rxComponent . is ( ) , " OComponentProxyAggregation::OComponentProxyAggregation: accessible is no XComponent! " ) ;
if ( _rxComponent . is ( ) )
2006-06-19 21:49:01 +00:00
componentAggregateProxyFor ( _rxComponent , m_refCount , * this ) ;
2003-05-19 11:57:46 +00:00
}
//-------------------------------------------------------------------------
OComponentProxyAggregation : : ~ OComponentProxyAggregation ( )
{
implEnsureDisposeInDtor ( ) ;
}
//-------------------------------------------------------------------------
2013-03-28 15:35:05 +01:00
IMPLEMENT_FORWARD_XINTERFACE2 ( OComponentProxyAggregation , WeakComponentImplHelperBase , OComponentProxyAggregationHelper )
2003-05-19 11:57:46 +00:00
//-------------------------------------------------------------------------
IMPLEMENT_GET_IMPLEMENTATION_ID ( OComponentProxyAggregation )
//-------------------------------------------------------------------------
Sequence < Type > SAL_CALL OComponentProxyAggregation : : getTypes ( ) throw ( RuntimeException )
{
Sequence < Type > aTypes ( OComponentProxyAggregationHelper : : getTypes ( ) ) ;
2013-03-28 15:35:05 +01:00
// append XComponent, coming from WeakComponentImplHelperBase
2003-05-19 11:57:46 +00:00
sal_Int32 nLen = aTypes . getLength ( ) ;
aTypes . realloc ( nLen + 1 ) ;
aTypes [ nLen ] = : : getCppuType ( static_cast < Reference < XComponent > * > ( NULL ) ) ;
return aTypes ;
}
//-------------------------------------------------------------------------
void OComponentProxyAggregation : : implEnsureDisposeInDtor ( )
{
if ( ! rBHelper . bDisposed )
{
acquire ( ) ; // to prevent duplicate dtor calls
dispose ( ) ;
}
}
//--------------------------------------------------------------------
void SAL_CALL OComponentProxyAggregation : : disposing ( const EventObject & _rSource ) throw ( RuntimeException )
{
2013-03-28 13:28:03 +01:00
// Simply disambiguate---this is necessary for MSVC to distinguish
// "disposing(EventObject)" from "disposing()"; but it is also a good
// place to check for recursive calls that would be caused by an object
// being registered as an XEventListener at itself (cf. rhbz#928568):
assert ( _rSource . Source ! = static_cast < cppu : : OWeakObject * > ( this ) ) ;
2003-05-19 11:57:46 +00:00
OComponentProxyAggregationHelper : : disposing ( _rSource ) ;
}
//--------------------------------------------------------------------
void SAL_CALL OComponentProxyAggregation : : disposing ( ) throw ( RuntimeException )
{
// call the dispose-functionality of the base, which will dispose our aggregated component
OComponentProxyAggregationHelper : : dispose ( ) ;
}
//--------------------------------------------------------------------
void SAL_CALL OComponentProxyAggregation : : dispose ( ) throw ( RuntimeException )
{
// simply disambiguate
2013-03-28 15:35:05 +01:00
WeakComponentImplHelperBase : : dispose ( ) ;
2003-05-19 11:57:46 +00:00
}
//.............................................................................
} // namespace comphelper
//.............................................................................
2010-10-14 08:27:31 +02:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */