Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / testtools / source / performance / pseudo.cxx
blobc97f6b4b9aa2e35c9f697d536c28523c37dfe984
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <osl/diagnose.h>
30 #include <osl/interlck.h>
31 #include <rtl/ustring.hxx>
32 #include <typelib/typedescription.h>
33 #include <uno/dispatcher.h>
34 #include <uno/environment.h>
35 #include <uno/mapping.h>
36 #include <uno/lbnames.h>
38 using ::rtl::OUString;
40 namespace pseudo_uno
43 //==================================================================================================
44 struct pseudo_Mapping : public uno_Mapping
46 oslInterlockedCount nRef;
48 uno_ExtEnvironment * pFrom;
49 uno_ExtEnvironment * pTo;
51 pseudo_Mapping( uno_ExtEnvironment * pFrom_, uno_ExtEnvironment * pTo_ );
52 ~pseudo_Mapping();
55 //==== a uno pseudo proxy =============================================================================
56 struct pseudo_unoInterfaceProxy : public uno_Interface
58 oslInterlockedCount nRef;
59 pseudo_Mapping * pPseudoMapping;
61 // mapping information
62 uno_Interface * pUnoI; // wrapped interface
63 typelib_InterfaceTypeDescription * pTypeDescr;
64 OUString oid;
66 // ctor
67 inline pseudo_unoInterfaceProxy( pseudo_Mapping * pPseudoMapping_,
68 uno_Interface * pUnoI_,
69 typelib_InterfaceTypeDescription * pTypeDescr_,
70 const OUString & rOId_ );
72 //--------------------------------------------------------------------------------------------------
73 static void SAL_CALL pseudo_unoInterfaceProxy_dispatch(
74 uno_Interface * pUnoI,
75 const typelib_TypeDescription * pMemberType,
76 void * pReturn,
77 void * pArgs[],
78 uno_Any ** ppException )
80 pseudo_unoInterfaceProxy * pThis = static_cast< pseudo_unoInterfaceProxy * >( pUnoI );
81 (*pThis->pUnoI->pDispatcher)( pThis->pUnoI, pMemberType, pReturn, pArgs, ppException );
84 //--------------------------------------------------------------------------------------------------
85 static void SAL_CALL pseudo_unoInterfaceProxy_free( uno_ExtEnvironment * pEnv, void * pProxy )
87 pseudo_unoInterfaceProxy * pThis =
88 static_cast< pseudo_unoInterfaceProxy * >(
89 reinterpret_cast< uno_Interface * >( pProxy ) );
90 OSL_ASSERT( pEnv == pThis->pPseudoMapping->pTo );
92 (*pThis->pPseudoMapping->pFrom->revokeInterface)( pThis->pPseudoMapping->pFrom, pThis->pUnoI );
93 (*pThis->pUnoI->release)( pThis->pUnoI );
94 typelib_typedescription_release( (typelib_TypeDescription *)pThis->pTypeDescr );
95 (*pThis->pPseudoMapping->release)( pThis->pPseudoMapping );
97 #if OSL_DEBUG_LEVEL > 1
98 *(int *)pProxy = 0xdeadbabe;
99 #endif
100 delete pThis;
102 //--------------------------------------------------------------------------------------------------
103 static void SAL_CALL pseudo_unoInterfaceProxy_acquire( uno_Interface * pUnoI )
105 if (1 == osl_incrementInterlockedCount( &static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->nRef ))
107 // rebirth of proxy zombie
108 // register at uno env
109 void * pThis = static_cast< uno_Interface * >( pUnoI );
110 (*static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo->registerProxyInterface)(
111 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo,
112 &pThis, pseudo_unoInterfaceProxy_free,
113 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->oid.pData,
114 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pTypeDescr );
115 OSL_ASSERT( pThis == static_cast< uno_Interface * >( pUnoI ) );
118 //--------------------------------------------------------------------------------------------------
119 static void SAL_CALL pseudo_unoInterfaceProxy_release( uno_Interface * pUnoI )
121 if (! osl_decrementInterlockedCount( & static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->nRef ))
123 // revoke from uno env on last release
124 (*static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo->revokeInterface)(
125 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo, pUnoI );
128 //__________________________________________________________________________________________________
129 inline pseudo_unoInterfaceProxy::pseudo_unoInterfaceProxy(
130 pseudo_Mapping * pPseudoMapping_, uno_Interface * pUnoI_,
131 typelib_InterfaceTypeDescription * pTypeDescr_, const OUString & rOId_ )
132 : nRef( 1 )
133 , pPseudoMapping( pPseudoMapping_ )
134 , pUnoI( pUnoI_ )
135 , pTypeDescr( pTypeDescr_ )
136 , oid( rOId_ )
138 (*pPseudoMapping->acquire)( pPseudoMapping );
139 typelib_typedescription_acquire( (typelib_TypeDescription *)pTypeDescr );
140 (*pPseudoMapping->pFrom->registerInterface)(
141 pPseudoMapping->pFrom, reinterpret_cast< void ** >( &pUnoI ), oid.pData, pTypeDescr );
142 (*pUnoI->acquire)( pUnoI );
144 // uno_Interface
145 uno_Interface::acquire = pseudo_unoInterfaceProxy_acquire;
146 uno_Interface::release = pseudo_unoInterfaceProxy_release;
147 uno_Interface::pDispatcher = pseudo_unoInterfaceProxy_dispatch;
150 //--------------------------------------------------------------------------------------------------
151 static void SAL_CALL pseudo_Mapping_mapInterface(
152 uno_Mapping * pMapping, void ** ppOut,
153 void * pUnoI, typelib_InterfaceTypeDescription * pTypeDescr )
155 OSL_ASSERT( ppOut && pTypeDescr );
156 if (*ppOut)
158 (*reinterpret_cast< uno_Interface * >( *ppOut )->release)(
159 reinterpret_cast< uno_Interface * >( *ppOut ) );
160 *ppOut = 0;
162 if (pUnoI && pTypeDescr)
164 // get object id of uno interface to be wrapped
165 rtl_uString * pOId = 0;
166 (*static_cast< pseudo_Mapping * >( pMapping )->pFrom->getObjectIdentifier)(
167 static_cast< pseudo_Mapping * >( pMapping )->pFrom, &pOId, pUnoI );
168 OSL_ASSERT( pOId );
170 if (pOId)
172 // try to get any known interface from target environment
173 (*static_cast< pseudo_Mapping * >( pMapping )->pTo->getRegisteredInterface)(
174 static_cast< pseudo_Mapping * >( pMapping )->pTo, ppOut, pOId, pTypeDescr );
175 if (! *ppOut) // no existing interface, register new proxy interface
177 // try to publish a new proxy (ref count initially 1)
178 void * pProxy = new pseudo_unoInterfaceProxy(
179 static_cast< pseudo_Mapping * >( pMapping ),
180 reinterpret_cast< uno_Interface * >( pUnoI ), pTypeDescr, pOId );
182 // proxy may be exchanged during registration
183 (*static_cast< pseudo_Mapping * >( pMapping )->pTo->registerProxyInterface)(
184 static_cast< pseudo_Mapping * >( pMapping )->pTo,
185 &pProxy, pseudo_unoInterfaceProxy_free, pOId, pTypeDescr );
187 *ppOut = pProxy;
189 rtl_uString_release( pOId );
193 //--------------------------------------------------------------------------------------------------
194 static void SAL_CALL pseudo_Mapping_free( uno_Mapping * pMapping )
196 delete static_cast< pseudo_Mapping * >( pMapping );
198 //--------------------------------------------------------------------------------------------------
199 static void SAL_CALL pseudo_Mapping_acquire( uno_Mapping * pMapping )
201 if (1 == osl_incrementInterlockedCount( & static_cast< pseudo_Mapping * >( pMapping )->nRef ))
203 OUString aMappingPurpose( RTL_CONSTASCII_USTRINGPARAM("pseudo") );
204 uno_registerMapping( &pMapping,
205 pseudo_Mapping_free,
206 (uno_Environment *)((pseudo_Mapping *)pMapping)->pFrom,
207 (uno_Environment *)((pseudo_Mapping *)pMapping)->pTo,
208 aMappingPurpose.pData );
211 //--------------------------------------------------------------------------------------------------
212 static void SAL_CALL pseudo_Mapping_release( uno_Mapping * pMapping )
214 if (! osl_decrementInterlockedCount( & static_cast< pseudo_Mapping * >( pMapping )->nRef ))
216 uno_revokeMapping( pMapping );
220 //__________________________________________________________________________________________________
221 pseudo_Mapping::pseudo_Mapping( uno_ExtEnvironment * pFrom_, uno_ExtEnvironment * pTo_ )
222 : nRef( 1 )
223 , pFrom( pFrom_ )
224 , pTo( pTo_ )
226 (*((uno_Environment *)pFrom)->acquire)( (uno_Environment *)pFrom );
227 (*((uno_Environment *)pTo)->acquire)( (uno_Environment *)pTo );
229 uno_Mapping::acquire = pseudo_Mapping_acquire;
230 uno_Mapping::release = pseudo_Mapping_release;
231 uno_Mapping::mapInterface = pseudo_Mapping_mapInterface;
233 //__________________________________________________________________________________________________
234 pseudo_Mapping::~pseudo_Mapping()
236 (*((uno_Environment *)pTo)->release)( (uno_Environment *)pTo );
237 (*((uno_Environment *)pFrom)->release)( (uno_Environment *)pFrom );
242 //##################################################################################################
243 extern "C" void SAL_CALL uno_initEnvironment( uno_Environment * pUnoEnv )
245 OSL_FAIL( "### no impl: unexpected call!" );
247 //##################################################################################################
248 extern "C" void SAL_CALL uno_ext_getMapping(
249 uno_Mapping ** ppMapping, uno_Environment * pFrom, uno_Environment * pTo )
251 OSL_ASSERT( ppMapping && pFrom && pTo );
252 if (ppMapping && pFrom && pTo && pFrom->pExtEnv && pTo->pExtEnv)
254 uno_Mapping * pMapping = 0;
256 if (0 == rtl_ustr_ascii_compare( pFrom->pTypeName->buffer, UNO_LB_UNO ) &&
257 0 == rtl_ustr_ascii_compare( pTo->pTypeName->buffer, UNO_LB_UNO ))
259 OUString aMappingPurpose( RTL_CONSTASCII_USTRINGPARAM("pseudo") );
260 // ref count is initially 1
261 pMapping = new pseudo_uno::pseudo_Mapping( pFrom->pExtEnv, pTo->pExtEnv );
262 uno_registerMapping( &pMapping, pseudo_uno::pseudo_Mapping_free,
263 (uno_Environment *)pFrom->pExtEnv,
264 (uno_Environment *)pTo->pExtEnv,
265 aMappingPurpose.pData );
268 if (*ppMapping)
269 (*(*ppMapping)->release)( *ppMapping );
270 *ppMapping = pMapping;
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */