Update ooo320-m1
[ooovba.git] / testtools / source / performance / pseudo.cxx
blob7693a489157e38cc77efc074c7701a17a1f421d3
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: pseudo.cxx,v $
10 * $Revision: 1.7 $
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_testtools.hxx"
33 #include <osl/diagnose.h>
34 #include <osl/interlck.h>
35 #include <rtl/ustring.hxx>
36 #include <typelib/typedescription.h>
37 #include <uno/dispatcher.h>
38 #include <uno/environment.h>
39 #include <uno/mapping.h>
40 #include <uno/lbnames.h>
42 using namespace rtl;
45 namespace pseudo_uno
48 //==================================================================================================
49 struct pseudo_Mapping : public uno_Mapping
51 oslInterlockedCount nRef;
53 uno_ExtEnvironment * pFrom;
54 uno_ExtEnvironment * pTo;
56 pseudo_Mapping( uno_ExtEnvironment * pFrom_, uno_ExtEnvironment * pTo_ );
57 ~pseudo_Mapping();
60 //==== a uno pseudo proxy =============================================================================
61 struct pseudo_unoInterfaceProxy : public uno_Interface
63 oslInterlockedCount nRef;
64 pseudo_Mapping * pPseudoMapping;
66 // mapping information
67 uno_Interface * pUnoI; // wrapped interface
68 typelib_InterfaceTypeDescription * pTypeDescr;
69 OUString oid;
71 // ctor
72 inline pseudo_unoInterfaceProxy( pseudo_Mapping * pPseudoMapping_,
73 uno_Interface * pUnoI_,
74 typelib_InterfaceTypeDescription * pTypeDescr_,
75 const OUString & rOId_ );
77 //--------------------------------------------------------------------------------------------------
78 static void SAL_CALL pseudo_unoInterfaceProxy_dispatch(
79 uno_Interface * pUnoI,
80 const typelib_TypeDescription * pMemberType,
81 void * pReturn,
82 void * pArgs[],
83 uno_Any ** ppException )
85 pseudo_unoInterfaceProxy * pThis = static_cast< pseudo_unoInterfaceProxy * >( pUnoI );
86 (*pThis->pUnoI->pDispatcher)( pThis->pUnoI, pMemberType, pReturn, pArgs, ppException );
89 //--------------------------------------------------------------------------------------------------
90 static void SAL_CALL pseudo_unoInterfaceProxy_free( uno_ExtEnvironment * pEnv, void * pProxy )
92 pseudo_unoInterfaceProxy * pThis =
93 static_cast< pseudo_unoInterfaceProxy * >(
94 reinterpret_cast< uno_Interface * >( pProxy ) );
95 OSL_ASSERT( pEnv == pThis->pPseudoMapping->pTo );
97 (*pThis->pPseudoMapping->pFrom->revokeInterface)( pThis->pPseudoMapping->pFrom, pThis->pUnoI );
98 (*pThis->pUnoI->release)( pThis->pUnoI );
99 typelib_typedescription_release( (typelib_TypeDescription *)pThis->pTypeDescr );
100 (*pThis->pPseudoMapping->release)( pThis->pPseudoMapping );
102 #if OSL_DEBUG_LEVEL > 1
103 *(int *)pProxy = 0xdeadbabe;
104 #endif
105 delete pThis;
107 //--------------------------------------------------------------------------------------------------
108 static void SAL_CALL pseudo_unoInterfaceProxy_acquire( uno_Interface * pUnoI )
110 if (1 == osl_incrementInterlockedCount( &static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->nRef ))
112 // rebirth of proxy zombie
113 // register at uno env
114 void * pThis = static_cast< uno_Interface * >( pUnoI );
115 (*static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo->registerProxyInterface)(
116 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo,
117 &pThis, pseudo_unoInterfaceProxy_free,
118 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->oid.pData,
119 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pTypeDescr );
120 OSL_ASSERT( pThis == static_cast< uno_Interface * >( pUnoI ) );
123 //--------------------------------------------------------------------------------------------------
124 static void SAL_CALL pseudo_unoInterfaceProxy_release( uno_Interface * pUnoI )
126 if (! osl_decrementInterlockedCount( & static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->nRef ))
128 // revoke from uno env on last release
129 (*static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo->revokeInterface)(
130 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo, pUnoI );
133 //__________________________________________________________________________________________________
134 inline pseudo_unoInterfaceProxy::pseudo_unoInterfaceProxy(
135 pseudo_Mapping * pPseudoMapping_, uno_Interface * pUnoI_,
136 typelib_InterfaceTypeDescription * pTypeDescr_, const OUString & rOId_ )
137 : nRef( 1 )
138 , pPseudoMapping( pPseudoMapping_ )
139 , pUnoI( pUnoI_ )
140 , pTypeDescr( pTypeDescr_ )
141 , oid( rOId_ )
143 (*pPseudoMapping->acquire)( pPseudoMapping );
144 typelib_typedescription_acquire( (typelib_TypeDescription *)pTypeDescr );
145 (*pPseudoMapping->pFrom->registerInterface)(
146 pPseudoMapping->pFrom, reinterpret_cast< void ** >( &pUnoI ), oid.pData, pTypeDescr );
147 (*pUnoI->acquire)( pUnoI );
149 // uno_Interface
150 uno_Interface::acquire = pseudo_unoInterfaceProxy_acquire;
151 uno_Interface::release = pseudo_unoInterfaceProxy_release;
152 uno_Interface::pDispatcher = pseudo_unoInterfaceProxy_dispatch;
155 //--------------------------------------------------------------------------------------------------
156 static void SAL_CALL pseudo_Mapping_mapInterface(
157 uno_Mapping * pMapping, void ** ppOut,
158 void * pUnoI, typelib_InterfaceTypeDescription * pTypeDescr )
160 OSL_ASSERT( ppOut && pTypeDescr );
161 if (*ppOut)
163 (*reinterpret_cast< uno_Interface * >( *ppOut )->release)(
164 reinterpret_cast< uno_Interface * >( *ppOut ) );
165 *ppOut = 0;
167 if (pUnoI && pTypeDescr)
169 // get object id of uno interface to be wrapped
170 rtl_uString * pOId = 0;
171 (*static_cast< pseudo_Mapping * >( pMapping )->pFrom->getObjectIdentifier)(
172 static_cast< pseudo_Mapping * >( pMapping )->pFrom, &pOId, pUnoI );
173 OSL_ASSERT( pOId );
175 if (pOId)
177 // try to get any known interface from target environment
178 (*static_cast< pseudo_Mapping * >( pMapping )->pTo->getRegisteredInterface)(
179 static_cast< pseudo_Mapping * >( pMapping )->pTo, ppOut, pOId, pTypeDescr );
180 if (! *ppOut) // no existing interface, register new proxy interface
182 // try to publish a new proxy (ref count initially 1)
183 void * pProxy = new pseudo_unoInterfaceProxy(
184 static_cast< pseudo_Mapping * >( pMapping ),
185 reinterpret_cast< uno_Interface * >( pUnoI ), pTypeDescr, pOId );
187 // proxy may be exchanged during registration
188 (*static_cast< pseudo_Mapping * >( pMapping )->pTo->registerProxyInterface)(
189 static_cast< pseudo_Mapping * >( pMapping )->pTo,
190 &pProxy, pseudo_unoInterfaceProxy_free, pOId, pTypeDescr );
192 *ppOut = pProxy;
194 rtl_uString_release( pOId );
198 //--------------------------------------------------------------------------------------------------
199 static void SAL_CALL pseudo_Mapping_free( uno_Mapping * pMapping )
201 delete static_cast< pseudo_Mapping * >( pMapping );
203 //--------------------------------------------------------------------------------------------------
204 static void SAL_CALL pseudo_Mapping_acquire( uno_Mapping * pMapping )
206 if (1 == osl_incrementInterlockedCount( & static_cast< pseudo_Mapping * >( pMapping )->nRef ))
208 OUString aMappingPurpose( RTL_CONSTASCII_USTRINGPARAM("pseudo") );
209 uno_registerMapping( &pMapping,
210 pseudo_Mapping_free,
211 (uno_Environment *)((pseudo_Mapping *)pMapping)->pFrom,
212 (uno_Environment *)((pseudo_Mapping *)pMapping)->pTo,
213 aMappingPurpose.pData );
216 //--------------------------------------------------------------------------------------------------
217 static void SAL_CALL pseudo_Mapping_release( uno_Mapping * pMapping )
219 if (! osl_decrementInterlockedCount( & static_cast< pseudo_Mapping * >( pMapping )->nRef ))
221 uno_revokeMapping( pMapping );
225 //__________________________________________________________________________________________________
226 pseudo_Mapping::pseudo_Mapping( uno_ExtEnvironment * pFrom_, uno_ExtEnvironment * pTo_ )
227 : nRef( 1 )
228 , pFrom( pFrom_ )
229 , pTo( pTo_ )
231 (*((uno_Environment *)pFrom)->acquire)( (uno_Environment *)pFrom );
232 (*((uno_Environment *)pTo)->acquire)( (uno_Environment *)pTo );
234 uno_Mapping::acquire = pseudo_Mapping_acquire;
235 uno_Mapping::release = pseudo_Mapping_release;
236 uno_Mapping::mapInterface = pseudo_Mapping_mapInterface;
238 //__________________________________________________________________________________________________
239 pseudo_Mapping::~pseudo_Mapping()
241 (*((uno_Environment *)pTo)->release)( (uno_Environment *)pTo );
242 (*((uno_Environment *)pFrom)->release)( (uno_Environment *)pFrom );
247 //##################################################################################################
248 extern "C" void SAL_CALL uno_initEnvironment( uno_Environment * pUnoEnv )
250 OSL_ENSURE( sal_False, "### no impl: unexpected call!" );
252 //##################################################################################################
253 extern "C" void SAL_CALL uno_ext_getMapping(
254 uno_Mapping ** ppMapping, uno_Environment * pFrom, uno_Environment * pTo )
256 OSL_ASSERT( ppMapping && pFrom && pTo );
257 if (ppMapping && pFrom && pTo && pFrom->pExtEnv && pTo->pExtEnv)
259 uno_Mapping * pMapping = 0;
261 if (0 == rtl_ustr_ascii_compare( pFrom->pTypeName->buffer, UNO_LB_UNO ) &&
262 0 == rtl_ustr_ascii_compare( pTo->pTypeName->buffer, UNO_LB_UNO ))
264 OUString aMappingPurpose( RTL_CONSTASCII_USTRINGPARAM("pseudo") );
265 // ref count is initially 1
266 pMapping = new pseudo_uno::pseudo_Mapping( pFrom->pExtEnv, pTo->pExtEnv );
267 uno_registerMapping( &pMapping, pseudo_uno::pseudo_Mapping_free,
268 (uno_Environment *)pFrom->pExtEnv,
269 (uno_Environment *)pTo->pExtEnv,
270 aMappingPurpose.pData );
273 if (*ppMapping)
274 (*(*ppMapping)->release)( *ppMapping );
275 *ppMapping = pMapping;