Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / testtools / source / performance / pseudo.cxx
blob95c54a0976939efa47a82e73b8cff51a181eefae
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <osl/interlck.h>
22 #include <rtl/ustring.hxx>
23 #include <typelib/typedescription.h>
24 #include <uno/dispatcher.h>
25 #include <uno/environment.h>
26 #include <uno/mapping.h>
27 #include <uno/lbnames.h>
29 using ::rtl::OUString;
31 namespace pseudo_uno
34 //==================================================================================================
35 struct pseudo_Mapping : public uno_Mapping
37 oslInterlockedCount nRef;
39 uno_ExtEnvironment * pFrom;
40 uno_ExtEnvironment * pTo;
42 pseudo_Mapping( uno_ExtEnvironment * pFrom_, uno_ExtEnvironment * pTo_ );
43 ~pseudo_Mapping();
46 //==== a uno pseudo proxy =============================================================================
47 struct pseudo_unoInterfaceProxy : public uno_Interface
49 oslInterlockedCount nRef;
50 pseudo_Mapping * pPseudoMapping;
52 // mapping information
53 uno_Interface * pUnoI; // wrapped interface
54 typelib_InterfaceTypeDescription * pTypeDescr;
55 OUString oid;
57 // ctor
58 inline pseudo_unoInterfaceProxy( pseudo_Mapping * pPseudoMapping_,
59 uno_Interface * pUnoI_,
60 typelib_InterfaceTypeDescription * pTypeDescr_,
61 const OUString & rOId_ );
63 //--------------------------------------------------------------------------------------------------
64 static void SAL_CALL pseudo_unoInterfaceProxy_dispatch(
65 uno_Interface * pUnoI,
66 const typelib_TypeDescription * pMemberType,
67 void * pReturn,
68 void * pArgs[],
69 uno_Any ** ppException )
71 pseudo_unoInterfaceProxy * pThis = static_cast< pseudo_unoInterfaceProxy * >( pUnoI );
72 (*pThis->pUnoI->pDispatcher)( pThis->pUnoI, pMemberType, pReturn, pArgs, ppException );
75 //--------------------------------------------------------------------------------------------------
76 static void SAL_CALL pseudo_unoInterfaceProxy_free( uno_ExtEnvironment * pEnv, void * pProxy )
78 pseudo_unoInterfaceProxy * pThis =
79 static_cast< pseudo_unoInterfaceProxy * >(
80 reinterpret_cast< uno_Interface * >( pProxy ) );
81 OSL_ASSERT( pEnv == pThis->pPseudoMapping->pTo );
83 (*pThis->pPseudoMapping->pFrom->revokeInterface)( pThis->pPseudoMapping->pFrom, pThis->pUnoI );
84 (*pThis->pUnoI->release)( pThis->pUnoI );
85 typelib_typedescription_release( (typelib_TypeDescription *)pThis->pTypeDescr );
86 (*pThis->pPseudoMapping->release)( pThis->pPseudoMapping );
88 #if OSL_DEBUG_LEVEL > 1
89 *(int *)pProxy = 0xdeadbabe;
90 #endif
91 delete pThis;
93 //--------------------------------------------------------------------------------------------------
94 static void SAL_CALL pseudo_unoInterfaceProxy_acquire( uno_Interface * pUnoI )
96 if (1 == osl_atomic_increment( &static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->nRef ))
98 // rebirth of proxy zombie
99 // register at uno env
100 void * pThis = static_cast< uno_Interface * >( pUnoI );
101 (*static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo->registerProxyInterface)(
102 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo,
103 &pThis, pseudo_unoInterfaceProxy_free,
104 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->oid.pData,
105 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pTypeDescr );
106 OSL_ASSERT( pThis == static_cast< uno_Interface * >( pUnoI ) );
109 //--------------------------------------------------------------------------------------------------
110 static void SAL_CALL pseudo_unoInterfaceProxy_release( uno_Interface * pUnoI )
112 if (! osl_atomic_decrement( & static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->nRef ))
114 // revoke from uno env on last release
115 (*static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo->revokeInterface)(
116 static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo, pUnoI );
119 //__________________________________________________________________________________________________
120 inline pseudo_unoInterfaceProxy::pseudo_unoInterfaceProxy(
121 pseudo_Mapping * pPseudoMapping_, uno_Interface * pUnoI_,
122 typelib_InterfaceTypeDescription * pTypeDescr_, const OUString & rOId_ )
123 : nRef( 1 )
124 , pPseudoMapping( pPseudoMapping_ )
125 , pUnoI( pUnoI_ )
126 , pTypeDescr( pTypeDescr_ )
127 , oid( rOId_ )
129 (*pPseudoMapping->acquire)( pPseudoMapping );
130 typelib_typedescription_acquire( (typelib_TypeDescription *)pTypeDescr );
131 (*pPseudoMapping->pFrom->registerInterface)(
132 pPseudoMapping->pFrom, reinterpret_cast< void ** >( &pUnoI ), oid.pData, pTypeDescr );
133 (*pUnoI->acquire)( pUnoI );
135 // uno_Interface
136 uno_Interface::acquire = pseudo_unoInterfaceProxy_acquire;
137 uno_Interface::release = pseudo_unoInterfaceProxy_release;
138 uno_Interface::pDispatcher = pseudo_unoInterfaceProxy_dispatch;
141 //--------------------------------------------------------------------------------------------------
142 static void SAL_CALL pseudo_Mapping_mapInterface(
143 uno_Mapping * pMapping, void ** ppOut,
144 void * pUnoI, typelib_InterfaceTypeDescription * pTypeDescr )
146 OSL_ASSERT( ppOut && pTypeDescr );
147 if (*ppOut)
149 (*reinterpret_cast< uno_Interface * >( *ppOut )->release)(
150 reinterpret_cast< uno_Interface * >( *ppOut ) );
151 *ppOut = 0;
153 if (pUnoI && pTypeDescr)
155 // get object id of uno interface to be wrapped
156 rtl_uString * pOId = 0;
157 (*static_cast< pseudo_Mapping * >( pMapping )->pFrom->getObjectIdentifier)(
158 static_cast< pseudo_Mapping * >( pMapping )->pFrom, &pOId, pUnoI );
159 OSL_ASSERT( pOId );
161 if (pOId)
163 // try to get any known interface from target environment
164 (*static_cast< pseudo_Mapping * >( pMapping )->pTo->getRegisteredInterface)(
165 static_cast< pseudo_Mapping * >( pMapping )->pTo, ppOut, pOId, pTypeDescr );
166 if (! *ppOut) // no existing interface, register new proxy interface
168 // try to publish a new proxy (ref count initially 1)
169 void * pProxy = new pseudo_unoInterfaceProxy(
170 static_cast< pseudo_Mapping * >( pMapping ),
171 reinterpret_cast< uno_Interface * >( pUnoI ), pTypeDescr, pOId );
173 // proxy may be exchanged during registration
174 (*static_cast< pseudo_Mapping * >( pMapping )->pTo->registerProxyInterface)(
175 static_cast< pseudo_Mapping * >( pMapping )->pTo,
176 &pProxy, pseudo_unoInterfaceProxy_free, pOId, pTypeDescr );
178 *ppOut = pProxy;
180 rtl_uString_release( pOId );
184 //--------------------------------------------------------------------------------------------------
185 static void SAL_CALL pseudo_Mapping_free( uno_Mapping * pMapping )
187 delete static_cast< pseudo_Mapping * >( pMapping );
189 //--------------------------------------------------------------------------------------------------
190 static void SAL_CALL pseudo_Mapping_acquire( uno_Mapping * pMapping )
192 if (1 == osl_atomic_increment( & static_cast< pseudo_Mapping * >( pMapping )->nRef ))
194 OUString aMappingPurpose( RTL_CONSTASCII_USTRINGPARAM("pseudo") );
195 uno_registerMapping( &pMapping,
196 pseudo_Mapping_free,
197 (uno_Environment *)((pseudo_Mapping *)pMapping)->pFrom,
198 (uno_Environment *)((pseudo_Mapping *)pMapping)->pTo,
199 aMappingPurpose.pData );
202 //--------------------------------------------------------------------------------------------------
203 static void SAL_CALL pseudo_Mapping_release( uno_Mapping * pMapping )
205 if (! osl_atomic_decrement( & static_cast< pseudo_Mapping * >( pMapping )->nRef ))
207 uno_revokeMapping( pMapping );
211 //__________________________________________________________________________________________________
212 pseudo_Mapping::pseudo_Mapping( uno_ExtEnvironment * pFrom_, uno_ExtEnvironment * pTo_ )
213 : nRef( 1 )
214 , pFrom( pFrom_ )
215 , pTo( pTo_ )
217 (*((uno_Environment *)pFrom)->acquire)( (uno_Environment *)pFrom );
218 (*((uno_Environment *)pTo)->acquire)( (uno_Environment *)pTo );
220 uno_Mapping::acquire = pseudo_Mapping_acquire;
221 uno_Mapping::release = pseudo_Mapping_release;
222 uno_Mapping::mapInterface = pseudo_Mapping_mapInterface;
224 //__________________________________________________________________________________________________
225 pseudo_Mapping::~pseudo_Mapping()
227 (*((uno_Environment *)pTo)->release)( (uno_Environment *)pTo );
228 (*((uno_Environment *)pFrom)->release)( (uno_Environment *)pFrom );
233 //##################################################################################################
234 extern "C" void SAL_CALL uno_initEnvironment( uno_Environment * pUnoEnv )
236 OSL_FAIL( "### no impl: unexpected call!" );
238 //##################################################################################################
239 extern "C" void SAL_CALL uno_ext_getMapping(
240 uno_Mapping ** ppMapping, uno_Environment * pFrom, uno_Environment * pTo )
242 OSL_ASSERT( ppMapping && pFrom && pTo );
243 if (ppMapping && pFrom && pTo && pFrom->pExtEnv && pTo->pExtEnv)
245 uno_Mapping * pMapping = 0;
247 if (0 == rtl_ustr_ascii_compare( pFrom->pTypeName->buffer, UNO_LB_UNO ) &&
248 0 == rtl_ustr_ascii_compare( pTo->pTypeName->buffer, UNO_LB_UNO ))
250 OUString aMappingPurpose( RTL_CONSTASCII_USTRINGPARAM("pseudo") );
251 // ref count is initially 1
252 pMapping = new pseudo_uno::pseudo_Mapping( pFrom->pExtEnv, pTo->pExtEnv );
253 uno_registerMapping( &pMapping, pseudo_uno::pseudo_Mapping_free,
254 (uno_Environment *)pFrom->pExtEnv,
255 (uno_Environment *)pTo->pExtEnv,
256 aMappingPurpose.pData );
259 if (*ppMapping)
260 (*(*ppMapping)->release)( *ppMapping );
261 *ppMapping = pMapping;
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */